From a41d0bc65bcb34a4b37983e789d2c0f4b4b5df99 Mon Sep 17 00:00:00 2001 From: Gang ZHANG Date: Sun, 13 Sep 2020 10:57:03 +0800 Subject: [PATCH] add go lang empty skeleton --- src/main/java/depends/LangRegister.java | 3 +- .../extractor/golang/GoBuiltInType.java | 50 +++++++++ .../extractor/golang/GoFileParser.java | 77 +++++++++++++ .../extractor/golang/GoHandlerContext.java | 39 +++++++ .../golang/GoImportLookupStrategy.java | 101 ++++++++++++++++++ .../depends/extractor/golang/GoListener.java | 22 ++++ .../depends/extractor/golang/GoProcessor.java | 88 +++++++++++++++ .../extractor/golang/GolangParserTest.java | 22 ++++ 8 files changed, 400 insertions(+), 2 deletions(-) create mode 100644 src/main/java/depends/extractor/golang/GoBuiltInType.java create mode 100644 src/main/java/depends/extractor/golang/GoFileParser.java create mode 100644 src/main/java/depends/extractor/golang/GoHandlerContext.java create mode 100644 src/main/java/depends/extractor/golang/GoImportLookupStrategy.java create mode 100644 src/main/java/depends/extractor/golang/GoListener.java create mode 100644 src/main/java/depends/extractor/golang/GoProcessor.java create mode 100644 src/test/java/depends/extractor/golang/GolangParserTest.java diff --git a/src/main/java/depends/LangRegister.java b/src/main/java/depends/LangRegister.java index ea074ab..2fed90e 100644 --- a/src/main/java/depends/LangRegister.java +++ b/src/main/java/depends/LangRegister.java @@ -28,8 +28,6 @@ import depends.extractor.AbstractLangProcessor; import depends.extractor.LangProcessorRegistration; public class LangRegister { - - public LangRegister() { add (new depends.extractor.java.JavaProcessor()); add (new depends.extractor.cpp.CppProcessor()); @@ -37,6 +35,7 @@ public class LangRegister { add (new depends.extractor.pom.PomProcessor()); add (new depends.extractor.kotlin.KotlinProcessor()); add (new depends.extractor.python.union.PythonProcessor()); + add (new depends.extractor.golang.GoProcessor()); } public void register() { diff --git a/src/main/java/depends/extractor/golang/GoBuiltInType.java b/src/main/java/depends/extractor/golang/GoBuiltInType.java new file mode 100644 index 0000000..c20e1f3 --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoBuiltInType.java @@ -0,0 +1,50 @@ +/* +MIT License + +Copyright (c) 2018-2019 Gang ZHANG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package depends.extractor.golang; + +import depends.entity.repo.BuiltInType; + +public class GoBuiltInType extends BuiltInType{ + + public GoBuiltInType() { + super.createBuiltInTypes(); + } + @Override + public String[] getBuiltInTypeStr() { + return new String[]{ + "" + }; + } + @Override + public String[] getBuiltInPrefixStr() { + return new String[]{ + }; + } + @Override + public String[] getBuiltInMethods() { + return new String[]{}; + } + +} diff --git a/src/main/java/depends/extractor/golang/GoFileParser.java b/src/main/java/depends/extractor/golang/GoFileParser.java new file mode 100644 index 0000000..0bb27aa --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoFileParser.java @@ -0,0 +1,77 @@ +/* +MIT License + +Copyright (c) 2018-2019 Gang ZHANG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package depends.extractor.golang; + +import depends.entity.Entity; +import depends.entity.FileEntity; +import depends.entity.repo.EntityRepo; +import depends.relations.Inferer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.atn.LexerATNSimulator; +import org.antlr.v4.runtime.atn.ParserATNSimulator; +import org.antlr.v4.runtime.atn.PredictionContextCache; +import org.antlr.v4.runtime.tree.ParseTreeWalker; + +import java.io.IOException; + + +public class GoFileParser implements depends.extractor.FileParser{ + private String fileFullPath; + private EntityRepo entityRepo; + private Inferer inferer; + public GoFileParser(String fileFullPath, EntityRepo entityRepo, Inferer inferer) { + this.fileFullPath = fileFullPath; + this.entityRepo = entityRepo; + this.inferer = inferer; + } + + @Override + public void parse() throws IOException { + CharStream input = CharStreams.fromFileName(fileFullPath); + Lexer lexer = new GoLexer(input); + lexer.setInterpreter(new LexerATNSimulator(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, new PredictionContextCache())); + CommonTokenStream tokens = new CommonTokenStream(lexer); + GoParser parser = new GoParser(tokens); + ParserATNSimulator interpreter = new ParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, new PredictionContextCache()); + parser.setInterpreter(interpreter); + GoListener bridge = new GoListener(fileFullPath, entityRepo,inferer); + ParseTreeWalker walker = new ParseTreeWalker(); + try { + walker.walk(bridge, parser.sourceFile()); + Entity fileEntity = entityRepo.getEntity(fileFullPath); + ((FileEntity)fileEntity).cacheAllExpressions(); + interpreter.clearDFA(); + bridge.done(); + }catch (Exception e) { + System.err.println("error encountered during parse..." ); + e.printStackTrace(); + } + + } + +} diff --git a/src/main/java/depends/extractor/golang/GoHandlerContext.java b/src/main/java/depends/extractor/golang/GoHandlerContext.java new file mode 100644 index 0000000..e541cc9 --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoHandlerContext.java @@ -0,0 +1,39 @@ +/* +MIT License + +Copyright (c) 2018-2019 Gang ZHANG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package depends.extractor.golang; + +import depends.entity.repo.EntityRepo; +import depends.extractor.HandlerContext; +import depends.relations.Inferer; + +public class GoHandlerContext extends HandlerContext { + + public GoHandlerContext(EntityRepo entityRepo, Inferer inferer) { + super(entityRepo,inferer); + } + + + +} diff --git a/src/main/java/depends/extractor/golang/GoImportLookupStrategy.java b/src/main/java/depends/extractor/golang/GoImportLookupStrategy.java new file mode 100644 index 0000000..0562031 --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoImportLookupStrategy.java @@ -0,0 +1,101 @@ +/* +MIT License + +Copyright (c) 2018-2019 Gang ZHANG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package depends.extractor.golang; + +import depends.entity.Entity; +import depends.entity.FileEntity; +import depends.entity.PackageEntity; +import depends.entity.repo.EntityRepo; +import depends.extractor.UnsolvedBindings; +import depends.importtypes.Import; +import depends.relations.ImportLookupStrategy; +import depends.relations.Inferer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class GoImportLookupStrategy implements ImportLookupStrategy{ + @Override + public Entity lookupImportedType(String name, FileEntity fileEntity, EntityRepo repo, Inferer inferer) { + //Java Strategy + String importedString = fileEntity.importedSuffixMatch(name); + if (importedString==null) return null; + return repo.getEntity(importedString); + } + + + @Override + public List getImportedRelationEntities(List importedList, EntityRepo repo) { + ArrayList result = new ArrayList<>(); + for (Import importedItem:importedList) { + Entity imported = repo.getEntity(importedItem.getContent()); + if (imported==null) continue; + if (imported instanceof PackageEntity) { + //ignore wildcard import relation + }else { + result.add(imported); + } + } + return result; + } + + @Override + public List getImportedTypes(List importedList, EntityRepo repo, Set unsolvedBindings) { + ArrayList result = new ArrayList<>(); + for (Import importedItem:importedList) { + Entity imported = repo.getEntity(importedItem.getContent()); + if (imported==null) { + unsolvedBindings.add(new UnsolvedBindings(importedItem.getContent(),null)); + continue; + } + if (imported instanceof PackageEntity) { + //expand import of package to all classes under the package due to we dis-courage the behavior + for (Entity child:imported.getChildren()) { + if (child instanceof FileEntity) { + child.getChildren().forEach(item->result.add(item)); + }else { + result.add(child); + } + } + }else { + result.add(imported); + } + } + return result; + } + + @Override + public List getImportedFiles(List importedList, EntityRepo repo) { + return new ArrayList(); + } + + + @Override + public boolean supportGlobalNameLookup() { + return true; + } + +} diff --git a/src/main/java/depends/extractor/golang/GoListener.java b/src/main/java/depends/extractor/golang/GoListener.java new file mode 100644 index 0000000..dd8a7b8 --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoListener.java @@ -0,0 +1,22 @@ +package depends.extractor.golang; + +import depends.entity.repo.EntityRepo; +import depends.relations.Inferer; + +public class GoListener extends GoParserBaseListener { + GoHandlerContext context; + @Override + public void enterSourceFile(GoParser.SourceFileContext ctx) { + super.enterSourceFile(ctx); + } + + public GoListener(String fileFullPath, EntityRepo entityRepo, Inferer inferer) { + context = new GoHandlerContext(entityRepo,inferer); + context.startFile(fileFullPath); + } + + + public void done() { + + } +} diff --git a/src/main/java/depends/extractor/golang/GoProcessor.java b/src/main/java/depends/extractor/golang/GoProcessor.java new file mode 100644 index 0000000..1d111ad --- /dev/null +++ b/src/main/java/depends/extractor/golang/GoProcessor.java @@ -0,0 +1,88 @@ +/* +MIT License + +Copyright (c) 2018-2019 Gang ZHANG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package depends.extractor.golang; + +import depends.entity.repo.BuiltInType; +import depends.extractor.AbstractLangProcessor; +import depends.extractor.FileParser; +import depends.relations.ImportLookupStrategy; + +import java.util.ArrayList; +import java.util.List; + +import static depends.deptypes.DependencyType.*; + +public class GoProcessor extends AbstractLangProcessor { + private static final String LANG = "go"; + private static final String[] SUFFIX = new String[] { ".go" }; + public GoProcessor() { + super(false); + } + + @Override + public String supportedLanguage() { + return LANG; + } + + @Override + public String[] fileSuffixes() { + return SUFFIX; + } + + @Override + protected FileParser createFileParser(String fileFullPath) { + return new GoFileParser(fileFullPath, entityRepo, inferer); + } + + @Override + public ImportLookupStrategy getImportLookupStrategy() { + return new GoImportLookupStrategy(); + } + + @Override + public BuiltInType getBuiltInType() { + return new GoBuiltInType(); + } + + @Override + public List supportedRelations() { + ArrayList depedencyTypes = new ArrayList<>(); + depedencyTypes.add(IMPORT); + depedencyTypes.add(CONTAIN); + depedencyTypes.add(IMPLEMENT); + depedencyTypes.add(INHERIT); + depedencyTypes.add(CALL); + depedencyTypes.add(PARAMETER); + depedencyTypes.add(RETURN); + depedencyTypes.add(SET); + depedencyTypes.add(CREATE); + depedencyTypes.add(USE); + depedencyTypes.add(CAST); + depedencyTypes.add(THROW); + depedencyTypes.add(IMPLLINK); + return depedencyTypes; + } + +} diff --git a/src/test/java/depends/extractor/golang/GolangParserTest.java b/src/test/java/depends/extractor/golang/GolangParserTest.java new file mode 100644 index 0000000..7c59f41 --- /dev/null +++ b/src/test/java/depends/extractor/golang/GolangParserTest.java @@ -0,0 +1,22 @@ +package depends.extractor.golang; + +import depends.entity.repo.EntityRepo; +import depends.entity.repo.InMemoryEntityRepo; +import depends.extractor.ParserTest; +import depends.relations.Inferer; +import multilang.depends.util.file.TemporaryFile; + +public abstract class GolangParserTest extends ParserTest{ + protected EntityRepo entityRepo ; + protected Inferer inferer ; + + public void init() { + entityRepo = new InMemoryEntityRepo(); + inferer = new Inferer(entityRepo,new GoImportLookupStrategy(),new GoBuiltInType(),false); + TemporaryFile.reset(); + } + + public GoFileParser createParser(String src) { + return new GoFileParser(src,entityRepo, inferer); + } +}