stop processing if syn err.

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6721]
This commit is contained in:
parrt 2010-02-23 11:53:59 -08:00
parent 3872f92017
commit 4fb0e7db70
6 changed files with 22 additions and 8 deletions

View File

@ -314,7 +314,11 @@ public class Tool {
ANTLRParser p = new ANTLRParser(tokens);
p.setTreeAdaptor(new GrammarASTAdaptor(in));
ParserRuleReturnScope r = p.grammarSpec();
return (GrammarAST)r.getTree();
GrammarAST root = (GrammarAST) r.getTree();
if ( root instanceof GrammarRootAST ) {
((GrammarRootAST)root).hasErrors = p.getNumberOfSyntaxErrors()>0;
}
return root;
}
catch (RecognitionException re) {
// TODO: do we gen errors now?
@ -324,9 +328,12 @@ public class Tool {
}
public void processGrammarsOnCommandLine() {
// TODO: process all files
GrammarAST t = load(grammarFileNames.get(0));
GrammarRootAST lexerAST = null;
if ( t instanceof GrammarASTErrorNode ) return; // came back as error node
if ( ((GrammarRootAST)t).hasErrors ) return;
GrammarRootAST ast = (GrammarRootAST)t;
Grammar g = new Grammar(this, ast);
g.fileName = grammarFileNames.get(0);

View File

@ -26,8 +26,7 @@ public class SemanticPipeline {
ASTVerifier walker = new ASTVerifier(nodes);
try {walker.grammarSpec();}
catch (RecognitionException re) {
ErrorManager.internalError("bad grammar AST structure", re);
return; // don't process; will get internal errors
ErrorManager.fatalInternalError("bad grammar AST structure", re);
}
// DO BASIC / EASY SEMANTIC CHECKS

View File

@ -181,7 +181,12 @@ public class ErrorManager {
state.get().listener.error(msg);
}
public static void internalError(String error, Throwable e) {
public static void fatalInternalError(String error, Throwable e) {
internalError(error, e);
throw new RuntimeException(error, e);
}
public static void internalError(String error, Throwable e) {
state.get().errors++;
StackTraceElement location = getLastNonErrorManagerCodeLocation(e);
String msg = "Exception "+e+"@"+location+": "+error;

View File

@ -104,6 +104,7 @@ public class Grammar implements AttributeResolver {
ParserRuleReturnScope r = p.grammarSpec();
if ( r.getTree() instanceof GrammarRootAST ) {
this.ast = (GrammarRootAST)r.getTree();
this.ast.hasErrors = p.getNumberOfSyntaxErrors()>0;
this.name = ((GrammarAST)ast.getChild(0)).getText();
}
initTokenSymbolTables();

View File

@ -1,8 +1,6 @@
package org.antlr.v4.tool;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.Token;
import org.antlr.v4.parse.ANTLRParser;
import java.util.HashMap;
import java.util.Map;
@ -15,6 +13,8 @@ public class GrammarRootAST extends GrammarASTWithOptions {
}
};
public int grammarType; // LEXER, PARSER, TREE, GRAMMAR (combined)
public boolean hasErrors;
public GrammarRootAST(int type) { super(type); }
public GrammarRootAST(Token t) { super(t); }
public GrammarRootAST(int type, Token t) { super(type, t); }

View File

@ -440,8 +440,10 @@ public abstract class BaseTest {
if ( g.ast!=null ) System.out.println(g.ast.toStringTree());
else System.out.println("null tree");
}
SemanticPipeline sem = new SemanticPipeline();
sem.process(g);
if ( g.ast!=null && !g.ast.hasErrors ) {
SemanticPipeline sem = new SemanticPipeline();
sem.process(g);
}
}
catch (RecognitionException re) {
re.printStackTrace(System.err);