@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() + ">"; }
/** * Method called in AntLR grammars for AntLR lexer to CLScript exceptions conversion * * @param e * @return */ public RuntimeException createException(RecognitionException e) { String message = ""; if (e instanceof NoViableAltException) { message = "Syntax error. "; } else if (e instanceof MissingTokenException) { message = "Missing token "; } else if (e instanceof UnwantedTokenException) { UnwantedTokenException ex = (UnwantedTokenException) e; ex.getUnexpectedToken().getText(); message = "Unkow token '" + ex.getUnexpectedToken().getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine(); } else { message = "Syntax error near "; } return new CLScriptException(message,e); }
/** * Method called in AntLR grammars for AntLR parser to CLScript exceptions conversion * * @param e * @return */ public RuntimeException createException(RecognitionException e) { String message = ""; boolean addTokenAndLine = true; if (e instanceof NoViableAltException) { message = "Syntax error. "; } else if (e instanceof MissingTokenException) { message = "Missing token "; } else if (e instanceof UnwantedTokenException) { UnwantedTokenException ex = (UnwantedTokenException) e; ex.getUnexpectedToken().getText(); message = "Unkow token '" + ex.getUnexpectedToken().getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine(); addTokenAndLine = false; } else if(e instanceof ParserHelperException){ message = e.toString(); } else { message = "Syntax error near "; } if (addTokenAndLine) { message = message + "'" + e.token.getText() + "' at line " + e.token.getLine() + ":" + e.token.getCharPositionInLine(); } return new CLScriptException(message,e); }
@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 testColor() throws RecognitionException { runParser("color [.5, 2.23, .17];"); assertNoError(); assertEquals(TraciParser.BLOCK, parseTree.getType()); assertEquals(1, parseTree.getChildCount()); Tree node = parseTree.getChild(0); assertEquals(TraciParser.COLOR, node.getType()); assertEquals(1, node.getChildCount()); node = node.getChild(0); assertEquals(TraciParser.ARGS, node.getType()); assertEquals(3, node.getChildCount()); runParser("color [.5, 2.23, .17, .5];"); assertNoError(); assertEquals(TraciParser.BLOCK, parseTree.getType()); assertEquals(1, parseTree.getChildCount()); node = parseTree.getChild(0); assertEquals(TraciParser.COLOR, node.getType()); assertEquals(1, node.getChildCount()); node = node.getChild(0); assertEquals(TraciParser.ARGS, node.getType()); assertEquals(4, node.getChildCount()); runParser("color [.5, 2.23, .17;"); assertError(MissingTokenException.class); runParser("color [.5, 2.23 .17];"); assertError(MissingTokenException.class); runParser("color color [.5, 2.23, .17];"); assertError(UnwantedTokenException.class); runParser("color [.5, 2.23];"); assertError(MismatchedTokenException.class); runParser("color [.5, 2.23, .17, 5, 7];"); assertError(MismatchedTokenException.class); assertError(UnwantedTokenException.class); }