Merging dev changes to main

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9853]
This commit is contained in:
sharwell 2012-01-09 10:26:23 -08:00
parent e8ae8ee005
commit 742a08862f
7 changed files with 42 additions and 12 deletions

View File

@ -68,8 +68,7 @@ public class CommonTokenStream extends BufferedTokenStream<CommonToken> {
p++;
sync(p);
CommonToken t = tokens.get(p);
int n = size();
while ( p<n && t.getChannel()!=channel ) {
while ( t.getType()!=Token.EOF && t.getChannel()!=channel ) {
p++;
sync(p);
t = tokens.get(p);
@ -123,9 +122,11 @@ public class CommonTokenStream extends BufferedTokenStream<CommonToken> {
*/
protected int skipOffTokenChannels(int i) {
sync(i);
while ( tokens.get(i).getChannel()!=channel ) { // also stops at EOF (it's onchannel)
Token token = tokens.get(i);
while ( token.getType()!=Token.EOF && token.getChannel()!=channel ) {
i++;
sync(i);
token = tokens.get(i);
}
return i;
}
@ -142,9 +143,11 @@ public class CommonTokenStream extends BufferedTokenStream<CommonToken> {
p = 0;
sync(0);
int i = 0;
while ( tokens.get(i).getChannel()!=channel ) {
Token token = tokens.get(i);
while ( token.getType()!=Token.EOF && token.getChannel()!=channel ) {
i++;
sync(i);
token = tokens.get(i);
}
p = i;
}

View File

@ -253,7 +253,7 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
int n = token.getStopIndex() - token.getStartIndex() + 1;
cpos = token.getCharPositionInLine()+n;
}
Token eof = _factory.create(this, Token.EOF, null, channel, _input.index(), _input.index()-1,
Token eof = _factory.create(this, Token.EOF, null, Token.DEFAULT_CHANNEL, _input.index(), _input.index()-1,
getLine(), cpos);
return eof;
}

View File

@ -177,11 +177,13 @@ public class ParserRuleContext<Symbol> extends RuleContext {
public void addChild(Token matchedToken) {
TerminalNodeImpl<?> t = new TerminalNodeImpl<Token>(matchedToken);
t.parent = this;
addChild(t);
}
public void addErrorNode(Token badToken) {
TerminalNodeImpl<?> t = new ErrorNodeImpl<Token>(badToken);
t.parent = this;
addChild(t);
}
@ -204,7 +206,7 @@ public class ParserRuleContext<Symbol> extends RuleContext {
public String toString(@NotNull Recognizer<?,?> recog, RuleContext stop) {
if ( recog==null ) return super.toString(recog, stop);
StringBuilder buf = new StringBuilder();
ParserRuleContext p = this;
ParserRuleContext<?> p = this;
buf.append("[");
while ( p != null && p != stop ) {
ATN atn = recog.getATN();
@ -215,7 +217,7 @@ public class ParserRuleContext<Symbol> extends RuleContext {
// ATNState invoker = atn.states.get(ctx.invokingState);
// RuleTransition rt = (RuleTransition)invoker.transition(0);
// buf.append(recog.getRuleNames()[rt.target.ruleIndex]);
p = (ParserRuleContext)p.parent;
p = (ParserRuleContext<?>)p.parent;
}
buf.append("]");
return buf.toString();

View File

@ -34,6 +34,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
public class ParseTreeWalker {
public static final ParseTreeWalker DEFAULT = new ParseTreeWalker();
@SuppressWarnings("unchecked")
public <Symbol> void walk(ParseTreeListener<Symbol> listener, ParseTree t) {
if ( t instanceof ParseTree.TerminalNode) {
visitTerminal(listener, (ParseTree.TerminalNode<Symbol>) t);
@ -48,12 +49,13 @@ public class ParseTreeWalker {
exitRule(listener, r);
}
@SuppressWarnings("unchecked")
protected <Symbol> void visitTerminal(ParseTreeListener<Symbol> listener,
ParseTree.TerminalNode<Symbol> t)
{
ParseTree.RuleNode r = (ParseTree.RuleNode)t.getParent();
ParserRuleContext<Symbol> ctx = null;
if ( r.getRuleContext() instanceof ParserRuleContext ) {
if ( r != null && r.getRuleContext() instanceof ParserRuleContext<?> ) {
ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
}
listener.visitTerminal(ctx, t.getSymbol());
@ -65,12 +67,14 @@ public class ParseTreeWalker {
* We to them in reverse order upon finishing the node.
*/
protected <Symbol> void enterRule(ParseTreeListener<Symbol> listener, ParseTree.RuleNode r) {
@SuppressWarnings("unchecked")
ParserRuleContext<Symbol> ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
listener.enterEveryRule((ParserRuleContext<Symbol>) r.getRuleContext());
listener.enterEveryRule(ctx);
ctx.enterRule(listener);
}
protected <Symbol> void exitRule(ParseTreeListener<Symbol> listener, ParseTree.RuleNode r) {
@SuppressWarnings("unchecked")
ParserRuleContext<Symbol> ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
ctx.exitRule(listener);
listener.exitEveryRule(ctx);

View File

@ -69,7 +69,7 @@ public class <parser.name> extends <superclass> {
<endif>
public static final String[] tokenNames = {
"\<INVALID>", "\<INVALID>", "\<INVALID>",
<parser.tokenNames:{k | "<k>"}; separator=", ", wrap, anchor>
<parser.tokenNames:{t | <t>}; separator=", ", wrap, anchor>
};
public static final int
<parser.rules:{r | RULE_<r.name> = <r.index>}; separator=", ", wrap, anchor>;

View File

@ -29,6 +29,7 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.actions.ActionChunk;
import org.antlr.v4.codegen.model.actions.ActionText;
@ -65,6 +66,16 @@ public class Parser extends OutputModelObject {
if ( ttype>0 ) tokens.put(t, ttype);
}
tokenNames = g.getTokenDisplayNames();
for (int i = 0; i < tokenNames.length; i++) {
if ( tokenNames[i]==null ) continue;
CodeGenerator gen = factory.getGenerator();
if ( tokenNames[i].charAt(0)=='\'' ) {
tokenNames[i] = gen.target.getTargetStringLiteralFromANTLRStringLiteral(gen, tokenNames[i]);
}
else {
tokenNames[i] = gen.target.getTargetStringLiteralFromString(tokenNames[i], true);
}
}
ruleNames = g.rules.keySet();
rules = g.rules.values();
atn = new SerializedATN(factory, g.atn);

View File

@ -90,8 +90,18 @@ public class ParserInterpreter {
public int matchATN(@NotNull TokenStream input,
@NotNull ATNState startState)
{
// return new v2ParserATNSimulator<Token>(new DummyParser(g, input), g.atn).matchATN(input, startState);
return 0;
if (startState.getNumberOfTransitions() == 1) {
return 1;
}
else if (startState instanceof DecisionState) {
return atnSimulator.adaptivePredict(input, ((DecisionState)startState).decision, null);
}
else if (startState.getNumberOfTransitions() > 0) {
return 1;
}
else {
return -1;
}
}
public v2ParserATNSimulator<Token> getATNSimulator() {