From 4f12de7930c471a11109175007bc4b319dbbfaaf Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 4 Oct 2023 10:18:03 +0200 Subject: [PATCH] Refactored CSharpModule to new framework. --- languages/csharp/pom.xml | 5 + .../java/de/jplag/csharp/CSharpLanguage.java | 18 +- .../java/de/jplag/csharp/CSharpListener.java | 590 +++--------------- .../de/jplag/csharp/CSharpParserAdapter.java | 76 +-- .../test/java/de/jplag/csharp/CSharpTest.java | 24 +- 5 files changed, 110 insertions(+), 603 deletions(-) diff --git a/languages/csharp/pom.xml b/languages/csharp/pom.xml index 9bf5f4b66..12213357f 100644 --- a/languages/csharp/pom.xml +++ b/languages/csharp/pom.xml @@ -13,6 +13,11 @@ org.antlr antlr4-runtime + + de.jplag + language-antlr-utils + ${revision} + diff --git a/languages/csharp/src/main/java/de/jplag/csharp/CSharpLanguage.java b/languages/csharp/src/main/java/de/jplag/csharp/CSharpLanguage.java index 401727bf6..455ee4179 100644 --- a/languages/csharp/src/main/java/de/jplag/csharp/CSharpLanguage.java +++ b/languages/csharp/src/main/java/de/jplag/csharp/CSharpLanguage.java @@ -1,29 +1,22 @@ package de.jplag.csharp; -import java.io.File; -import java.util.List; -import java.util.Set; - import org.kohsuke.MetaInfServices; -import de.jplag.ParsingException; -import de.jplag.Token; +import de.jplag.antlr.AbstractAntlrLanguage; /** * C# language with full support of C# 6 features and below. * @author Timur Saglam */ @MetaInfServices(de.jplag.Language.class) -public class CSharpLanguage implements de.jplag.Language { +public class CSharpLanguage extends AbstractAntlrLanguage { private static final String NAME = "C# 6 Parser"; private static final String IDENTIFIER = "csharp"; private static final String[] FILE_ENDINGS = new String[] {".cs", ".CS"}; private static final int DEFAULT_MIN_TOKEN_MATCH = 8; - private final CSharpParserAdapter parser; - public CSharpLanguage() { - parser = new CSharpParserAdapter(); + super(new CSharpParserAdapter()); } @Override @@ -45,9 +38,4 @@ public String getIdentifier() { public int minimumTokenMatch() { return DEFAULT_MIN_TOKEN_MATCH; } - - @Override - public List parse(Set files) throws ParsingException { - return parser.parse(files); - } } diff --git a/languages/csharp/src/main/java/de/jplag/csharp/CSharpListener.java b/languages/csharp/src/main/java/de/jplag/csharp/CSharpListener.java index b43ac849c..159e5a2b9 100644 --- a/languages/csharp/src/main/java/de/jplag/csharp/CSharpListener.java +++ b/languages/csharp/src/main/java/de/jplag/csharp/CSharpListener.java @@ -1,518 +1,80 @@ package de.jplag.csharp; -import static de.jplag.csharp.CSharpTokenType.ACCESSORS_BEGIN; -import static de.jplag.csharp.CSharpTokenType.ACCESSORS_END; -import static de.jplag.csharp.CSharpTokenType.ACCESSOR_BEGIN; -import static de.jplag.csharp.CSharpTokenType.ACCESSOR_END; -import static de.jplag.csharp.CSharpTokenType.ARRAY_CREATION; -import static de.jplag.csharp.CSharpTokenType.ASSIGNMENT; -import static de.jplag.csharp.CSharpTokenType.ATTRIBUTE; -import static de.jplag.csharp.CSharpTokenType.BREAK; -import static de.jplag.csharp.CSharpTokenType.CASE; -import static de.jplag.csharp.CSharpTokenType.CATCH; -import static de.jplag.csharp.CSharpTokenType.CHECKED; -import static de.jplag.csharp.CSharpTokenType.CLASS; -import static de.jplag.csharp.CSharpTokenType.CLASS_BEGIN; -import static de.jplag.csharp.CSharpTokenType.CLASS_END; -import static de.jplag.csharp.CSharpTokenType.CONSTANT; -import static de.jplag.csharp.CSharpTokenType.CONSTRUCTOR; -import static de.jplag.csharp.CSharpTokenType.CONTINUE; -import static de.jplag.csharp.CSharpTokenType.DELEGATE; -import static de.jplag.csharp.CSharpTokenType.DESTRUCTOR; -import static de.jplag.csharp.CSharpTokenType.DO; -import static de.jplag.csharp.CSharpTokenType.ENUM; -import static de.jplag.csharp.CSharpTokenType.ENUMERAL; -import static de.jplag.csharp.CSharpTokenType.ENUM_BEGIN; -import static de.jplag.csharp.CSharpTokenType.ENUM_END; -import static de.jplag.csharp.CSharpTokenType.EVENT; -import static de.jplag.csharp.CSharpTokenType.FIELD; -import static de.jplag.csharp.CSharpTokenType.FINALLY; -import static de.jplag.csharp.CSharpTokenType.FIXED; -import static de.jplag.csharp.CSharpTokenType.FOR; -import static de.jplag.csharp.CSharpTokenType.FOREACH; -import static de.jplag.csharp.CSharpTokenType.GOTO; -import static de.jplag.csharp.CSharpTokenType.IF; -import static de.jplag.csharp.CSharpTokenType.IF_BEGIN; -import static de.jplag.csharp.CSharpTokenType.IF_END; -import static de.jplag.csharp.CSharpTokenType.INDEXER; -import static de.jplag.csharp.CSharpTokenType.INTERFACE; -import static de.jplag.csharp.CSharpTokenType.INTERFACE_BEGIN; -import static de.jplag.csharp.CSharpTokenType.INTERFACE_END; -import static de.jplag.csharp.CSharpTokenType.INVOCATION; -import static de.jplag.csharp.CSharpTokenType.LOCAL_VARIABLE; -import static de.jplag.csharp.CSharpTokenType.LOCK; -import static de.jplag.csharp.CSharpTokenType.METHOD; -import static de.jplag.csharp.CSharpTokenType.METHOD_BEGIN; -import static de.jplag.csharp.CSharpTokenType.METHOD_END; -import static de.jplag.csharp.CSharpTokenType.NAMESPACE_BEGIN; -import static de.jplag.csharp.CSharpTokenType.NAMESPACE_END; -import static de.jplag.csharp.CSharpTokenType.OBJECT_CREATION; -import static de.jplag.csharp.CSharpTokenType.OPERATOR; -import static de.jplag.csharp.CSharpTokenType.PROPERTY; -import static de.jplag.csharp.CSharpTokenType.RETURN; -import static de.jplag.csharp.CSharpTokenType.STRUCT; -import static de.jplag.csharp.CSharpTokenType.STRUCT_BEGIN; -import static de.jplag.csharp.CSharpTokenType.STRUCT_END; -import static de.jplag.csharp.CSharpTokenType.SWITCH_BEGIN; -import static de.jplag.csharp.CSharpTokenType.SWITCH_END; -import static de.jplag.csharp.CSharpTokenType.THROW; -import static de.jplag.csharp.CSharpTokenType.TRY; -import static de.jplag.csharp.CSharpTokenType.UNCHECKED; -import static de.jplag.csharp.CSharpTokenType.UNSAFE; -import static de.jplag.csharp.CSharpTokenType.USING; -import static de.jplag.csharp.CSharpTokenType.USING_DIRECTIVE; -import static de.jplag.csharp.CSharpTokenType.WHILE; - -import org.antlr.v4.runtime.Token; - -import de.jplag.csharp.grammar.CSharpParser.Accessor_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Accessor_declarationsContext; -import de.jplag.csharp.grammar.CSharpParser.Array_initializerContext; -import de.jplag.csharp.grammar.CSharpParser.Assignment_operatorContext; -import de.jplag.csharp.grammar.CSharpParser.AttributeContext; -import de.jplag.csharp.grammar.CSharpParser.BreakStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Catch_clausesContext; -import de.jplag.csharp.grammar.CSharpParser.CheckedStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Class_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Class_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.Constant_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Constructor_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.ContinueStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Delegate_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.Destructor_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.DoStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Enum_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Enum_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.Enum_member_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Event_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Field_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Finally_clauseContext; -import de.jplag.csharp.grammar.CSharpParser.FixedStatementContext; -import de.jplag.csharp.grammar.CSharpParser.ForStatementContext; -import de.jplag.csharp.grammar.CSharpParser.ForeachStatementContext; -import de.jplag.csharp.grammar.CSharpParser.GotoStatementContext; -import de.jplag.csharp.grammar.CSharpParser.IfStatementContext; -import de.jplag.csharp.grammar.CSharpParser.If_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Indexer_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Interface_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Interface_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.Local_variable_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.LockStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Method_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Method_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Method_invocationContext; -import de.jplag.csharp.grammar.CSharpParser.Namespace_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Object_creation_expressionContext; -import de.jplag.csharp.grammar.CSharpParser.Operator_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.Property_declarationContext; -import de.jplag.csharp.grammar.CSharpParser.ReturnStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Struct_bodyContext; -import de.jplag.csharp.grammar.CSharpParser.Struct_definitionContext; -import de.jplag.csharp.grammar.CSharpParser.SwitchStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Switch_sectionContext; -import de.jplag.csharp.grammar.CSharpParser.ThrowStatementContext; -import de.jplag.csharp.grammar.CSharpParser.TryStatementContext; -import de.jplag.csharp.grammar.CSharpParser.UncheckedExpressionContext; -import de.jplag.csharp.grammar.CSharpParser.UnsafeStatementContext; -import de.jplag.csharp.grammar.CSharpParser.UsingStatementContext; -import de.jplag.csharp.grammar.CSharpParser.Using_directivesContext; -import de.jplag.csharp.grammar.CSharpParser.WhileStatementContext; -import de.jplag.csharp.grammar.CSharpParserBaseListener; - -/** - * Listener class for visiting the C# ANTLR parse tree. Transforms selected ANTLR token into JPlag tokens. - * @author Timur Saglam - */ -public class CSharpListener extends CSharpParserBaseListener { - - private final CSharpParserAdapter parserAdapter; - - /** - * Creates the listener. - * @param parserAdapter is the JPlag parser adapter which receives the transformed tokens. - */ - public CSharpListener(CSharpParserAdapter parserAdapter) { - this.parserAdapter = parserAdapter; - } - - /** - * Transforms an ANTLR Token into a JPlag token and transfers it to the token adapter. - * @param targetType is the type of the JPlag token to be created. - * @param token is the ANTLR token. - */ - private void transformToken(CSharpTokenType targetType, Token token) { - parserAdapter.addToken(targetType, token.getLine(), token.getCharPositionInLine() + 1, token.getText().length()); - } - - @Override - public void enterMethod_invocation(Method_invocationContext context) { - transformToken(INVOCATION, context.getStart()); - super.enterMethod_invocation(context); - } - - @Override - public void enterObject_creation_expression(Object_creation_expressionContext context) { - transformToken(OBJECT_CREATION, context.getStart()); - super.enterObject_creation_expression(context); - } - - @Override - public void enterArray_initializer(Array_initializerContext context) { - transformToken(ARRAY_CREATION, context.getStart()); - super.enterArray_initializer(context); - } - - @Override - public void enterAssignment_operator(Assignment_operatorContext context) { - transformToken(ASSIGNMENT, context.getStart()); - super.enterAssignment_operator(context); - } - - @Override - public void enterIfStatement(IfStatementContext context) { - transformToken(IF, context.getStart()); - super.enterIfStatement(context); - } - - @Override - public void enterIf_body(If_bodyContext context) { - transformToken(IF_BEGIN, context.getStart()); - super.enterIf_body(context); - } - - @Override - public void exitIf_body(If_bodyContext context) { - transformToken(IF_END, context.getStop()); - super.exitIf_body(context); - } - - @Override - public void enterSwitch_section(Switch_sectionContext context) { - transformToken(SWITCH_BEGIN, context.getStart()); - super.enterSwitch_section(context); - } - - @Override - public void exitSwitch_section(Switch_sectionContext context) { - transformToken(SWITCH_END, context.getStop()); - super.exitSwitch_section(context); - } - - @Override - public void enterSwitchStatement(SwitchStatementContext context) { - transformToken(CASE, context.getStart()); - super.enterSwitchStatement(context); - } - - @Override - public void enterDoStatement(DoStatementContext context) { - transformToken(DO, context.getStart()); - super.enterDoStatement(context); - } - - @Override - public void enterWhileStatement(WhileStatementContext context) { - transformToken(WHILE, context.getStart()); - super.enterWhileStatement(context); - } - - @Override - public void enterForStatement(ForStatementContext context) { - transformToken(FOR, context.getStart()); - super.enterForStatement(context); - } - - @Override - public void enterForeachStatement(ForeachStatementContext context) { - transformToken(FOREACH, context.getStart()); - super.enterForeachStatement(context); - } - - @Override - public void enterBreakStatement(BreakStatementContext context) { - transformToken(BREAK, context.getStart()); - super.enterBreakStatement(context); - } - - @Override - public void enterContinueStatement(ContinueStatementContext context) { - transformToken(CONTINUE, context.getStart()); - super.enterContinueStatement(context); - } - - @Override - public void enterGotoStatement(GotoStatementContext context) { - transformToken(GOTO, context.getStart()); - super.enterGotoStatement(context); - } - - @Override - public void enterReturnStatement(ReturnStatementContext context) { - transformToken(RETURN, context.getStart()); - super.enterReturnStatement(context); - } - - @Override - public void enterThrowStatement(ThrowStatementContext context) { - transformToken(THROW, context.getStart()); - super.enterThrowStatement(context); - } - - @Override - public void enterCheckedStatement(CheckedStatementContext context) { - transformToken(CHECKED, context.getStart()); - super.enterCheckedStatement(context); - } - - @Override - public void enterUncheckedExpression(UncheckedExpressionContext context) { - transformToken(UNCHECKED, context.getStart()); - super.enterUncheckedExpression(context); - } - - @Override - public void enterLockStatement(LockStatementContext context) { - transformToken(LOCK, context.getStart()); - super.enterLockStatement(context); - } - - @Override - public void enterUsingStatement(UsingStatementContext context) { - transformToken(USING, context.getStart()); - super.enterUsingStatement(context); - } - - @Override - public void enterTryStatement(TryStatementContext context) { - transformToken(TRY, context.getStart()); - super.enterTryStatement(context); - } - - @Override - public void enterCatch_clauses(Catch_clausesContext context) { - transformToken(CATCH, context.getStart()); - super.enterCatch_clauses(context); - } - - @Override - public void enterFinally_clause(Finally_clauseContext context) { - transformToken(FINALLY, context.getStart()); - super.enterFinally_clause(context); - } - - @Override - public void enterNamespace_body(Namespace_bodyContext context) { - transformToken(NAMESPACE_BEGIN, context.getStart()); - super.enterNamespace_body(context); - } - - @Override - public void exitNamespace_body(Namespace_bodyContext context) { - transformToken(NAMESPACE_END, context.getStop()); - super.exitNamespace_body(context); - } - - @Override - public void enterUsing_directives(Using_directivesContext context) { - transformToken(USING_DIRECTIVE, context.getStart()); - super.enterUsing_directives(context); - } - - @Override - public void enterClass_definition(Class_definitionContext context) { - transformToken(CLASS, context.getStart()); - super.enterClass_definition(context); - } - - @Override - public void enterClass_body(Class_bodyContext context) { - transformToken(CLASS_BEGIN, context.getStart()); - super.enterClass_body(context); - } - - @Override - public void exitClass_body(Class_bodyContext context) { - transformToken(CLASS_END, context.getStop()); - super.exitClass_body(context); - } - - @Override - public void enterMethod_declaration(Method_declarationContext context) { - transformToken(METHOD, context.getStart()); - super.enterMethod_declaration(context); - } - - @Override - public void enterMethod_body(Method_bodyContext context) { - transformToken(METHOD_BEGIN, context.getStart()); - super.enterMethod_body(context); - } - - @Override - public void exitMethod_body(Method_bodyContext context) { - transformToken(METHOD_END, context.getStop()); - super.exitMethod_body(context); - } - - @Override - public void enterProperty_declaration(Property_declarationContext context) { - transformToken(PROPERTY, context.getStart()); - super.enterProperty_declaration(context); - } - - @Override - public void enterEvent_declaration(Event_declarationContext context) { - transformToken(EVENT, context.getStart()); - super.enterEvent_declaration(context); - } - - @Override - public void enterIndexer_declaration(Indexer_declarationContext context) { - transformToken(INDEXER, context.getStart()); - super.enterIndexer_declaration(context); - } - - @Override - public void enterOperator_declaration(Operator_declarationContext context) { - transformToken(OPERATOR, context.getStart()); - super.enterOperator_declaration(context); - } - - @Override - public void enterConstructor_declaration(Constructor_declarationContext context) { - transformToken(CONSTRUCTOR, context.getStart()); - super.enterConstructor_declaration(context); - } - - @Override - public void enterDestructor_definition(Destructor_definitionContext context) { - transformToken(DESTRUCTOR, context.getStart()); - super.enterDestructor_definition(context); - } - - @Override - public void enterStruct_definition(Struct_definitionContext context) { - transformToken(STRUCT, context.getStart()); - super.enterStruct_definition(context); - } - - @Override - public void enterStruct_body(Struct_bodyContext context) { - transformToken(STRUCT_BEGIN, context.getStart()); - super.enterStruct_body(context); - } - - @Override - public void exitStruct_body(Struct_bodyContext context) { - transformToken(STRUCT_END, context.getStop()); - super.exitStruct_body(context); - } - - @Override - public void enterInterface_definition(Interface_definitionContext context) { - transformToken(INTERFACE, context.getStart()); - super.enterInterface_definition(context); - } - - @Override - public void enterInterface_body(Interface_bodyContext context) { - transformToken(INTERFACE_BEGIN, context.getStart()); - super.enterInterface_body(context); - } - - @Override - public void exitInterface_body(Interface_bodyContext context) { - transformToken(INTERFACE_END, context.getStart()); - super.exitInterface_body(context); - } - - @Override - public void enterEnum_definition(Enum_definitionContext context) { - transformToken(ENUM, context.getStart()); - super.enterEnum_definition(context); - } - - @Override - public void enterEnum_body(Enum_bodyContext context) { - transformToken(ENUM_BEGIN, context.getStart()); - super.enterEnum_body(context); - } - - @Override - public void exitEnum_body(Enum_bodyContext context) { - transformToken(ENUM_END, context.getStop()); - super.exitEnum_body(context); - } - - @Override - public void enterEnum_member_declaration(Enum_member_declarationContext context) { - transformToken(ENUMERAL, context.getStart()); - super.enterEnum_member_declaration(context); - } - - @Override - public void enterAttribute(AttributeContext context) { - transformToken(ATTRIBUTE, context.getStart()); - super.enterAttribute(context); - } - - @Override - public void enterDelegate_definition(Delegate_definitionContext context) { - transformToken(DELEGATE, context.getStart()); - super.enterDelegate_definition(context); - } - - @Override - public void enterUnsafeStatement(UnsafeStatementContext context) { - transformToken(UNSAFE, context.getStart()); - super.enterUnsafeStatement(context); - } - - @Override - public void enterFixedStatement(FixedStatementContext context) { - transformToken(FIXED, context.getStart()); - super.enterFixedStatement(context); - } - - @Override - public void enterAccessor_declarations(Accessor_declarationsContext context) { - transformToken(ACCESSORS_BEGIN, context.getStart()); - super.enterAccessor_declarations(context); - } - - @Override - public void exitAccessor_declarations(Accessor_declarationsContext context) { - transformToken(ACCESSORS_END, context.getStop()); - super.enterAccessor_declarations(context); - } - - @Override - public void enterAccessor_body(Accessor_bodyContext context) { - transformToken(ACCESSOR_BEGIN, context.getStart()); - super.enterAccessor_body(context); - } - - @Override - public void exitAccessor_body(Accessor_bodyContext context) { - transformToken(ACCESSOR_END, context.getStop()); - super.exitAccessor_body(context); - } - - @Override - public void enterConstant_declaration(Constant_declarationContext context) { - transformToken(CONSTANT, context.getStart()); - super.enterConstant_declaration(context); - } - - @Override - public void enterField_declaration(Field_declarationContext context) { - transformToken(FIELD, context.getStart()); - super.enterField_declaration(context); - } - - @Override - public void enterLocal_variable_declaration(Local_variable_declarationContext context) { - transformToken(LOCAL_VARIABLE, context.getStart()); - super.enterLocal_variable_declaration(context); +import static de.jplag.csharp.CSharpTokenType.*; + +import org.antlr.v4.runtime.ParserRuleContext; + +import de.jplag.antlr.AbstractAntlrListener; +import de.jplag.csharp.grammar.CSharpParser.*; + +public class CSharpListener extends AbstractAntlrListener { + public CSharpListener() { + visit(UsingStatementContext.class).map(USING); + visit(Using_directivesContext.class).map(USING_DIRECTIVE); + visit(Method_invocationContext.class).map(INVOCATION); + visit(Object_creation_expressionContext.class).map(OBJECT_CREATION); + visit(Array_initializerContext.class).map(ARRAY_CREATION); + visit(Assignment_operatorContext.class).map(ASSIGNMENT); + visit(IfStatementContext.class).map(IF); + visit(If_bodyContext.class).map(IF_BEGIN, IF_END); + visit(Switch_sectionContext.class).map(SWITCH_BEGIN, SWITCH_END); + visit(SwitchStatementContext.class).map(CASE); + + visit(DoStatementContext.class).map(DO); + visit(WhileStatementContext.class).map(WHILE); + visit(ForStatementContext.class).map(FOR); + visit(ForeachStatementContext.class).map(FOREACH); + + visit(BreakStatementContext.class).map(BREAK); + visit(ContinueStatementContext.class).map(CONTINUE); + visit(GotoStatementContext.class).map(GOTO); + + visit(ReturnStatementContext.class).map(RETURN); + visit(ThrowStatementContext.class).map(THROW); + + visit(CheckedStatementContext.class).map(CHECKED); + visit(UncheckedExpressionContext.class).map(UNCHECKED); + + visit(LockStatementContext.class).map(LOCK); + + visit(TryStatementContext.class).map(TRY); + visit(Catch_clausesContext.class).map(CATCH); + visit(Finally_clauseContext.class).map(FINALLY); + + visit(Namespace_bodyContext.class).map(NAMESPACE_BEGIN, NAMESPACE_END); + visit(Class_definitionContext.class).map(CLASS); + visit(Class_bodyContext.class, this::isClassBody).map(CLASS_BEGIN, CLASS_END); + visit(Method_declarationContext.class).map(METHOD); + visit(Method_bodyContext.class).map(METHOD_BEGIN, METHOD_END); + visit(Property_declarationContext.class).map(PROPERTY); + visit(Event_declarationContext.class).map(EVENT); + visit(Indexer_declarationContext.class).map(INDEXER); + visit(Operator_declarationContext.class).map(OPERATOR); + visit(Constructor_declarationContext.class).map(CONSTRUCTOR); + visit(Destructor_definitionContext.class).map(DESTRUCTOR); + visit(Struct_definitionContext.class).map(STRUCT); + visit(Struct_bodyContext.class).map(STRUCT_BEGIN, STRUCT_END); + visit(Interface_definitionContext.class).map(INTERFACE); + visit(Class_bodyContext.class, this::isInterfaceBody).map(INTERFACE_BEGIN, INTERFACE_END); + visit(Enum_definitionContext.class).map(ENUM); + visit(Enum_bodyContext.class).map(ENUM_BEGIN, ENUM_END); + visit(Enum_member_declarationContext.class).map(ENUMERAL); + visit(AttributeContext.class).map(ATTRIBUTE); + visit(Delegate_definitionContext.class).map(DELEGATE); + visit(UnsafeStatementContext.class).map(UNSAFE); + visit(FixedStatementContext.class).map(FIXED); + visit(Accessor_declarationsContext.class).map(ACCESSORS_BEGIN, ACCESSORS_END); + visit(Accessor_bodyContext.class).map(ACCESSOR_BEGIN, ACCESSOR_END); + visit(Constant_declarationContext.class).map(CONSTANT); + visit(Field_declarationContext.class).map(FIELD); + visit(Local_variable_declarationContext.class).map(LOCAL_VARIABLE); + } + + private boolean isClassBody(ParserRuleContext context) { + return hasAncestor(context, Class_definitionContext.class, Interface_definitionContext.class); + } + + private boolean isInterfaceBody(ParserRuleContext context) { + return hasAncestor(context, Interface_definitionContext.class, Class_definitionContext.class); } } diff --git a/languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java b/languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java index 3b2b0d8a2..41560085f 100644 --- a/languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java +++ b/languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java @@ -1,80 +1,38 @@ package de.jplag.csharp; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import de.jplag.AbstractParser; -import de.jplag.ParsingException; -import de.jplag.Token; -import de.jplag.TokenType; +import de.jplag.antlr.AbstractAntlrListener; +import de.jplag.antlr.AbstractAntlrParserAdapter; import de.jplag.csharp.grammar.CSharpLexer; import de.jplag.csharp.grammar.CSharpParser; -import de.jplag.util.FileUtils; /** * Parser adapter for the ANTLR 4 CSharp Parser and Lexer. It receives file to parse and passes them to the ANTLR * pipeline. Then it walks the produced parse tree and creates JPlag token with the {@link CSharpListener}. * @author Timur Saglam */ -public class CSharpParserAdapter extends AbstractParser { - private List tokens; - private File currentFile; - - /** - * Creates the parser adapter. - */ - public CSharpParserAdapter() { - super(); +public class CSharpParserAdapter extends AbstractAntlrParserAdapter { + @Override + protected Lexer createLexer(CharStream input) { + return new CSharpLexer(input); } - /** - * Parses all tokens from a set of files. - * @param files is the set of files. - * @return the list of parsed tokens. - */ - public List parse(Set files) throws ParsingException { - tokens = new ArrayList<>(); - for (File file : files) { - parseFile(file); - tokens.add(Token.fileEnd(file)); - } - return tokens; + @Override + protected CSharpParser createParser(CommonTokenStream tokenStream) { + return new CSharpParser(tokenStream); } - private void parseFile(File file) throws ParsingException { - try (BufferedReader reader = FileUtils.openFileReader(file)) { - currentFile = file; - - // create a lexer, a parser and a buffer between them. - CSharpLexer lexer = new CSharpLexer(CharStreams.fromReader(reader)); - CommonTokenStream tokens = new CommonTokenStream(lexer); - CSharpParser parser = new CSharpParser(tokens); - - // Create a tree walker and the entry context defined by the parser grammar - ParserRuleContext entryContext = parser.compilation_unit(); - ParseTreeWalker treeWalker = new ParseTreeWalker(); - - // Walk over the parse tree: - for (int i = 0; i < entryContext.getChildCount(); i++) { - ParseTree parseTree = entryContext.getChild(i); - treeWalker.walk(new CSharpListener(this), parseTree); - } - } catch (IOException exception) { - throw new ParsingException(file, exception.getMessage(), exception); - } + @Override + protected ParserRuleContext getEntryContext(CSharpParser parser) { + return parser.compilation_unit(); } - /* package-private */ void addToken(TokenType type, int line, int column, int length) { - tokens.add(new Token(type, currentFile, line, column, length)); + @Override + protected AbstractAntlrListener getListener() { + return new CSharpListener(); } } diff --git a/languages/csharp/src/test/java/de/jplag/csharp/CSharpTest.java b/languages/csharp/src/test/java/de/jplag/csharp/CSharpTest.java index 2e589e179..508637247 100644 --- a/languages/csharp/src/test/java/de/jplag/csharp/CSharpTest.java +++ b/languages/csharp/src/test/java/de/jplag/csharp/CSharpTest.java @@ -1,12 +1,5 @@ package de.jplag.csharp; -import de.jplag.testutils.LanguageModuleTest; -import de.jplag.testutils.datacollector.TestDataCollector; -import de.jplag.testutils.datacollector.TestSourceIgnoredLinesCollector; - -import java.util.ArrayList; -import java.util.List; - import static de.jplag.csharp.CSharpTokenType.ACCESSORS_BEGIN; import static de.jplag.csharp.CSharpTokenType.ACCESSORS_END; import static de.jplag.csharp.CSharpTokenType.ACCESSOR_BEGIN; @@ -28,6 +21,10 @@ import static de.jplag.csharp.CSharpTokenType.PROPERTY; import static de.jplag.csharp.CSharpTokenType.RETURN; +import de.jplag.testutils.LanguageModuleTest; +import de.jplag.testutils.datacollector.TestDataCollector; +import de.jplag.testutils.datacollector.TestSourceIgnoredLinesCollector; + public class CSharpTest extends LanguageModuleTest { public CSharpTest() { super(new CSharpLanguage(), CSharpTokenType.class); @@ -35,15 +32,12 @@ public CSharpTest() { @Override protected void collectTestData(TestDataCollector collector) { - collector.testFile("TestClass.cs").testSourceCoverage().testTokenSequence(CLASS, CLASS_BEGIN, FIELD, CONSTRUCTOR, LOCAL_VARIABLE, METHOD, METHOD_BEGIN, IF, IF_BEGIN, - INVOCATION, IF_END, IF_BEGIN, INVOCATION, IF_END, METHOD_END, PROPERTY, ACCESSORS_BEGIN, ACCESSOR_BEGIN, ACCESSOR_END, ACCESSOR_BEGIN, - ACCESSOR_END, ACCESSORS_END, FIELD, PROPERTY, ACCESSORS_BEGIN, ACCESSOR_BEGIN, RETURN, ACCESSOR_END, ACCESSOR_BEGIN, ASSIGNMENT, - ACCESSOR_END, ACCESSORS_END, CLASS_END); + collector.testFile("TestClass.cs").testSourceCoverage().testTokenSequence(CLASS, CLASS_BEGIN, FIELD, CONSTRUCTOR, LOCAL_VARIABLE, METHOD, + METHOD_BEGIN, IF, IF_BEGIN, INVOCATION, IF_END, IF_BEGIN, INVOCATION, IF_END, METHOD_END, PROPERTY, ACCESSORS_BEGIN, ACCESSOR_BEGIN, + ACCESSOR_END, ACCESSOR_BEGIN, ACCESSOR_END, ACCESSORS_END, FIELD, PROPERTY, ACCESSORS_BEGIN, ACCESSOR_BEGIN, RETURN, ACCESSOR_END, + ACCESSOR_BEGIN, ASSIGNMENT, ACCESSOR_END, ACCESSORS_END, CLASS_END); - ArrayList tokens = new ArrayList<>(List.of(CSharpTokenType.values())); - tokens.remove(CSharpTokenType.INTERFACE_BEGIN); - tokens.remove(CSharpTokenType.INTERFACE_END); - collector.testFile("AllInOneNoPreprocessor.cs")./*testSourceCoverage().*/testContainedTokens(tokens.toArray(CSharpTokenType[]::new)); + collector.testFile("AllInOneNoPreprocessor.cs")./* testSourceCoverage(). */testTokenCoverage(); } @Override