Skip to content

Commit

Permalink
#4448: LexerMode Parser-Attribute (#79)
Browse files Browse the repository at this point in the history
* #4448: new method setMode

* update test

* refactoring
  • Loading branch information
maritabreuer authored Jan 20, 2025
1 parent 7c7d1c2 commit a383dff
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -54,6 +60,7 @@ public Optional<ASTCDClass> decorate(ASTCDCompilationUnit input){
.addAllCDMembers(createCreateMethods(grammarName))
.addAllCDMembers(createParseMethods(startRuleName, qualifiedStartRuleName))
.addAllCDMembers(createParseMethodsForProds(grammarName, prods))
.addAllCDMembers(createMode())
.build());
}
}
Expand All @@ -79,10 +86,22 @@ protected List<ASTCDMethod> createCreateMethods(String grammarName){
this.replaceTemplate(EMPTY_BODY, createReader, new TemplateHookPoint(TEMPLATE_PATH + "CreateReader", grammarName));
methods.add(createReader);


return methods;
}

protected List<ASTCDMember> createMode(){
List<ASTCDMember> 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<ASTCDMethod> createParseMethods(String startRuleName, String startRuleFullName){
List<ASTCDMethod> methods = Lists.newArrayList();
ASTMCQualifiedName ioException = MCBasicTypesMill.mCQualifiedNameBuilder()
Expand Down
8 changes: 8 additions & 0 deletions monticore-generator/src/main/resources/_parser/Create.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

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

}

0 comments on commit a383dff

Please sign in to comment.