groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] branch master updated: groovysh: prepare RelaxedParser for switch to antlr4
Date Fri, 27 Sep 2019 10:19:35 GMT
This is an automated email from the ASF dual-hosted git repository.

paulk 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 160170c  groovysh: prepare RelaxedParser for switch to antlr4
160170c is described below

commit 160170cf8ff86873dcda4ef113de7bb061973c6b
Author: Paul King <paulk@asert.com.au>
AuthorDate: Fri Sep 27 20:19:23 2019 +1000

    groovysh: prepare RelaxedParser for switch to antlr4
---
 .../org/apache/groovy/groovysh/Groovysh.groovy     |  3 +-
 .../org/apache/groovy/groovysh/Parser.groovy       | 83 +++------------------
 .../apache/groovy/groovysh/RelaxedParser.groovy    | 84 ++++++++++++++++++++++
 .../groovy/groovysh/antlr4/RelaxedParser.groovy    | 73 +++++++++++++++++++
 4 files changed, 167 insertions(+), 76 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Groovysh.groovy
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Groovysh.groovy
index d0fc0ff..a2c55ca 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Groovysh.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Groovysh.groovy
@@ -18,7 +18,6 @@
  */
 package org.apache.groovy.groovysh
 
-import antlr.TokenStreamException
 import groovy.transform.CompileStatic
 import jline.Terminal
 import jline.WindowsTerminal
@@ -366,7 +365,7 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname;
         // read all tokens
         try {
             while (lexer.nextToken().getType() != CurlyCountingGroovyLexer.EOF) {}
-        } catch (TokenStreamException ignore) {
+        } catch (Exception ignore) { // TokenStreamException for antlr2
             // pass
         }
         int curlyIndent = (lexer.getCurlyLevel()) * indentSize
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Parser.groovy
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Parser.groovy
index 44ce5ac..b793d60 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Parser.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Parser.groovy
@@ -18,15 +18,9 @@
  */
 package org.apache.groovy.groovysh
 
-import antlr.RecognitionException
-import antlr.TokenStreamException
-import antlr.collections.AST
-import org.codehaus.groovy.antlr.SourceBuffer
-import org.codehaus.groovy.antlr.UnicodeEscapingReader
-import org.codehaus.groovy.antlr.parser.GroovyLexer
-import org.codehaus.groovy.antlr.parser.GroovyRecognizer
+import org.codehaus.groovy.antlr.AntlrParserPluginFactory
 import org.codehaus.groovy.control.CompilationFailedException
-import org.codehaus.groovy.control.ParserPluginFactory
+import org.codehaus.groovy.control.CompilerConfiguration
 import org.codehaus.groovy.control.SourceUnit
 import org.codehaus.groovy.tools.shell.util.Logger
 import org.codehaus.groovy.tools.shell.util.Preferences
