made listener for action splitter

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6686]
This commit is contained in:
parrt 2010-02-11 11:17:29 -08:00
parent b51019140c
commit e825a77466
12 changed files with 1204 additions and 932 deletions

View File

@ -182,6 +182,7 @@ public class CommonTree extends BaseTree {
}
// TODO: move to basetree when i settle on how runtime works
// TODO: don't include this node!!
/** include this node */
public CommonTree getFirstDescendantWithType(int type) {
if ( getType()==type ) return this;

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ANTLRLexer.g 2010-02-10 17:19:36
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ANTLRLexer.g 2010-02-11 11:14:45
/*
[The "BSD licence"]

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ANTLRParser.g 2010-02-10 17:19:39
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ANTLRParser.g 2010-02-11 11:14:47
/*
[The "BSD licence"]
@ -284,7 +284,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: DOC_COMMENT, grammarType, id, rules, prequelConstruct
// elements: DOC_COMMENT, id, grammarType, rules, prequelConstruct
// token labels:
// rule labels: retval
// token list labels:
@ -788,7 +788,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: option, OPTIONS
// elements: OPTIONS, option
// token labels:
// rule labels: retval
// token list labels:
@ -1559,7 +1559,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ASSIGN, STRING_LITERAL, id
// elements: STRING_LITERAL, id, ASSIGN
// token labels:
// rule labels: retval
// token list labels:
@ -1853,7 +1853,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, actionScopeName, id, AT
// elements: ACTION, AT, id, actionScopeName
// token labels:
// rule labels: retval
// token list labels:
@ -2353,7 +2353,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, altListAsBlock, ARG_ACTION, exceptionGroup, ruleReturns, rulePrequel, DOC_COMMENT, ruleModifiers
// elements: exceptionGroup, ruleReturns, rulePrequel, ARG_ACTION, DOC_COMMENT, ruleModifiers, altListAsBlock, id
// token labels:
// rule labels: retval
// token list labels:
@ -2579,7 +2579,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ACTION, CATCH, ARG_ACTION
// elements: CATCH, ACTION, ARG_ACTION
// token labels:
// rule labels: retval
// token list labels:
@ -2976,7 +2976,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: qid, THROWS
// elements: THROWS, qid
// token labels:
// rule labels: retval
// token list labels:
@ -3185,7 +3185,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: SCOPE, id
// elements: id, SCOPE
// token labels:
// rule labels: retval
// token list labels:
@ -3284,7 +3284,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: AT, ACTION, id
// elements: ACTION, AT, id
// token labels:
// rule labels: retval
// token list labels:
@ -3800,7 +3800,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: elements, rewrite
// elements: rewrite, elements
// token labels:
// rule labels: retval
// token list labels:
@ -4165,7 +4165,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ebnfSuffix, labeledElement
// elements: labeledElement, ebnfSuffix
// token labels:
// rule labels: retval
// token list labels:
@ -4279,7 +4279,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: atom, ebnfSuffix
// elements: ebnfSuffix, atom
// token labels:
// rule labels: retval
// token list labels:
@ -4504,7 +4504,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: treeSpec, ebnfSuffix
// elements: ebnfSuffix, treeSpec
// token labels:
// rule labels: retval
// token list labels:
@ -4944,7 +4944,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: blockSuffixe, block
// elements: block, blockSuffixe
// token labels:
// rule labels: retval
// token list labels:
@ -5450,7 +5450,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: DOT, id, ruleref
// elements: id, DOT, ruleref
// token labels:
// rule labels: retval
// token list labels:
@ -5508,7 +5508,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, DOT, terminal
// elements: id, terminal, DOT
// token labels:
// rule labels: retval
// token list labels:
@ -5750,7 +5750,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: NOT, block
// elements: block, NOT
// token labels:
// rule labels: retval
// token list labels:
@ -6014,7 +6014,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: altList, ra, ACTION, optionsSpec
// elements: ACTION, altList, ra, optionsSpec
// token labels:
// rule labels: retval
// token list labels:
@ -6242,7 +6242,7 @@ public class ANTLRParser extends Parser {
{
// AST REWRITE
// elements: ARG_ACTION, RULE_REF
// elements: RULE_REF, ARG_ACTION
// token labels:
// rule labels: retval
// token list labels:
@ -7229,7 +7229,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: predicatedRewrite, nakedRewrite
// elements: nakedRewrite, predicatedRewrite
// token labels:
// rule labels: retval
// token list labels:
@ -7317,7 +7317,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: SEMPRED, rewriteAlt, rewriteAlt, SEMPRED
// elements: rewriteAlt, SEMPRED, SEMPRED, rewriteAlt
// token labels:
// rule labels: retval
// token list labels:
@ -7776,7 +7776,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: ebnfSuffix, rewriteTreeAtom
// elements: rewriteTreeAtom, ebnfSuffix
// token labels:
// rule labels: retval
// token list labels:
@ -8098,7 +8098,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: elementOptions, ARG_ACTION, TOKEN_REF
// elements: elementOptions, TOKEN_REF, ARG_ACTION
// token labels:
// rule labels: retval
// token list labels:
@ -8339,7 +8339,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: rewriteTreeAlt, ebnfSuffix
// elements: ebnfSuffix, rewriteTreeAlt
// token labels:
// rule labels: retval
// token list labels:
@ -8476,7 +8476,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: rewriteTreeAtom, TREE_BEGIN, rewriteTreeElement
// elements: rewriteTreeElement, rewriteTreeAtom, TREE_BEGIN
// token labels:
// rule labels: retval
// token list labels:
@ -8631,7 +8631,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: TEMPLATE, str, rewriteTemplateArgs
// elements: TEMPLATE, rewriteTemplateArgs, str
// token labels: str
// rule labels: retval
// token list labels:
@ -9138,7 +9138,7 @@ public class ANTLRParser extends Parser {
// AST REWRITE
// elements: id, ACTION
// elements: ACTION, id
// token labels:
// rule labels: retval
// token list labels:

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ASTVerifier.g 2010-02-10 17:19:39
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 ASTVerifier.g 2010-02-11 11:14:48
/*
[The "BSD license"]

View File

@ -8,24 +8,33 @@ import org.antlr.v4.tool.*;
}
@members {
/*
public void setQualifiedAttr(Token x, Token y, Token expr) { }
public void qualifiedAttr(Token x, Token y) { }
public void setDynamicScopeAttr() { }
public void dynamicScopeAttr() { }
public void setDynamicNegativeIndexedScopeAttr() { }
public void dynamicNegativeIndexedScopeAttr() { }
public void setDynamicAbsoluteIndexedScopeAttr() { }
public void dynamicAbsoluteIndexedScopeAttr() { }
public void setAttr() { }
public void attr() { }
public void setDynamicScopeAttr(Token x, Token y, Token expr) { }
public void dynamicScopeAttr(Token x, Token y) { }
public void setDynamicNegativeIndexedScopeAttr(Token x, Token y, Token index, Token expr) { }
public void dynamicNegativeIndexedScopeAttr(Token x, Token y, Token index) { }
public void setDynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index, Token expr) { }
public void dynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index) { }
public void setAttr(Token x, Token expr) { }
public void attr(Token x) { }
public void templateInstance() { }
public void indirectTemplateInstance() { }
public void setExprAttribute() { }
public void setAttribute() { }
public void templateExpr() { }
public void unknownSyntax() { }
public void text() { }
public void unknownSyntax(String text) { }
public void text(String text) { }
*/
ActionSplitterListener delegate;
public ActionSplitter(CharStream input, ActionSplitterListener delegate) {
this(input, new RecognizerSharedState());
this.delegate = delegate;
}
/** force filtering (and return tokens). triggers all above actions. */
public List<Token> getActionChunks() {
List<Token> chunks = new ArrayList<Token>();
Token t = nextToken();
@ -37,21 +46,32 @@ public List<Token> getActionChunks() {
}
}
// ignore comments right away
COMMENT
: '/*' ( options {greedy=false;} : . )* '*/' {delegate.text($text);}
;
LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {delegate.text($text);}
;
SET_QUALIFIED_ATTR
: '$' x=ID '.' y=ID WS? '=' expr=ATTR_VALUE_EXPR ';'
{setQualifiedAttr($x, $y, $expr);}
{delegate.setQualifiedAttr($x, $y, $expr);}
;
QUALIFIED_ATTR
: '$' x=ID '.' y=ID {input.LA(1)!='('}? {qualifiedAttr($x, $y);}
: '$' x=ID '.' y=ID {input.LA(1)!='('}? {delegate.qualifiedAttr($x, $y);}
;
SET_DYNAMIC_SCOPE_ATTR
: '$' x=ID '::' y=ID WS? '=' expr=ATTR_VALUE_EXPR ';'
{delegate.setDynamicScopeAttr($x, $y, $expr);}
;
DYNAMIC_SCOPE_ATTR
: '$' x=ID '::' y=ID
: '$' x=ID '::' y=ID {delegate.dynamicScopeAttr($x, $y);}
;
/** To access deeper (than top of stack) scopes, use the notation:
@ -63,29 +83,33 @@ DYNAMIC_SCOPE_ATTR
* $x[0]::y is the absolute 0 indexed element (bottom of the stack)
*/
SET_DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR
: '$' x=ID '[' '-' expr=SCOPE_INDEX_EXPR ']' '::' y=ID
: '$' x=ID '[' '-' index=SCOPE_INDEX_EXPR ']' '::' y=ID
WS? ('=' expr=ATTR_VALUE_EXPR ';')?
{delegate.setDynamicNegativeIndexedScopeAttr($x, $y, $index, $expr);}
;
DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR
: '$' x=ID '[' '-' expr=SCOPE_INDEX_EXPR ']' '::' y=ID
: '$' x=ID '[' '-' index=SCOPE_INDEX_EXPR ']' '::' y=ID
{delegate.dynamicNegativeIndexedScopeAttr($x, $y, $index);}
;
SET_DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR
: '$' x=ID '[' expr=SCOPE_INDEX_EXPR ']' '::' y=ID
: '$' x=ID '[' index=SCOPE_INDEX_EXPR ']' '::' y=ID
WS? ('=' expr=ATTR_VALUE_EXPR ';')?
{delegate.setDynamicAbsoluteIndexedScopeAttr($x, $y, $index, $expr);}
;
DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR
: '$' x=ID '[' expr=SCOPE_INDEX_EXPR ']' '::' y=ID
: '$' x=ID '[' index=SCOPE_INDEX_EXPR ']' '::' y=ID
{delegate.dynamicAbsoluteIndexedScopeAttr($x, $y, $index);}
;
SET_ATTR
: ATTR WS? '=' expr=ATTR_VALUE_EXPR ';'
: '$' x=ID WS? '=' expr=ATTR_VALUE_EXPR ';' {delegate.setAttr($x, $expr);}
;
ATTR
: '$' ID
: '$' x=ID {delegate.attr($x);}
;
/** %foo(a={},b={},...) ctor */
@ -117,26 +141,15 @@ TEMPLATE_EXPR
;
UNKNOWN_SYNTAX
: '$'
{
// chunks.add(getText());
// shouldn't need an error here. Just accept \$ if it doesn't look like anything
}
| '%' (ID|'.'|'('|')'|','|'{'|'}'|'"')*
{
/*
chunks.add(getText());
ErrorManager.grammarError(ErrorManager.MSG_INVALID_TEMPLATE_ACTION,
grammar,
actionToken,
getText());
*/
}
@after {delegate.unknownSyntax($text);}
: '%' (ID|'.'|'('|')'|','|'{'|'}'|'"')*
;
TEXT: ( '\\$'
// Anything else is just random text
TEXT
@after {delegate.text($text);}
: ( '\\$'
| '\\%'
| ~('$'|'%')
)+
;

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -0,0 +1,40 @@
package org.antlr.v4.parse;
import org.antlr.runtime.Token;
/** */
public interface ActionSplitterListener {
void setQualifiedAttr(Token x, Token y, Token expr);
void qualifiedAttr(Token x, Token y);
void setDynamicScopeAttr(Token x, Token y, Token expr);
void dynamicScopeAttr(Token x, Token y);
void setDynamicNegativeIndexedScopeAttr(Token x, Token y, Token index, Token expr);
void dynamicNegativeIndexedScopeAttr(Token x, Token y, Token index);
void setDynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index, Token expr);
void dynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index);
void setAttr(Token x, Token expr);
void attr(Token x);
void templateInstance();
void indirectTemplateInstance();
void setExprAttribute();
void setAttribute();
void templateExpr();
void unknownSyntax(String text);
void text(String text);
}

