cleaned up a bit so that TestToolSyntaxErrors works. added some cast checks and some bailout upon error before semantic processing

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9483]
This commit is contained in:
parrt 2011-11-29 15:59:23 -08:00
parent e00fa44996
commit 0bf1aca47f
2 changed files with 53 additions and 48 deletions

View File

@ -286,6 +286,8 @@ public class Tool {
if ( g.ast!=null && internalOption_PrintGrammarTree ) System.out.println(g.ast.toStringTree()); if ( g.ast!=null && internalOption_PrintGrammarTree ) System.out.println(g.ast.toStringTree());
//g.ast.inspect(); //g.ast.inspect();
if ( errMgr.getNumErrors()>0 ) return;
// MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT) // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
SemanticPipeline sem = new SemanticPipeline(g); SemanticPipeline sem = new SemanticPipeline(g);
sem.process(); sem.process();
@ -368,12 +370,13 @@ public class Tool {
ToolANTLRParser p = new ToolANTLRParser(tokens, this); ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor); p.setTreeAdaptor(adaptor);
ParserRuleReturnScope r = p.grammarSpec(); ParserRuleReturnScope r = p.grammarSpec();
GrammarRootAST root = (GrammarRootAST)r.getTree(); GrammarAST root = (GrammarAST)r.getTree();
if ( root instanceof GrammarRootAST) { if ( root instanceof GrammarRootAST) {
root.hasErrors = p.getNumberOfSyntaxErrors()>0; ((GrammarRootAST)root).hasErrors = p.getNumberOfSyntaxErrors()>0;
root.tokens = tokens; ((GrammarRootAST)root).tokens = tokens;
return ((GrammarRootAST)root);
} }
return root; return null;
} }
catch (RecognitionException re) { catch (RecognitionException re) {
// TODO: do we gen errors now? // TODO: do we gen errors now?

View File

@ -198,6 +198,8 @@ public class Grammar implements AttributeResolver {
in.name = fileName; in.name = fileName;
this.ast = tool.load(in); this.ast = tool.load(in);
if ( ast==null ) return;
// ensure each node has pointer to surrounding grammar // ensure each node has pointer to surrounding grammar
final Grammar thiz = this; final Grammar thiz = this;
TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());