瀏覽代碼

grammar: resolve #116 ColorUtils, ColorParser
grammar: resolve #115 error handling

Ranides Atterwim 2 年之前
父節點
當前提交
1037a576b6

+ 1 - 1
assira.grammar-tool/pom.xml

@@ -9,7 +9,7 @@
         <version>2.8.4-SNAPSHOT</version>
     </parent>
 
-    <artifactId>assira.grammar-tool</artifactId>
+    <artifactId>assira.g4interpreter</artifactId>
     <packaging>jar</packaging>
 
     <name>${project.groupId}:${project.artifactId}</name>

+ 17 - 5
assira.grammar-tool/src/main/java/net/ranides/assira/grammar/impl/G4ParserInterpreter.java

@@ -3,6 +3,8 @@ package net.ranides.assira.grammar.impl;
 import net.ranides.assira.collection.maps.CrossMap;
 import net.ranides.assira.collection.maps.OpenCrossMap;
 import net.ranides.assira.collection.sets.OpenSet;
+import net.ranides.assira.grammar.G4ErrorHandler;
+import net.ranides.assira.grammar.G4Exception;
 import net.ranides.assira.grammar.G4Node;
 import net.ranides.assira.grammar.G4Parser;
 import net.ranides.assira.generic.Ref;
@@ -31,7 +33,9 @@ import java.util.Set;
 
 public class G4ParserInterpreter implements G4Parser {
 
-    protected final Grammar grammar;
+    private final Grammar grammar;
+
+    private final G4ErrorHandler handler;
 
     private final CrossMap<Integer, Integer, String> branchLabels;
 
@@ -39,9 +43,10 @@ public class G4ParserInterpreter implements G4Parser {
 
     private final CrossMap<String, String, Integer> expressionLabels;
 
-    public G4ParserInterpreter(String grammar) {
+    public G4ParserInterpreter(String grammar, G4ErrorHandler handler) {
         try {
             this.grammar = new Grammar(grammar);
+            this.handler = handler;
             this.branchLabels = deduceBranchLabels(this.grammar);
             this.expressionLabels = deduceExpressionLabels(this.grammar);
 
@@ -51,7 +56,7 @@ public class G4ParserInterpreter implements G4Parser {
                 branchValidator.add(ruleName(ruleIndex) + NodeSupport.QUALIFIER_NAME + label);
             });
         } catch (RecognitionException e) {
-            throw new RuntimeException(e);
+            throw new G4Exception(e);
         }
     }
 
@@ -139,7 +144,7 @@ public class G4ParserInterpreter implements G4Parser {
             }
         }
 
-        throw new UnsupportedOperationException("Awkward label '" + labelName + "' inside rule: " + ruleName);
+        throw new G4Exception("Awkward label '" + labelName + "' inside rule: " + ruleName);
     }
 
     @Override
