Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support golang syntax parsing #787

Merged
merged 9 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package run.mone.antlr.golang;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.ArrayList;
import java.util.List;

/**
* @author [email protected]
* @date 2024/1/29 16:56
*/
public class GoCode {


public static List<GoMethod> methods(String code) {
GoLexer lexer = new GoLexer(new ANTLRInputStream(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
GoParser parser = new GoParser(tokens);
ParseTree tree = parser.sourceFile();
ParseTreeWalker walker = new ParseTreeWalker();
List<GoMethod> list = new ArrayList<>();
GoParserListener listener = new GoParserBaseListener() {
@Override
public void enterFunctionDecl(GoParser.FunctionDeclContext ctx) {
GoParser.SignatureContext signature = ctx.signature();
List<GoParam> params = new ArrayList<>();

//获取参数信息
if (signature != null) {
GoParser.ParametersContext parameters = signature.parameters();
if (parameters != null) {
for (GoParser.ParameterDeclContext paramCtx : parameters.parameterDecl()) {
// 获取参数类型
String paramType = paramCtx.type_().getText();
// 获取参数名称
List<TerminalNode> paramNames = paramCtx.identifierList().IDENTIFIER();
for (TerminalNode paramName : paramNames) {
params.add(GoParam.builder().name(paramName.getText()).type(paramType).build());
}
}
}
}

Token startToken = ctx.getStart();
Token stopToken = ctx.getStop();
// 获取原始的、格式化的函数文本
int startIndex = startToken.getTokenIndex();
int stopIndex = stopToken.getTokenIndex();
List<Token> t = tokens.getTokens(startIndex, stopIndex);
StringBuilder functionText = new StringBuilder();
for (Token token : t) {
functionText.append(token.getText());
}
list.add(GoMethod.builder().name(ctx.IDENTIFIER().getText()).code(functionText.toString()).paramList(params).build());
}
};
walker.walk(listener, tree);
return list;
}

}
301 changes: 301 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoLexer.interp

Large diffs are not rendered by default.

604 changes: 604 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoLexer.java

Large diffs are not rendered by default.

153 changes: 153 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoLexer.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
BREAK=1
DEFAULT=2
FUNC=3
INTERFACE=4
SELECT=5
CASE=6
DEFER=7
GO=8
MAP=9
STRUCT=10
CHAN=11
ELSE=12
GOTO=13
PACKAGE=14
SWITCH=15
CONST=16
FALLTHROUGH=17
IF=18
RANGE=19
TYPE=20
CONTINUE=21
FOR=22
IMPORT=23
RETURN=24
VAR=25
NIL_LIT=26
IDENTIFIER=27
L_PAREN=28
R_PAREN=29
L_CURLY=30
R_CURLY=31
L_BRACKET=32
R_BRACKET=33
ASSIGN=34
COMMA=35
SEMI=36
COLON=37
DOT=38
PLUS_PLUS=39
MINUS_MINUS=40
DECLARE_ASSIGN=41
ELLIPSIS=42
LOGICAL_OR=43
LOGICAL_AND=44
EQUALS=45
NOT_EQUALS=46
LESS=47
LESS_OR_EQUALS=48
GREATER=49
GREATER_OR_EQUALS=50
OR=51
DIV=52
MOD=53
LSHIFT=54
RSHIFT=55
BIT_CLEAR=56
UNDERLYING=57
EXCLAMATION=58
PLUS=59
MINUS=60
CARET=61
STAR=62
AMPERSAND=63
RECEIVE=64
DECIMAL_LIT=65
BINARY_LIT=66
OCTAL_LIT=67
HEX_LIT=68
FLOAT_LIT=69
DECIMAL_FLOAT_LIT=70
HEX_FLOAT_LIT=71
IMAGINARY_LIT=72
RUNE_LIT=73
BYTE_VALUE=74
OCTAL_BYTE_VALUE=75
HEX_BYTE_VALUE=76
LITTLE_U_VALUE=77
BIG_U_VALUE=78
RAW_STRING_LIT=79
INTERPRETED_STRING_LIT=80
WS=81
COMMENT=82
TERMINATOR=83
LINE_COMMENT=84
NEWLINE=85
WS_NLSEMI=86
COMMENT_NLSEMI=87
LINE_COMMENT_NLSEMI=88
EOS=89
OTHER=90
'break'=1
'default'=2
'func'=3
'interface'=4
'select'=5
'case'=6
'defer'=7
'go'=8
'map'=9
'struct'=10
'chan'=11
'else'=12
'goto'=13
'package'=14
'switch'=15
'const'=16
'fallthrough'=17
'if'=18
'range'=19
'type'=20
'continue'=21
'for'=22
'import'=23
'return'=24
'var'=25
'nil'=26
'('=28
')'=29
'{'=30
'}'=31
'['=32
']'=33
'='=34
','=35
';'=36
':'=37
'.'=38
'++'=39
'--'=40
':='=41
'...'=42
'||'=43
'&&'=44
'=='=45
'!='=46
'<'=47
'<='=48
'>'=49
'>='=50
'|'=51
'/'=52
'%'=53
'<<'=54
'>>'=55
'&^'=56
'~'=57
'!'=58
'+'=59
'-'=60
'^'=61
'*'=62
'&'=63
'<-'=64
22 changes: 22 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.mone.antlr.golang;

import lombok.Builder;
import lombok.Data;

import java.util.List;

/**
* @author [email protected]
* @date 2024/1/29 16:56
*/
@Data
@Builder
public class GoMethod {

private String name;

private List<GoParam> paramList;

private String code;

}
18 changes: 18 additions & 0 deletions jcommon/antlr/src/main/java/run/mone/antlr/golang/GoParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package run.mone.antlr.golang;

import lombok.Builder;
import lombok.Data;

/**
* @author [email protected]
* @date 2024/1/29 17:02
*/
@Data
@Builder
public class GoParam {

private String name;

private String type;

}
Loading
Loading