diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 34be6508a..593c189e6 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -188,9 +188,26 @@ if (!()) throw new FailedPredicateException(input, "", " rulePropToModelMap = new HashMap() {{ + 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 tokenPropToModelMap = new HashMap() {{ + 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 chunks = new ArrayList(); @@ -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 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 exprchunks = translateActionChunk(factory,rf,rhs.getText(),node); - chunks.add(new SetAttr(x.getText(), exprchunks)); + List 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; diff --git a/tool/src/org/antlr/v4/codegen/src/InvokeRule.java b/tool/src/org/antlr/v4/codegen/src/InvokeRule.java index 15c1ee90e..dce9f9075 100644 --- a/tool/src/org/antlr/v4/codegen/src/InvokeRule.java +++ b/tool/src/org/antlr/v4/codegen/src/InvokeRule.java @@ -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); diff --git a/tool/src/org/antlr/v4/codegen/src/ParserFile.java b/tool/src/org/antlr/v4/codegen/src/ParserFile.java index a523f487c..f45d3018f 100644 --- a/tool/src/org/antlr/v4/codegen/src/ParserFile.java +++ b/tool/src/org/antlr/v4/codegen/src/ParserFile.java @@ -11,11 +11,15 @@ public class ParserFile extends OutputModelObject { public Parser parser; public List dfaDecls = new ArrayList(); public List bitSetDecls = new ArrayList(); - + 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); } diff --git a/tool/src/org/antlr/v4/codegen/src/actions/QRetValueRef.java b/tool/src/org/antlr/v4/codegen/src/actions/QRetValueRef.java new file mode 100644 index 000000000..572fcfae1 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/QRetValueRef.java @@ -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; + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef.java new file mode 100644 index 000000000..fb1f3a2ae --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef.java @@ -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; + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_st.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_st.java new file mode 100644 index 000000000..4f927a1a0 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_st.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class RulePropertyRef_st extends RulePropertyRef { + public RulePropertyRef_st(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_start.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_start.java new file mode 100644 index 000000000..7ef8b145f --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_start.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class RulePropertyRef_start extends RulePropertyRef { + public RulePropertyRef_start(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_stop.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_stop.java new file mode 100644 index 000000000..38ddf60be --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_stop.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class RulePropertyRef_stop extends RulePropertyRef { + public RulePropertyRef_stop(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_text.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_text.java new file mode 100644 index 000000000..bc89193cb --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_text.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class RulePropertyRef_text extends RulePropertyRef { + public RulePropertyRef_text(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_tree.java b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_tree.java new file mode 100644 index 000000000..83648c6f8 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/RulePropertyRef_tree.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class RulePropertyRef_tree extends RulePropertyRef { + public RulePropertyRef_tree(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef.java new file mode 100644 index 000000000..839aa100d --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef.java @@ -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; + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_channel.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_channel.java new file mode 100644 index 000000000..f48d7b6d9 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_channel.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_channel extends TokenPropertyRef { + public TokenPropertyRef_channel(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_index.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_index.java new file mode 100644 index 000000000..60b0587f0 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_index.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_index extends TokenPropertyRef { + public TokenPropertyRef_index(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_int.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_int.java new file mode 100644 index 000000000..44c086391 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_int.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_int extends TokenPropertyRef { + public TokenPropertyRef_int(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_line.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_line.java new file mode 100644 index 000000000..fa5ccf8f9 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_line.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_line extends TokenPropertyRef { + public TokenPropertyRef_line(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_pos.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_pos.java new file mode 100644 index 000000000..a70d58940 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_pos.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_pos extends TokenPropertyRef { + public TokenPropertyRef_pos(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_text.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_text.java new file mode 100644 index 000000000..d3ad3f080 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_text.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_text extends TokenPropertyRef { + public TokenPropertyRef_text(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_tree.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_tree.java new file mode 100644 index 000000000..6ada9f4fd --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_tree.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_tree extends TokenPropertyRef { + public TokenPropertyRef_tree(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_type.java b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_type.java new file mode 100644 index 000000000..2bfcb5f8c --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/src/actions/TokenPropertyRef_type.java @@ -0,0 +1,8 @@ +package org.antlr.v4.codegen.src.actions; + +/** */ +public class TokenPropertyRef_type extends TokenPropertyRef { + public TokenPropertyRef_type(String label) { + super(label); + } +} diff --git a/tool/src/org/antlr/v4/parse/ActionSplitterListener.java b/tool/src/org/antlr/v4/parse/ActionSplitterListener.java index 31eee7c93..dee68f8a5 100644 --- a/tool/src/org/antlr/v4/parse/ActionSplitterListener.java +++ b/tool/src/org/antlr/v4/parse/ActionSplitterListener.java @@ -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); diff --git a/tool/src/org/antlr/v4/semantics/AttributeChecks.java b/tool/src/org/antlr/v4/semantics/AttributeChecks.java index 4b7617836..7ce488ee3 100644 --- a/tool/src/org/antlr/v4/semantics/AttributeChecks.java +++ b/tool/src/org/antlr/v4/semantics/AttributeChecks.java @@ -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 diff --git a/tool/src/org/antlr/v4/semantics/BlankActionSplitterListener.java b/tool/src/org/antlr/v4/semantics/BlankActionSplitterListener.java index 431bbb522..a3845e972 100644 --- a/tool/src/org/antlr/v4/semantics/BlankActionSplitterListener.java +++ b/tool/src/org/antlr/v4/semantics/BlankActionSplitterListener.java @@ -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) {