@@ -182,12 +187,19 @@ public class G4ParserInterpreter implements G4Parser {
 
         public MyRoot(String ruleName, String text) {
             LexerInterpreter lexer = G4ParserInterpreter.this.grammar.createLexerInterpreter(CharStreams.fromString(text));
+
+            lexer.removeErrorListeners();
+            lexer.addErrorListener(new NodeSupport.DelegateListener(handler));
+
             CommonTokenStream tokens = new CommonTokenStream(lexer);
             ParserInterpreter interpreter = G4ParserInterpreter.this.grammar.createGrammarParserInterpreter(tokens);
 
+            interpreter.removeErrorListeners();
+            interpreter.addErrorListener(new NodeSupport.DelegateListener(handler));
+
             Rule rule = G4ParserInterpreter.this.grammar.getRule(ruleName);
             if (rule == null) {
-                throw new IllegalArgumentException("Unknown rule: " + ruleName);
+                throw new G4Exception("Unknown rule: " + ruleName);
             }
 
             this.tree = new NodeForRule(this, null, interpreter.parse(rule.index));

+ 3 - 3
assira.grammar-tool/src/test/java/net/ranides/assira/grammar/InspectionTest.java

@@ -16,7 +16,7 @@ public class InspectionTest {
 
     @Test
     public void stream() throws IOException {
-        G4Parser parser = G4Parser.newInstance(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Node tree = parser.parse("expression", "(1 + 2) * 3 + 4^2");
 
@@ -53,7 +53,7 @@ public class InspectionTest {
 
     @Test
     public void printTree() throws IOException {
-        G4Parser parser = G4Parser.newInstance(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Node tree1 = parser.parse("expression", "(4 + 2) * 3 ");
 
@@ -62,7 +62,7 @@ public class InspectionTest {
 
     @Test
     public void printPaths() throws IOException {
-        G4Parser g4 = G4Parser.newInstance(ResourceUtils.text("Calculator.g4"));
+        G4Parser g4 = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Node tree = g4.parse("expression", "(4 + 2) * 3 ");
 

+ 1 - 1
assira.grammar-tool/src/test/java/net/ranides/assira/grammar/LabelTest.java

@@ -16,7 +16,7 @@ public class LabelTest {
 
     @Test
     public void labelForExpressions() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("LabelTest.g4"));
+        G4Parser parser = G4Parser.builder().resource("LabelTest.g4").build();
 
         G4Node expr1 = parser.parse("manyLabels", "! ( 2 3 + 4 + 5 6 !");
         assertEquals("2", expr1.child("@num1").text());

+ 1 - 1
assira.grammar-tool/src/test/java/net/ranides/assira/grammar/NodeTest.java

@@ -13,7 +13,7 @@ public class NodeTest {
 
     @Test
     public void findAll() throws IOException {
-        G4Parser parser = G4Parser.newInstance(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Selector<Number> num1 = new G4SelectorBuilder<Number>()
             .withName("number")

+ 5 - 5
assira.grammar-tool/src/test/java/net/ranides/assira/grammar/SelectorTest.java

@@ -38,13 +38,13 @@ public class SelectorTest {
 
     @Test
     public void compose() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
         assertEquals(34, ops1.first(parser.parse("expression", "2*3 + 7*4")).intValue() );
     }
 
     @Test
     public void skip() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("SkipTest.g4"));
+        G4Parser parser = G4Parser.builder().resource("SkipTest.g4").build();
 
         G4Selector<Integer> num1 = new G4SelectorBuilder<Integer>()
             .withName("number")
@@ -66,7 +66,7 @@ public class SelectorTest {
 
     @Test
     public void findIf() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Selector<Number> num1 = new G4SelectorBuilder<Number>()
             .withName("number")
@@ -99,7 +99,7 @@ public class SelectorTest {
 
     @Test
     public void nested() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         G4Selector<Number> selectNum = new G4SelectorBuilder<Number>()
             .withName("number")
@@ -146,7 +146,7 @@ public class SelectorTest {
 
     @Test
     public void wrongSelector() throws IOException {
-        G4Parser parser = new G4ParserInterpreter(ResourceUtils.text("Calculator.g4"));
+        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();
 
         // without validation: will not throw
         new G4SelectorBuilder<Number>()

assira.grammar-tool/src/test/resources/Calculator.g4 → assira.g4interpreter/src/test/resources/Calculator.g4


assira.grammar-tool/src/test/resources/Calculator.paths.txt → assira.g4interpreter/src/test/resources/Calculator.paths.txt


assira.grammar-tool/src/test/resources/Calculator.tree.txt → assira.g4interpreter/src/test/resources/Calculator.tree.txt


assira.grammar-tool/src/test/resources/LabelTest.g4 → assira.g4interpreter/src/test/resources/LabelTest.g4


assira.grammar-tool/src/test/resources/SkipTest.g4 → assira.g4interpreter/src/test/resources/SkipTest.g4


+ 1 - 1
pom.xml

@@ -82,7 +82,7 @@
                 <module>assira.commons</module>
                 <module>assira.enterprise</module>
                 <module>assira.xls</module>
-                <module>assira.grammar-tool</module>
+                <module>assira.g4interpreter</module>
             </modules>
         </profile>
         <profile>