From a383dffb73020980cc8a98a2ffc5c7979c16ad97 Mon Sep 17 00:00:00 2001 From: maritabreuer Date: Mon, 20 Jan 2025 15:02:13 +0100 Subject: [PATCH] #4448: LexerMode Parser-Attribute (#79) * #4448: new method setMode * update test * refactoring --- .../cd2java/_parser/ParserClassDecorator.java | 27 ++++++++++++++++--- .../src/main/resources/_parser/Create.ftl | 8 ++++++ .../main/resources/_parser/CreateReader.ftl | 8 ++++++ .../_parser/ParserClassDecoratorTest.java | 6 ++--- .../ParserTest.java | 8 +++++- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/monticore-generator/src/main/java/de/monticore/codegen/cd2java/_parser/ParserClassDecorator.java b/monticore-generator/src/main/java/de/monticore/codegen/cd2java/_parser/ParserClassDecorator.java index 841da0069f..fda25111f4 100644 --- a/monticore-generator/src/main/java/de/monticore/codegen/cd2java/_parser/ParserClassDecorator.java +++ b/monticore-generator/src/main/java/de/monticore/codegen/cd2java/_parser/ParserClassDecorator.java @@ -3,15 +3,19 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import de.monticore.cd.facade.CDAttributeFacade; import de.monticore.cd4analysis.CD4AnalysisMill; import de.monticore.cd4code.CD4CodeMill; import de.monticore.cd4codebasis.CD4CodeBasisMill; -import de.monticore.cd4codebasis._ast.*; +import de.monticore.cd4codebasis._ast.ASTCDMethod; +import de.monticore.cd4codebasis._ast.ASTCDParameter; import de.monticore.cdbasis._ast.*; -import de.monticore.symbols.basicsymbols._symboltable.DiagramSymbol; import de.monticore.codegen.cd2java.AbstractDecorator; +import de.monticore.codegen.cd2java.methods.MethodDecorator; import de.monticore.generating.templateengine.GlobalExtensionManagement; +import de.monticore.generating.templateengine.StringHookPoint; import de.monticore.generating.templateengine.TemplateHookPoint; +import de.monticore.symbols.basicsymbols._symboltable.DiagramSymbol; import de.monticore.types.mcbasictypes.MCBasicTypesMill; import de.monticore.types.mcbasictypes._ast.ASTMCQualifiedName; import de.monticore.types.mcbasictypes._ast.ASTMCQualifiedType; @@ -22,8 +26,10 @@ import java.util.Map; import java.util.Optional; -import static de.monticore.cd.facade.CDModifier.*; import static de.monticore.cd.codegen.CD2JavaTemplates.EMPTY_BODY; +import static de.monticore.cd.codegen.CD2JavaTemplates.VALUE; +import static de.monticore.cd.facade.CDModifier.PROTECTED; +import static de.monticore.cd.facade.CDModifier.PUBLIC; public class ParserClassDecorator extends AbstractDecorator { @@ -54,6 +60,7 @@ public Optional decorate(ASTCDCompilationUnit input){ .addAllCDMembers(createCreateMethods(grammarName)) .addAllCDMembers(createParseMethods(startRuleName, qualifiedStartRuleName)) .addAllCDMembers(createParseMethodsForProds(grammarName, prods)) + .addAllCDMembers(createMode()) .build()); } } @@ -79,10 +86,22 @@ protected List createCreateMethods(String grammarName){ this.replaceTemplate(EMPTY_BODY, createReader, new TemplateHookPoint(TEMPLATE_PATH + "CreateReader", grammarName)); methods.add(createReader); - return methods; } + protected List createMode(){ + List members = Lists.newArrayList(); + + ASTCDAttribute modeAttribute = CDAttributeFacade.getInstance().createAttribute(PROTECTED.build(), "String", "lexerMode"); + this.replaceTemplate(VALUE, modeAttribute, new StringHookPoint("= \"\";")); + members.add(modeAttribute); + + MethodDecorator methodDecorator = new MethodDecorator(glex, service); + members.addAll(methodDecorator.getMutatorDecorator().decorate(modeAttribute)); + + return members; + } + protected List createParseMethods(String startRuleName, String startRuleFullName){ List methods = Lists.newArrayList(); ASTMCQualifiedName ioException = MCBasicTypesMill.mCQualifiedNameBuilder() diff --git a/monticore-generator/src/main/resources/_parser/Create.ftl b/monticore-generator/src/main/resources/_parser/Create.ftl index 0a96aae6d6..b859ae21a6 100644 --- a/monticore-generator/src/main/resources/_parser/Create.ftl +++ b/monticore-generator/src/main/resources/_parser/Create.ftl @@ -8,4 +8,12 @@ ${tc.signature("grammarName")} lexer.addErrorListener(new de.monticore.antlr4.MCErrorListener(parser)); parser.setFilename(fileName); setError(false); + if (!lexerMode.isEmpty()) { + int index = Arrays.asList(lexer.getModeNames()).indexOf(lexerMode); + if (index>=0) { + lexer.mode(index); + } else { + Log.error("0xA0110${service.getGeneratedErrorCode(grammarName)} Invalid mode name " + lexerMode); + } + } return parser; \ No newline at end of file diff --git a/monticore-generator/src/main/resources/_parser/CreateReader.ftl b/monticore-generator/src/main/resources/_parser/CreateReader.ftl index d3e6d70c27..2bf8b54f7c 100644 --- a/monticore-generator/src/main/resources/_parser/CreateReader.ftl +++ b/monticore-generator/src/main/resources/_parser/CreateReader.ftl @@ -8,4 +8,12 @@ ${tc.signature("grammarName")} lexer.addErrorListener(new de.monticore.antlr4.MCErrorListener(parser)); parser.setFilename("StringReader"); setError(false); + if (!lexerMode.isEmpty()) { + int index = Arrays.asList(lexer.getModeNames()).indexOf(lexerMode); + if (index>=0) { + lexer.mode(index); + } else { + Log.error("0xA01101${service.getGeneratedErrorCode(grammarName)} Invalid mode name " + lexerMode); + } + } return parser; \ No newline at end of file diff --git a/monticore-generator/src/test/java/de/monticore/codegen/cd2java/_parser/ParserClassDecoratorTest.java b/monticore-generator/src/test/java/de/monticore/codegen/cd2java/_parser/ParserClassDecoratorTest.java index 52ddb9cd0f..0c92e8e442 100644 --- a/monticore-generator/src/test/java/de/monticore/codegen/cd2java/_parser/ParserClassDecoratorTest.java +++ b/monticore-generator/src/test/java/de/monticore/codegen/cd2java/_parser/ParserClassDecoratorTest.java @@ -97,8 +97,8 @@ public void testSuperclass(){ } @Test - public void testNoAttributes(){ - assertTrue(parserClass.getCDAttributeList().isEmpty()); + public void testNoOfAttributes(){ + assertEquals(1, parserClass.getCDAttributeList().size()); assertTrue(Log.getFindings().isEmpty()); } @@ -112,7 +112,7 @@ public void testNoConstructors(){ @Test public void testMethodCount(){ - assertEquals(29, parserClass.getCDMethodList().size()); + assertEquals(30, parserClass.getCDMethodList().size()); assertTrue(Log.getFindings().isEmpty()); } diff --git a/monticore-grammar/src/test/java/de/monticore/expressions/combineexpressionswithliterals/ParserTest.java b/monticore-grammar/src/test/java/de/monticore/expressions/combineexpressionswithliterals/ParserTest.java index 93e654adc6..5661fa77f5 100644 --- a/monticore-grammar/src/test/java/de/monticore/expressions/combineexpressionswithliterals/ParserTest.java +++ b/monticore-grammar/src/test/java/de/monticore/expressions/combineexpressionswithliterals/ParserTest.java @@ -1,6 +1,7 @@ /* (c) https://github.com/MontiCore/monticore */ package de.monticore.expressions.combineexpressionswithliterals; +import de.monticore.expressions.combineexpressionswithliterals._parser.CombineExpressionsWithLiteralsParser; import de.monticore.expressions.expressionsbasis._ast.ASTExpression; import org.junit.Test; @@ -40,6 +41,11 @@ public void parseBigExpr2() throws IOException { assertTrue(ast.isPresent()); } - + @Test + public void parseWithMode() throws IOException { + CombineExpressionsWithLiteralsParser parser = CombineExpressionsWithLiteralsMill.parser(); + parser.setLexerMode("REGEX"); + parser.parse_StringCharRange("a-c").get(); + } } \ No newline at end of file