groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: Avoid potential invalid syntax (#1067)
Date Tue, 05 Nov 2019 16:53:53 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new cd23b19  Avoid potential invalid syntax (#1067)
cd23b19 is described below

commit cd23b19909bc0e32e7cba405c8dd5cd990ad5c5c
Author: Daniel.Sun <sunlan@apache.org>
AuthorDate: Wed Nov 6 00:31:03 2019 +0800

    Avoid potential invalid syntax (#1067)
---
 src/antlr/GroovyParser.g4                          | 16 +++++++++-------
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 22 +++++-----------------
 2 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index ba238c5..7d802fa 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -98,11 +98,17 @@ options {
 // starting point for parsing a groovy file
 compilationUnit
     :   nls
-        packageDeclaration? sep? statements? EOF
+        packageDeclaration? sep? scriptStatements? EOF
     ;
 
-statements
-    :   statement (sep statement)* sep?
+scriptStatements
+    :   scriptStatement (sep scriptStatement)* sep?
+    ;
+
+scriptStatement
+    :   importDeclaration // Import statement.  Can be used in any scope.  Has "import x
as y" also.
+    |   typeDeclaration
+    |   statement
     ;
 
 packageDeclaration
@@ -667,12 +673,8 @@ statement
 
     |   identifier COLON nls statement                                                  
                   #labeledStmtAlt
 
-    // Import statement.  Can be used in any scope.  Has "import x as y" also.
-    |   importDeclaration                                                               
                   #importStmtAlt
-
     |   assertStatement                                                                 
                   #assertStmtAlt
 
-    |   typeDeclaration                                                                 
                   #typeDeclarationStmtAlt
     |   localVariableDeclaration                                                        
                   #localVariableDeclarationStmtAlt
 
     // validate the method in the AstBuilder#visitMethodDeclaration, e.g. method without
method body is not allowed
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 84c9824..51fd986 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -244,7 +244,6 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.IdentifierContext
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.IdentifierPrmrAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.IfElseStatementContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.ImportDeclarationContext;
-import static org.apache.groovy.parser.antlr4.GroovyLangParser.ImportStmtAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.InclusiveOrExprAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.IndexPropertyArgsContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.IntegerLiteralAltContext;
@@ -306,11 +305,11 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.ReturnStmtAltCont
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.ReturnTypeContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.STATIC;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.SUB;
+import static org.apache.groovy.parser.antlr4.GroovyLangParser.ScriptStatementsContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.ShiftExprAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StandardLambdaExpressionContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StandardLambdaParametersContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StatementContext;
-import static org.apache.groovy.parser.antlr4.GroovyLangParser.StatementsContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StringLiteralAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StringLiteralContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.SuperPrmrAltContext;
@@ -329,7 +328,6 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeArgumentsOrDi
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeBoundContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeDeclarationContext;
-import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeDeclarationStmtAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeListContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeNamePairContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeNamePairsContext;
@@ -449,7 +447,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
     public ModuleNode visitCompilationUnit(CompilationUnitContext ctx) {
         this.visit(ctx.packageDeclaration());
 
-        this.visitStatements(ctx.statements())
+        this.visitScriptStatements(ctx.scriptStatements())
                 .forEach(e -> {
                     if (e instanceof DeclarationListStatement) { // local variable declaration
                         ((DeclarationListStatement) e).getDeclarationStatements().forEach(moduleNode::addStatement);
@@ -481,12 +479,12 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
     }
 
     @Override
-    public List<ASTNode> visitStatements(StatementsContext ctx) {
+    public List<ASTNode> visitScriptStatements(ScriptStatementsContext ctx) {
         if (!asBoolean(ctx)) {
             return Collections.emptyList();
         }
 
-        return ctx.statement().stream()
+        return ctx.scriptStatement().stream()
                 .map(e -> (ASTNode) visit(e))
                 .collect(Collectors.toList());
     }
@@ -1092,16 +1090,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
     }
 
     @Override
-    public ImportNode visitImportStmtAlt(ImportStmtAltContext ctx) {
-        return configureAST(this.visitImportDeclaration(ctx.importDeclaration()), ctx);
-    }
-
-    @Override
-    public ClassNode visitTypeDeclarationStmtAlt(TypeDeclarationStmtAltContext ctx) {
-        return configureAST(this.visitTypeDeclaration(ctx.typeDeclaration()), ctx);
-    }
-
-    @Override
     public Statement visitLocalVariableDeclarationStmtAlt(LocalVariableDeclarationStmtAltContext
ctx) {
         return configureAST(this.visitLocalVariableDeclaration(ctx.localVariableDeclaration()),
ctx);
     }
@@ -3065,7 +3053,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                                 && Types.LEFT_SQUARE_BRACKET == ((BinaryExpression)
leftExpr).getOperation().getType()) // e.g. map[a] = 123 OR map['a'] = 123 OR map["$a"] =
123
                 )
 
-                ) {
+            ) {
 
             throw createParsingFailedException("The LHS of an assignment should be a variable
or a field accessing expression", ctx);
         }


Mime
View raw message