new nodes, added visitor

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9136]
This commit is contained in:
parrt 2011-10-10 14:01:27 -08:00
parent 9784ce3d22
commit fd9027b4a0
29 changed files with 129 additions and 15 deletions

View File

@ -40,6 +40,7 @@ import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.decl.*; import org.antlr.v4.codegen.model.decl.*;
import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.codegen.model.ast.*;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;

View File

@ -704,9 +704,9 @@ blockSuffix
; ;
ebnfSuffix ebnfSuffix
: QUESTION -> OPTIONAL[$start] : QUESTION -> OPTIONAL<OptionalBlockAST>[$start]
| STAR -> CLOSURE[$start] | STAR -> CLOSURE<StarBlockAST>[$start]
| PLUS -> POSITIVE_CLOSURE[$start] | PLUS -> POSITIVE_CLOSURE<PlusBlockAST>[$start]
; ;
atom atom
@ -722,13 +722,17 @@ atom
// lexically contiguous (no spaces either side of the DOT) // lexically contiguous (no spaces either side of the DOT)
// otherwise it is two references with a wildcard in between // otherwise it is two references with a wildcard in between
// and not a qualified reference. // and not a qualified reference.
/*
{ {
input.LT(1).getCharPositionInLine()+input.LT(1).getText().length()== input.LT(1).getCharPositionInLine()+input.LT(1).getText().length()==
input.LT(2).getCharPositionInLine() && input.LT(2).getCharPositionInLine() &&
input.LT(2).getCharPositionInLine()+1==input.LT(3).getCharPositionInLine() input.LT(2).getCharPositionInLine()+1==input.LT(3).getCharPositionInLine()
}? }?
id DOT ruleref -> ^(DOT id ruleref) id DOT ruleref -> ^(DOT id ruleref)
| range (ROOT^ | BANG^)? // Range x..y - only valid in lexers
|
*/
range (ROOT^ | BANG^)? // Range x..y - only valid in lexers
| terminal (ROOT^ | BANG^)? | terminal (ROOT^ | BANG^)?
| ruleref | ruleref
| notSet (ROOT^|BANG^)? | notSet (ROOT^|BANG^)?
@ -749,8 +753,8 @@ atom
// A set of characters (in a lexer) or terminal tokens, if a parser, // A set of characters (in a lexer) or terminal tokens, if a parser,
// that are then used to create the inverse set of them. // that are then used to create the inverse set of them.
notSet notSet
: NOT setElement -> ^(NOT ^(SET[$setElement.start,"SET"] setElement)) : NOT setElement -> ^(NOT<NotAST>[$NOT] ^(SET<SetAST>[$setElement.start,"SET"] setElement))
| NOT blockSet -> ^(NOT blockSet) | NOT blockSet -> ^(NOT<NotAST>[$NOT] blockSet)
; ;
blockSet blockSet
@ -822,7 +826,7 @@ ruleref
// error about any abuse of the .. operator. // error about any abuse of the .. operator.
// //
range range
: STRING_LITERAL<TerminalAST> RANGE^ STRING_LITERAL<TerminalAST> : STRING_LITERAL<TerminalAST> RANGE<RangeAST>^ STRING_LITERAL<TerminalAST>
; ;
terminal terminal

View File

