[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6694]
This commit is contained in:
parrt 2010-02-15 13:28:11 -08:00
parent e38535a9bc
commit d23c9cb527
14 changed files with 1062 additions and 920 deletions

View File

@ -185,8 +185,8 @@ REPEATED_PREQUEL(arg) ::=
"repeated grammar prequel spec (option, token, or import); please merge"
UNKNOWN_DYNAMIC_SCOPE(arg) ::=
"unknown dynamic scope: <arg>"
UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE(arg,arg2) ::=
"unknown dynamically-scoped attribute for scope <arg>: <arg2>"
UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE(arg,arg2,arg3) ::=
"unknown dynamically-scoped attribute for scope <arg>: <arg2> in <arg3>"
RULE_REF_AMBIG_WITH_RULE_IN_ALT(arg) ::=
"reference $<arg> is ambiguous; rule <arg> is enclosing rule and referenced in the production (assuming enclosing rule)"
ISOLATED_RULE_ATTRIBUTE(arg) ::=

View File

@ -1,4 +1,4 @@
// $ANTLR ${project.version} ${buildNumber} ANTLRLexer.g 2010-02-15 12:12:15
// $ANTLR ${project.version} ${buildNumber} ANTLRLexer.g 2010-02-15 13:04:55
/*
[The "BSD licence"]
@ -31,11 +31,6 @@ package org.antlr.v4.parse;
import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
/** Read in an ANTLR grammar and build an AST. Try not to do
* any actions, just build the tree.
*
@ -268,7 +263,7 @@ public class ANTLRLexer extends Lexer {
if ( (( input.LA(2) != '/')) ) {
alt3=1;
}
else if ( (((( true )&&( !(input.LA(1) == '*' && input.LA(2) == '/') ))||( true ))) ) {
else if ( ((( true )||(( true )&&( !(input.LA(1) == '*' && input.LA(2) == '/') )))) ) {
alt3=2;
}
else {

View File

@ -1,4 +1,4 @@
// $ANTLR ${project.version} ${buildNumber} ANTLRParser.g 2010-02-15 12:12:17
// $ANTLR ${project.version} ${buildNumber} ANTLRParser.g 2010-02-15 13:04:58
/*
[The "BSD licence"]
@ -29,17 +29,12 @@
*/
package org.antlr.v4.parse;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.v4.tool.*;
import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.antlr.runtime.tree.*;
import java.util.List;
/** The definitive ANTLR v3 grammar to parse ANTLR v4 grammars.
* The grammar builds ASTs that are sniffed by subsequent stages.
@ -289,7 +284,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, grammarType, prequelConstruct, DOC_COMMENT, rules
// elements: id, grammarType, prequelConstruct, rules, DOC_COMMENT
// token labels:
// rule labels: retval
// token list labels:
@ -1128,7 +1123,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: IMPORT, delegateGrammar
// elements: delegateGrammar, IMPORT
// token labels:
// rule labels: retval
// token list labels:
@ -1564,7 +1559,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, ASSIGN, STRING_LITERAL
// elements: ASSIGN, id, STRING_LITERAL
// token labels:
// rule labels: retval
// token list labels:
@ -1704,7 +1699,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, SCOPE, id
// elements: id, ACTION, SCOPE
// token labels:
// rule labels: retval
// token list labels:
@ -1858,7 +1853,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: actionScopeName, id, AT, ACTION
// elements: id, AT, ACTION, actionScopeName
// token labels:
// rule labels: retval
// token list labels:
@ -2358,7 +2353,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, ruleReturns, DOC_COMMENT, rulePrequel, ARG_ACTION, ruleModifiers, altListAsBlock, exceptionGroup
// elements: rulePrequel, ruleReturns, id, DOC_COMMENT, altListAsBlock, exceptionGroup, ARG_ACTION, ruleModifiers
// token labels:
// rule labels: retval
// token list labels:
@ -2584,7 +2579,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: CATCH, ARG_ACTION, ACTION
// elements: ARG_ACTION, ACTION, CATCH
// token labels:
// rule labels: retval
// token list labels:
@ -2667,7 +2662,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, FINALLY
// elements: FINALLY, ACTION
// token labels:
// rule labels: retval
// token list labels:
@ -3111,7 +3106,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: SCOPE, ACTION
// elements: ACTION, SCOPE
// token labels:
// rule labels: retval
// token list labels:
@ -3289,7 +3284,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, id, AT
// elements: AT, id, ACTION
// token labels:
// rule labels: retval
// token list labels:
@ -4170,7 +4165,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: labeledElement, ebnfSuffix
// elements: ebnfSuffix, labeledElement
// token labels:
// rule labels: retval
// token list labels:
@ -4284,7 +4279,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: atom, ebnfSuffix
// elements: ebnfSuffix, atom
// token labels:
// rule labels: retval
// token list labels:
@ -4509,7 +4504,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: treeSpec, ebnfSuffix
// elements: ebnfSuffix, treeSpec
// token labels:
// rule labels: retval
// token list labels:
@ -4949,7 +4944,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: block, blockSuffixe
// elements: blockSuffixe, block
// token labels:
// rule labels: retval
// token list labels:
@ -5455,7 +5450,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ruleref, id, DOT
// elements: ruleref, DOT, id
// token labels:
// rule labels: retval
// token list labels:
@ -5513,7 +5508,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, terminal, DOT
// elements: terminal, DOT, id
// token labels:
// rule labels: retval
// token list labels:
@ -5989,7 +5984,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: optionsSpec, altList, ra
// elements: ra, altList, optionsSpec
// token labels:
// rule labels: retval
// token list labels:
@ -6164,7 +6159,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ARG_ACTION, RULE_REF, op
// elements: ARG_ACTION, op, RULE_REF
// token labels: op
// rule labels: retval
// token list labels:
@ -6517,7 +6512,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: TOKEN_REF, elementOptions, ARG_ACTION
// elements: ARG_ACTION, TOKEN_REF, elementOptions
// token labels:
// rule labels: retval
// token list labels:
@ -6588,7 +6583,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: STRING_LITERAL, elementOptions
// elements: elementOptions, STRING_LITERAL
// token labels:
// rule labels: retval
// token list labels:
@ -6709,7 +6704,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: terminal, ROOT
// elements: ROOT, terminal
// token labels:
// rule labels: retval
// token list labels:
@ -7198,7 +7193,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: predicatedRewrite, nakedRewrite
// elements: nakedRewrite, predicatedRewrite
// token labels:
// rule labels: retval
// token list labels:
@ -8067,7 +8062,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: TOKEN_REF, elementOptions, ARG_ACTION
// elements: elementOptions, ARG_ACTION, TOKEN_REF
// token labels:
// rule labels: retval
// token list labels:
@ -8445,7 +8440,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: rewriteTreeAtom, TREE_BEGIN, rewriteTreeElement
// elements: TREE_BEGIN, rewriteTreeElement, rewriteTreeAtom
// token labels:
// rule labels: retval
// token list labels:
@ -8600,7 +8595,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: str, rewriteTemplateArgs, TEMPLATE
// elements: str, TEMPLATE, rewriteTemplateArgs
// token labels: str
// rule labels: retval
// token list labels:
@ -8861,7 +8856,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, rewriteTemplateArgs
// elements: rewriteTemplateArgs, ACTION
// token labels:
// rule labels: retval
// token list labels:

View File

@ -1,4 +1,4 @@
// $ANTLR ${project.version} ${buildNumber} ASTVerifier.g 2010-02-15 12:12:18
// $ANTLR ${project.version} ${buildNumber} ASTVerifier.g 2010-02-15 13:04:59
/*
[The "BSD license"]
@ -26,14 +26,15 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.parse;
import org.antlr.v4.tool.*;
import org.antlr.v4.runtime.tree.CommonTree; // use updated v4 one not v3
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;import java.util.Stack;
import org.antlr.runtime.tree.TreeNodeStream;
import org.antlr.runtime.tree.TreeParser;
import org.antlr.runtime.tree.TreeRuleReturnScope;
import org.antlr.v4.runtime.tree.CommonTree;
import org.antlr.v4.tool.GrammarAST;
import java.util.List;
import java.util.ArrayList;
/** The definitive ANTLR v3 tree grammar to parse ANTLR v4 grammars.
* Parses trees created in ANTLRParser.g.

View File

@ -43,6 +43,10 @@ LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {delegate.text($text);}
;
ESC : '\\$'
| '\\%'
;
SET_QUALIFIED_ATTR
: '$' x=ID '.' y=ID WS? '=' expr=ATTR_VALUE_EXPR ';'
{delegate.setQualifiedAttr($text, $x, $y, $expr);}
@ -135,9 +139,7 @@ UNKNOWN_SYNTAX
// Anything else is just random text
TEXT
@after {delegate.text($text);}
: ( '\\$'
| '\\%'
)+
: ~('$'|'%')+
;
fragment

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,26 @@
INDIRECT_TEMPLATE_INSTANCE=23
INDIRECT_TEMPLATE_INSTANCE=24
LINE_COMMENT=5
DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR=15
SET_DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR=14
SET_ATTRIBUTE=25
TEMPLATE_EXPR=26
TEXT=28
ID=6
QUALIFIED_ATTR=10
ACTION=22
UNKNOWN_SYNTAX=27
SET_QUALIFIED_ATTR=9
SET_DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR=16
WS=7
ARG=20
TEMPLATE_INSTANCE=21
SET_EXPR_ATTRIBUTE=24
ATTR_VALUE_EXPR=8
SET_DYNAMIC_SCOPE_ATTR=11
SCOPE_INDEX_EXPR=13
DYNAMIC_SCOPE_ATTR=12
SET_ATTR=18
ESC=6
DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR=16
SET_DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR=15
SET_ATTRIBUTE=26
TEMPLATE_EXPR=27
TEXT=29
ID=7
QUALIFIED_ATTR=11
ACTION=23
UNKNOWN_SYNTAX=28
SET_QUALIFIED_ATTR=10
SET_DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR=17
WS=8
ARG=21
TEMPLATE_INSTANCE=22
SET_EXPR_ATTRIBUTE=25
ATTR_VALUE_EXPR=9
SET_DYNAMIC_SCOPE_ATTR=12
SCOPE_INDEX_EXPR=14
DYNAMIC_SCOPE_ATTR=13
SET_ATTR=19
COMMENT=4
ATTR=19
DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR=17
ATTR=20
DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR=18

View File

@ -95,7 +95,6 @@ public class AttributeChecks implements ActionSplitterListener {
}
public void setAttr(String expr, Token x, Token rhs) {
System.out.println("setAttr x="+x+" = "+rhs+"; expr="+expr);
if ( !node.resolver.resolves(x.getText(), node) ) {
ErrorManager.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE,
g.fileName, x, x.getText(), expr);
@ -115,19 +114,42 @@ public class AttributeChecks implements ActionSplitterListener {
}
}
public void setDynamicScopeAttr(String expr, Token x, Token y, Token rhs) { }
public void setDynamicScopeAttr(String expr, Token x, Token y, Token rhs) {
System.out.println("SET "+x+" :: "+y);
}
public void dynamicScopeAttr(String expr, Token x, Token y) {
System.out.println(x+" :: "+y);
if ( !node.resolver.resolves(x.getText(), y.getText(), node) ) {
if ( !node.resolver.resolves(x.getText(), node) &&
(r==null || !r.name.equals(x.getText())) )
{
ErrorManager.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE,
g.fileName, x, x.getText(), expr);
}
else {
ErrorManager.grammarError(ErrorType.UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE,
g.fileName, y, x.getText(), y.getText(), expr);
}
}
}
public void setDynamicNegativeIndexedScopeAttr(String expr, Token x, Token y, Token index, Token rhs) { }
public void setDynamicNegativeIndexedScopeAttr(String expr, Token x, Token y,
Token index, Token rhs) {
public void dynamicNegativeIndexedScopeAttr(String expr, Token x, Token y, Token index) { }
}
public void setDynamicAbsoluteIndexedScopeAttr(String expr, Token x, Token y, Token index, Token rhs) { }
public void dynamicNegativeIndexedScopeAttr(String expr, Token x, Token y,
Token index) {
}
public void dynamicAbsoluteIndexedScopeAttr(String expr, Token x, Token y, Token index) { }
public void setDynamicAbsoluteIndexedScopeAttr(String expr, Token x, Token y,
Token index, Token rhs) {
}
public void dynamicAbsoluteIndexedScopeAttr(String expr, Token x, Token y,
Token index) {
}
public void unknownSyntax(String text) {
System.err.println("unknown: "+text);

View File

@ -1,4 +1,4 @@
// $ANTLR ${project.version} ${buildNumber} BasicSemanticTriggers.g 2010-02-15 12:12:19
// $ANTLR ${project.version} ${buildNumber} BasicSemanticTriggers.g 2010-02-15 13:05:00
/*
[The "BSD license"]
@ -26,15 +26,14 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.semantics;
import org.antlr.v4.tool.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;import java.util.Stack;
import java.util.List;
import org.antlr.runtime.tree.TreeNodeStream;
import org.antlr.runtime.tree.TreeRuleReturnScope;
import org.antlr.v4.tool.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
/** Triggers for the basic semantics of the input. Side-effects:
* Set token, block, rule options in the tree. Load field option
* with grammar options. Only legal options are set.

View File

@ -1,4 +1,4 @@
// $ANTLR ${project.version} ${buildNumber} CollectSymbols.g 2010-02-15 12:12:19
// $ANTLR ${project.version} ${buildNumber} CollectSymbols.g 2010-02-15 13:05:00
/*
[The "BSD license"]
@ -26,19 +26,15 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.semantics;
import org.antlr.v4.tool.*;
import org.antlr.v4.parse.*;
import java.util.Set;
import java.util.HashSet;
import org.stringtemplate.v4.misc.MultiMap;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;import java.util.Stack;
import java.util.List;
import org.antlr.runtime.tree.TreeNodeStream;
import org.antlr.runtime.tree.TreeRuleReturnScope;
import org.antlr.v4.parse.ScopeParser;
import org.antlr.v4.tool.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
/** Collects rules, terminals, strings, actions, scopes etc... from AST
* Side-effects: None
*/