@@ -40,8 +34,7 @@ interface Parsing {
 /**
  * Provides a facade over the parser to recognize valid Groovy syntax.
  */
-class Parser
-{
+class Parser {
     static final String NEWLINE = System.getProperty('line.separator')
 
     private static final Logger log = Logger.create(Parser)
@@ -55,7 +48,8 @@ class Parser
 
         switch (flavor) {
             case Preferences.PARSER_RELAXED:
-                delegate = new RelaxedParser()
+                boolean oldParserEnabled = CompilerConfiguration.DEFAULT.getPluginFactory()
instanceof AntlrParserPluginFactory;
+                delegate = oldParserEnabled ? new RelaxedParser() : new org.apache.groovy.groovysh.antlr4.RelaxedParser()
                 break
 
             case Preferences.PARSER_RIGID:
@@ -74,68 +68,11 @@ class Parser
     }
 }
 
-/**
- * A relaxed parser, which tends to allow more, but won't really catch valid syntax errors.
- */
-final class RelaxedParser implements Parsing
-{
-    private final Logger log = Logger.create(this.class)
-
-    private SourceBuffer sourceBuffer
-
-    private String[] tokenNames
-
-    @Override
-    ParseStatus parse(final Collection<String> buffer) {
-        assert buffer
-
-        sourceBuffer = new SourceBuffer()
-
-        def source = buffer.join(Parser.NEWLINE)
-
-        log.debug("Parsing: $source")
-
-        try {
-            doParse(new UnicodeEscapingReader(new StringReader(source), sourceBuffer))
-
-            log.debug('Parse complete')
-
-            return new ParseStatus(ParseCode.COMPLETE)
-        }
-        catch (e) {
-            switch (e.getClass()) {
-                case TokenStreamException:
-                case RecognitionException:
-                    log.debug("Parse incomplete: $e (${e.getClass().name})")
-
-                    return new ParseStatus(ParseCode.INCOMPLETE)
-
-                default:
-                    log.debug("Parse error: $e (${e.getClass().name})")
-
-                    return new ParseStatus(e)
-            }
-        }
-    }
-
-    protected AST doParse(final UnicodeEscapingReader reader) throws Exception {
-        GroovyLexer lexer = new GroovyLexer(reader)
-        reader.setLexer(lexer)
-
-        def parser = GroovyRecognizer.make(lexer)
-        parser.setSourceBuffer(sourceBuffer)
-        tokenNames = parser.tokenNames
-
-        parser.compilationUnit()
-        return parser.AST
-    }
-}
 
 /**
  * A more rigid parser which catches more syntax errors, but also tends to barf on stuff
that is really valid from time to time.
  */
-final class RigidParser implements Parsing
-{
+final class RigidParser implements Parsing {
     private static final Pattern ANNOTATION_PATTERN = Pattern.compile('^@[a-zA-Z_][a-zA-Z_0-9]*(.*)$')
     static final String SCRIPT_FILENAME = 'groovysh_parse'
 
@@ -217,7 +154,7 @@ final class RigidParser implements Parsing
         int parens = 0
         int brackets = 0
         for (ch in source) {
-            switch(ch) {
+            switch (ch) {
                 case '[': ++brackets; break;
                 case ']': --brackets; break;
                 case '(': ++parens; break;
@@ -237,8 +174,7 @@ final class RigidParser implements Parsing
 /**
  * Container for the parse code.
  */
-final class ParseCode
-{
+final class ParseCode {
     static final ParseCode COMPLETE = new ParseCode(0)
 
     static final ParseCode INCOMPLETE = new ParseCode(1)
@@ -260,8 +196,7 @@ final class ParseCode
 /**
  * Container for parse status details.
  */
-final class ParseStatus
-{
+final class ParseStatus {
     final ParseCode code
 
     final Throwable cause
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/RelaxedParser.groovy
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/RelaxedParser.groovy
new file mode 100644
index 0000000..21389f9
--- /dev/null
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/RelaxedParser.groovy
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.groovysh
+
+import antlr.RecognitionException
+import antlr.TokenStreamException
+import antlr.collections.AST
+import org.apache.groovy.parser.antlr4.GroovyLangLexer
+import org.codehaus.groovy.antlr.SourceBuffer
+import org.codehaus.groovy.antlr.UnicodeEscapingReader
+import org.codehaus.groovy.antlr.parser.GroovyRecognizer
+import org.codehaus.groovy.tools.shell.util.Logger
+
+/**
+ * A relaxed parser, which tends to allow more, but won't really catch valid syntax errors.
+ */
+final class RelaxedParser implements Parsing {
+    private final Logger log = Logger.create(this.class)
+
+    private SourceBuffer sourceBuffer
+
+    private String[] tokenNames
+
+    @Override
+    ParseStatus parse(final Collection<String> buffer) {
+        assert buffer
+
+        sourceBuffer = new SourceBuffer()
+
+        def source = buffer.join(Parser.NEWLINE)
+
+        log.debug("Parsing: $source")
+
+        try {
+            doParse(new UnicodeEscapingReader(new StringReader(source), sourceBuffer))
+
+            log.debug('Parse complete')
+
+            return new ParseStatus(ParseCode.COMPLETE)
+        }
+        catch (e) {
+            switch (e.getClass()) {
+                case TokenStreamException:
+                case RecognitionException:
+                    log.debug("Parse incomplete: $e (${e.getClass().name})")
+
+                    return new ParseStatus(ParseCode.INCOMPLETE)
+
+                default:
+                    log.debug("Parse error: $e (${e.getClass().name})")
+
+                    return new ParseStatus(e)
+            }
+        }
+    }
+
+    protected AST doParse(final UnicodeEscapingReader reader) throws Exception {
+        GroovyLangLexer lexer = new GroovyLangLexer(reader)
+        reader.setLexer(lexer)
+
+        def parser = GroovyRecognizer.make(lexer)
+        parser.setSourceBuffer(sourceBuffer)
+        tokenNames = parser.tokenNames
+
+        parser.compilationUnit()
+        return parser.AST
+    }
+}
\ No newline at end of file
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/antlr4/RelaxedParser.groovy
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/antlr4/RelaxedParser.groovy
new file mode 100644
index 0000000..6102eea
--- /dev/null
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/antlr4/RelaxedParser.groovy
@@ -0,0 +1,73 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.groovysh.antlr4
+
+import org.antlr.v4.runtime.CharStream
+import org.antlr.v4.runtime.CharStreams
+import org.antlr.v4.runtime.CommonTokenStream
+import org.antlr.v4.runtime.ConsoleErrorListener
+import org.apache.groovy.groovysh.ParseCode
+import org.apache.groovy.groovysh.ParseStatus
+import org.apache.groovy.groovysh.Parser
+import org.apache.groovy.groovysh.Parsing
+import org.apache.groovy.parser.antlr4.GroovyLangLexer
+import org.codehaus.groovy.tools.shell.util.Logger
+
+/**
+ * A relaxed parser, which tends to allow more, but won't really catch valid syntax errors.
+ */
+final class RelaxedParser implements Parsing {
+    private final Logger log = Logger.create(this.class)
+
+    @Override
+    ParseStatus parse(final Collection<String> buffer) {
+        assert buffer
+        def source = buffer.join(Parser.NEWLINE)
+
+        log.debug("Parsing: $source")
+
+        try {
+            CharStream charStream = CharStreams.fromReader(new StringReader(source))
+            GroovyLangLexer lexer = new GroovyLangLexer(charStream)
+            lexer.removeErrorListener(ConsoleErrorListener.INSTANCE)
+            def tokenStream = new CommonTokenStream(lexer)
+            tokenStream.fill()
+
+            log.debug('Parse complete')
+
+            return new ParseStatus(ParseCode.COMPLETE)
+        }
+        catch (e) {
+            log.debug(e)
+            switch (e.getClass()) {
+                // TODO determine appropriate antlr4 exceptions or detect EOF earlier at
end of stream
+//                case TokenStreamException:
+//                case RecognitionException:
+//                    log.debug("Parse incomplete: $e (${e.getClass().name})")
+//
+//                    return new ParseStatus(ParseCode.INCOMPLETE)
+
+                default:
+                    log.debug("Parse error: $e (${e.getClass().name})")
+
+                    return new ParseStatus(e)
+            }
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message