Skip to content

Commit

Permalink
Merge branch 'parsevisitors' into 'dev'
Browse files Browse the repository at this point in the history
Parsevisitors

See merge request monticore/monticore!1022
  • Loading branch information
maritabreuer committed Sep 25, 2024
2 parents 02a7b1f + 834e44f commit 78f7643
Show file tree
Hide file tree
Showing 47 changed files with 1,754 additions and 730 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public ASTCDClass decorate(List<ASTCDPackage> packageList) {
List<ASTCDClass> classList = cd.getCDElementList()
.stream()
.filter(x -> x instanceof ASTCDClass)
.map(x -> (ASTCDClass) x)
.map(ASTCDClass.class::cast)
.filter(x -> !x.getModifier().isAbstract())
.filter(this::checkIncludeInMill)
.map(x -> x.deepClone())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import de.monticore.cd.codegen.CDGenerator;
import de.monticore.cd.codegen.CdUtilsPrinter;
import de.monticore.cd.codegen.TopDecorator;
import de.monticore.cd.methodtemplates.CD4C;
import de.monticore.cdbasis._ast.ASTCDClass;
import de.monticore.cdbasis._ast.ASTCDCompilationUnit;
import de.monticore.codegen.cd2java.AbstractService;
import de.monticore.codegen.cd2java.DecorationHelper;
Expand All @@ -15,6 +17,7 @@
import de.monticore.codegen.cd2java._parser.ParserService;
import de.monticore.codegen.parser.antlr.AntlrTool;
import de.monticore.codegen.parser.antlr.Grammar2Antlr;
import de.monticore.codegen.parser.antlr.Grammar2ParseVisitor;
import de.monticore.generating.GeneratorEngine;
import de.monticore.generating.GeneratorSetup;
import de.monticore.generating.templateengine.GlobalExtensionManagement;
Expand Down Expand Up @@ -63,8 +66,8 @@ public static void generateFullParser(
MCPath templatePath,
File targetDir)
{
generateParser(glex, astGrammar, symbolTable, handcodedPath, templatePath, targetDir);
generateParserWrapper(glex, astClassDiagram, handcodedPath, templatePath, targetDir);
ASTCDClass parseVisitorClass = generateParser(glex, astGrammar, symbolTable, handcodedPath, templatePath, targetDir);
generateParserWrapper(glex, astClassDiagram, parseVisitorClass, handcodedPath, templatePath, targetDir);
}


Expand All @@ -76,15 +79,15 @@ public static void generateFullParser(
* @param astGrammar - grammar AST
* @param targetDir - target file
*/
public static void generateParser(
public static ASTCDClass generateParser(
GlobalExtensionManagement glex,
ASTMCGrammar astGrammar,
IGrammar_WithConceptsGlobalScope symbolTable,
MCPath handcodedPath,
MCPath templatePath,
File targetDir)
{
generateParser(glex, astGrammar, symbolTable, handcodedPath, templatePath, targetDir, true, Languages.JAVA);
return generateParser(glex, astGrammar, symbolTable, handcodedPath, templatePath, targetDir, true, Languages.JAVA);
}


Expand All @@ -97,7 +100,7 @@ public static void generateParser(
* @param targetDir - target file
* @param embeddedJavaCode - embed Java Code
*/
public static void generateParser(
public static ASTCDClass generateParser(
GlobalExtensionManagement glex,
ASTMCGrammar astGrammar,
IGrammar_WithConceptsGlobalScope symbolTable,
Expand Down Expand Up @@ -131,7 +134,7 @@ public static void generateParser(
if (astGrammar.isComponent()) {
ParserInfoGenerator.generateParserInfoForComponent(astGrammar, setup, genHelper.getParserPackage(), lang);
Log.info("No parser generation for the grammar " + astGrammar.getName(), LOG);
return;
return null;
}

Log.debug("Start parser generation for the grammar " + astGrammar.getName(), LOG);
Expand All @@ -148,6 +151,15 @@ public static void generateParser(
astGrammar.getName() + "AntlrParser" + suffix + ".g4");
new GeneratorEngine(setup).generate("parser.Parser", parserPath, astGrammar, grammar2Antlr, suffix);

// 1.5 ParseVisitor
CD4C.init(setup);
traverser = Grammar_WithConceptsMill.traverser();
Grammar2ParseVisitor parserVisitor = new Grammar2ParseVisitor(glex, genHelper, grammarInfo, grammar2Antlr.getTmpNameDict());
traverser.add4Grammar(parserVisitor);
traverser.setGrammarHandler(parserVisitor);
astGrammar.accept(traverser);


// 2. Lexer
suffix = GeneratorEngine.existsHandwrittenClass(handcodedPath,
genHelper.getParserPackage()+"."+astGrammar.getName()+"AntlrLexer")?"TOP":"";
Expand All @@ -162,7 +174,14 @@ public static void generateParser(
Path gLexer = Paths.get(targetDir.getPath(), lexerPath.toString());

AntlrTool antlrTool = new AntlrTool(
new String[] { outputLang, "-o", antlrPath.toString(), "-Xexact-output-dir", "-no-listener", gLexer.toString(), gParser.toString() },
new String[] { outputLang,
"-o", antlrPath.toString(),
"-package", genHelper.getParserPackage(),
"-Xexact-output-dir",
"-no-listener",
"-visitor",
gLexer.toString().replace('\\', '/'), // fix windows paths unicode \\u
gParser.toString().replace('\\', '/') },
grammarSymbol,
grammar2Antlr.getTmpNameDict()
);
Expand All @@ -174,6 +193,8 @@ public static void generateParser(
Log.debug("End parser generation for the grammar " + astGrammar.getName(), LOG);

ParserInfoGenerator.generateParserInfo(astGrammar, setup, antlrTool.getNonTerminalToParserStates(), genHelper.getParserPackage(), lang);

return parserVisitor.getVisitorClass();
}

/**
Expand All @@ -185,6 +206,7 @@ public static void generateParser(
public static void generateParserWrapper(
GlobalExtensionManagement glex,
ASTCDCompilationUnit astClassDiagram,
ASTCDClass parseVisitorClass,
MCPath handcodedPath,
MCPath templatePath,
File targetDir) {
Expand All @@ -195,6 +217,11 @@ public static void generateParserWrapper(

ASTCDCompilationUnit decoratedCD = parserCDDecorator.decorate(astClassDiagram);

if (parseVisitorClass != null) {
// add the ASTBuilder class if exists
decoratedCD.getCDDefinition().getCDPackagesList().iterator().next().addCDElement(parseVisitorClass);
}

TopDecorator topDecorator = new TopDecorator(handcodedPath);
topDecorator.decorate(decoratedCD);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void createMessage(ANTLRMessage message, boolean isError) {
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof String) {
String name = StringTransformations.capitalize((String) args[i]);
Optional<ProdSymbol> rule = grammarSymbol==null?grammarSymbol.getProd(name):Optional.empty();
Optional<ProdSymbol> rule = grammarSymbol!=null?grammarSymbol.getProd(name):Optional.empty();
if (rule.isPresent()) {
args[i] = name;
if (i == 0) {
Expand Down
Loading

0 comments on commit 78f7643

Please sign in to comment.