Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified names for map functions in AbstractAntlrListener.java #1180

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void exitEveryRule(ParserRuleContext rule) {
* @param <T> The type of {@link ParserRuleContext}
* @return The builder for the token
*/
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStartMapping(Class<T> antlrType, TokenType jplagType) {
return this.createStartMapping(antlrType, jplagType, it -> true);
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapEnter(Class<T> antlrType, TokenType jplagType) {
return this.mapEnter(antlrType, jplagType, it -> true);
}

/**
Expand All @@ -107,8 +107,7 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStartMappin
* @return The builder for the token
*/
@SuppressWarnings("unchecked")
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStartMapping(Class<T> antlrType, TokenType jplagType,
Predicate<T> condition) {
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapEnter(Class<T> antlrType, TokenType jplagType, Predicate<T> condition) {
ContextTokenBuilder<T> builder = initTypeBuilder(antlrType, jplagType, condition, ContextTokenBuilderType.START);
this.startMappings.add((ContextTokenBuilder<ParserRuleContext>) builder);
return builder;
Expand All @@ -121,8 +120,8 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStartMappin
* @param <T> The type of {@link ParserRuleContext}
* @return The builder for the token
*/
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStopMapping(Class<T> antlrType, TokenType jplagType) {
return this.createStopMapping(antlrType, jplagType, it -> true);
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapExit(Class<T> antlrType, TokenType jplagType) {
return this.mapExit(antlrType, jplagType, it -> true);
}

/**
Expand All @@ -134,8 +133,7 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStopMapping
* @return The builder for the token
*/
@SuppressWarnings("unchecked")
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStopMapping(Class<T> antlrType, TokenType jplagType,
Predicate<T> condition) {
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapExit(Class<T> antlrType, TokenType jplagType, Predicate<T> condition) {
ContextTokenBuilder<T> builder = initTypeBuilder(antlrType, jplagType, condition, ContextTokenBuilderType.STOP);
this.endMappings.add((ContextTokenBuilder<ParserRuleContext>) builder);
return builder;
Expand All @@ -149,8 +147,8 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createStopMapping
* @param <T> The type of {@link ParserRuleContext}
* @return The builder for the token
*/
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createRangeMapping(Class<T> antlrType, TokenType jplagType) {
return this.createRangeMapping(antlrType, jplagType, it -> true);
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapRange(Class<T> antlrType, TokenType jplagType) {
return this.mapRange(antlrType, jplagType, it -> true);
}

/**
Expand All @@ -163,8 +161,7 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createRangeMappin
* @return The builder for the token
*/
@SuppressWarnings("unchecked")
protected <T extends ParserRuleContext> ContextTokenBuilder<T> createRangeMapping(Class<T> antlrType, TokenType jplagType,
Predicate<T> condition) {
protected <T extends ParserRuleContext> ContextTokenBuilder<T> mapRange(Class<T> antlrType, TokenType jplagType, Predicate<T> condition) {
ContextTokenBuilder<T> builder = initTypeBuilder(antlrType, jplagType, condition, ContextTokenBuilderType.RANGE);
this.rangeMappings.add((ContextTokenBuilder<ParserRuleContext>) builder);
return builder;
Expand All @@ -178,8 +175,8 @@ protected <T extends ParserRuleContext> ContextTokenBuilder<T> createRangeMappin
* @param <T> The type of {@link ParserRuleContext}
* @return The builder for the token
*/
protected <T extends ParserRuleContext> RangeBuilder<T> createStartStopMapping(Class<T> antlrType, TokenType startType, TokenType stopType) {
return createStartStopMapping(antlrType, startType, stopType, it -> true);
protected <T extends ParserRuleContext> RangeBuilder<T> mapEnterExit(Class<T> antlrType, TokenType startType, TokenType stopType) {
return mapEnterExit(antlrType, startType, stopType, it -> true);
}

/**
Expand All @@ -191,10 +188,10 @@ protected <T extends ParserRuleContext> RangeBuilder<T> createStartStopMapping(C
* @param <T> The type of {@link ParserRuleContext}
* @return The builder for the token
*/
protected <T extends ParserRuleContext> RangeBuilder<T> createStartStopMapping(Class<T> antlrType, TokenType startType, TokenType stopType,
protected <T extends ParserRuleContext> RangeBuilder<T> mapEnterExit(Class<T> antlrType, TokenType startType, TokenType stopType,
Predicate<T> condition) {
ContextTokenBuilder<T> start = this.createStartMapping(antlrType, startType, condition);
ContextTokenBuilder<T> end = this.createStopMapping(antlrType, stopType, condition);
ContextTokenBuilder<T> start = this.mapEnter(antlrType, startType, condition);
ContextTokenBuilder<T> end = this.mapExit(antlrType, stopType, condition);
return new RangeBuilder<>(start, end);
}

Expand All @@ -204,8 +201,8 @@ protected <T extends ParserRuleContext> RangeBuilder<T> createStartStopMapping(C
* @param jplagType The jplag token type
* @return The builder for the token
*/
protected TerminalTokenBuilder createTerminalMapping(int terminalType, TokenType jplagType) {
return this.createTerminalMapping(terminalType, jplagType, it -> true);
protected TerminalTokenBuilder mapTerminal(int terminalType, TokenType jplagType) {
return this.mapTerminal(terminalType, jplagType, it -> true);
}

/**
Expand All @@ -215,7 +212,7 @@ protected TerminalTokenBuilder createTerminalMapping(int terminalType, TokenType
* @param condition The condition under which the mapping applies
* @return The builder for the token
*/
protected TerminalTokenBuilder createTerminalMapping(int terminalType, TokenType jplagType, Predicate<org.antlr.v4.runtime.Token> condition) {
protected TerminalTokenBuilder mapTerminal(int terminalType, TokenType jplagType, Predicate<org.antlr.v4.runtime.Token> condition) {
TerminalTokenBuilder builder = new TerminalTokenBuilder(jplagType, token -> token.getType() == terminalType && condition.test(token),
this.collector, this.currentFile);
this.terminalMapping.add(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ public class TestListener extends AbstractAntlrListener {
public TestListener(TokenCollector collector, File currentFile) {
super(collector, currentFile, true);

createStartMapping(VarDefContext.class, VARDEF).addAsVariable(VariableScope.FILE, false, rule -> rule.VAR_NAME().getText())
mapEnter(VarDefContext.class, VARDEF).addAsVariable(VariableScope.FILE, false, rule -> rule.VAR_NAME().getText())
.withSemantics(CodeSemantics.createKeep());

createRangeMapping(CalcExpressionContext.class, ADDITION, rule -> rule.operator() != null && rule.operator().PLUS() != null)
.withControlSemantics();
createRangeMapping(OperatorContext.class, SUBTRACTION, rule -> rule.MINUS() != null).withControlSemantics();
createStartStopMapping(SubExpressionContext.class, SUB_EXPRESSION_BEGIN, SUB_EXPRESSION_END)
mapRange(CalcExpressionContext.class, ADDITION, rule -> rule.operator() != null && rule.operator().PLUS() != null).withControlSemantics();
mapRange(OperatorContext.class, SUBTRACTION, rule -> rule.MINUS() != null).withControlSemantics();
mapEnterExit(SubExpressionContext.class, SUB_EXPRESSION_BEGIN, SUB_EXPRESSION_END)
// .addEndSemanticHandler(registry -> registry.addAllNonLocalVariablesAsReads())
// does not work here, because there is no class context. Is still here as an example.
.addLocalScope().withControlSemantics();
createTerminalMapping(TestParser.NUMBER, NUMBER).withSemantics(CodeSemantics.createKeep());
createStartMapping(VarRefContext.class, VARREF).withSemantics(CodeSemantics.createKeep());
mapTerminal(TestParser.NUMBER, NUMBER).withSemantics(CodeSemantics.createKeep());
mapEnter(VarRefContext.class, VARREF).withSemantics(CodeSemantics.createKeep());
}
}
72 changes: 36 additions & 36 deletions languages/cpp2/src/main/java/de/jplag/cpp2/CPPListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,50 @@ public class CPPListener extends AbstractAntlrListener {
public CPPListener(TokenCollector collector, File currentFile) {
super(collector, currentFile);

createStartStopMapping(ClassSpecifierContext.class, UNION_BEGIN, UNION_END, rule -> rule.classHead().Union() != null);
createStartStopMapping(ClassSpecifierContext.class, CLASS_BEGIN, CLASS_END,
mapEnterExit(ClassSpecifierContext.class, UNION_BEGIN, UNION_END, rule -> rule.classHead().Union() != null);
mapEnterExit(ClassSpecifierContext.class, CLASS_BEGIN, CLASS_END,
rule -> rule.classHead().classKey() != null && rule.classHead().classKey().Class() != null);
createStartStopMapping(ClassSpecifierContext.class, STRUCT_BEGIN, STRUCT_END,
mapEnterExit(ClassSpecifierContext.class, STRUCT_BEGIN, STRUCT_END,
rule -> rule.classHead().classKey() != null && rule.classHead().classKey().Struct() != null);
createStartStopMapping(EnumSpecifierContext.class, ENUM_BEGIN, ENUM_END);
mapEnterExit(EnumSpecifierContext.class, ENUM_BEGIN, ENUM_END);

createStartStopMapping(FunctionDefinitionContext.class, FUNCTION_BEGIN, FUNCTION_END);
mapEnterExit(FunctionDefinitionContext.class, FUNCTION_BEGIN, FUNCTION_END);

createStartStopMapping(IterationStatementContext.class, DO_BEGIN, DO_END, rule -> rule.Do() != null);
createStartStopMapping(IterationStatementContext.class, FOR_BEGIN, FOR_END, rule -> rule.For() != null);
createStartStopMapping(IterationStatementContext.class, WHILE_BEGIN, WHILE_END, rule -> rule.While() != null && rule.Do() == null);
mapEnterExit(IterationStatementContext.class, DO_BEGIN, DO_END, rule -> rule.Do() != null);
mapEnterExit(IterationStatementContext.class, FOR_BEGIN, FOR_END, rule -> rule.For() != null);
mapEnterExit(IterationStatementContext.class, WHILE_BEGIN, WHILE_END, rule -> rule.While() != null && rule.Do() == null);

createStartStopMapping(SelectionStatementContext.class, SWITCH_BEGIN, SWITCH_END, rule -> rule.Switch() != null);
createStartStopMapping(SelectionStatementContext.class, IF_BEGIN, IF_END, rule -> rule.If() != null);
createTerminalMapping(CPP14Parser.Else, ELSE);
mapEnterExit(SelectionStatementContext.class, SWITCH_BEGIN, SWITCH_END, rule -> rule.Switch() != null);
mapEnterExit(SelectionStatementContext.class, IF_BEGIN, IF_END, rule -> rule.If() != null);
mapTerminal(CPP14Parser.Else, ELSE);

createStartMapping(LabeledStatementContext.class, CASE, rule -> rule.Case() != null);
createStartMapping(LabeledStatementContext.class, DEFAULT, rule -> rule.Default() != null);
mapEnter(LabeledStatementContext.class, CASE, rule -> rule.Case() != null);
mapEnter(LabeledStatementContext.class, DEFAULT, rule -> rule.Default() != null);

createStartMapping(TryBlockContext.class, TRY);
createStartStopMapping(HandlerContext.class, CATCH_BEGIN, CATCH_END);
mapEnter(TryBlockContext.class, TRY);
mapEnterExit(HandlerContext.class, CATCH_BEGIN, CATCH_END);

createStartMapping(JumpStatementContext.class, BREAK, rule -> rule.Break() != null);
createStartMapping(JumpStatementContext.class, CONTINUE, rule -> rule.Continue() != null);
createStartMapping(JumpStatementContext.class, GOTO, rule -> rule.Goto() != null);
createStartMapping(JumpStatementContext.class, RETURN, rule -> rule.Return() != null);
mapEnter(JumpStatementContext.class, BREAK, rule -> rule.Break() != null);
mapEnter(JumpStatementContext.class, CONTINUE, rule -> rule.Continue() != null);
mapEnter(JumpStatementContext.class, GOTO, rule -> rule.Goto() != null);
mapEnter(JumpStatementContext.class, RETURN, rule -> rule.Return() != null);

createStartMapping(ThrowExpressionContext.class, THROW);
mapEnter(ThrowExpressionContext.class, THROW);

createStartMapping(NewExpressionContext.class, NEWCLASS, rule -> rule.newInitializer() != null);
createStartMapping(NewExpressionContext.class, NEWARRAY, rule -> rule.newInitializer() == null);
mapEnter(NewExpressionContext.class, NEWCLASS, rule -> rule.newInitializer() != null);
mapEnter(NewExpressionContext.class, NEWARRAY, rule -> rule.newInitializer() == null);

createStartMapping(TemplateDeclarationContext.class, GENERIC);
mapEnter(TemplateDeclarationContext.class, GENERIC);

createStartMapping(AssignmentOperatorContext.class, ASSIGN);
createStartMapping(BraceOrEqualInitializerContext.class, ASSIGN, rule -> rule.Assign() != null);
createStartMapping(UnaryExpressionContext.class, ASSIGN, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null);
mapEnter(AssignmentOperatorContext.class, ASSIGN);
mapEnter(BraceOrEqualInitializerContext.class, ASSIGN, rule -> rule.Assign() != null);
mapEnter(UnaryExpressionContext.class, ASSIGN, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null);

createStartMapping(StaticAssertDeclarationContext.class, STATIC_ASSERT);
createStartMapping(EnumeratorDefinitionContext.class, VARDEF);
createStartStopMapping(BracedInitListContext.class, BRACED_INIT_BEGIN, BRACED_INIT_END);
mapEnter(StaticAssertDeclarationContext.class, STATIC_ASSERT);
mapEnter(EnumeratorDefinitionContext.class, VARDEF);
mapEnterExit(BracedInitListContext.class, BRACED_INIT_BEGIN, BRACED_INIT_END);

createStartMapping(SimpleTypeSpecifierContext.class, VARDEF, rule -> {
mapEnter(SimpleTypeSpecifierContext.class, VARDEF, rule -> {
if (hasAncestor(rule, MemberdeclarationContext.class, FunctionDefinitionContext.class)) {
return true;
}
Expand All @@ -82,7 +82,7 @@ public CPPListener(TokenCollector collector, File currentFile) {
return false;
});

createStartMapping(SimpleDeclarationContext.class, APPLY, rule -> {
mapEnter(SimpleDeclarationContext.class, APPLY, rule -> {
if (!hasAncestor(rule, FunctionBodyContext.class)) {
return false;
}
Expand All @@ -91,12 +91,12 @@ public CPPListener(TokenCollector collector, File currentFile) {
return noPointerInFunctionCallContext(noPointerDecl);
});

createStartMapping(InitDeclaratorContext.class, APPLY, rule -> rule.initializer() != null && rule.initializer().LeftParen() != null);
createStartMapping(ParameterDeclarationContext.class, VARDEF);
createStartMapping(ConditionalExpressionContext.class, QUESTIONMARK, rule -> rule.Question() != null);
mapEnter(InitDeclaratorContext.class, APPLY, rule -> rule.initializer() != null && rule.initializer().LeftParen() != null);
mapEnter(ParameterDeclarationContext.class, VARDEF);
mapEnter(ConditionalExpressionContext.class, QUESTIONMARK, rule -> rule.Question() != null);

createStartMapping(PostfixExpressionContext.class, APPLY, rule -> rule.LeftParen() != null);
createStartMapping(PostfixExpressionContext.class, ASSIGN, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null);
mapEnter(PostfixExpressionContext.class, APPLY, rule -> rule.LeftParen() != null);
mapEnter(PostfixExpressionContext.class, ASSIGN, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null);
}

/**
Expand Down
Loading
Loading