add go lang empty skeleton

This commit is contained in:
Gang ZHANG 2020-09-13 10:57:03 +08:00
parent 0239dbad2c
commit a41d0bc65b
8 changed files with 400 additions and 2 deletions

View File

@ -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() {

View File

@ -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[]{
"<Built-in>"
};
}
@Override
public String[] getBuiltInPrefixStr() {
return new String[]{
};
}
@Override
public String[] getBuiltInMethods() {
return new String[]{};
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<Entity> getImportedRelationEntities(List<Import> importedList, EntityRepo repo) {
ArrayList<Entity> 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<Entity> getImportedTypes(List<Import> importedList, EntityRepo repo, Set<UnsolvedBindings> unsolvedBindings) {
ArrayList<Entity> 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<Entity> getImportedFiles(List<Import> importedList, EntityRepo repo) {
return new ArrayList<Entity>();
}
@Override
public boolean supportGlobalNameLookup() {
return true;
}
}

View File

@ -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() {
}
}

View File

@ -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<String> supportedRelations() {
ArrayList<String> 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;
}
}

View File

@ -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);
}
}