track AST nodes -> NFA now and add more code gen
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6846]
This commit is contained in:
parent
5baff5d37e
commit
8fa7179c6d
|
@ -23,6 +23,20 @@ parserFunction(f,code) ::= <<
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
|
codeBlock(c, ops) ::= <<
|
||||||
|
<ops>
|
||||||
|
>>
|
||||||
|
|
||||||
|
switch(c, alts) ::= <<
|
||||||
|
switch ( input.LA(1) ) {
|
||||||
|
<alts>
|
||||||
|
}
|
||||||
|
>>
|
||||||
|
|
||||||
|
matchToken(m) ::= <<
|
||||||
|
match(<m.ttype>);
|
||||||
|
>>
|
||||||
|
|
||||||
codeFileExtension() ::= ".java"
|
codeFileExtension() ::= ".java"
|
||||||
|
|
||||||
true() ::= "true"
|
true() ::= "true"
|
||||||
|
|
|
@ -65,34 +65,10 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
Handle h = new Handle(start, stop);
|
Handle h = new Handle(start, stop);
|
||||||
// FASerializer ser = new FASerializer(g, h.left);
|
// FASerializer ser = new FASerializer(g, h.left);
|
||||||
// System.out.println(ruleAST.toStringTree()+":\n"+ser);
|
// System.out.println(ruleAST.toStringTree()+":\n"+ser);
|
||||||
|
ruleAST.nfaState = start;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NFAState newState(Class nodeType, GrammarAST node) {
|
|
||||||
try {
|
|
||||||
Constructor ctor = nodeType.getConstructor(NFA.class);
|
|
||||||
NFAState s = (NFAState)ctor.newInstance(nfa);
|
|
||||||
s.ast = node;
|
|
||||||
s.rule = currentRule;
|
|
||||||
nfa.addState(s);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
ErrorManager.internalError("can't create NFA node: "+nodeType.getName(), e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BasicState newState(GrammarAST node) {
|
|
||||||
BasicState n = new BasicState(nfa);
|
|
||||||
n.rule = currentRule;
|
|
||||||
n.ast = node;
|
|
||||||
nfa.addState(n);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BasicState newState() { return newState(null); }
|
|
||||||
|
|
||||||
/** From label A build Graph o-A->o */
|
/** From label A build Graph o-A->o */
|
||||||
public Handle tokenRef(TerminalAST node) {
|
public Handle tokenRef(TerminalAST node) {
|
||||||
BasicState left = newState(node);
|
BasicState left = newState(node);
|
||||||
|
@ -100,7 +76,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
int ttype = g.getTokenType(node.getText());
|
int ttype = g.getTokenType(node.getText());
|
||||||
left.transition = new AtomTransition(ttype, right);
|
left.transition = new AtomTransition(ttype, right);
|
||||||
right.incidentTransition = left.transition;
|
right.incidentTransition = left.transition;
|
||||||
|
node.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +88,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
BasicState right = newState(associatedAST);
|
BasicState right = newState(associatedAST);
|
||||||
left.transition = new SetTransition(set, right);
|
left.transition = new SetTransition(set, right);
|
||||||
right.incidentTransition = left.transition;
|
right.incidentTransition = left.transition;
|
||||||
|
associatedAST.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,6 +144,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
RuleStopState stop = nfa.ruleToStopState.get(r);
|
RuleStopState stop = nfa.ruleToStopState.get(r);
|
||||||
epsilon(stop, right);
|
epsilon(stop, right);
|
||||||
|
|
||||||
|
node.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,6 +153,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
BasicState left = newState(node);
|
BasicState left = newState(node);
|
||||||
BasicState right = newState(node);
|
BasicState right = newState(node);
|
||||||
epsilon(left, right);
|
epsilon(left, right);
|
||||||
|
node.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,6 +166,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
BasicState left = newState(pred);
|
BasicState left = newState(pred);
|
||||||
NFAState right = newState(pred);
|
NFAState right = newState(pred);
|
||||||
left.transition = new PredicateTransition(pred, right);
|
left.transition = new PredicateTransition(pred, right);
|
||||||
|
pred.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +184,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
BasicState left = newState(action);
|
BasicState left = newState(action);
|
||||||
NFAState right = newState(action);
|
NFAState right = newState(action);
|
||||||
left.transition = new ActionTransition(action, right);
|
left.transition = new ActionTransition(action, right);
|
||||||
|
action.nfaState = left;
|
||||||
return new Handle(left, right);
|
return new Handle(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,6 +228,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
Handle h = new Handle(start, end);
|
Handle h = new Handle(start, end);
|
||||||
// FASerializer ser = new FASerializer(g, h.left);
|
// FASerializer ser = new FASerializer(g, h.left);
|
||||||
// System.out.println(blkAST.toStringTree()+":\n"+ser);
|
// System.out.println(blkAST.toStringTree()+":\n"+ser);
|
||||||
|
blkAST.nfaState = start;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,6 +274,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
Handle h = new Handle(start, end);
|
Handle h = new Handle(start, end);
|
||||||
// FASerializer ser = new FASerializer(g, h.left);
|
// FASerializer ser = new FASerializer(g, h.left);
|
||||||
// System.out.println(optAST.toStringTree()+":\n"+ser);
|
// System.out.println(optAST.toStringTree()+":\n"+ser);
|
||||||
|
optAST.nfaState = start;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,6 +297,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
epsilon(blk.right, loop);
|
epsilon(blk.right, loop);
|
||||||
epsilon(loop, end);
|
epsilon(loop, end);
|
||||||
nfa.defineDecisionState(loop);
|
nfa.defineDecisionState(loop);
|
||||||
|
plusAST.nfaState = start;
|
||||||
return new Handle(start, end);
|
return new Handle(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,6 +334,7 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
epsilon(loop, end);
|
epsilon(loop, end);
|
||||||
nfa.defineDecisionState(start);
|
nfa.defineDecisionState(start);
|
||||||
nfa.defineDecisionState(loop);
|
nfa.defineDecisionState(loop);
|
||||||
|
starAST.nfaState = start;
|
||||||
return new Handle(start, end);
|
return new Handle(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,4 +388,29 @@ public class ParserNFAFactory implements NFAFactory {
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NFAState newState(Class nodeType, GrammarAST node) {
|
||||||
|
try {
|
||||||
|
Constructor ctor = nodeType.getConstructor(NFA.class);
|
||||||
|
NFAState s = (NFAState)ctor.newInstance(nfa);
|
||||||
|
s.ast = node;
|
||||||
|
s.rule = currentRule;
|
||||||
|
nfa.addState(s);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
ErrorManager.internalError("can't create NFA node: "+nodeType.getName(), e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicState newState(GrammarAST node) {
|
||||||
|
BasicState n = new BasicState(nfa);
|
||||||
|
n.rule = currentRule;
|
||||||
|
n.ast = node;
|
||||||
|
nfa.addState(n);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicState newState() { return newState(null); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,15 @@ import java.io.Writer;
|
||||||
public abstract class CodeGenerator {
|
public abstract class CodeGenerator {
|
||||||
public static final String TEMPLATE_ROOT = "org/antlr/v4/tool/templates/codegen";
|
public static final String TEMPLATE_ROOT = "org/antlr/v4/tool/templates/codegen";
|
||||||
public static final String VOCAB_FILE_EXTENSION = ".tokens";
|
public static final String VOCAB_FILE_EXTENSION = ".tokens";
|
||||||
protected final static String vocabFilePattern =
|
public final static String vocabFilePattern =
|
||||||
"<tokens:{<attr.name>=<attr.type>\n}>" +
|
"<tokens:{<attr.name>=<attr.type>\n}>" +
|
||||||
"<literals:{<attr.name>=<attr.type>\n}>";
|
"<literals:{<attr.name>=<attr.type>\n}>";
|
||||||
|
|
||||||
Grammar g;
|
public Grammar g;
|
||||||
Target target;
|
public Target target;
|
||||||
STGroup templates;
|
public STGroup templates;
|
||||||
|
|
||||||
int lineWidth = 72;
|
public int lineWidth = 72;
|
||||||
|
|
||||||
public CodeGenerator(Grammar g) {
|
public CodeGenerator(Grammar g) {
|
||||||
this.g = g;
|
this.g = g;
|
||||||
|
|
|
@ -14,6 +14,9 @@ public class ParserGenerator extends CodeGenerator {
|
||||||
put(Parser.class, "parser");
|
put(Parser.class, "parser");
|
||||||
put(RuleFunction.class, "parserFunction");
|
put(RuleFunction.class, "parserFunction");
|
||||||
put(DFADef.class, "DFA");
|
put(DFADef.class, "DFA");
|
||||||
|
put(CodeBlock.class, "codeBlock");
|
||||||
|
put(LL1Choice.class, "switch");
|
||||||
|
put(MatchToken.class, "matchToken");
|
||||||
}};
|
}};
|
||||||
|
|
||||||
public ParserGenerator(Grammar g) {
|
public ParserGenerator(Grammar g) {
|
||||||
|
@ -21,7 +24,7 @@ public class ParserGenerator extends CodeGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputModelObject buildOutputModel() {
|
public OutputModelObject buildOutputModel() {
|
||||||
Parser p = new Parser(g);
|
Parser p = new Parser(this);
|
||||||
return new ParserFile(p, getRecognizerFileName());
|
return new ParserFile(this, p, getRecognizerFileName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,31 +8,43 @@ options {
|
||||||
|
|
||||||
@header {
|
@header {
|
||||||
package org.antlr.v4.codegen;
|
package org.antlr.v4.codegen;
|
||||||
import org.antlr.v4.codegen.nfa.*;
|
import org.antlr.v4.codegen.src.*;
|
||||||
import org.antlr.v4.tool.GrammarAST;
|
import org.antlr.v4.tool.*;
|
||||||
import org.antlr.v4.tool.GrammarASTWithOptions;
|
|
||||||
import org.antlr.v4.tool.LexerGrammar;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
block
|
@members {
|
||||||
|
// TODO: identical grammar to NFABytecodeTriggers; would be nice to combine
|
||||||
|
public CodeGenerator gen;
|
||||||
|
public SourceGenTriggers(TreeNodeStream input, CodeGenerator gen) {
|
||||||
|
this(input);
|
||||||
|
this.gen = gen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
block returns [CodeBlock omo]
|
||||||
: ^( BLOCK (^(OPTIONS .+))?
|
: ^( BLOCK (^(OPTIONS .+))?
|
||||||
( alternative
|
{List<CodeBlock> alts = new ArrayList<CodeBlock>();}
|
||||||
)+
|
( alternative {alts.add($alternative.omo);} )+
|
||||||
)
|
)
|
||||||
|
{
|
||||||
|
Choice c = new LL1Choice(gen, alts); // TODO: assumes LL1
|
||||||
|
$omo = new CodeBlock(gen, c);
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
alternative
|
alternative returns [CodeBlock omo]
|
||||||
|
@init {List<SrcOp> elems = new ArrayList<SrcOp>();}
|
||||||
: ^(ALT_REWRITE a=alternative .)
|
: ^(ALT_REWRITE a=alternative .)
|
||||||
| ^(ALT EPSILON)
|
| ^(ALT EPSILON) {$omo = new CodeBlock(gen);}
|
||||||
| ^(ALT (e=element )+)
|
| ^( ALT ( element {elems.add($element.omo);} )+ ) {$omo = new CodeBlock(gen, elems);}
|
||||||
;
|
;
|
||||||
|
|
||||||
element
|
element returns [SrcOp omo]
|
||||||
: labeledElement
|
: labeledElement
|
||||||
| atom
|
| atom {$omo = $atom.omo;}
|
||||||
| ebnf
|
| ebnf
|
||||||
| ACTION
|
| ACTION
|
||||||
| SEMPRED
|
| SEMPRED
|
||||||
|
@ -40,22 +52,18 @@ element
|
||||||
| treeSpec
|
| treeSpec
|
||||||
;
|
;
|
||||||
|
|
||||||
labeledElement
|
labeledElement returns [SrcOp omo]
|
||||||
: ^(ASSIGN ID atom )
|
: ^(ASSIGN ID atom )
|
||||||
| ^(ASSIGN ID block)
|
| ^(ASSIGN ID block)
|
||||||
| ^(PLUS_ASSIGN ID atom)
|
| ^(PLUS_ASSIGN ID atom)
|
||||||
| ^(PLUS_ASSIGN ID block)
|
| ^(PLUS_ASSIGN ID block)
|
||||||
;
|
;
|
||||||
|
|
||||||
treeSpec
|
treeSpec returns [SrcOp omo]
|
||||||
: ^(TREE_BEGIN (e=element )+)
|
: ^(TREE_BEGIN (e=element )+)
|
||||||
;
|
;
|
||||||
|
|
||||||
ebnf
|
ebnf returns [SrcOp omo]
|
||||||
@init {
|
|
||||||
GrammarASTWithOptions blk = (GrammarASTWithOptions)$start.getChild(0);
|
|
||||||
String greedyOption = blk.getOption("greedy");
|
|
||||||
}
|
|
||||||
: ^(astBlockSuffix block)
|
: ^(astBlockSuffix block)
|
||||||
| ^(OPTIONAL block)
|
| ^(OPTIONAL block)
|
||||||
|
|
||||||
|
@ -72,7 +80,7 @@ astBlockSuffix
|
||||||
| BANG
|
| BANG
|
||||||
;
|
;
|
||||||
|
|
||||||
atom
|
atom returns [SrcOp omo]
|
||||||
: ^(ROOT range)
|
: ^(ROOT range)
|
||||||
| ^(BANG range)
|
| ^(BANG range)
|
||||||
| ^(ROOT notSet)
|
| ^(ROOT notSet)
|
||||||
|
@ -83,32 +91,32 @@ atom
|
||||||
| ^(DOT ID ruleref)
|
| ^(DOT ID ruleref)
|
||||||
| ^(WILDCARD .)
|
| ^(WILDCARD .)
|
||||||
| WILDCARD
|
| WILDCARD
|
||||||
| terminal
|
| terminal {$omo = $terminal.omo;}
|
||||||
| ruleref
|
| ruleref {$omo = $ruleref.omo;}
|
||||||
;
|
;
|
||||||
|
|
||||||
notSet
|
notSet returns [SrcOp omo]
|
||||||
: ^(NOT terminal)
|
: ^(NOT terminal)
|
||||||
| ^(NOT block)
|
| ^(NOT block)
|
||||||
;
|
;
|
||||||
|
|
||||||
ruleref
|
ruleref returns [SrcOp omo]
|
||||||
: ^(ROOT ^(RULE_REF ARG_ACTION?))
|
: ^(ROOT ^(RULE_REF ARG_ACTION?))
|
||||||
| ^(BANG ^(RULE_REF ARG_ACTION?))
|
| ^(BANG ^(RULE_REF ARG_ACTION?))
|
||||||
| ^(RULE_REF ARG_ACTION?)
|
| ^(RULE_REF ARG_ACTION?)
|
||||||
;
|
;
|
||||||
|
|
||||||
range
|
range returns [SrcOp omo]
|
||||||
: ^(RANGE a=STRING_LITERAL b=STRING_LITERAL)
|
: ^(RANGE a=STRING_LITERAL b=STRING_LITERAL)
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
terminal
|
terminal returns [MatchToken omo]
|
||||||
: ^(STRING_LITERAL .)
|
: ^(STRING_LITERAL .)
|
||||||
| STRING_LITERAL
|
| STRING_LITERAL
|
||||||
| ^(TOKEN_REF ARG_ACTION .)
|
| ^(TOKEN_REF ARG_ACTION .)
|
||||||
| ^(TOKEN_REF .)
|
| ^(TOKEN_REF .)
|
||||||
| TOKEN_REF
|
| TOKEN_REF {$omo = new MatchToken(gen, (TerminalAST)$TOKEN_REF);}
|
||||||
| ^(ROOT terminal)
|
| ^(ROOT terminal)
|
||||||
| ^(BANG terminal)
|
| ^(BANG terminal)
|
||||||
;
|
;
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class Choice extends SrcOp {
|
public abstract class Choice extends SrcOp {
|
||||||
public DFADef dfaDef;
|
public DFADef dfaDef;
|
||||||
public List<CodeBlock> alts;
|
public List<CodeBlock> alts;
|
||||||
|
|
||||||
public Choice(Alternative[] alts) {
|
public Choice(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
|
this.gen = gen;
|
||||||
|
this.alts = alts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -7,6 +9,17 @@ import java.util.List;
|
||||||
public class CodeBlock extends SrcOp {
|
public class CodeBlock extends SrcOp {
|
||||||
public List<SrcOp> ops;
|
public List<SrcOp> ops;
|
||||||
|
|
||||||
|
public CodeBlock(CodeGenerator gen) { this.gen = gen; }
|
||||||
|
|
||||||
|
public CodeBlock(CodeGenerator gen, List<SrcOp> ops) {
|
||||||
|
this.gen = gen;
|
||||||
|
this.ops = ops;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CodeBlock(CodeGenerator gen, final SrcOp elem) {
|
||||||
|
this(gen, new ArrayList<SrcOp>() {{ add(elem); }});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getChildren() {
|
public List<String> getChildren() {
|
||||||
return new ArrayList<String>() {{ add("ops"); }};
|
return new ArrayList<String>() {{ add("ops"); }};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
import org.antlr.v4.misc.IntervalSet;
|
import org.antlr.v4.misc.IntervalSet;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,12 +12,14 @@ public class InvokeRule extends SrcOp {
|
||||||
public List<String> args;
|
public List<String> args;
|
||||||
public IntervalSet[] follow;
|
public IntervalSet[] follow;
|
||||||
|
|
||||||
public InvokeRule(String name, String argAction, IntervalSet[] follow) {
|
public InvokeRule(CodeGenerator gen, String name, String argAction, IntervalSet[] follow) {
|
||||||
|
this.gen = gen;
|
||||||
// split and translate argAction
|
// split and translate argAction
|
||||||
// compute follow
|
// compute follow
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeRule(String name, IntervalSet[] follow) {
|
public InvokeRule(CodeGenerator gen, String name, IntervalSet[] follow) {
|
||||||
|
this.gen = gen;
|
||||||
// split and translate argAction
|
// split and translate argAction
|
||||||
// compute follow
|
// compute follow
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public class LL1Choice extends Choice {
|
||||||
|
public LL1Choice(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
|
super(gen, alts);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1OptionalBlock extends OptionalBlock {
|
public class LL1OptionalBlock extends OptionalBlock {
|
||||||
public LL1OptionalBlock(Alternative[] alts) {
|
public LL1OptionalBlock(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
super(alts);
|
super(gen, alts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1OptionalBlockSingleAlt extends OptionalBlock {
|
public class LL1OptionalBlockSingleAlt extends OptionalBlock {
|
||||||
public LL1OptionalBlockSingleAlt(Alternative[] alts) {
|
public LL1OptionalBlockSingleAlt(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
super(alts);
|
super(gen, alts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LLStarOptionalBlock extends OptionalBlock {
|
public class LLStarOptionalBlock extends OptionalBlock {
|
||||||
public LLStarOptionalBlock(Alternative[] alts) {
|
public LLStarOptionalBlock(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
super(alts);
|
super(gen, alts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LLkOptionalBlock extends OptionalBlock {
|
public class LLkOptionalBlock extends OptionalBlock {
|
||||||
public LLkOptionalBlock(Alternative[] alts) {
|
public LLkOptionalBlock(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
super(alts);
|
super(gen, alts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
import org.antlr.v4.misc.IntervalSet;
|
import org.antlr.v4.misc.IntervalSet;
|
||||||
|
import org.antlr.v4.tool.TerminalAST;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class MatchToken extends SrcOp {
|
public class MatchToken extends SrcOp {
|
||||||
public int ttype;
|
public int ttype;
|
||||||
public IntervalSet[] follow;
|
public IntervalSet[] follow;
|
||||||
|
|
||||||
|
public MatchToken(CodeGenerator gen, TerminalAST ast) {
|
||||||
|
this.gen = gen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.tool.Alternative;
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class OptionalBlock extends Choice {
|
public class OptionalBlock extends Choice {
|
||||||
public OptionalBlock(Alternative[] alts) {
|
public OptionalBlock(CodeGenerator gen, List<CodeBlock> alts) {
|
||||||
super(alts);
|
super(gen, alts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public abstract class OutputModelObject {
|
public abstract class OutputModelObject {
|
||||||
//public ST st;
|
public CodeGenerator gen;
|
||||||
|
|
||||||
/** If the output model object encloses some other model objects,
|
/** If the output model object encloses some other model objects,
|
||||||
* we need to be able to walk them. Rather than make each class
|
* we need to be able to walk them. Rather than make each class
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.v4.automata.DFA;
|
import org.antlr.v4.automata.DFA;
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
import org.antlr.v4.misc.IntSet;
|
import org.antlr.v4.misc.IntSet;
|
||||||
import org.antlr.v4.tool.Grammar;
|
|
||||||
import org.antlr.v4.tool.Rule;
|
import org.antlr.v4.tool.Rule;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -15,12 +15,13 @@ public class Parser extends OutputModelObject {
|
||||||
public List<DFADef> dfaDefs = new ArrayList<DFADef>();
|
public List<DFADef> dfaDefs = new ArrayList<DFADef>();
|
||||||
public List<IntSet> bitsetDefs;
|
public List<IntSet> bitsetDefs;
|
||||||
|
|
||||||
public Parser(Grammar g) {
|
public Parser(CodeGenerator gen) {
|
||||||
name = g.getRecognizerName();
|
this.gen = gen;
|
||||||
for (Rule r : g.rules.values()) funcs.add( new RuleFunction(r) );
|
name = gen.g.getRecognizerName();
|
||||||
|
for (Rule r : gen.g.rules.values()) funcs.add( new RuleFunction(gen, r) );
|
||||||
|
|
||||||
// build DFA, bitset defs
|
// build DFA, bitset defs
|
||||||
for (DFA dfa : g.decisionDFAs.values()) {
|
for (DFA dfa : gen.g.decisionDFAs.values()) {
|
||||||
dfaDefs.add( new DFADef("DFA"+dfa.decision, dfa) );
|
dfaDefs.add( new DFADef("DFA"+dfa.decision, dfa) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -7,7 +9,12 @@ import java.util.List;
|
||||||
public class ParserFile extends OutputModelObject {
|
public class ParserFile extends OutputModelObject {
|
||||||
public String fileName;
|
public String fileName;
|
||||||
public Parser parser;
|
public Parser parser;
|
||||||
public ParserFile(Parser p, String fileName) { parser = p; this.fileName = fileName; }
|
|
||||||
|
public ParserFile(CodeGenerator gen, Parser p, String fileName) {
|
||||||
|
this.gen = gen;
|
||||||
|
parser = p;
|
||||||
|
this.fileName = fileName;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getChildren() {
|
public List<String> getChildren() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.antlr.v4.codegen.src;
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||||
|
import org.antlr.v4.codegen.CodeGenerator;
|
||||||
import org.antlr.v4.codegen.SourceGenTriggers;
|
import org.antlr.v4.codegen.SourceGenTriggers;
|
||||||
import org.antlr.v4.misc.Utils;
|
import org.antlr.v4.misc.Utils;
|
||||||
import org.antlr.v4.parse.ANTLRParser;
|
import org.antlr.v4.parse.ANTLRParser;
|
||||||
|
@ -29,7 +30,8 @@ public class RuleFunction extends OutputModelObject {
|
||||||
|
|
||||||
public CodeBlock code;
|
public CodeBlock code;
|
||||||
|
|
||||||
public RuleFunction(Rule r) {
|
public RuleFunction(CodeGenerator gen, Rule r) {
|
||||||
|
this.gen = gen;
|
||||||
this.name = r.name;
|
this.name = r.name;
|
||||||
if ( r.modifiers!=null && r.modifiers.size()>0 ) {
|
if ( r.modifiers!=null && r.modifiers.size()>0 ) {
|
||||||
this.modifiers = new ArrayList<String>();
|
this.modifiers = new ArrayList<String>();
|
||||||
|
@ -48,9 +50,9 @@ public class RuleFunction extends OutputModelObject {
|
||||||
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
|
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
|
||||||
GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
|
GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
|
||||||
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
|
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
|
||||||
SourceGenTriggers gen = new SourceGenTriggers(nodes);
|
SourceGenTriggers genTriggers = new SourceGenTriggers(nodes, gen);
|
||||||
try {
|
try {
|
||||||
gen.block(); // GEN Instr OBJECTS
|
code = genTriggers.block(); // GEN Instr OBJECTS
|
||||||
}
|
}
|
||||||
catch (Exception e){
|
catch (Exception e){
|
||||||
e.printStackTrace(System.err);
|
e.printStackTrace(System.err);
|
||||||
|
|
|
@ -3,6 +3,6 @@ package org.antlr.v4.codegen.src;
|
||||||
import org.antlr.v4.tool.GrammarAST;
|
import org.antlr.v4.tool.GrammarAST;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class SrcOp extends OutputModelObject {
|
public abstract class SrcOp extends OutputModelObject {
|
||||||
public GrammarAST ast;
|
public GrammarAST ast;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.antlr.runtime.BitSet;
|
||||||
import org.antlr.runtime.CommonToken;
|
import org.antlr.runtime.CommonToken;
|
||||||
import org.antlr.runtime.Token;
|
import org.antlr.runtime.Token;
|
||||||
import org.antlr.runtime.tree.Tree;
|
import org.antlr.runtime.tree.Tree;
|
||||||
|
import org.antlr.v4.automata.NFAState;
|
||||||
import org.antlr.v4.parse.ANTLRParser;
|
import org.antlr.v4.parse.ANTLRParser;
|
||||||
import org.antlr.v4.runtime.tree.CommonTree;
|
import org.antlr.v4.runtime.tree.CommonTree;
|
||||||
|
|
||||||
|
@ -12,6 +13,9 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GrammarAST extends CommonTree {
|
public class GrammarAST extends CommonTree {
|
||||||
|
/** If we build an NFA, we make AST node point at left edge of NFA construct */
|
||||||
|
public NFAState nfaState;
|
||||||
|
|
||||||
public GrammarAST() {;}
|
public GrammarAST() {;}
|
||||||
public GrammarAST(Token t) { super(t); }
|
public GrammarAST(Token t) { super(t); }
|
||||||
public GrammarAST(GrammarAST node) { super(node); }
|
public GrammarAST(GrammarAST node) { super(node); }
|
||||||
|
|
Loading…
Reference in New Issue