View File

@ -59,7 +59,15 @@ public class Alternative implements AttributeResolver {
return getParent().resolves(x, y, node);
}
public Rule resolveRefToRule(String x, ActionAST node) {
public boolean dynScopeResolves(String x, ActionAST node) {
return getParent().dynScopeResolves(x,node);
}
public boolean dynScopeResolves(String x, String y, ActionAST node) {
return getParent().dynScopeResolves(x,y,node);
}
public Rule resolveRefToRule(String x, ActionAST node) {
if ( ruleRefs.get(x)!=null ) return rule.g.getRule(x);
List<LabelElementPair> labels = labelDefs.get(x);
if ( labels!=null ) { // it's a label ref. is it a rule label?

View File

@ -7,7 +7,9 @@ package org.antlr.v4.tool;
*/
public interface AttributeResolver {
public AttributeResolver getParent();
public boolean resolves(String x, ActionAST node);
public boolean resolves(String x, String y, ActionAST node);
public boolean resolves(String x, ActionAST node);
public boolean resolves(String x, String y, ActionAST node);
public boolean dynScopeResolves(String x, ActionAST node);
public boolean dynScopeResolves(String x, String y, ActionAST node);
public Rule resolveRefToRule(String x, ActionAST node);
}

View File

@ -224,6 +224,21 @@ public class Grammar implements AttributeResolver {
*/
public boolean resolves(String x, String y, ActionAST node) { return false; }
public boolean dynScopeResolves(String x, ActionAST node) {
if ( scopes.get(x)!=null ) return true;
// resolve inside of a rule? x can be any rule ref
if ( !(node.resolver instanceof Grammar) ) {
Rule r = getRule(x);
if ( r!=null && r.scope!=null ) return true;
}
return false;
}
public boolean dynScopeResolves(String x, String y, ActionAST node) {
AttributeScope s = scopes.get(x);
return s.get(y)!=null;
}
/** Can't be a rule ref in grammar named action */
public Rule resolveRefToRule(String x, ActionAST node) { return null; }

View File

@ -93,33 +93,23 @@ public class Rule implements AttributeResolver {
/** For $x.y, is x an arg, retval, predefined prop, token/rule/label ref?
* If so, make sure y resolves within that perspective.
* For $x::y, is x this rule or another? If so, is y in that scope?
*/
public boolean resolves(String x, String y, ActionAST node) {
Rule r = resolveRefToRule(x, node);
if ( r!=null ) r.resolvesAsRetvalOrProperty(y);
if ( r!=null ) return r.resolvesAsRetvalOrProperty(y);
return getParent().resolves(x,y,node);
// if ( x.equals(this.name) ) { // $x.y ref in rule x is same as $y
// return resolves(y, node);
// }
//
// if ( node.resolver == this ) { // action not in alt (attr space is this rule)
// List<LabelElementPair> labels = getLabelDefs().get(x);
// if ( labels!=null ) {
// // it's a label ref, compute scope from label type and grammar type
// LabelElementPair anyLabelDef = labels.get(0);
// // predefined?
// if ( getPredefinedScope(anyLabelDef.type).get(y)!=null) return true;
// if ( anyLabelDef.type==LabelType.RULE_LABEL ) {
// Rule ref = g.getRule(anyLabelDef.element.getText());
// return ref.resolvesAsRetvalOrProperty(y);
// }
// }
// }
// return false;
}
public Rule resolveRefToRule(String x, ActionAST node) {
public boolean dynScopeResolves(String x, ActionAST node) {
return x.equals(this.name);
}
public boolean dynScopeResolves(String x, String y, ActionAST node) {
return x.equals(this.name) && scope.get(y)!=null;
}
public Rule resolveRefToRule(String x, ActionAST node) {
if ( x.equals(this.name) ) return this;
if ( node.resolver == this ) { // action not in alt (attr space is this rule)
List<LabelElementPair> labels = getLabelDefs().get(x);