forked from jasder/antlr
clean up
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9085]
This commit is contained in:
parent
c535d45f66
commit
82360bfaa8
|
@ -29,7 +29,8 @@
|
|||
|
||||
package org.antlr.v4.runtime.tree;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** A generic AST implementation with no payload. You must subclass to
|
||||
* actually have any user data. ANTLR v3 uses a list of children approach
|
||||
|
@ -186,12 +187,13 @@ public abstract class BaseAST implements AST {
|
|||
return false;
|
||||
}
|
||||
|
||||
/** Insert child t at child position i (0..n-1) by shifting children
|
||||
/** Insert child t at child position i (0..n) by shifting children
|
||||
i+1..n-1 to the right one position. Set parent / indexes properly
|
||||
but does NOT collapse nil-rooted t's that come in here like addChild.
|
||||
Set position i==n to add to end.
|
||||
*/
|
||||
public void insertChild(int i, BaseAST t) {
|
||||
if (i < 0 || i >= getChildCount()) {
|
||||
if (i < 0 || i > getChildCount()) {
|
||||
throw new IndexOutOfBoundsException(i+" out or range");
|
||||
}
|
||||
|
||||
|
|
|
@ -29,12 +29,16 @@
|
|||
|
||||
package org.antlr.v4.automata;
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.RuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||
import org.antlr.v4.tool.*;
|
||||
import org.antlr.v4.tool.Grammar;
|
||||
import org.antlr.v4.tool.GrammarAST;
|
||||
import org.antlr.v4.tool.TreePatternAST;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Build ATNs for tree grammars */
|
||||
public class TreeParserATNFactory extends ParserATNFactory {
|
||||
|
@ -60,9 +64,8 @@ public class TreeParserATNFactory extends ParserATNFactory {
|
|||
|
||||
if ( look.member(Token.UP) ) {
|
||||
// nullable child list if we can see the UP as the next token.
|
||||
// convert r DN kids UP to r (DN kids UP)?; leave AST
|
||||
// convert r DN kids UP to r (DN kids UP)?; leave AST alone--
|
||||
// that drives code gen. This just affects analysis
|
||||
root.isNullable = true;
|
||||
epsilon(downStates.get(i), upTargetStates.get(i));
|
||||
}
|
||||
}
|
||||
|
@ -75,15 +78,17 @@ public class TreeParserATNFactory extends ParserATNFactory {
|
|||
* Elems are [root, DOWN_TOKEN, x, y, UP_TOKEN]
|
||||
*/
|
||||
public Handle tree(GrammarAST node, List<Handle> els) {
|
||||
Handle h = elemList(els);
|
||||
TreePatternAST root = (TreePatternAST) node;
|
||||
|
||||
treePatternRootNodes.add((TreePatternAST)node);
|
||||
Handle h = elemList(els);
|
||||
treePatternRootNodes.add(root);
|
||||
// find DOWN node then first child
|
||||
for (Handle elh : els) {
|
||||
Transition trans = elh.left.transition(0);
|
||||
if ( !trans.isEpsilon() && trans.label().member(Token.DOWN) ) {
|
||||
ATNState downState = elh.left;
|
||||
downStates.add(downState);
|
||||
root.downState = downState;
|
||||
firstChildStates.add(downState.transition(0).target);
|
||||
break;
|
||||
}
|
||||
|
@ -93,6 +98,7 @@ public class TreeParserATNFactory extends ParserATNFactory {
|
|||
Transition trans = elh.left.transition(0);
|
||||
if ( trans instanceof AtomTransition && trans.label().member(Token.UP) ) {
|
||||
ATNState upTargetState = elh.right;
|
||||
root.upState = elh.left;
|
||||
upTargetStates.add(upTargetState);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -31,8 +31,13 @@ package org.antlr.v4.codegen.model;
|
|||
|
||||
import org.antlr.v4.codegen.OutputModelFactory;
|
||||
import org.antlr.v4.misc.Utils;
|
||||
import org.antlr.v4.runtime.RuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.tool.*;
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
import org.antlr.v4.runtime.atn.LL1Analyzer;
|
||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||
import org.antlr.v4.tool.GrammarAST;
|
||||
import org.antlr.v4.tool.TreePatternAST;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -49,7 +54,7 @@ public class MatchTree extends RuleElement {
|
|||
public MatchTree(OutputModelFactory factory, GrammarAST ast, List<? extends SrcOp> elems) {
|
||||
super(factory, ast);
|
||||
TreePatternAST rootNode = (TreePatternAST)ast;
|
||||
this.isNullable = rootNode.isNullable;
|
||||
this.isNullable = isNullable(rootNode);
|
||||
List<? extends SrcOp> afterRoot = elems.subList(1, elems.size());
|
||||
int downIndex =
|
||||
Utils.indexOf(afterRoot, new Utils.Filter<SrcOp>() {
|
||||
|
@ -72,4 +77,12 @@ public class MatchTree extends RuleElement {
|
|||
this.kids = elems.subList(downIndex+1, upIndex);
|
||||
}
|
||||
|
||||
boolean isNullable(TreePatternAST rootNode) {
|
||||
ATNState firstChildState = rootNode.downState.transition(0).target;
|
||||
LL1Analyzer analyzer = new LL1Analyzer(firstChildState.atn);
|
||||
IntervalSet look = analyzer.LOOK(firstChildState, RuleContext.EMPTY);
|
||||
System.out.println(rootNode.toStringTree()+"==nullable? "+look.member(Token.UP));
|
||||
return look.member(Token.UP);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -665,7 +665,8 @@ treeSpec
|
|||
i--;
|
||||
p = (GrammarAST)$tree.getChild(i);
|
||||
}
|
||||
$tree.insertChild(i+1, up); // ADD UP
|
||||
if ( i+1 >= $tree.getChildCount() ) $tree.addChild(up);
|
||||
else $tree.insertChild(i+1, up); // ADD UP
|
||||
}
|
||||
: begin=TREE_BEGIN
|
||||
// Only a subset of elements are allowed to be a root node. However
|
||||
|
|
|
@ -30,9 +30,12 @@
|
|||
package org.antlr.v4.tool;
|
||||
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
|
||||
public class TreePatternAST extends GrammarAST {
|
||||
public boolean isNullable;
|
||||
/** Record ATN DN, UP nodes so we can find easily later */
|
||||
public ATNState downState;
|
||||
public ATNState upState;
|
||||
|
||||
public TreePatternAST(Token t) {
|
||||
super(t);
|
||||
|
|
Loading…
Reference in New Issue