Token COMMENT() { match('!'); while ( !(c=='!' && input.LA(2)==delimiterStopChar) ) { if ( c==EOF ) { RecognitionException re = new MismatchedTokenException((int)'!', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "Nonterminated comment starting at "+startLine+":"+startCharPositionInLine+": '!"+delimiterStopChar+"' missing", templateToken, re); break; } consume(); } consume(); consume(); // grab !> return newToken(COMMENT); }
Token COMMENT() { match('!'); while ( !(c=='!' && input.LA(2)==delimiterStopChar) ) { if ( c==EOF ) { RecognitionException re = new MismatchedTokenException((int)'!', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "Nonterminated comment starting at " + startLine +":"+startCharPositionInLine + ": '!" + delimiterStopChar + "' missing", templateToken, re); break; } consume(); } consume(); consume(); // grab !> return newToken(COMMENT); }
Token COMMENT() { match('!'); while ( !(c=='!' && input.LA(2)==delimiterStopChar) ) { if ( c==EOF ) { RecognitionException re = new MismatchedTokenException((int)'!', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "Nonterminated comment starting at " + startLine +":" + startCharPositionInLine + ": '!" + delimiterStopChar + "' missing" , templateToken, re); break; } consume(); } consume(); consume(); // grab !> return newToken(COMMENT); }
Token COMMENT() { match('!'); while ( !(c=='!' && input.LA(2)==delimiterStopChar) ) { if (c==EOF) { RecognitionException re = new MismatchedTokenException((int)'!', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "Nonterminated comment starting at " + startLine+":"+startCharPositionInLine+": '!"+ delimiterStopChar+"' missing", templateToken, re); break; } consume(); } consume(); consume(); // grab !> return newToken(COMMENT); }
@Override public String toString() { if (trappedException instanceof MissingTokenException) { return "<missing type: " + ( (MissingTokenException)trappedException ).getMissingType() + ">"; } else if (trappedException instanceof UnwantedTokenException) { return "<extraneous: " + ( (UnwantedTokenException)trappedException ).getUnexpectedToken() + ", resync=" + getText() + ">"; } else if (trappedException instanceof MismatchedTokenException) { return "<mismatched token: " + trappedException.token + ", resync=" + getText() + ">"; } else if (trappedException instanceof NoViableAltException) { return "<unexpected: " + trappedException.token + ", resync=" + getText() + ">"; } return "<error: " + getText() + ">"; }
Token COMMENT() { match('!'); while ( !(c=='!' && lookingAtDelimiterStop(1)) ) { if (c==EOF) { RecognitionException re = new MismatchedTokenException((int)'!', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "Nonterminated comment starting at " + startLine+":"+startCharPositionInLine+": '!"+ new String(delimiterStopChars) +"' missing", templateToken, re); break; } consume(); } consume(); consumeDelimiterStop(null); return newToken(COMMENT); }
@Test public void testInterior() throws RecognitionException { runParser("interior(1.1);"); assertNoError(); assertEquals(TraciParser.BLOCK, parseTree.getType()); assertEquals(1, parseTree.getChildCount()); Tree node = parseTree.getChild(0); assertEquals(TraciParser.INTERIOR, node.getType()); assertEquals(1, node.getChildCount()); node = node.getChild(0); assertEquals(TraciParser.ARGS, node.getType()); assertEquals(1, node.getChildCount()); node = node.getChild(0); assertEquals(TraciParser.FLOAT, node.getType()); runParser("interior;"); assertError(MismatchedTokenException.class); }
@Test public void testAssert() throws RecognitionException { MismatchedTokenException exception = null; try { runTestFor("/error/mismatchedToken.rfx"); } catch (org.antlr.runtime.MismatchedTokenException e) { exception = e; } assertNotNull(exception); }
/** <pre> * STRING : '"' * ( '\\' '"' * | '\\' ~'"' * | ~('\\'|'"') * )* * '"' * ; * </pre> */ Token mSTRING() { //{setText(getText().substring(1, getText().length()-1));} boolean sawEscape = false; StringBuilder buf = new StringBuilder(); buf.append(c); consume(); while ( c != '"' ) { if ( c=='\\' ) { sawEscape = true; consume(); switch ( c ) { case 'n' : buf.append('\n'); break; case 'r' : buf.append('\r'); break; case 't' : buf.append('\t'); break; default : buf.append(c); break; } consume(); continue; } buf.append(c); consume(); if ( c==EOF ) { RecognitionException re = new MismatchedTokenException((int)'"', input); re.line = input.getLine(); re.charPositionInLine = input.getCharPositionInLine(); errMgr.lexerError(input.getSourceName(), "EOF in string", templateToken, re); break; } } buf.append(c); consume(); if ( sawEscape ) return newToken(STRING, buf.toString()); else return newToken(STRING); }
/** * Simplify error message text for end users. * @param e exception that occurred * @param msg as formatted by ANTLR * @return a more readable error message */ public static String makeUserMsg(final RecognitionException e, final String msg) { if (e instanceof NoViableAltException) { return msg.replace("no viable alternative at", "unrecognized"); } else if (e instanceof UnwantedTokenException) { return msg.replace("extraneous input", "unexpected token"); } else if (e instanceof MismatchedTokenException) { if (msg.contains("mismatched input '<EOF>'")) { return msg.replace("mismatched input '<EOF>' expecting", "reached end of file looking for"); } else { return msg.replace("mismatched input", "unexpected token"); } } else if (e instanceof EarlyExitException) { return msg.replace("required (...)+ loop did not match anything", "required tokens not found"); } else if (e instanceof FailedPredicateException) { if (msg.contains("picture_string failed predicate: {Unbalanced parentheses}")) { return "Unbalanced parentheses in picture string"; } if (msg.contains("PICTURE_PART failed predicate: {Contains invalid picture symbols}")) { return "Picture string contains invalid symbols"; } if (msg.contains("PICTURE_PART failed predicate: {Syntax error in last picture clause}")) { return "Syntax error in last picture clause"; } if (msg.contains("DATA_NAME failed predicate: {Syntax error in last clause}")) { return "Syntax error in last COBOL clause"; } } return msg; }
@Test(expected = MismatchedTokenException.class) public void testNegative2() throws Exception { String query = "ship = load 'x';"; try { ParserTestingUtils.generateLogicalPlan( query ); } catch(Exception ex) { MismatchedTokenException mex = (MismatchedTokenException)ex; assertTrue( mex.token.getText().equals("ship") ); throw ex; } }
@Test(expected = MismatchedTokenException.class) public void testNegative3() throws Exception { String query = "A = load 'y'; all = load 'x';"; try { ParserTestingUtils.generateLogicalPlan( query ); } catch(Exception ex) { MismatchedTokenException mex = (MismatchedTokenException)ex; assertTrue( mex.token.getText().equals("all") ); throw ex; } }
/** * Re-throw the exception but read its data * * @param e * the previous recognition exception (Antlr specific) */ public CtfAntlrException(MismatchedTokenException e) { super(e); this.fErrorLine = e.line; this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata parseMismatchedException(e); }
@Test public void testMismatchedToken() throws RecognitionException { runParser("17+23"); assertError(MismatchedTokenException.class); runParser("17+23)"); assertError(MismatchedTokenException.class); }
@Test public void testVector() throws RecognitionException { runParser("[.5, 2.23, .17];"); assertNoError(); assertEquals(TraciParser.BLOCK, parseTree.getType()); assertEquals(1, parseTree.getChildCount()); Tree node = parseTree.getChild(0); assertEquals(TraciParser.VECTOR, node.getType()); assertEquals(2, node.getChildCount()); assertEquals(TraciParser.LBRACKET, node.getChild(0).getType()); node = node.getChild(1); assertEquals(TraciParser.ARGS, node.getType()); assertEquals(3, node.getChildCount()); runParser("[.5, 2.23, .17;"); assertError(MissingTokenException.class); runParser("[.5, 2.23 .17];"); assertError(MissingTokenException.class); runParser("[.5, 2.23];"); assertError(MismatchedTokenException.class); runParser("[.5, 2.23, .17, 5];"); assertError(MismatchedTokenException.class); assertError(UnwantedTokenException.class); }
@Test public void testString() { runLexer("\"hej\""); assertNoError(); assertToken(0, TraciLexer.QSTRING, "\"hej\"", Token.DEFAULT_CHANNEL, 1, 0); assertToken(1, TraciLexer.EOF, null, Token.DEFAULT_CHANNEL, 1, 5); runLexer("\"hej"); assertError(MismatchedTokenException.class, 1, 4); runLexer("\"foo/bar.hej\""); assertToken(0, TraciLexer.QSTRING, "\"foo/bar.hej\"", Token.DEFAULT_CHANNEL, 1, 0); assertToken(1, TraciLexer.EOF, null, Token.DEFAULT_CHANNEL, 1, 13); }
@Test public void testNegative2() { String query = "ship = load 'x';"; try { ParserTestingUtils.generateLogicalPlan( query ); } catch(Exception ex) { Assert.assertTrue( ex instanceof MismatchedTokenException ); MismatchedTokenException mex = (MismatchedTokenException)ex; Assert.assertTrue( mex.token.getText().equals("ship") ); return; } Assert.fail( "Query is supposed to be failing." ); }
@Test public void testNegative3() { String query = "A = load 'y'; all = load 'x';"; try { ParserTestingUtils.generateLogicalPlan( query ); } catch(Exception ex) { Assert.assertTrue( ex instanceof MismatchedTokenException ); MismatchedTokenException mex = (MismatchedTokenException)ex; Assert.assertTrue( mex.token.getText().equals("all") ); return; } Assert.fail( "Query is supposed to be failing." ); }
@Override protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException { throw new MismatchedTokenException(ttype, input); }