got basic attr action stuff in
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6877]
This commit is contained in:
parent
1b0186329c
commit
05ef22d9e7
|
@ -188,9 +188,26 @@ if (!(<p.ast.text>)) throw new FailedPredicateException(input, "<ruleName>", "<d
|
|||
ActionText(t) ::= "<t.text>"
|
||||
ArgRef(a) ::= "_ctx.<a.name>"
|
||||
RetValueRef(a) ::= "_ctx.<a.name>"
|
||||
QRetValueRef(a) ::= "<a.dict>.<a.name>"
|
||||
/** How to translate $tokenLabel */
|
||||
TokenRef(t) ::= "<t.name>"
|
||||
SetAttr(s,rhsChunks) ::= "_ctx.<s.name> = <rhsChunks>;"
|
||||
SetQAttr(s,rhsChunks) ::= "<s.dict>.<s.name> = <rhsChunks>;"
|
||||
|
||||
TokenPropertyRef_text(t) ::= "(<t.label>!=null?<t.label>.getText():null)"
|
||||
TokenPropertyRef_type(t) ::= "(<t.label>!=null?<t.label>.getType():0)"
|
||||
TokenPropertyRef_line(t) ::= "(<t.label>!=null?<t.label>.getLine():0)"
|
||||
TokenPropertyRef_pos(t) ::= "(<t.label>!=null?<t.label>.getCharPositionInLine():0)"
|
||||
TokenPropertyRef_channel(t) ::= "(<t.label>!=null?<t.label>.getChannel():0)"
|
||||
TokenPropertyRef_index(t) ::= "(<t.label>!=null?<t.label>.getTokenIndex():0)"
|
||||
TokenPropertyRef_tree(t) ::= "<t.label>_tree"
|
||||
TokenPropertyRef_int(t) ::= "(<t.label>!=null?Integer.valueOf(<t.label>.getText()):0)"
|
||||
|
||||
RulePropertyRef_start(r) ::= "(<r.label>!=null?((<file.TokenLabelType>)<r.label>.start):null)"
|
||||
RulePropertyRef_stop(r) ::= "(<r.label>!=null?((<file.TokenLabelType>)<r.label>.stop):null)"
|
||||
RulePropertyRef_tree(r) ::= "(<r.label>!=null?((<file.ASTLabelType>)<r.label>.tree):null)"
|
||||
RulePropertyRef_text(r) ::= "(<r.label>!=null?((TokenStream)state.input).toString(<r.label>.start,<r.label>.stop):null)"
|
||||
RulePropertyRef_st(r) ::= "(<r.label>!=null?<r.label>.st:null)"
|
||||
|
||||
AddToList(a) ::= "<a.listName>.add(<a.opWithResultToAdd.label>);"
|
||||
|
||||
|
|
|
@ -9,13 +9,34 @@ import org.antlr.v4.parse.ActionSplitterListener;
|
|||
import org.antlr.v4.tool.ActionAST;
|
||||
import org.antlr.v4.tool.Attribute;
|
||||
import org.antlr.v4.tool.ErrorType;
|
||||
import org.antlr.v4.tool.Rule;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** */
|
||||
public class ActionTranslator implements ActionSplitterListener {
|
||||
public static final Map<String, Class> rulePropToModelMap = new HashMap<String, Class>() {{
|
||||
put("start", RulePropertyRef_start.class);
|
||||
put("stop", RulePropertyRef_stop.class);
|
||||
put("tree", RulePropertyRef_tree.class);
|
||||
put("text", RulePropertyRef_text.class);
|
||||
put("st", RulePropertyRef_st.class);
|
||||
}};
|
||||
|
||||
public static final Map<String, Class> tokenPropToModelMap = new HashMap<String, Class>() {{
|
||||
put("text", TokenPropertyRef_text.class);
|
||||
put("type", TokenPropertyRef_type.class);
|
||||
put("line", TokenPropertyRef_line.class);
|
||||
put("index", TokenPropertyRef_index.class);
|
||||
put("pos", TokenPropertyRef_pos.class);
|
||||
put("channel", TokenPropertyRef_channel.class);
|
||||
put("tree", TokenPropertyRef_tree.class);
|
||||
put("int", TokenPropertyRef_int.class);
|
||||
}};
|
||||
|
||||
ActionAST node;
|
||||
RuleFunction rf;
|
||||
List<ActionChunk> chunks = new ArrayList<ActionChunk>();
|
||||
|
@ -60,7 +81,6 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
|
||||
public void attr(String expr, Token x) {
|
||||
System.out.println("attr "+x);
|
||||
|
||||
Attribute a = node.resolver.resolveToAttribute(x.getText(), node);
|
||||
if ( a!=null ) {
|
||||
switch ( a.dict.type ) {
|
||||
|
@ -70,22 +90,16 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
// case PREDEFINED_TREE_RULE: chunks.add(new RetValueRef(x.getText())); break;
|
||||
}
|
||||
}
|
||||
if ( node.resolver.resolveToDynamicScope(x.getText(), node)!=null ) {
|
||||
return; // $S for scope S is ok
|
||||
}
|
||||
if ( node.resolver.resolvesToToken(x.getText(), node) ) {
|
||||
if ( node.resolver.resolvesToLabel(x.getText(), node) ) {
|
||||
chunks.add(new TokenRef(x.getText())); // $label
|
||||
}
|
||||
else { // $ID for token ref or label of token; find label
|
||||
String label = factory.gen.target.getImplicitTokenLabel(x.getText());
|
||||
chunks.add(new TokenRef(label)); // $label
|
||||
}
|
||||
chunks.add(new TokenRef(getTokenLabel(x.getText()))); // $label
|
||||
return;
|
||||
}
|
||||
if ( node.resolver.resolvesToListLabel(x.getText(), node) ) {
|
||||
return; // $ids for ids+=ID etc...
|
||||
}
|
||||
if ( node.resolver.resolveToDynamicScope(x.getText(), node)!=null ) {
|
||||
return; // $S for scope S is ok
|
||||
}
|
||||
// switch ( a.dict.type ) {
|
||||
// case ARG: chunks.add(new ArgRef(x.getText())); break;
|
||||
// case RET: chunks.add(new RetValueRef(x.getText())); break;
|
||||
|
@ -98,38 +112,31 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
// }
|
||||
}
|
||||
|
||||
/** $x.y = expr; */
|
||||
public void setQualifiedAttr(String expr, Token x, Token y, Token rhs) {
|
||||
System.out.println("setQAttr "+x+"."+y+"="+rhs);
|
||||
// x has to be current rule; just set y attr
|
||||
List<ActionChunk> rhsChunks = translateActionChunk(factory,rf,rhs.getText(),node);
|
||||
chunks.add(new SetAttr(y.getText(), rhsChunks));
|
||||
}
|
||||
|
||||
public void qualifiedAttr(String expr, Token x, Token y) {
|
||||
System.out.println("qattr "+x+"."+y);
|
||||
if ( node.resolver.resolveToAttribute(x.getText(), y.getText(), node)==null ) {
|
||||
Rule rref = isolatedRuleRef(x.getText());
|
||||
if ( rref!=null ) {
|
||||
if ( rref!=null && rref.args!=null && rref.args.get(y.getText())!=null ) {
|
||||
g.tool.errMgr.grammarError(ErrorType.INVALID_RULE_PARAMETER_REF,
|
||||
g.fileName, y, y.getText(), expr);
|
||||
}
|
||||
else {
|
||||
errMgr.grammarError(ErrorType.UNKNOWN_RULE_ATTRIBUTE,
|
||||
g.fileName, y, y.getText(), rref.name, expr);
|
||||
}
|
||||
}
|
||||
else if ( !node.resolver.resolvesToAttributeDict(x.getText(), node) ) {
|
||||
errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE,
|
||||
g.fileName, x, x.getText(), expr);
|
||||
}
|
||||
else {
|
||||
errMgr.grammarError(ErrorType.UNKNOWN_ATTRIBUTE_IN_SCOPE,
|
||||
g.fileName, y, y.getText(), expr);
|
||||
}
|
||||
Attribute a = node.resolver.resolveToAttribute(x.getText(), y.getText(), node);
|
||||
switch ( a.dict.type ) {
|
||||
case ARG: chunks.add(new ArgRef(y.getText())); break; // has to be current rule
|
||||
case RET: chunks.add(new QRetValueRef(getRuleLabel(x.getText()), y.getText())); break;
|
||||
case PREDEFINED_RULE: chunks.add(getRulePropertyRef(x, y)); break;
|
||||
case TOKEN: chunks.add(getTokenPropertyRef(x, y)); break;
|
||||
// case PREDEFINED_LEXER_RULE: chunks.add(new RetValueRef(x.getText())); break;
|
||||
// case PREDEFINED_TREE_RULE: chunks.add(new RetValueRef(x.getText())); break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setAttr(String expr, Token x, Token rhs) {
|
||||
System.out.println("setAttr "+x+" "+rhs);
|
||||
List<ActionChunk> exprchunks = translateActionChunk(factory,rf,rhs.getText(),node);
|
||||
chunks.add(new SetAttr(x.getText(), exprchunks));
|
||||
List<ActionChunk> rhsChunks = translateActionChunk(factory,rf,rhs.getText(),node);
|
||||
chunks.add(new SetAttr(x.getText(), rhsChunks));
|
||||
}
|
||||
|
||||
public void setDynamicScopeAttr(String expr, Token x, Token y, Token rhs) {
|
||||
|
@ -159,7 +166,7 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
public void setExprAttribute(String expr) {
|
||||
}
|
||||
|
||||
public void setAttribute(String expr) {
|
||||
public void setSTAttribute(String expr) {
|
||||
}
|
||||
|
||||
public void templateExpr(String expr) {
|
||||
|
@ -172,6 +179,44 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
chunks.add(new ActionText(text));
|
||||
}
|
||||
|
||||
TokenPropertyRef getTokenPropertyRef(Token x, Token y) {
|
||||
try {
|
||||
Class c = tokenPropToModelMap.get(y.getText());
|
||||
Constructor ctor = c.getConstructor(new Class[] {String.class});
|
||||
TokenPropertyRef ref =
|
||||
(TokenPropertyRef)ctor.newInstance(getRuleLabel(x.getText()));
|
||||
return ref;
|
||||
}
|
||||
catch (Exception e) {
|
||||
factory.g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
RulePropertyRef getRulePropertyRef(Token x, Token y) {
|
||||
try {
|
||||
Class c = rulePropToModelMap.get(y.getText());
|
||||
Constructor ctor = c.getConstructor(new Class[] {String.class});
|
||||
RulePropertyRef ref =
|
||||
(RulePropertyRef)ctor.newInstance(getRuleLabel(x.getText()));
|
||||
return ref;
|
||||
}
|
||||
catch (Exception e) {
|
||||
factory.g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getTokenLabel(String x) {
|
||||
if ( node.resolver.resolvesToLabel(x, node) ) return x;
|
||||
return factory.gen.target.getImplicitTokenLabel(x);
|
||||
}
|
||||
|
||||
public String getRuleLabel(String x) {
|
||||
if ( node.resolver.resolvesToLabel(x, node) ) return x;
|
||||
return factory.gen.target.getImplicitRuleLabel(x);
|
||||
}
|
||||
|
||||
// public String getTokenLabel(String x, ActionAST node) {
|
||||
// Alternative alt = node.resolver.
|
||||
// Rule r = node.nfaState.rule;
|
||||
|
|
|
@ -39,7 +39,7 @@ public class InvokeRule extends SrcOp implements LabeledOp {
|
|||
argExprs = ast.getChild(0).getText();
|
||||
}
|
||||
|
||||
// If action refs as rulename not label, we need to define implicit label
|
||||
// If action refs rule as rulename not label, we need to define implicit label
|
||||
if ( factory.currentAlt.ruleRefsInActions.containsKey(ast.getText()) ) {
|
||||
String label = factory.gen.target.getImplicitRuleLabel(ast.getText());
|
||||
labels.add(label);
|
||||
|
|
|
@ -11,11 +11,15 @@ public class ParserFile extends OutputModelObject {
|
|||
public Parser parser;
|
||||
public List<DFADecl> dfaDecls = new ArrayList<DFADecl>();
|
||||
public List<BitSetDecl> bitSetDecls = new ArrayList<BitSetDecl>();
|
||||
public String TokenLabelType;
|
||||
public String ASTLabelType;
|
||||
|
||||
public ParserFile(OutputModelFactory factory, String fileName) {
|
||||
super(factory);
|
||||
this.fileName = fileName;
|
||||
factory.file = this;
|
||||
TokenLabelType = factory.gen.g.getOption("TokenLabelType");
|
||||
ASTLabelType = factory.gen.g.getOption("ASTLabelType");
|
||||
parser = new Parser(factory, this);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class QRetValueRef extends RetValueRef {
|
||||
public String dict;
|
||||
public QRetValueRef(String dict, String name) {
|
||||
super(name);
|
||||
this.dict = dict;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef extends ActionChunk {
|
||||
public String label;
|
||||
|
||||
public RulePropertyRef(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef_st extends RulePropertyRef {
|
||||
public RulePropertyRef_st(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef_start extends RulePropertyRef {
|
||||
public RulePropertyRef_start(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef_stop extends RulePropertyRef {
|
||||
public RulePropertyRef_stop(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef_text extends RulePropertyRef {
|
||||
public RulePropertyRef_text(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class RulePropertyRef_tree extends RulePropertyRef {
|
||||
public RulePropertyRef_tree(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef extends ActionChunk {
|
||||
public String label;
|
||||
|
||||
public TokenPropertyRef(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_channel extends TokenPropertyRef {
|
||||
public TokenPropertyRef_channel(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_index extends TokenPropertyRef {
|
||||
public TokenPropertyRef_index(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_int extends TokenPropertyRef {
|
||||
public TokenPropertyRef_int(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_line extends TokenPropertyRef {
|
||||
public TokenPropertyRef_line(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_pos extends TokenPropertyRef {
|
||||
public TokenPropertyRef_pos(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_text extends TokenPropertyRef {
|
||||
public TokenPropertyRef_text(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_tree extends TokenPropertyRef {
|
||||
public TokenPropertyRef_tree(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.codegen.src.actions;
|
||||
|
||||
/** */
|
||||
public class TokenPropertyRef_type extends TokenPropertyRef {
|
||||
public TokenPropertyRef_type(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -18,8 +18,8 @@ public interface ActionSplitterListener {
|
|||
|
||||
void templateInstance(String expr);
|
||||
void indirectTemplateInstance(String expr);
|
||||
void setExprAttribute(String expr);
|
||||
void setAttribute(String expr);
|
||||
void setExprAttribute(String expr); // TODO: rename
|
||||
void setSTAttribute(String expr);
|
||||
void templateExpr(String expr);
|
||||
|
||||
void unknownSyntax(Token t);
|
||||
|
|
|
@ -187,7 +187,7 @@ public class AttributeChecks implements ActionSplitterListener {
|
|||
public void templateInstance(String expr) { }
|
||||
public void indirectTemplateInstance(String expr) { }
|
||||
public void setExprAttribute(String expr) { }
|
||||
public void setAttribute(String expr) { }
|
||||
public void setSTAttribute(String expr) { }
|
||||
public void templateExpr(String expr) { }
|
||||
|
||||
// SUPPORT
|
||||
|
|
|
@ -43,7 +43,7 @@ public class BlankActionSplitterListener implements ActionSplitterListener {
|
|||
public void setExprAttribute(String expr) {
|
||||
}
|
||||
|
||||
public void setAttribute(String expr) {
|
||||
public void setSTAttribute(String expr) {
|
||||
}
|
||||
|
||||
public void templateExpr(String expr) {
|
||||
|
|
Loading…
Reference in New Issue