forked from jasder/antlr
add -Xlog, add .ctx to rule refs. parse tree test pass.
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9436]
This commit is contained in:
parent
95f3afa562
commit
72684907bc
|
@ -89,14 +89,14 @@ public class LogManager {
|
|||
}
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
public String save() throws IOException {
|
||||
String dir = System.getProperty("java.io.tmpdir");
|
||||
dir = ".";
|
||||
String defaultFilename =
|
||||
dir + "/antlr-" +
|
||||
new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss").format(new Date()) + ".log";
|
||||
System.out.println(defaultFilename);
|
||||
save(defaultFilename);
|
||||
return defaultFilename;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -504,12 +504,14 @@ RulePropertyRef_stop(r) ::= "(_localctx.<r.label>!=null?(_localctx.<r.label>.st
|
|||
RulePropertyRef_tree(r) ::= "(_localctx.<r.label>!=null?(_localctx.<r.label>.tree):null)"
|
||||
RulePropertyRef_text(r) ::= "(_localctx.<r.label>!=null?_input.toString(_localctx.<r.label>.start,_localctx.<r.label>.stop):null)"
|
||||
RulePropertyRef_st(r) ::= "(_localctx.<r.label>!=null?_localctx.<r.label>.st:null)"
|
||||
RulePropertyRef_ctx(r) ::= "_localctx.<r.label>"
|
||||
|
||||
ThisRulePropertyRef_start(r) ::= "_localctx.start"
|
||||
ThisRulePropertyRef_stop(r) ::= "_localctx.stop"
|
||||
ThisRulePropertyRef_tree(r) ::= "_localctx.tree"
|
||||
ThisRulePropertyRef_text(r) ::= "_input.toString(_localctx.start, _input.LT(-1))"
|
||||
ThisRulePropertyRef_st(r) ::= "_localctx.st"
|
||||
ThisRulePropertyRef_ctx(r) ::= "_localctx"
|
||||
|
||||
/*
|
||||
TreeRulePropertyRef_text(r) ::= <%
|
||||
|
|
|
@ -92,22 +92,24 @@ public class Tool {
|
|||
public boolean saveLexer = false;
|
||||
public boolean genListener = true;
|
||||
public boolean launch_ST_inspector = false;
|
||||
public boolean force_atn = false;
|
||||
public boolean force_atn = false;
|
||||
public boolean log = false;
|
||||
|
||||
public static Option[] optionDefs = {
|
||||
new Option("outputDirectory", "-o", OptionArgType.STRING, "specify output directory where all output is generated"),
|
||||
new Option("libDirectory", "-lib", OptionArgType.STRING, "specify location of .token files"),
|
||||
new Option("report", "-report", "print out a report about the grammar(s) processed"),
|
||||
new Option("printGrammar", "-print", "print out the grammar without actions"),
|
||||
new Option("debug", "-debug", "generate a parser that emits debugging events"),
|
||||
new Option("profile", "-profile", "generate a parser that computes profiling information"),
|
||||
new Option("trace", "-trace", "generate a recognizer that traces rule entry/exit"),
|
||||
new Option("generate_ATN_dot", "-atn", "generate rule augmented transition networks"),
|
||||
new Option("msgFormat", "-message-format", OptionArgType.STRING, "specify output style for messages"),
|
||||
new Option("genListener", "-walker", "generate parse tree walker and listener"),
|
||||
new Option("saveLexer", "-Xsavelexer", "save temp lexer file created for combined grammars"),
|
||||
new Option("launch_ST_inspector", "-XdbgST", "launch StringTemplate visualizer on generated code"),
|
||||
new Option("force_atn", "-Xforceatn", "use the ATN simulator for all predictions"),
|
||||
public static Option[] optionDefs = {
|
||||
new Option("outputDirectory", "-o", OptionArgType.STRING, "specify output directory where all output is generated"),
|
||||
new Option("libDirectory", "-lib", OptionArgType.STRING, "specify location of .token files"),
|
||||
new Option("report", "-report", "print out a report about the grammar(s) processed"),
|
||||
new Option("printGrammar", "-print", "print out the grammar without actions"),
|
||||
new Option("debug", "-debug", "generate a parser that emits debugging events"),
|
||||
new Option("profile", "-profile", "generate a parser that computes profiling information"),
|
||||
new Option("trace", "-trace", "generate a recognizer that traces rule entry/exit"),
|
||||
new Option("generate_ATN_dot", "-atn", "generate rule augmented transition networks"),
|
||||
new Option("msgFormat", "-message-format", OptionArgType.STRING, "specify output style for messages"),
|
||||
new Option("genListener", "-walker", "generate parse tree walker and listener"),
|
||||
new Option("saveLexer", "-Xsavelexer", "save temp lexer file created for combined grammars"),
|
||||
new Option("launch_ST_inspector", "-XdbgST", "launch StringTemplate visualizer on generated code"),
|
||||
new Option("force_atn", "-Xforceatn", "use the ATN simulator for all predictions"),
|
||||
new Option("log", "-Xlog", "dump lots of logging info to antlr-timestamp.log"),
|
||||
};
|
||||
|
||||
// helper vars for option management
|
||||
|
@ -135,11 +137,23 @@ public class Tool {
|
|||
DefaultToolListener defaultListener = new DefaultToolListener(this);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Tool antlr = new Tool(args);
|
||||
if ( args.length == 0 ) { antlr.help(); antlr.exit(0); }
|
||||
|
||||
antlr.processGrammarsOnCommandLine();
|
||||
Tool antlr = new Tool(args);
|
||||
if ( args.length == 0 ) { antlr.help(); antlr.exit(0); }
|
||||
|
||||
try {
|
||||
antlr.processGrammarsOnCommandLine();
|
||||
}
|
||||
finally {
|
||||
if ( antlr.log ) {
|
||||
try {
|
||||
String logname = antlr.logMgr.save();
|
||||
System.out.println("wrote "+logname);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
antlr.errMgr.toolError(ErrorType.INTERNAL_ERROR, ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( antlr.return_dont_exit ) return;
|
||||
|
||||
if (antlr.errMgr.getNumErrors() > 0) {
|
||||
|
|
|
@ -51,31 +51,33 @@ import java.util.Map;
|
|||
public class ActionTranslator implements ActionSplitterListener {
|
||||
public static final Map<String, Class> thisRulePropToModelMap = new HashMap<String, Class>() {{
|
||||
put("start", ThisRulePropertyRef_start.class);
|
||||
put("stop", ThisRulePropertyRef_stop.class);
|
||||
put("tree", ThisRulePropertyRef_tree.class);
|
||||
put("text", ThisRulePropertyRef_text.class);
|
||||
put("st", ThisRulePropertyRef_st.class);
|
||||
put("stop", ThisRulePropertyRef_stop.class);
|
||||
put("tree", ThisRulePropertyRef_tree.class);
|
||||
put("text", ThisRulePropertyRef_text.class);
|
||||
put("st", ThisRulePropertyRef_st.class);
|
||||
put("ctx", ThisRulePropertyRef_ctx.class);
|
||||
}};
|
||||
|
||||
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);
|
||||
put("stop", RulePropertyRef_stop.class);
|
||||
put("tree", RulePropertyRef_tree.class);
|
||||
put("text", RulePropertyRef_text.class);
|
||||
put("st", RulePropertyRef_st.class);
|
||||
put("ctx", RulePropertyRef_ctx.class);
|
||||
}};
|
||||
|
||||
public static final Map<String, Class> treeRulePropToModelMap = rulePropToModelMap;
|
||||
|
||||
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("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("pos", TokenPropertyRef_pos.class);
|
||||
put("channel", TokenPropertyRef_channel.class);
|
||||
put("tree", TokenPropertyRef_tree.class);
|
||||
put("int", TokenPropertyRef_int.class);
|
||||
put("tree", TokenPropertyRef_tree.class);
|
||||
put("int", TokenPropertyRef_int.class);
|
||||
}};
|
||||
|
||||
CodeGenerator gen;
|
||||
|
@ -295,7 +297,7 @@ public class ActionTranslator implements ActionSplitterListener {
|
|||
return ref;
|
||||
}
|
||||
catch (Exception e) {
|
||||
g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e);
|
||||
g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, prop.getText());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.codegen.model.actions;
|
||||
|
||||
public class RulePropertyRef_ctx extends RulePropertyRef {
|
||||
public RulePropertyRef_ctx(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.codegen.model.actions;
|
||||
|
||||
public class ThisRulePropertyRef_ctx extends RulePropertyRef {
|
||||
public ThisRulePropertyRef_ctx(String label) {
|
||||
super(label);
|
||||
}
|
||||
}
|
|
@ -111,7 +111,6 @@ public class AttributeChecks implements ActionSplitterListener {
|
|||
if ( node.resolver.resolveToAttribute(x.getText(), y.getText(), node)==null ) {
|
||||
Rule rref = isolatedRuleRef(x.getText());
|
||||
if ( rref!=null ) {
|
||||
//if ( r.name.equals(x.getText()) ) return; // $a.x in rule a is ok
|
||||
if ( rref.args!=null && rref.args.get(y.getText())!=null ) {
|
||||
g.tool.errMgr.grammarError(ErrorType.INVALID_RULE_PARAMETER_REF,
|
||||
g.fileName, y, y.getText(), expr);
|
||||
|
|
|
@ -93,19 +93,11 @@ public class Alternative implements AttributeResolver {
|
|||
return false;
|
||||
}
|
||||
|
||||
// public String getTokenLabel(String x, ActionAST node) {
|
||||
// LabelElementPair anyLabelDef = getAnyLabelDef(x);
|
||||
// if ( anyLabelDef!=null ) return anyLabelDef.label.getText();
|
||||
// if ( tokenRefs.get(x)!=null ) {
|
||||
//
|
||||
// }
|
||||
// LabelElementPair anyLabelDef = getAnyLabelDef(x);
|
||||
// if ( anyLabelDef!=null && anyLabelDef.type==LabelType.TOKEN_LABEL ) return true;
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public boolean resolvesToAttributeDict(String x, ActionAST node) {
|
||||
if ( resolvesToToken(x, node) ) return true;
|
||||
if ( ruleRefs.get(x)!=null ) return true; // rule ref in this alt?
|
||||
LabelElementPair anyLabelDef = getAnyLabelDef(x);
|
||||
if ( anyLabelDef!=null && anyLabelDef.type==LabelType.RULE_LABEL ) return true;
|
||||
if ( x.equals(rule.name) ) return true; // $r for action in rule r, $r is a dict
|
||||
if ( rule!=null && rule.scope!=null ) return true;
|
||||
return false;
|
||||
|
|
|
@ -38,8 +38,10 @@ import org.stringtemplate.v4.misc.MultiMap;
|
|||
import java.util.*;
|
||||
|
||||
public class Rule implements AttributeResolver {
|
||||
/** Rule refs have a predefined set of attributes as well as
|
||||
/** Rule refs have a predefined set of attributes as well as
|
||||
* the return values and args.
|
||||
*
|
||||
* These must be consistent with ActionTranslator.rulePropToModelMap, ...
|
||||
*/
|
||||
public static AttributeDict predefinedRulePropertiesDict =
|
||||
new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE) {{
|
||||
|
@ -48,6 +50,7 @@ public class Rule implements AttributeResolver {
|
|||
add(new Attribute("stop"));
|
||||
add(new Attribute("tree"));
|
||||
add(new Attribute("st"));
|
||||
add(new Attribute("ctx"));
|
||||
}};
|
||||
|
||||
public static AttributeDict predefinedTreeRulePropertiesDict =
|
||||
|
@ -56,6 +59,7 @@ public class Rule implements AttributeResolver {
|
|||
add(new Attribute("start")); // note: no stop; not meaningful
|
||||
add(new Attribute("tree"));
|
||||
add(new Attribute("st"));
|
||||
add(new Attribute("ctx"));
|
||||
}};
|
||||
|
||||
public static AttributeDict predefinedLexerRulePropertiesDict =
|
||||
|
|
|
@ -8,7 +8,7 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" :r=a ;\n" +
|
||||
"a : 'x' {System.out.println(getRuleInvocationStack());} ;\n";
|
||||
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false);
|
||||
|
@ -21,7 +21,7 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" :r=a ;\n" +
|
||||
"a : 'x' 'y'\n" +
|
||||
" ;\n";
|
||||
|
@ -35,7 +35,7 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" :r=a ;\n" +
|
||||
"a : 'x' | 'y'\n" +
|
||||
" ;\n";
|
||||
|
@ -49,7 +49,7 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" :r=a ;\n" +
|
||||
"a : ('x' | 'y')* 'z'\n" +
|
||||
" ;\n";
|
||||
|
@ -63,7 +63,7 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" : r=a ;\n" +
|
||||
"a : b 'x'\n" +
|
||||
" ;\n" +
|
||||
|
@ -80,13 +80,13 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" : r=a ;\n" +
|
||||
"a : 'x' 'y'\n" +
|
||||
" ;\n" +
|
||||
"Z : 'z'; \n";
|
||||
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzy", false);
|
||||
String expecting = "(a x <ERROR:z> y)\n";
|
||||
String expecting = "(a x z y)\n"; // ERRORs not shown. z is colored red in tree view
|
||||
assertEquals(expecting, result);
|
||||
}
|
||||
|
||||
|
@ -95,13 +95,13 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" : r=a ;\n" +
|
||||
"a : 'x' | 'y'\n" +
|
||||
" ;\n" +
|
||||
"Z : 'z'; \n";
|
||||
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "z", false);
|
||||
String expecting = "(a <ERROR:z>)\n";
|
||||
String expecting = "(a z)\n";
|
||||
assertEquals(expecting, result);
|
||||
}
|
||||
|
||||
|
@ -110,13 +110,13 @@ public class TestParseTrees extends BaseTest {
|
|||
"grammar T;\n" +
|
||||
"s\n" +
|
||||
"@init {setBuildParseTree(true);}\n" +
|
||||
"@after {System.out.println($r.toStringTree(this));}\n" +
|
||||
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
|
||||
" : r=a ;\n" +
|
||||
"a : 'x' 'y'* '!'\n" +
|
||||
" ;\n" +
|
||||
"Z : 'z'; \n";
|
||||
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzyy!", false);
|
||||
String expecting = "(a x <ERROR:z> y y !)\n";
|
||||
String expecting = "(a x z y y !)\n";
|
||||
assertEquals(expecting, result);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue