cleanup, rm dead etypes, add check for ids that cause code gen issues; added notion of one-off error

This commit is contained in:
Terence Parr 2012-11-18 12:53:21 -08:00
parent aba4034051
commit d2054012f0
7 changed files with 42 additions and 15 deletions

View File

@ -5,6 +5,8 @@ November 18, 2012
* fixed: undefined rule refs caused exception * fixed: undefined rule refs caused exception
* cleanup, rm dead etypes, add check for ids that cause code gen issues * cleanup, rm dead etypes, add check for ids that cause code gen issues
* added notion of one-off error * added notion of one-off error
* added check for v3 backward incompatibilities:
** tree grammars
November 17, 2012 November 17, 2012

View File

@ -46,6 +46,7 @@ import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.parse.GrammarASTAdaptor; import org.antlr.v4.parse.GrammarASTAdaptor;
import org.antlr.v4.parse.GrammarTreeVisitor; import org.antlr.v4.parse.GrammarTreeVisitor;
import org.antlr.v4.parse.ToolANTLRParser; import org.antlr.v4.parse.ToolANTLRParser;
import org.antlr.v4.parse.v3TreeGrammarException;
import org.antlr.v4.runtime.misc.LogManager; import org.antlr.v4.runtime.misc.LogManager;
import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.semantics.SemanticPipeline;
@ -556,7 +557,7 @@ public class Tool {
} }
ANTLRFileStream in = new ANTLRFileStream(file.getAbsolutePath(), grammarEncoding); ANTLRFileStream in = new ANTLRFileStream(file.getAbsolutePath(), grammarEncoding);
GrammarRootAST t = load(in); GrammarRootAST t = load(fileName, in);
return t; return t;
} }
catch (IOException ioe) { catch (IOException ioe) {
@ -586,17 +587,17 @@ public class Tool {
} }
ANTLRFileStream in = new ANTLRFileStream(importedFile.getAbsolutePath()); ANTLRFileStream in = new ANTLRFileStream(importedFile.getAbsolutePath());
GrammarRootAST root = load(in); GrammarRootAST root = load(g.fileName, in);
Grammar imported = createGrammar(root); Grammar imported = createGrammar(root);
imported.fileName = importedFile.getAbsolutePath(); imported.fileName = importedFile.getAbsolutePath();
return imported; return imported;
} }
public GrammarRootAST loadFromString(String grammar) { public GrammarRootAST loadFromString(String grammar) {
return load(new ANTLRStringStream(grammar)); return load("<string>", new ANTLRStringStream(grammar));
} }
public GrammarRootAST load(CharStream in) { public GrammarRootAST load(String fileName, CharStream in) {
try { try {
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
ANTLRLexer lexer = new ANTLRLexer(in); ANTLRLexer lexer = new ANTLRLexer(in);
@ -604,15 +605,20 @@ public class Tool {
lexer.tokens = tokens; lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, this); ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor); p.setTreeAdaptor(adaptor);
ParserRuleReturnScope r = p.grammarSpec(); try {
GrammarAST root = (GrammarAST)r.getTree(); ParserRuleReturnScope r = p.grammarSpec();
if ( root instanceof GrammarRootAST) { GrammarAST root = (GrammarAST)r.getTree();
((GrammarRootAST)root).hasErrors = p.getNumberOfSyntaxErrors()>0; if ( root instanceof GrammarRootAST) {
((GrammarRootAST)root).tokens = tokens; ((GrammarRootAST)root).hasErrors = p.getNumberOfSyntaxErrors()>0;
if ( grammarOptions!=null ) { ((GrammarRootAST)root).tokens = tokens;
((GrammarRootAST)root).cmdLineOptions = grammarOptions; if ( grammarOptions!=null ) {
((GrammarRootAST)root).cmdLineOptions = grammarOptions;
}
return ((GrammarRootAST)root);
} }
return ((GrammarRootAST)root); }
catch (v3TreeGrammarException e) {
errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
} }
return null; return null;
} }

View File

@ -398,6 +398,7 @@ FRAGMENT : 'fragment' ;
LEXER : 'lexer' ; LEXER : 'lexer' ;
PARSER : 'parser' ; PARSER : 'parser' ;
GRAMMAR : 'grammar' ; GRAMMAR : 'grammar' ;
TREE_GRAMMAR : 'tree' WSNLCHARS* 'grammar' ;
PROTECTED : 'protected' ; PROTECTED : 'protected' ;
PUBLIC : 'public' ; PUBLIC : 'public' ;
PRIVATE : 'private' ; PRIVATE : 'private' ;

View File

@ -1,6 +1,6 @@
/* /*
[The "BSD license"] [The "BSD license"]
Copyright (c) 2010 Jim Idle, Terence Parr Copyright (c) 2012 Jim Idle, Terence Parr
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -99,7 +99,7 @@ tokens {
@header { @header {
/* /*
[The "BSD licence"] [The "BSD licence"]
Copyright (c) 2005-2009 Terence Parr Copyright (c) 2005-20012 Terence Parr
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -202,6 +202,7 @@ if ( options!=null ) {
grammarType grammarType
@after { @after {
if ( $tg!=null ) throw new v3TreeGrammarException(tg);
if ( $t!=null ) ((GrammarRootAST)$tree).grammarType = $t.type; if ( $t!=null ) ((GrammarRootAST)$tree).grammarType = $t.type;
else ((GrammarRootAST)$tree).grammarType=COMBINED; else ((GrammarRootAST)$tree).grammarType=COMBINED;
} }
@ -211,6 +212,8 @@ grammarType
// A combined lexer and parser specification // A combined lexer and parser specification
| g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "COMBINED_GRAMMAR"] | g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "COMBINED_GRAMMAR"]
| tg=TREE_GRAMMAR
) )
; ;

View File

@ -0,0 +1,12 @@
package org.antlr.v4.parse;
import org.antlr.runtime.Token;
import org.antlr.v4.runtime.misc.ParseCancellationException;
public class v3TreeGrammarException extends ParseCancellationException {
public Token location;
public v3TreeGrammarException(Token location) {
this.location = location;
}
}

View File

@ -116,6 +116,9 @@ public enum ErrorType {
LEXER_COMMAND_PLACEMENT_ISSUE(133, "->command in lexer rule <arg> must be last element of single outermost alt", ErrorSeverity.ERROR), LEXER_COMMAND_PLACEMENT_ISSUE(133, "->command in lexer rule <arg> must be last element of single outermost alt", ErrorSeverity.ERROR),
USE_OF_BAD_WORD(134, "symbol <arg> conflicts with generated code in target language or runtime", ErrorSeverity.ERROR), USE_OF_BAD_WORD(134, "symbol <arg> conflicts with generated code in target language or runtime", ErrorSeverity.ERROR),
// Backward incompatibility errors
V3_TREE_GRAMMAR(200, "tree grammars are not supported in ANTLR v4", ErrorSeverity.ERROR_ONE_OFF),
// Dependency sorting errors // Dependency sorting errors
/** t1.g4 -> t2.g4 -> t3.g4 ->t1.g4 */ /** t1.g4 -> t2.g4 -> t3.g4 ->t1.g4 */

View File

@ -238,7 +238,7 @@ public class Grammar implements AttributeResolver {
org.antlr.runtime.ANTLRStringStream in = new org.antlr.runtime.ANTLRStringStream(grammarText); org.antlr.runtime.ANTLRStringStream in = new org.antlr.runtime.ANTLRStringStream(grammarText);
in.name = fileName; in.name = fileName;
this.ast = tool.load(in); this.ast = tool.load(fileName, in);
if ( ast==null ) return; if ( ast==null ) return;
// ensure each node has pointer to surrounding grammar // ensure each node has pointer to surrounding grammar