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