v4: specify default Parser and TreeParser superclass in the code generation template

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9350]
This commit is contained in:
sharwell 2011-11-17 18:12:28 -08:00
parent 7e6eb1fda4
commit 0fb5baca1c
5 changed files with 89 additions and 7 deletions

View File

@ -59,11 +59,11 @@ public class Blank<listener.grammarName>Listener implements <listener.grammarNam
}
>>
Parser(parser, funcs, atn, sempredFuncs) ::= <<
Parser(parser, funcs, atn, sempredFuncs, superclass) ::= <<
<Parser_(ctor="parser_ctor", ...)>
>>
TreeParserModel(parser, funcs, atn, sempredFuncs) ::= <<
TreeParserModel(parser, funcs, atn, sempredFuncs, superclass) ::= <<
<Parser_(ctor="treeparser_ctor",
extras={
@Override
@ -73,9 +73,9 @@ public <file.ASTLabelType> match(int ttype) throws RecognitionException {
}, ...)>
>>
Parser_(parser, funcs, atn, sempredFuncs, ctor, extras) ::= <<
Parser_(parser, funcs, atn, sempredFuncs, ctor, extras, superclass) ::= <<
@SuppressWarnings({"all", "warnings", "unchecked", "unused"})
public class <parser.name> extends <parser.superclass> {
public class <parser.name> extends <superclass> {
<if(parser.tokens)>
public static final int
<parser.tokens:{k | <k>=<parser.tokens.(k)>}; separator=", ", wrap, anchor>;
@ -459,6 +459,9 @@ setState(<p.stateNumber>);
if (!(<chunks>)) throw new FailedPredicateException(this, <if(p.msg)><p.msg><else><failChunks><endif>);
>>
DefaultParserSuperClass(s) ::= "Parser"
DefaultTreeParserSuperClass(s) ::= "TreeParser"
ActionText(t) ::= "<t.text>"
ArgRef(a) ::= "_localctx.<a.name>"
LocalRef(a) ::= "_localctx.<a.name>"

View File

@ -30,6 +30,9 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.actions.ActionChunk;
import org.antlr.v4.codegen.model.actions.ActionText;
import org.antlr.v4.codegen.model.actions.DefaultParserSuperClass;
import org.antlr.v4.tool.*;
import java.util.*;
@ -38,7 +41,7 @@ import java.util.*;
public class Parser extends OutputModelObject {
public String name;
public String grammarName;
public String superclass;
@ModelElement public ActionChunk superclass;
public Map<String,Integer> tokens;
public String[] tokenNames;
public Set<String> ruleNames;
@ -65,6 +68,10 @@ public class Parser extends OutputModelObject {
ruleNames = g.rules.keySet();
rules = g.rules.values();
atn = new SerializedATN(factory, g.atn);
superclass = g.getOptionString("superClass", "Parser");
if (g.getOptionString("superClass") != null) {
superclass = new ActionText(g.getOptionString("superClass"));
} else {
superclass = new DefaultParserSuperClass();
}
}
}

View File

@ -30,12 +30,18 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.actions.ActionText;
import org.antlr.v4.codegen.model.actions.DefaultTreeParserSuperClass;
import org.antlr.v4.tool.Grammar;
public class TreeParserModel extends Parser {
public TreeParserModel(OutputModelFactory factory, ParserFile file) {
super(factory, file);
Grammar g = factory.getGrammar();
superclass = g.getOptionString("superClass", "TreeParser");
if (g.getOptionString("superClass") != null) {
superclass = new ActionText(g.getOptionString("superClass"));
} else {
superclass = new DefaultTreeParserSuperClass();
}
}
}

View File

@ -0,0 +1,33 @@
/*
[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 DefaultParserSuperClass extends ActionChunk {
}

View File

@ -0,0 +1,33 @@
/*
[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 DefaultTreeParserSuperClass extends ActionChunk {
}