View File

@ -3,29 +3,58 @@ package org.antlr.v4.semantics;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.Token;
import org.antlr.v4.parse.ActionSplitter;
import org.antlr.v4.parse.ActionSplitterListener;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarAST;
import org.antlr.v4.tool.Rule;
/** Trigger checks for various kinds of attribute expressions. no side-effects */
public class AttributeChecks extends ActionSplitter {
public class AttributeChecks implements ActionSplitterListener {
public Grammar g;
public Rule r;
public GrammarAST node;
String action;
public AttributeChecks(Grammar g, String action) {
super(new ANTLRStringStream(action));
public AttributeChecks(Grammar g, Rule r, GrammarAST node, String action) {
this.g = g;
this.r = r;
this.node = node;
this.action = action;
}
public void setQualifiedAttr(Token x, Token y, Token expr) { }
public void examine() {
ActionSplitter splitter =
new ActionSplitter(new ANTLRStringStream(action), this);
splitter.getActionChunks(); // forces eval, fills extractor
}
public void setQualifiedAttr(Token x, Token y, Token expr) {
System.out.println(x+"."+y+"="+expr);
new AttributeChecks(g, r, node, expr.getText()).examine();
}
public void qualifiedAttr(Token x, Token y) {
System.out.println(x+"."+y);
}
public void setDynamicScopeAttr() { }
public void dynamicScopeAttr() { }
public void setDynamicNegativeIndexedScopeAttr() { }
public void dynamicNegativeIndexedScopeAttr() { }
public void setDynamicAbsoluteIndexedScopeAttr() { }
public void dynamicAbsoluteIndexedScopeAttr() { }
public void setAttr() { }
public void attr() { }
public void unknownSyntax() { }
public void text() { }
public void setDynamicScopeAttr(Token x, Token y, Token expr) { }
public void dynamicScopeAttr(Token x, Token y) { }
public void setDynamicNegativeIndexedScopeAttr(Token x, Token y, Token index, Token expr) { }
public void dynamicNegativeIndexedScopeAttr(Token x, Token y, Token index) { }
public void setDynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index, Token expr) { }
public void dynamicAbsoluteIndexedScopeAttr(Token x, Token y, Token index) { }
public void setAttr(Token x, Token expr) {
System.out.println(x+"="+expr);
new AttributeChecks(g, r, node, expr.getText()).examine();
}
public void attr(Token x) {
System.out.println(x);
}
public void unknownSyntax(String text) { }
public void text(String text) { }
// don't care
public void templateInstance() { }
public void indirectTemplateInstance() { }
public void setExprAttribute() { }
public void setAttribute() { }
public void templateExpr() { }
}

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-10 17:19:41
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-11 11:14:49
/*
[The "BSD license"]

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 CollectSymbols.g 2010-02-10 17:19:40
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 CollectSymbols.g 2010-02-11 11:14:49
/*
[The "BSD license"]

View File

@ -59,12 +59,12 @@ public class SemanticPipeline {
public void checkAttributeExpressions(Grammar g, CollectSymbols collector) {
for (Rule r : collector.rules) {
for (GrammarAST a : r.namedActions.values()) {
AttributeChecks extractor = new AttributeChecks(g, a.getText());
extractor.getActionChunks(); // forces eval, fills extractor
AttributeChecks checker = new AttributeChecks(g, r, a, a.getText());
checker.examine();
}
for (GrammarAST a : r.inlineActions) {
AttributeChecks extractor = new AttributeChecks(g, a.getText());
extractor.getActionChunks(); // forces eval, fills extractor
AttributeChecks checker = new AttributeChecks(g, r, a, a.getText());
checker.examine();
}
}
}