private void handleParseException(final Exception e) { // Extract message from exception. The message will be in error // message format. String message = e.getMessage(); // If empty message. if (message == null) { message = e.toString(); } // Issue message. if (e instanceof ParserException) { errors.error((ParserException)e); } else { errors.error(message); } if (env._dump_on_error) { e.printStackTrace(env.getErr()); } }
/** * Report an error. * * @param errorType The error type * @param message Error message. * @return ParserException upon failure. Caller should throw and not ignore */ protected final ParserException error(final JSErrorType errorType, final String message) { // TODO - column needs to account for tabs. final int position = Token.descPosition(token); final int column = position - linePosition; final String formatted = ErrorManager.format(message, source, line, column, token); return new ParserException(errorType, formatted, source, line, column, token); }
/** * Check next token, get its value and advance. * * @param expected Expected tokenType. * @return The JavaScript value of the token * @throws ParserException on unexpected token type */ protected final Object expectValue(final TokenType expected) throws ParserException { if (type != expected) { throw error(expectMessage(expected)); } final Object value = getValue(); next(); return value; }
/** * Get the value of a specific token * * @param valueToken the token * * @return JavaScript value of the token */ protected final Object getValue(final long valueToken) { try { return lexer.getValueOf(valueToken, isStrictMode); } catch (final ParserException e) { errors.error(e); } return null; }
/** * Create a LiteralNode from the current token * * @return LiteralNode representing the current token * @throws ParserException if any literals fails to parse */ protected final LiteralNode<?> getLiteral() throws ParserException { // Capture LITERAL token. final long literalToken = token; // Create literal node. final Object value = getValue(); // Advance to have a correct finish next(); LiteralNode<?> node = null; if (value == null) { node = LiteralNode.newInstance(literalToken, finish); } else if (value instanceof Number) { node = LiteralNode.newInstance(literalToken, finish, (Number)value); } else if (value instanceof String) { node = LiteralNode.newInstance(literalToken, finish, (String)value); } else if (value instanceof LexerToken) { if (value instanceof RegexToken) { final RegexToken regex = (RegexToken)value; try { RegExpFactory.validate(regex.getExpression(), regex.getOptions()); } catch (final ParserException e) { throw error(e.getMessage()); } } node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value); } else { assert false : "unknown type for LiteralNode: " + value.getClass(); } return node; }
private static void checkFunctionParameters(final String params) { final Parser parser = getParser(params); try { parser.parseFormalParameterList(); } catch (final ParserException pe) { pe.throwAsEcmaException(); } }
private static void checkFunctionBody(final String funcBody) { final Parser parser = getParser(funcBody); try { parser.parseFunctionBody(); } catch (final ParserException pe) { pe.throwAsEcmaException(); } }
NativeRegExp(final String input, final String flagString, final Global global) { this(global); try { this.regexp = RegExpFactory.create(input, flagString); } catch (final ParserException e) { // translate it as SyntaxError object and throw it e.throwAsEcmaException(); throw new AssertionError(); //guard against null warnings below } this.setLastIndex(0); }
/** * Construct a Regular expression from the given {@code source} and {@code flags} strings. * * @param source RegExp source string * @param flags RegExp flag string * @throws ParserException if flags is invalid or source string has syntax error. */ public JdkRegExp(final String source, final String flags) throws ParserException { super(source, flags); int intFlags = 0; if (isIgnoreCase()) { intFlags |= CASE_INSENSITIVE | UNICODE_CASE; } if (isMultiline()) { intFlags |= MULTILINE; } try { RegExpScanner parsed; try { parsed = RegExpScanner.scan(source); } catch (final PatternSyntaxException e) { // refine the exception with a better syntax error, if this // passes, just rethrow what we have Pattern.compile(source, intFlags); throw e; } if (parsed != null) { this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags); this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead(); } } catch (final PatternSyntaxException e2) { throwParserException("syntax", e2.getMessage()); } }
/** * Construct a Regular expression from the given {@code pattern} and {@code flags} strings. * * @param pattern RegExp pattern string * @param flags RegExp flag string * @throws ParserException if flags is invalid or pattern string has syntax error. */ public JoniRegExp(final String pattern, final String flags) throws ParserException { super(pattern, flags); int option = Option.SINGLELINE; if (this.isIgnoreCase()) { option |= Option.IGNORECASE; } if (this.isMultiline()) { option &= ~Option.SINGLELINE; option |= Option.NEGATE_SINGLELINE; } try { RegExpScanner parsed; try { parsed = RegExpScanner.scan(pattern); } catch (final PatternSyntaxException e) { // refine the exception with a better syntax error, if this // passes, just rethrow what we have Pattern.compile(pattern, 0); throw e; } if (parsed != null) { final char[] javaPattern = parsed.getJavaPattern().toCharArray(); this.regex = new Regex(javaPattern, 0, javaPattern.length, option, Syntax.JAVASCRIPT); this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead(); } } catch (final PatternSyntaxException | JOniException e2) { throwParserException("syntax", e2.getMessage()); } }
/** * Returns AST as JSON compatible string. * * @param context context * @param code code to be parsed * @param name name of the code source (used for location) * @param includeLoc tells whether to include location information for nodes or not * @return JSON string representation of AST of the supplied code */ public static String parse(final Context context, final String code, final String name, final boolean includeLoc) { final Parser parser = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class)); final JSONWriter jsonWriter = new JSONWriter(includeLoc); try { final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default functionNode.accept(jsonWriter); return jsonWriter.getString(); } catch (final ParserException e) { e.throwAsEcmaException(); return null; } }
private void throwParserException(final String message, final Node origin) { if (origin == null) { throw new ParserException(message); } final Source source = compiler.getSource(); final long token = origin.getToken(); final int line = source.getLine(origin.getStart()); final int column = source.getColumn(origin.getStart()); final String formatted = ErrorManager.format(message, source, line, column, token); throw new ParserException(JSErrorType.SYNTAX_ERROR, formatted, source, line, column, token); }
@SuppressWarnings("unchecked") public <T extends CarveNode> T childGet(int index) { if (childExists(index)) { Node node = jjtGetChild(index); try { return (T) node; } catch (ClassCastException e) { throw new ParserException("Cannot cast child #" + index + " to expected type."); } } return null; }
ParserException error(final String message, final int start, final int length) throws ParserException { final long token = Token.toDesc(STRING, start, length); final int pos = Token.descPosition(token); final Source src = Source.sourceFor("<json>", source); final int lineNum = src.getLine(pos); final int columnNum = src.getColumn(pos); final String formatted = ErrorManager.format(message, src, lineNum, columnNum, token); return new ParserException(JSErrorType.SYNTAX_ERROR, formatted, src, lineNum, columnNum, token); }
NativeRegExp(final String input, final String flagString, final Global global, final ScriptObject proto) { super(proto, $nasgenmap$); try { this.regexp = RegExpFactory.create(input, flagString); } catch (final ParserException e) { // translate it as SyntaxError object and throw it e.throwAsEcmaException(); throw new AssertionError(); //guard against null warnings below } this.globalObject = global; this.setLastIndex(0); }