Wed, 20 Aug 2014 10:26:01 +0200
8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
8027958: NASHORN TEST: Create tests to test markdown javascript engine work with Nashorn
8028345: Remove nashorn repo "bin" scripts to avoid confusion with JDK bin launcher programs
8029090: Developers should be able to pass nashorn properties and enable/disable JFR from command line
8030169: Need regression test for bug JDK-8010731
8033105: Make sure Nashorn test harness can run zlib benchmark
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
8035820: Optimistic recompilation
8035836: Array performance improvements
8036127: Prototype filter needs to be applied to getter guard as well, not just getter
8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
8037086: Check that deoptimizing recompilations are correct
8037177: -Dnashorn.optimistic should be enabled by default, meaning that it has to be explicitly set to false to run with the jdk 8 style conservative types
8037534: Use scope types to determine optimistic types
8037572: Add more test cases to check static types
8037967: Broke the build, by commiting without saving the last review comment
8038223: Symbol trace debug output takes time
8038396: fix for the compiler expression evaluator to be more inquisitive about types
8038398: OptimisticRecompilationTest fails on staging repo nashorn/jdk9/nashorn due to test framework
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
8038413: NPE in unboxInteger
8038416: Access to undefined scoped variables deoptimized too much
8038426: Move all loggers from process wide scope into Global scope
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
8038945: Simplify strict undefined checks
8039044: Expand undefined intrinsics for all commutative combinators of scrict undefined checks
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
8040024: BranchOptimizer produces bad code for NaN FP comparison
8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
8040093: Make sure that optimistic splitting works in optimistic types
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
8040655: When processing a RewriteException debug object, the return value has already been reset to null. We need to catch this value before that.
8041434: Add synchronization to the common global constants structure
8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
8041905: Fix apply2call bug that prevented avatar.js unit tests from running correctly
8041995: Problems when loading tree expressions with several optimistic program points when optimistically initializing ObjectNodes
8042118: Separate types from symbols
8043002: Improve performance of Nashorn equality operators
8043003: Use strongly referenced generic invokers
8043004: Reduce variability at JavaAdapter call sites
8043132: Nashorn : all tests failed with java.security.AccessControlException
8043133: Fix corner cases of JDK-8041995
8043137: Collapse long sequences of NOP in Nashorn bytecode output
8043232: Index selection of overloaded java new constructors
8043235: Type-based optimizations interfere with continuation methods
8043431: Fix yet another corner case of JDK-8041995
8043504: Octane test harness was missing argument to print_always at one callsite, causing erroneous logging
8043605: Enable history for empty property maps
8043608: Make equality tests inline better
8043611: Move timing dependent benchmark for apply2call specialization to currently_failing. It is dependent that nothing takes machine time when doing the two runs, causing spurious assertions. Suggest running octane.raytrace manually instead to verify that this works, or incorporating it in the nightly test suite
8043632: Parallelize class installation and various script fixes.
8043633: In order to remove global state outside of contexts, make sure Timing class is an instance and not a static global collection of data. Move into Context. Move -Dnashorn.timing to an official logging option.
8043956: Make code caching work with optimistic typing and lazy compilation
8044012: Integrate the latest best known performance flags int ant octane jobs, and make sure that it's easy to compare 'ant octane-nashorn' and 'ant octane-v8' at the push of a button. (or rather; the entry of a command line)
8044102: Ensure bechmark exclude list for Octane benchmarks is in only one place, project.properties, and fix benchmark harness
8044154: Nashorn : all tests failed with java.security.AccessControlException
8044171: Make optimistic exception handlers smaller
8044502: Get rid of global optimistic flag
8044518: Ensure exceptions related to optimistic recompilation are not serializable
8044533: Deoptimizing negation produces wrong result for zero
8044534: Constant folding for unary + should produce int for boolean literals
8044760: Avoid PropertyMap duplicate for global instances
8044786: Some tests fail with non-optimistic compilation
8044803: Unnecessary restOf check
8044816: On-demand compiled top-level program doesn't need :createProgramFunction
8044851: nashorn properties leak memory
8046013: TypeError: Cannot apply "with" to non script object
8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
8046025: AccessorProperty.getGetter is not threadsafe
8046026: CompiledFunction.relinkComposableInvoker assert is being hit
8046201: Avoid repeated flattening of nested ConsStrings
8046215: Running uncompilable scripts throws NullPointerException
8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
8046905: apply on apply is broken
8046921: Deoptimization type information peristence
8047035: (function() "hello")() crashes in Lexer with jdk9
8047057: Add a regression test for the passing test cases from JDK-8042304
8047067: all eval arguments need to be copied in Lower
8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
8047166: 'do with({}) break ; while(0);' crashes in CodeGenerator
8047331: Assertion in CompiledFunction when running earley-boyer after Merge
8047357: More precise synthetic return + unreachable throw
8047359: large string size RangeError should be thrown rather than reporting negative length
8047369: Add regression tests for passing test cases of JDK-8024971
8047371: local variable declaration in TypeEvaluator should use ScriptObject.addOwnProperty instead of .set
8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
8047959: bindings created for declarations in eval code are not mutable
8048009: Type info caching accidentally defeated
8048071: eval within 'with' statement does not use correct scope if with scope expression has a copy of eval
8048079: Persistent code store is broken after optimistic types merge
8048505: ScriptingFunctions.readFully couldn't handle file names represented as ConsStrings
8048586: String concatenation with optimistic types is slow
8048718: JSON.parse('{"0":0, "64":0}') throws ArrayindexOutOfBoundsException
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
8049086: Minor API convenience functions on "Java" object
8049222: JSType class exposes public mutable arrays
8049223: RewriteException class exposes public mutable arrays
8049242: Explicit constructor overload selection should work with StaticClass as well
8049318: Test hideLocationProperties.js fails on Window due to backslash in path
8049524: Global object initialization via javax.script API should be minimal
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
8051019: Separate src and test execution sandbox directories
8051346: Test262 tests for ECMAScript 5 now in branch "es5-tests"
8051439: Wrong type calculated for ADD operator with undefined operand
8051839: GuardedInvocation needs to clone an argument
8053908: jdeps is not PATH on Mac, results in ant clean test failure on Mac
8053910: ScriptObjectMirror causing havoc with Invocation interface
8053913: Auto format caused warning in CompositeTypeBasedGuardingDynamicLinker
8054223: Nashorn: AssertionError when use __DIR__ and ScriptEngine.eval()
8054411: Add nashorn.args.prepend system property
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
8054898: Avoid creation of empty type info files
8054993: type info cache may be disabled for test262 and tests explicitly changing that property should use @fork
8055034: jjs exits interactive mode if exception was thrown when trying to print value of last evaluated expression
8055042: Compile-time expression evaluator was missing variables
8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
8055139: test/script/trusted/JDK-8055107.js fails with access control exception
8055186: Backport Nashorn optimistic typing to 8u repository
8055529: Clean up the bin directory
Reviewed-by: jlaskey, lagergren, sundar
Contributed-by: marcus.largergren@oracle.com, hannes.wallnoefer@oracle.com, sundararajan.athijegannathan@oracle.com
jlaskey@3 | 1 | /* |
jlaskey@7 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
jlaskey@3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jlaskey@3 | 4 | * |
jlaskey@3 | 5 | * This code is free software; you can redistribute it and/or modify it |
jlaskey@3 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jlaskey@3 | 7 | * published by the Free Software Foundation. Oracle designates this |
jlaskey@3 | 8 | * particular file as subject to the "Classpath" exception as provided |
jlaskey@3 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jlaskey@3 | 10 | * |
jlaskey@3 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jlaskey@3 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jlaskey@3 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jlaskey@3 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jlaskey@3 | 15 | * accompanied this code). |
jlaskey@3 | 16 | * |
jlaskey@3 | 17 | * You should have received a copy of the GNU General Public License version |
jlaskey@3 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jlaskey@3 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jlaskey@3 | 20 | * |
jlaskey@3 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jlaskey@3 | 22 | * or visit www.oracle.com if you need additional information or have any |
jlaskey@3 | 23 | * questions. |
jlaskey@3 | 24 | */ |
jlaskey@3 | 25 | |
jlaskey@3 | 26 | package jdk.nashorn.internal.parser; |
jlaskey@3 | 27 | |
jlaskey@460 | 28 | import static jdk.nashorn.internal.parser.TokenType.COMMENT; |
sundar@761 | 29 | import static jdk.nashorn.internal.parser.TokenType.DIRECTIVE_COMMENT; |
jlaskey@3 | 30 | import static jdk.nashorn.internal.parser.TokenType.EOF; |
jlaskey@3 | 31 | import static jdk.nashorn.internal.parser.TokenType.EOL; |
jlaskey@3 | 32 | import static jdk.nashorn.internal.parser.TokenType.IDENT; |
jlaskey@3 | 33 | |
jlaskey@3 | 34 | import jdk.nashorn.internal.ir.IdentNode; |
jlaskey@3 | 35 | import jdk.nashorn.internal.ir.LiteralNode; |
jlaskey@3 | 36 | import jdk.nashorn.internal.parser.Lexer.LexerToken; |
jlaskey@3 | 37 | import jdk.nashorn.internal.parser.Lexer.RegexToken; |
jlaskey@3 | 38 | import jdk.nashorn.internal.runtime.ECMAErrors; |
jlaskey@3 | 39 | import jdk.nashorn.internal.runtime.ErrorManager; |
jlaskey@3 | 40 | import jdk.nashorn.internal.runtime.JSErrorType; |
jlaskey@3 | 41 | import jdk.nashorn.internal.runtime.ParserException; |
attila@235 | 42 | import jdk.nashorn.internal.runtime.Source; |
hannesw@114 | 43 | import jdk.nashorn.internal.runtime.regexp.RegExpFactory; |
jlaskey@3 | 44 | |
jlaskey@3 | 45 | /** |
jlaskey@3 | 46 | * Base class for parsers. |
jlaskey@3 | 47 | */ |
jlaskey@3 | 48 | public abstract class AbstractParser { |
jlaskey@3 | 49 | /** Source to parse. */ |
jlaskey@3 | 50 | protected final Source source; |
jlaskey@3 | 51 | |
jlaskey@3 | 52 | /** Error manager to report errors. */ |
jlaskey@3 | 53 | protected final ErrorManager errors; |
jlaskey@3 | 54 | |
jlaskey@3 | 55 | /** Stream of lex tokens to parse. */ |
jlaskey@3 | 56 | protected TokenStream stream; |
jlaskey@3 | 57 | |
jlaskey@3 | 58 | /** Index of current token. */ |
jlaskey@3 | 59 | protected int k; |
jlaskey@3 | 60 | |
attila@963 | 61 | /** Previous token - accessible to sub classes */ |
attila@963 | 62 | protected long previousToken; |
attila@963 | 63 | |
jlaskey@3 | 64 | /** Descriptor of current token. */ |
jlaskey@3 | 65 | protected long token; |
jlaskey@3 | 66 | |
jlaskey@3 | 67 | /** Type of current token. */ |
jlaskey@3 | 68 | protected TokenType type; |
jlaskey@3 | 69 | |
jlaskey@3 | 70 | /** Type of last token. */ |
jlaskey@3 | 71 | protected TokenType last; |
jlaskey@3 | 72 | |
jlaskey@3 | 73 | /** Start position of current token. */ |
jlaskey@3 | 74 | protected int start; |
jlaskey@3 | 75 | |
jlaskey@3 | 76 | /** Finish position of previous token. */ |
jlaskey@3 | 77 | protected int finish; |
jlaskey@3 | 78 | |
jlaskey@3 | 79 | /** Current line number. */ |
jlaskey@3 | 80 | protected int line; |
jlaskey@3 | 81 | |
jlaskey@3 | 82 | /** Position of last EOL + 1. */ |
jlaskey@3 | 83 | protected int linePosition; |
jlaskey@3 | 84 | |
jlaskey@3 | 85 | /** Lexer used to scan source content. */ |
jlaskey@3 | 86 | protected Lexer lexer; |
jlaskey@3 | 87 | |
jlaskey@3 | 88 | /** Is this parser running under strict mode? */ |
jlaskey@3 | 89 | protected boolean isStrictMode; |
jlaskey@3 | 90 | |
attila@963 | 91 | /** What should line numbers be counted from? */ |
attila@963 | 92 | protected final int lineOffset; |
sundar@761 | 93 | |
jlaskey@3 | 94 | /** |
jlaskey@3 | 95 | * Construct a parser. |
jlaskey@3 | 96 | * |
attila@963 | 97 | * @param source Source to parse. |
attila@963 | 98 | * @param errors Error reporting manager. |
attila@963 | 99 | * @param strict True if we are in strict mode |
attila@963 | 100 | * @param lineOffset Offset from which lines should be counted |
jlaskey@3 | 101 | */ |
attila@963 | 102 | protected AbstractParser(final Source source, final ErrorManager errors, final boolean strict, final int lineOffset) { |
jlaskey@3 | 103 | this.source = source; |
jlaskey@3 | 104 | this.errors = errors; |
jlaskey@3 | 105 | this.k = -1; |
jlaskey@3 | 106 | this.token = Token.toDesc(EOL, 0, 1); |
jlaskey@3 | 107 | this.type = EOL; |
jlaskey@3 | 108 | this.last = EOL; |
jlaskey@3 | 109 | this.isStrictMode = strict; |
attila@963 | 110 | this.lineOffset = lineOffset; |
jlaskey@3 | 111 | } |
jlaskey@3 | 112 | |
jlaskey@3 | 113 | /** |
jlaskey@3 | 114 | * Get the ith token. |
jlaskey@3 | 115 | * |
jlaskey@3 | 116 | * @param i Index of token. |
jlaskey@3 | 117 | * |
jlaskey@3 | 118 | * @return the token |
jlaskey@3 | 119 | */ |
jlaskey@3 | 120 | protected final long getToken(final int i) { |
jlaskey@3 | 121 | // Make sure there are enough tokens available. |
jlaskey@3 | 122 | while (i > stream.last()) { |
jlaskey@3 | 123 | // If we need to buffer more for lookahead. |
jlaskey@3 | 124 | if (stream.isFull()) { |
jlaskey@3 | 125 | stream.grow(); |
jlaskey@3 | 126 | } |
jlaskey@3 | 127 | |
jlaskey@3 | 128 | // Get more tokens. |
jlaskey@3 | 129 | lexer.lexify(); |
jlaskey@3 | 130 | } |
jlaskey@3 | 131 | |
jlaskey@3 | 132 | return stream.get(i); |
jlaskey@3 | 133 | } |
jlaskey@3 | 134 | |
jlaskey@3 | 135 | /** |
jlaskey@3 | 136 | * Return the tokenType of the ith token. |
jlaskey@3 | 137 | * |
jlaskey@3 | 138 | * @param i Index of token |
jlaskey@3 | 139 | * |
jlaskey@3 | 140 | * @return the token type |
jlaskey@3 | 141 | */ |
jlaskey@3 | 142 | protected final TokenType T(final int i) { |
jlaskey@3 | 143 | // Get token descriptor and extract tokenType. |
jlaskey@3 | 144 | return Token.descType(getToken(i)); |
jlaskey@3 | 145 | } |
jlaskey@3 | 146 | |
jlaskey@3 | 147 | /** |
jlaskey@460 | 148 | * Seek next token that is not an EOL or comment. |
jlaskey@3 | 149 | * |
jlaskey@3 | 150 | * @return tokenType of next token. |
jlaskey@3 | 151 | */ |
jlaskey@3 | 152 | protected final TokenType next() { |
jlaskey@3 | 153 | do { |
jlaskey@3 | 154 | nextOrEOL(); |
jlaskey@460 | 155 | } while (type == EOL || type == COMMENT); |
jlaskey@460 | 156 | |
jlaskey@460 | 157 | return type; |
jlaskey@460 | 158 | } |
jlaskey@460 | 159 | |
jlaskey@460 | 160 | /** |
jlaskey@460 | 161 | * Seek next token or EOL (skipping comments.) |
jlaskey@460 | 162 | * |
jlaskey@460 | 163 | * @return tokenType of next token. |
jlaskey@460 | 164 | */ |
jlaskey@460 | 165 | protected final TokenType nextOrEOL() { |
jlaskey@460 | 166 | do { |
jlaskey@460 | 167 | nextToken(); |
sundar@761 | 168 | if (type == DIRECTIVE_COMMENT) { |
sundar@761 | 169 | checkDirectiveComment(); |
sundar@761 | 170 | } |
sundar@761 | 171 | } while (type == COMMENT || type == DIRECTIVE_COMMENT); |
jlaskey@3 | 172 | |
jlaskey@3 | 173 | return type; |
jlaskey@3 | 174 | } |
jlaskey@3 | 175 | |
sundar@761 | 176 | // sourceURL= after directive comment |
sundar@761 | 177 | private static final String SOURCE_URL_PREFIX = "sourceURL="; |
sundar@761 | 178 | |
sundar@761 | 179 | // currently only @sourceURL=foo supported |
sundar@761 | 180 | private void checkDirectiveComment() { |
sundar@761 | 181 | // if already set, ignore this one |
attila@963 | 182 | if (source.getExplicitURL() != null) { |
sundar@761 | 183 | return; |
sundar@761 | 184 | } |
sundar@761 | 185 | |
sundar@761 | 186 | final String comment = (String) lexer.getValueOf(token, isStrictMode); |
sundar@761 | 187 | final int len = comment.length(); |
sundar@761 | 188 | // 4 characters for directive comment marker //@\s or //#\s |
sundar@761 | 189 | if (len > 4 && comment.substring(4).startsWith(SOURCE_URL_PREFIX)) { |
attila@963 | 190 | source.setExplicitURL(comment.substring(4 + SOURCE_URL_PREFIX.length())); |
sundar@761 | 191 | } |
sundar@761 | 192 | } |
sundar@761 | 193 | |
jlaskey@3 | 194 | /** |
jlaskey@3 | 195 | * Seek next token. |
jlaskey@3 | 196 | * |
jlaskey@3 | 197 | * @return tokenType of next token. |
jlaskey@3 | 198 | */ |
sundar@761 | 199 | private TokenType nextToken() { |
jlaskey@3 | 200 | // Capture last token tokenType. |
jlaskey@3 | 201 | last = type; |
jlaskey@3 | 202 | if (type != EOF) { |
jlaskey@3 | 203 | |
jlaskey@3 | 204 | // Set up next token. |
jlaskey@3 | 205 | k++; |
jlaskey@3 | 206 | final long lastToken = token; |
attila@963 | 207 | previousToken = token; |
jlaskey@3 | 208 | token = getToken(k); |
jlaskey@3 | 209 | type = Token.descType(token); |
jlaskey@3 | 210 | |
jlaskey@3 | 211 | // do this before the start is changed below |
jlaskey@3 | 212 | if (last != EOL) { |
jlaskey@3 | 213 | finish = start + Token.descLength(lastToken); |
jlaskey@3 | 214 | } |
jlaskey@3 | 215 | |
jlaskey@3 | 216 | if (type == EOL) { |
attila@963 | 217 | line = Token.descLength(token); |
jlaskey@3 | 218 | linePosition = Token.descPosition(token); |
jlaskey@3 | 219 | } else { |
jlaskey@3 | 220 | start = Token.descPosition(token); |
jlaskey@3 | 221 | } |
jlaskey@3 | 222 | |
jlaskey@3 | 223 | } |
jlaskey@3 | 224 | |
jlaskey@3 | 225 | return type; |
jlaskey@3 | 226 | } |
jlaskey@3 | 227 | |
jlaskey@3 | 228 | /** |
jlaskey@3 | 229 | * Get the message string for a message ID and arguments |
jlaskey@3 | 230 | * |
jlaskey@3 | 231 | * @param msgId The Message ID |
jlaskey@3 | 232 | * @param args The arguments |
jlaskey@3 | 233 | * |
jlaskey@3 | 234 | * @return The message string |
jlaskey@3 | 235 | */ |
jlaskey@3 | 236 | protected static String message(final String msgId, final String... args) { |
jlaskey@3 | 237 | return ECMAErrors.getMessage("parser.error." + msgId, args); |
jlaskey@3 | 238 | } |
jlaskey@3 | 239 | |
jlaskey@3 | 240 | /** |
jlaskey@3 | 241 | * Report an error. |
jlaskey@3 | 242 | * |
jlaskey@3 | 243 | * @param message Error message. |
jlaskey@3 | 244 | * @param errorToken Offending token. |
lagergren@211 | 245 | * @return ParserException upon failure. Caller should throw and not ignore |
jlaskey@3 | 246 | */ |
lagergren@211 | 247 | protected final ParserException error(final String message, final long errorToken) { |
lagergren@211 | 248 | return error(JSErrorType.SYNTAX_ERROR, message, errorToken); |
jlaskey@3 | 249 | } |
jlaskey@3 | 250 | |
jlaskey@3 | 251 | /** |
jlaskey@3 | 252 | * Report an error. |
jlaskey@3 | 253 | * |
jlaskey@3 | 254 | * @param errorType The error type |
jlaskey@3 | 255 | * @param message Error message. |
jlaskey@3 | 256 | * @param errorToken Offending token. |
lagergren@211 | 257 | * @return ParserException upon failure. Caller should throw and not ignore |
jlaskey@3 | 258 | */ |
lagergren@211 | 259 | protected final ParserException error(final JSErrorType errorType, final String message, final long errorToken) { |
jlaskey@3 | 260 | final int position = Token.descPosition(errorToken); |
jlaskey@3 | 261 | final int lineNum = source.getLine(position); |
jlaskey@3 | 262 | final int columnNum = source.getColumn(position); |
jlaskey@3 | 263 | final String formatted = ErrorManager.format(message, source, lineNum, columnNum, errorToken); |
lagergren@211 | 264 | return new ParserException(errorType, formatted, source, lineNum, columnNum, errorToken); |
jlaskey@3 | 265 | } |
jlaskey@3 | 266 | |
jlaskey@3 | 267 | /** |
jlaskey@3 | 268 | * Report an error. |
jlaskey@3 | 269 | * |
jlaskey@3 | 270 | * @param message Error message. |
lagergren@211 | 271 | * @return ParserException upon failure. Caller should throw and not ignore |
jlaskey@3 | 272 | */ |
lagergren@211 | 273 | protected final ParserException error(final String message) { |
lagergren@211 | 274 | return error(JSErrorType.SYNTAX_ERROR, message); |
jlaskey@3 | 275 | } |
jlaskey@3 | 276 | |
jlaskey@3 | 277 | /** |
jlaskey@3 | 278 | * Report an error. |
jlaskey@3 | 279 | * |
jlaskey@3 | 280 | * @param errorType The error type |
jlaskey@3 | 281 | * @param message Error message. |
lagergren@211 | 282 | * @return ParserException upon failure. Caller should throw and not ignore |
jlaskey@3 | 283 | */ |
lagergren@211 | 284 | protected final ParserException error(final JSErrorType errorType, final String message) { |
jlaskey@3 | 285 | // TODO - column needs to account for tabs. |
jlaskey@3 | 286 | final int position = Token.descPosition(token); |
jlaskey@3 | 287 | final int column = position - linePosition; |
jlaskey@3 | 288 | final String formatted = ErrorManager.format(message, source, line, column, token); |
lagergren@211 | 289 | return new ParserException(errorType, formatted, source, line, column, token); |
jlaskey@3 | 290 | } |
jlaskey@3 | 291 | |
jlaskey@3 | 292 | /** |
attila@235 | 293 | * Report a warning to the error manager. |
attila@235 | 294 | * |
attila@235 | 295 | * @param errorType The error type of the warning |
attila@235 | 296 | * @param message Warning message. |
lagergren@247 | 297 | * @param errorToken error token |
attila@235 | 298 | */ |
attila@235 | 299 | protected final void warning(final JSErrorType errorType, final String message, final long errorToken) { |
attila@235 | 300 | errors.warning(error(errorType, message, errorToken)); |
attila@235 | 301 | } |
attila@235 | 302 | |
attila@235 | 303 | /** |
jlaskey@3 | 304 | * Generate 'expected' message. |
jlaskey@3 | 305 | * |
jlaskey@3 | 306 | * @param expected Expected tokenType. |
jlaskey@3 | 307 | * |
jlaskey@3 | 308 | * @return the message string |
jlaskey@3 | 309 | */ |
jlaskey@3 | 310 | protected final String expectMessage(final TokenType expected) { |
sundar@97 | 311 | final String tokenString = Token.toString(source, token); |
jlaskey@3 | 312 | String msg; |
jlaskey@3 | 313 | |
jlaskey@3 | 314 | if (expected == null) { |
jlaskey@3 | 315 | msg = AbstractParser.message("expected.stmt", tokenString); |
jlaskey@3 | 316 | } else { |
jlaskey@3 | 317 | final String expectedName = expected.getNameOrType(); |
jlaskey@3 | 318 | msg = AbstractParser.message("expected", expectedName, tokenString); |
jlaskey@3 | 319 | } |
jlaskey@3 | 320 | |
jlaskey@3 | 321 | return msg; |
jlaskey@3 | 322 | } |
jlaskey@3 | 323 | |
jlaskey@3 | 324 | /** |
jlaskey@3 | 325 | * Check next token and advance. |
jlaskey@3 | 326 | * |
jlaskey@3 | 327 | * @param expected Expected tokenType. |
jlaskey@3 | 328 | * |
jlaskey@3 | 329 | * @throws ParserException on unexpected token type |
jlaskey@3 | 330 | */ |
jlaskey@3 | 331 | protected final void expect(final TokenType expected) throws ParserException { |
jlaskey@3 | 332 | if (type != expected) { |
lagergren@211 | 333 | throw error(expectMessage(expected)); |
jlaskey@3 | 334 | } |
jlaskey@3 | 335 | |
jlaskey@3 | 336 | next(); |
jlaskey@3 | 337 | } |
jlaskey@3 | 338 | |
jlaskey@3 | 339 | /** |
jlaskey@3 | 340 | * Check next token, get its value and advance. |
jlaskey@3 | 341 | * |
jlaskey@3 | 342 | * @param expected Expected tokenType. |
jlaskey@3 | 343 | * @return The JavaScript value of the token |
jlaskey@3 | 344 | * @throws ParserException on unexpected token type |
jlaskey@3 | 345 | */ |
jlaskey@3 | 346 | protected final Object expectValue(final TokenType expected) throws ParserException { |
jlaskey@3 | 347 | if (type != expected) { |
lagergren@211 | 348 | throw error(expectMessage(expected)); |
jlaskey@3 | 349 | } |
jlaskey@3 | 350 | |
jlaskey@3 | 351 | final Object value = getValue(); |
jlaskey@3 | 352 | |
jlaskey@3 | 353 | next(); |
jlaskey@3 | 354 | |
jlaskey@3 | 355 | return value; |
jlaskey@3 | 356 | } |
jlaskey@3 | 357 | |
jlaskey@3 | 358 | /** |
jlaskey@3 | 359 | * Get the value of the current token. |
jlaskey@3 | 360 | * |
jlaskey@3 | 361 | * @return JavaScript value of the token. |
jlaskey@3 | 362 | */ |
jlaskey@3 | 363 | protected final Object getValue() { |
jlaskey@3 | 364 | return getValue(token); |
jlaskey@3 | 365 | } |
jlaskey@3 | 366 | |
jlaskey@3 | 367 | /** |
jlaskey@3 | 368 | * Get the value of a specific token |
jlaskey@3 | 369 | * |
jlaskey@3 | 370 | * @param valueToken the token |
jlaskey@3 | 371 | * |
jlaskey@3 | 372 | * @return JavaScript value of the token |
jlaskey@3 | 373 | */ |
jlaskey@3 | 374 | protected final Object getValue(final long valueToken) { |
jlaskey@3 | 375 | try { |
jlaskey@3 | 376 | return lexer.getValueOf(valueToken, isStrictMode); |
jlaskey@3 | 377 | } catch (final ParserException e) { |
jlaskey@3 | 378 | errors.error(e); |
jlaskey@3 | 379 | } |
jlaskey@3 | 380 | |
jlaskey@3 | 381 | return null; |
jlaskey@3 | 382 | } |
jlaskey@3 | 383 | |
jlaskey@3 | 384 | /** |
jlaskey@3 | 385 | * Certain future reserved words can be used as identifiers in |
jlaskey@3 | 386 | * non-strict mode. Check if the current token is one such. |
jlaskey@3 | 387 | * |
jlaskey@3 | 388 | * @return true if non strict mode identifier |
jlaskey@3 | 389 | */ |
jlaskey@3 | 390 | protected final boolean isNonStrictModeIdent() { |
jlaskey@3 | 391 | return !isStrictMode && type.getKind() == TokenKind.FUTURESTRICT; |
jlaskey@3 | 392 | } |
jlaskey@3 | 393 | |
jlaskey@3 | 394 | /** |
jlaskey@3 | 395 | * Get ident. |
jlaskey@3 | 396 | * |
jlaskey@3 | 397 | * @return Ident node. |
jlaskey@3 | 398 | */ |
jlaskey@3 | 399 | protected final IdentNode getIdent() { |
jlaskey@3 | 400 | // Capture IDENT token. |
jlaskey@3 | 401 | long identToken = token; |
jlaskey@3 | 402 | |
jlaskey@3 | 403 | if (isNonStrictModeIdent()) { |
jlaskey@3 | 404 | // Fake out identifier. |
jlaskey@3 | 405 | identToken = Token.recast(token, IDENT); |
jlaskey@3 | 406 | // Get IDENT. |
jlaskey@3 | 407 | final String ident = (String)getValue(identToken); |
jlaskey@3 | 408 | |
jlaskey@3 | 409 | next(); |
jlaskey@3 | 410 | |
jlaskey@3 | 411 | // Create IDENT node. |
sundar@594 | 412 | return new IdentNode(identToken, finish, ident).setIsFutureStrictName(); |
jlaskey@3 | 413 | } |
jlaskey@3 | 414 | |
jlaskey@3 | 415 | // Get IDENT. |
jlaskey@3 | 416 | final String ident = (String)expectValue(IDENT); |
jlaskey@3 | 417 | if (ident == null) { |
jlaskey@3 | 418 | return null; |
jlaskey@3 | 419 | } |
jlaskey@3 | 420 | // Create IDENT node. |
lagergren@252 | 421 | return new IdentNode(identToken, finish, ident); |
jlaskey@3 | 422 | } |
jlaskey@3 | 423 | |
jlaskey@3 | 424 | /** |
jlaskey@3 | 425 | * Check if current token is in identifier name |
jlaskey@3 | 426 | * |
jlaskey@3 | 427 | * @return true if current token is an identifier name |
jlaskey@3 | 428 | */ |
jlaskey@3 | 429 | protected final boolean isIdentifierName() { |
jlaskey@3 | 430 | final TokenKind kind = type.getKind(); |
jlaskey@3 | 431 | if (kind == TokenKind.KEYWORD || kind == TokenKind.FUTURE || kind == TokenKind.FUTURESTRICT) { |
jlaskey@3 | 432 | return true; |
jlaskey@3 | 433 | } |
jlaskey@3 | 434 | // Fake out identifier. |
jlaskey@3 | 435 | final long identToken = Token.recast(token, IDENT); |
jlaskey@3 | 436 | // Get IDENT. |
jlaskey@3 | 437 | final String ident = (String)getValue(identToken); |
jlaskey@3 | 438 | return !ident.isEmpty() && Character.isJavaIdentifierStart(ident.charAt(0)); |
jlaskey@3 | 439 | } |
jlaskey@3 | 440 | |
jlaskey@3 | 441 | /** |
jlaskey@3 | 442 | * Create an IdentNode from the current token |
jlaskey@3 | 443 | * |
jlaskey@3 | 444 | * @return an IdentNode representing the current token |
jlaskey@3 | 445 | */ |
jlaskey@3 | 446 | protected final IdentNode getIdentifierName() { |
jlaskey@3 | 447 | if (type == IDENT) { |
jlaskey@3 | 448 | return getIdent(); |
jlaskey@3 | 449 | } else if (isIdentifierName()) { |
jlaskey@3 | 450 | // Fake out identifier. |
jlaskey@3 | 451 | final long identToken = Token.recast(token, IDENT); |
jlaskey@3 | 452 | // Get IDENT. |
jlaskey@3 | 453 | final String ident = (String)getValue(identToken); |
jlaskey@3 | 454 | next(); |
jlaskey@3 | 455 | // Create IDENT node. |
lagergren@252 | 456 | return new IdentNode(identToken, finish, ident); |
jlaskey@3 | 457 | } else { |
jlaskey@3 | 458 | expect(IDENT); |
jlaskey@3 | 459 | return null; |
jlaskey@3 | 460 | } |
jlaskey@3 | 461 | } |
jlaskey@3 | 462 | |
jlaskey@3 | 463 | /** |
jlaskey@3 | 464 | * Create a LiteralNode from the current token |
jlaskey@3 | 465 | * |
jlaskey@3 | 466 | * @return LiteralNode representing the current token |
jlaskey@3 | 467 | * @throws ParserException if any literals fails to parse |
jlaskey@3 | 468 | */ |
jlaskey@3 | 469 | protected final LiteralNode<?> getLiteral() throws ParserException { |
jlaskey@3 | 470 | // Capture LITERAL token. |
jlaskey@3 | 471 | final long literalToken = token; |
jlaskey@3 | 472 | |
jlaskey@3 | 473 | // Create literal node. |
jlaskey@3 | 474 | final Object value = getValue(); |
jlaskey@9 | 475 | // Advance to have a correct finish |
jlaskey@9 | 476 | next(); |
jlaskey@3 | 477 | |
jlaskey@3 | 478 | LiteralNode<?> node = null; |
jlaskey@3 | 479 | |
jlaskey@3 | 480 | if (value == null) { |
lagergren@252 | 481 | node = LiteralNode.newInstance(literalToken, finish); |
jlaskey@3 | 482 | } else if (value instanceof Number) { |
lagergren@252 | 483 | node = LiteralNode.newInstance(literalToken, finish, (Number)value); |
jlaskey@3 | 484 | } else if (value instanceof String) { |
lagergren@252 | 485 | node = LiteralNode.newInstance(literalToken, finish, (String)value); |
jlaskey@3 | 486 | } else if (value instanceof LexerToken) { |
jlaskey@3 | 487 | if (value instanceof RegexToken) { |
jlaskey@3 | 488 | final RegexToken regex = (RegexToken)value; |
jlaskey@3 | 489 | try { |
hannesw@114 | 490 | RegExpFactory.validate(regex.getExpression(), regex.getOptions()); |
jlaskey@3 | 491 | } catch (final ParserException e) { |
lagergren@211 | 492 | throw error(e.getMessage()); |
jlaskey@3 | 493 | } |
jlaskey@3 | 494 | } |
lagergren@252 | 495 | node = LiteralNode.newInstance(literalToken, finish, (LexerToken)value); |
jlaskey@3 | 496 | } else { |
jlaskey@3 | 497 | assert false : "unknown type for LiteralNode: " + value.getClass(); |
jlaskey@3 | 498 | } |
jlaskey@3 | 499 | |
jlaskey@3 | 500 | return node; |
jlaskey@3 | 501 | } |
jlaskey@3 | 502 | } |