@ -65,6 +65,7 @@ options {
*/ */
package org.antlr.v4.parse; package org.antlr.v4.parse;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
} }
@members { @members {

View File

@ -61,6 +61,7 @@ options {
*/ */
package org.antlr.v4.parse; package org.antlr.v4.parse;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
import org.antlr.v4.automata.ATNFactory; import org.antlr.v4.automata.ATNFactory;
} }

View File

@ -5,6 +5,7 @@ options { filter=true; }
@header { @header {
package org.antlr.v4.parse; package org.antlr.v4.parse;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
} }
@members { @members {

View File

@ -12,6 +12,7 @@ package org.antlr.v4.parse;
import org.antlr.v4.misc.Utils; import org.antlr.v4.misc.Utils;
import org.antlr.v4.misc.*; import org.antlr.v4.misc.*;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.HashSet; import java.util.HashSet;

View File

@ -34,7 +34,7 @@ import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTreeAdaptor; import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.v4.tool.ast.GrammarAST; import org.antlr.v4.tool.ast.GrammarAST;
import org.antlr.v4.tool.ast.GrammarASTErrorNode; import org.antlr.v4.tool.ast.GrammarASTErrorNode;
import org.antlr.v4.tool.ast.GrammarASTWithOptions; import org.antlr.v4.tool.ast.RuleAST;
import org.antlr.v4.tool.ast.TerminalAST; import org.antlr.v4.tool.ast.TerminalAST;
public class GrammarASTAdaptor extends CommonTreeAdaptor { public class GrammarASTAdaptor extends CommonTreeAdaptor {
@ -52,7 +52,7 @@ public class GrammarASTAdaptor extends CommonTreeAdaptor {
GrammarAST t = null; GrammarAST t = null;
if ( tokenType==ANTLRParser.RULE ) { if ( tokenType==ANTLRParser.RULE ) {
// needed by TreeWizard to make RULE tree // needed by TreeWizard to make RULE tree
t = new GrammarASTWithOptions(new CommonToken(tokenType, text)); t = new RuleAST(new CommonToken(tokenType, text));
} }
else if ( tokenType==ANTLRParser.STRING_LITERAL ) { else if ( tokenType==ANTLRParser.STRING_LITERAL ) {
// implicit lexer construction done with wizard; needs this node type // implicit lexer construction done with wizard; needs this node type

View File

@ -76,6 +76,7 @@ options {
*/ */
package org.antlr.v4.parse; package org.antlr.v4.parse;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
} }

View File

@ -43,6 +43,7 @@ package org.antlr.v4.parse;
import org.antlr.v4.misc.*; import org.antlr.v4.misc.*;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*;
} }
@members { @members {

View File

@ -35,7 +35,7 @@ import org.antlr.v4.tool.AttributeResolver;
import java.util.List; import java.util.List;
public class ActionAST extends GrammarAST { public class ActionAST extends GrammarAST implements RuleElementAST {
// Alt, rule, grammar space // Alt, rule, grammar space
public AttributeResolver resolver; public AttributeResolver resolver;
public List<Token> chunks; // useful for ANTLR IDE developers public List<Token> chunks; // useful for ANTLR IDE developers
@ -53,4 +53,6 @@ public class ActionAST extends GrammarAST {
@Override @Override
public Tree dupNode() { return new ActionAST(this); } public Tree dupNode() { return new ActionAST(this); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -62,4 +62,7 @@ public class AltAST extends GrammarAST {
@Override @Override
public Tree dupNode() { return new AltAST(this); } public Tree dupNode() { return new AltAST(this); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -35,7 +35,7 @@ import org.antlr.runtime.tree.Tree;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BlockAST extends GrammarASTWithOptions { public class BlockAST extends GrammarASTWithOptions implements RuleElementAST {
// TODO: maybe I need a Subrule object like Rule so these options mov to that? // TODO: maybe I need a Subrule object like Rule so these options mov to that?
/** What are the default options for a subrule? */ /** What are the default options for a subrule? */
public static final Map defaultBlockOptions = public static final Map defaultBlockOptions =
@ -55,4 +55,7 @@ public class BlockAST extends GrammarASTWithOptions {
@Override @Override
public Tree dupNode() { return new BlockAST(this); } public Tree dupNode() { return new BlockAST(this); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -46,4 +46,7 @@ public class DownAST extends TerminalAST {
public String toString() { public String toString() {
return getText(); return getText();
} }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -215,4 +215,5 @@ public class GrammarAST extends CommonTree {
return buf.toString(); return buf.toString();
} }
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -8,4 +8,17 @@ t.visit(v);
public interface GrammarASTVisitor { public interface GrammarASTVisitor {
Object visit(RuleAST node); Object visit(RuleAST node);
Object visit(AltAST node); Object visit(AltAST node);
Object visit(DownAST node);
Object visit(GrammarAST node);
Object visit(GrammarRootAST node);
Object visit(NotAST node);
Object visit(OptionalBlockAST node);
Object visit(PlusBlockAST node);
Object visit(PredAST node);
Object visit(RangeAST node);
Object visit(SetAST node);
Object visit(StarBlockAST node);
Object visit(TerminalAST node);
Object visit(TreePatternAST node);
Object visit(UpAST node);
} }

View File

@ -34,7 +34,7 @@ import org.antlr.runtime.Token;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class GrammarASTWithOptions extends GrammarAST { public abstract class GrammarASTWithOptions extends GrammarAST {
protected Map<String, String> options; protected Map<String, String> options;
public GrammarASTWithOptions(GrammarAST node) { public GrammarASTWithOptions(GrammarAST node) {

View File

@ -63,4 +63,7 @@ public class GrammarRootAST extends GrammarASTWithOptions {
public GrammarRootAST(int type, Token t, String text) { public GrammarRootAST(int type, Token t, String text) {
super(type,t,text); super(type,t,text);
} }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -0,0 +1,10 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class NotAST extends GrammarAST implements RuleElementAST {
public NotAST(int type, Token t) { super(type, t); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
}

View File

@ -0,0 +1,7 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class OptionalBlockAST extends GrammarAST implements RuleElementAST {
public OptionalBlockAST(int type, Token t) { super(type, t); }
}

View File

@ -0,0 +1,10 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class PlusBlockAST extends GrammarAST implements RuleElementAST {
public PlusBlockAST(int type, Token t) { super(type, t); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
}

View File

@ -41,4 +41,7 @@ public class PredAST extends ActionAST {
public PredAST(Token t) { super(t); } public PredAST(Token t) { super(t); }
public PredAST(int type) { super(type); } public PredAST(int type) { super(type); }
public PredAST(int type, Token t) { super(type, t); } public PredAST(int type, Token t) { super(type, t); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -0,0 +1,10 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class RangeAST extends GrammarAST implements RuleElementAST {
public RangeAST(Token t) { super(t); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
}

View File

@ -58,5 +58,6 @@ public class RuleAST extends GrammarASTWithOptions {
return null; return null;
} }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); } public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -0,0 +1,5 @@
package org.antlr.v4.tool.ast;
/** Tag indicated AST node is a rule element like token or rule ref. */
public interface RuleElementAST {
}

View File

@ -0,0 +1,10 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class SetAST extends GrammarAST implements RuleElementAST {
public SetAST(int type, Token t, String text) { super(type,t,text); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
}

View File

@ -0,0 +1,10 @@
package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token;
public class StarBlockAST extends GrammarAST implements RuleElementAST {
public StarBlockAST(int type, Token t) { super(type, t); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
}

View File

@ -32,7 +32,7 @@ package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token; import org.antlr.runtime.Token;
import org.antlr.runtime.tree.Tree; import org.antlr.runtime.tree.Tree;
public class TerminalAST extends GrammarASTWithOptions { public class TerminalAST extends GrammarASTWithOptions implements RuleElementAST {
public static final String defaultTokenOption = "node"; public static final String defaultTokenOption = "node";
public TerminalAST(GrammarAST node) { public TerminalAST(GrammarAST node) {
@ -45,4 +45,7 @@ public class TerminalAST extends GrammarASTWithOptions {
@Override @Override
public Tree dupNode() { return new TerminalAST(this); } public Tree dupNode() { return new TerminalAST(this); }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -32,7 +32,7 @@ package org.antlr.v4.tool.ast;
import org.antlr.runtime.Token; import org.antlr.runtime.Token;
import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.ATNState;
public class TreePatternAST extends GrammarAST { public class TreePatternAST extends GrammarAST implements RuleElementAST {
/** Record ATN DN, UP nodes so we can find easily later */ /** Record ATN DN, UP nodes so we can find easily later */
public ATNState downState; public ATNState downState;
public ATNState upState; public ATNState upState;
@ -40,4 +40,7 @@ public class TreePatternAST extends GrammarAST {
public TreePatternAST(Token t) { public TreePatternAST(Token t) {
super(t); super(t);
} }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }

View File

@ -45,4 +45,7 @@ public class UpAST extends TerminalAST {
public String toString() { public String toString() {
return getText(); return getText();
} }
@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }
} }