-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'XiaoMi:master' into master
- Loading branch information
Showing
922 changed files
with
139,858 additions
and
4,572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
jcommon/antlr/src/main/java/run/mone/antlr/golang/GoCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.