Skip to content

Commit

Permalink
Merge pull request #13 from sharwell/parrt-master
Browse files Browse the repository at this point in the history
Several fixes on the latest updates
  • Loading branch information
parrt committed Feb 15, 2012
2 parents 3d56d40 + 685cf7b commit b44b0a3
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
package org.antlr.v4.runtime;

import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.misc.Utils;

public class LexerNoViableAltException extends RecognitionException {
/** Matching attempted at what input index? */
Expand All @@ -47,7 +48,19 @@ public LexerNoViableAltException(Lexer lexer,
this.deadEndConfigs = deadEndConfigs;
}

@Override
public CharStream getInputStream() {
return (CharStream)super.getInputStream();
}

@Override
public String toString() {
return "NoViableAltException('')";
String symbol = "";
if (startIndex >= 0 && startIndex < input.size()) {
symbol = getInputStream().substring(startIndex, startIndex);
symbol = Utils.escapeWhitespace(symbol, false);
}

return "NoViableAltException('" + symbol + "')";
}
}

This file was deleted.

103 changes: 71 additions & 32 deletions runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* satisfy the superclass interface.
*/
public class ParserRuleContext<Symbol> extends RuleContext {
public static final ParserRuleContext EMPTY = new ParserRuleContext();
public static final ParserRuleContext<?> EMPTY = new ParserRuleContext<Object>();

/** If we are debugging or building a parse tree for a visitor,
* we need to track all of the tokens and rule invocations associated
Expand Down Expand Up @@ -176,61 +176,100 @@ public void addErrorNode(Symbol badToken) {

@Override
public ParseTree getChild(int i) {
return children!=null ? children.get(i) : null;
return children!=null && i>=0 && i<children.size() ? children.get(i) : null;
}

public Object getChild(Class ctxType, int i) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
public <T extends ParseTree> T getChild(Class<? extends T> ctxType, int i) {
if ( children==null || i < 0 || i >= children.size() ) {
return null;
}

int j = -1; // what element have we found with ctxType?
for (Object o : children) {
if ( ctxType.isAssignableFrom(o.getClass()) ) {
j = j+1;
if ( j == i ) return o;
for (ParseTree o : children) {
if ( ctxType.isInstance(o) ) {
j++;
if ( j == i ) {
return ctxType.cast(o);
}
}
}
return null;
}

public Token getToken(int ttype, int i) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
if ( children==null || i < 0 || i >= children.size() ) {
return null;
}

int j = -1; // what token with ttype have we found?
for (Object o : children) {
if ( o instanceof TerminalNode ) {
TerminalNode<Token> tnode = (TerminalNode<Token>)o;
if ( tnode.getSymbol().getType()==ttype ) {
j++;
if ( j == i ) return tnode.getSymbol();
for (ParseTree o : children) {
if ( o instanceof TerminalNode<?> ) {
TerminalNode<?> tnode = (TerminalNode<?>)o;
if ( tnode.getSymbol() instanceof Token ) {
Token symbol = (Token)tnode.getSymbol();
if ( symbol.getType()==ttype ) {
j++;
if ( j == i ) {
return symbol;
}
}
}
}
}

return null;
}

public List<Token> getTokens(int ttype) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
public List<? extends Token> getTokens(int ttype) {
if ( children==null ) {
return Collections.emptyList();
}

List<Token> tokens = null;
for (Object o : children) {
if ( o instanceof Token ) {
if ( tokens==null ) tokens = new ArrayList<Token>();
tokens.add((Token)o);
for (ParseTree o : children) {
if ( o instanceof TerminalNode<?> ) {
TerminalNode<?> tnode = (TerminalNode<?>)o;
if ( tnode.getSymbol() instanceof Token ) {
Token symbol = (Token)tnode.getSymbol();
if ( tokens==null ) {
tokens = new ArrayList<Token>();
}
tokens.add(symbol);
}
}
}

if ( tokens==null ) {
return Collections.emptyList();
}

return tokens;
}

public ParserRuleContext getRuleContext(Class ctxType, int i) {
return (ParserRuleContext)getChild(ctxType, i);
public <T extends ParserRuleContext<?>> T getRuleContext(Class<? extends T> ctxType, int i) {
return getChild(ctxType, i);
}

public List<? extends ParserRuleContext> getRuleContexts(Class ctxType) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
List<ParserRuleContext> contexts = null;
for (Object o : children) {
if ( o.getClass().isInstance(ctxType) ) {
if ( contexts==null ) contexts = new ArrayList<ParserRuleContext>();
contexts.add((ParserRuleContext)o);
public <T extends ParserRuleContext<?>> List<? extends T> getRuleContexts(Class<? extends T> ctxType) {
if ( children==null ) {
return Collections.emptyList();
}

List<T> contexts = null;
for (ParseTree o : children) {
if ( ctxType.isInstance(o) ) {
if ( contexts==null ) {
contexts = new ArrayList<T>();
}

contexts.add(ctxType.cast(o));
}
}

if ( contexts==null ) {
return Collections.emptyList();
}

return contexts;
}

Expand All @@ -247,7 +286,7 @@ public List<? extends ParserRuleContext> getRuleContexts(Class ctxType) {
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();
Expand All @@ -258,7 +297,7 @@ public String toString(@NotNull Recognizer<?,?> recog, RuleContext stop) {
// 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();
Expand Down
5 changes: 5 additions & 0 deletions tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<target>1.6</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new
ContextTokenGetterDecl(t) ::=
"public Token <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
ContextTokenListGetterDecl(t) ::=
"public List\<Token> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
"public List\<? extends Token> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
ContextTokenListIndexedGetterDecl(t) ::= <<
public Token <t.name>(int i) {
return getToken(<parser.name>.<t.name>, i);
Expand All @@ -508,7 +508,7 @@ public <r.ctxName> <r.name>() {
}
>>
ContextRuleListGetterDecl(r) ::= <<
public List\<<r.ctxName>\> <r.name>() {
public List\<? extends <r.ctxName>\> <r.name>() {
return (List\<<r.ctxName>\>)getRuleContexts(<r.ctxName>.class);
}
>>
Expand Down
4 changes: 2 additions & 2 deletions tool/test/org/antlr/v4/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public abstract class BaseTest {
public static final String newline = System.getProperty("line.separator");
public static final String pathSep = System.getProperty("path.separator");

public static final boolean TEST_IN_SAME_PROCESS = false;
public static final boolean TEST_IN_SAME_PROCESS = Boolean.parseBoolean(System.getProperty("antlr.testinprocess"));

/**
* Build up the full classpath we need, including the surefire path (if present)
Expand Down Expand Up @@ -510,7 +510,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName,
}
if ( parserName!=null ) {
files.add(parserName+".java");
files.add("Blank"+grammarFileName.substring(0, grammarFileName.lastIndexOf('.'))+"Listener.java");
files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.'))+"BaseListener.java");
}
ok = compile(files.toArray(new String[files.size()]));
if ( !ok ) { allIsWell = false; }
Expand Down
Loading

0 comments on commit b44b0a3

Please sign in to comment.