got AST construction ops working it seems.

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8797]
This commit is contained in:
parrt 2011-06-29 10:42:23 -08:00
parent 68cef48483
commit 9da8639ba8
8 changed files with 47 additions and 31 deletions

View File

@ -100,13 +100,12 @@ public class CommonToken implements Token, Serializable {
if ( input==null ) {
return null;
}
if ( start<input.size() && stop<input.size() ) {
text = input.substring(start,stop);
}
else {
text = "<EOF>";
}
return text;
if ( start<input.size() && stop<input.size() ) {
return input.substring(start,stop);
}
else {
return "<EOF>";
}
}
/** Override the text for this token. getText() will return this text

View File

@ -28,14 +28,14 @@
package org.antlr.v4.runtime;
import org.antlr.v4.runtime.tree.TreeAdaptor;
import org.antlr.v4.runtime.tree.*;
/** A parser for TokenStreams. "parser grammars" result in a subclass
* of this.
*/
public class Parser extends BaseRecognizer {
public TreeAdaptor _adaptor;
public TreeAdaptor _adaptor = new CommonTreeAdaptor();
public Parser(TokenStream input) {
super(input);

View File

@ -27,8 +27,7 @@
*/
package org.antlr.v4.runtime.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/** A generic tree implementation with no payload. You must subclass to
* actually have any user data. ANTLR v3 uses a list of children approach
@ -130,6 +129,7 @@ public abstract class BaseTree implements Tree {
/** Add all elements of kids list as children of this node */
public void addChildren(List kids) {
if ( kids==null ) return;
for (int i = 0; i < kids.size(); i++) {
Tree t = (Tree) kids.get(i);
addChild(t);

View File

@ -42,27 +42,34 @@ public abstract class BaseTreeAdaptor implements TreeAdaptor {
// BEGIN v4 stuff
public Object becomeRoot(Object newRoot, Object oldRoot, List kids) {
public Object becomeRoot(Object oldRoot, Object newRoot, List kids) {
return null;
}
/** if oldRoot is null then:
* create root for rootToken using kids.
/** if oldRoot is nil then:
* create newRoot for rootToken
* add kids to newRoot
* clear kids
* return as new root
* if oldRoot not null then:
* if oldRoot not nil then:
* add kids to oldRoot
* clear kids
* create rootToken using kids
* create rootToken
* return as new root
*/
public Object becomeRoot(Object newRoot, Token rootToken, List kids) {
if ( newRoot==null ) {
newRoot = create(rootToken);
addChildren(newRoot, kids);
public Object becomeRoot(Object oldRoot, Token rootToken, List kids) {
// first flush all previous children onto old root
if ( ((Tree)oldRoot).isNil() ) {
oldRoot = create(rootToken);
addChildren(oldRoot, kids);
kids.clear();
return newRoot;
return oldRoot;
}
return null;
addChildren(oldRoot, kids);
kids.clear();
Object newRoot = create(rootToken);
addChild(newRoot, oldRoot); // newRoot becomes root of old root
return newRoot;
}
public void addChildren(Object root, List kids) {

View File

@ -45,18 +45,20 @@ public interface TreeAdaptor {
// x^
public Object becomeRoot(Object oldRoot, Object newRoot, List kids);
/** if oldRoot is null then:
* create root for rootToken using kids.
/** if oldRoot is nil then:
* create newRoot for rootToken
* add kids to newRoot
* clear kids
* return as new root
* if oldRoot not null then:
* if oldRoot not nil then:
* add kids to oldRoot
* clear kids
* create rootToken using kids
* create rootToken
* return as new root
*/
public Object becomeRoot(Object oldRoot, Token rootToken, List kids);
// end of outer rule block, set return value
// If not null root, add kids to it
public void addChildren(Object root, List kids);
// END new v4 stuff

View File

@ -1,11 +1,13 @@
grammar T;
options {output=AST;}
a : x+=A^ y+=b B! B b!;
a : A^ C^ D ;
b : B ;
A : 'a';
B : 'b';
C : 'c';
D : 'd';
WS : ' '|'\t'|'\n' {skip();} ;
/*

View File

@ -2,6 +2,7 @@ import org.antlr.v4.Tool;
import org.antlr.v4.automata.ParserATNFactory;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.semantics.SemanticPipeline;
import org.antlr.v4.tool.*;
@ -9,8 +10,11 @@ import java.util.List;
public class TestT {
public static void main(String[] args) throws Exception {
// TLexer t = new TLexer(new ANTLRFileStream(args[0]));
// CommonTokenStream tokens = new CommonTokenStream(t);
TLexer t = new TLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
TParser p = new TParser(tokens);
ParserRuleContext ret = p.a();
System.out.println(((Tree)ret.tree).toStringTree());
}
public static void dump() throws Exception {

View File

@ -365,8 +365,10 @@ KidsListName(level) ::= "_kids<level>"
AddTokenLeaf(a) ::= "_kids0.add(_adaptor.create(<labelref(a.label)>));"
AddRuleLeaf(a) ::= "_kids0.add(<labelref(a.label)>.tree);"
BecomeRoot(r) ::= "_root0 = _root0==null ? <labelref(r.label)> : _adaptor.becomeRoot(_root0, <labelref(r.label)>, _kids0);"
// TODO: need RuleBecomeRoot
BecomeRoot(r) ::= "_root0 = _root0==null ? _adaptor.create(<labelref(r.label)>) : _adaptor.becomeRoot(_root0, <labelref(r.label)>, _kids0);"
AssignTreeResult(a) ::= <<
if ( _root0==null && _kids0.size()>0 ) _root0 = _adaptor.nil();
_adaptor.addChildren(_root0, _kids0);
_localctx.tree = _root0;
>>