src/jdk/nashorn/internal/parser/Parser.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.parser;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.EVAL;
  29 import static jdk.nashorn.internal.codegen.CompilerConstants.FUNCTION_PREFIX;
  30 import static jdk.nashorn.internal.codegen.CompilerConstants.RUN_SCRIPT;
  31 import static jdk.nashorn.internal.parser.TokenType.ASSIGN;
  32 import static jdk.nashorn.internal.parser.TokenType.CASE;
  33 import static jdk.nashorn.internal.parser.TokenType.CATCH;
  34 import static jdk.nashorn.internal.parser.TokenType.COLON;
  35 import static jdk.nashorn.internal.parser.TokenType.COMMARIGHT;
  36 import static jdk.nashorn.internal.parser.TokenType.DECPOSTFIX;
  37 import static jdk.nashorn.internal.parser.TokenType.DECPREFIX;
  38 import static jdk.nashorn.internal.parser.TokenType.ELSE;
  39 import static jdk.nashorn.internal.parser.TokenType.EOF;
  40 import static jdk.nashorn.internal.parser.TokenType.EOL;
  41 import static jdk.nashorn.internal.parser.TokenType.FINALLY;
  42 import static jdk.nashorn.internal.parser.TokenType.FUNCTION;
  43 import static jdk.nashorn.internal.parser.TokenType.IDENT;
  44 import static jdk.nashorn.internal.parser.TokenType.IF;
  45 import static jdk.nashorn.internal.parser.TokenType.INCPOSTFIX;
  46 import static jdk.nashorn.internal.parser.TokenType.LBRACE;
  47 import static jdk.nashorn.internal.parser.TokenType.LPAREN;
  48 import static jdk.nashorn.internal.parser.TokenType.RBRACE;
  49 import static jdk.nashorn.internal.parser.TokenType.RBRACKET;


 372      */
 373     private Block newBlock() {
 374         return lc.push(new Block(token, Token.descPosition(token)));
 375     }
 376 
 377     /**
 378      * Set up a new function block.
 379      *
 380      * @param ident Name of function.
 381      * @return New block.
 382      */
 383     private FunctionNode newFunctionNode(final long startToken, final IdentNode ident, final List<IdentNode> parameters, final FunctionNode.Kind kind) {
 384         // Build function name.
 385         final StringBuilder sb = new StringBuilder();
 386 
 387         final FunctionNode parentFunction = lc.getCurrentFunction();
 388         if (parentFunction != null && !parentFunction.isProgram()) {
 389             sb.append(parentFunction.getName()).append('$');
 390         }
 391 
 392         sb.append(ident != null ? ident.getName() : FUNCTION_PREFIX.symbolName());


 393         final String name = namespace.uniqueName(sb.toString());
 394         assert parentFunction != null || name.equals(RUN_SCRIPT.symbolName())  : "name = " + name;// must not rename runScript().
 395 
 396         int flags = 0;
 397         if (parentFunction == null) {
 398             flags |= FunctionNode.IS_PROGRAM;
 399         }
 400         if (isStrictMode) {
 401             flags |= FunctionNode.IS_STRICT;
 402         }
 403         if (env._specialize_calls != null) {
 404             if (env._specialize_calls.contains(name)) {
 405                 flags |= FunctionNode.CAN_SPECIALIZE;
 406             }
 407         }
 408 
 409         // Start new block.
 410         FunctionNode functionNode =
 411             new FunctionNode(
 412                 source,


2431         final long functionToken = token;
2432         final int  functionLine  = line;
2433         // FUNCTION is tested in caller.
2434         next();
2435 
2436         IdentNode name = null;
2437 
2438         if (type == IDENT || isNonStrictModeIdent()) {
2439             name = getIdent();
2440             verifyStrictIdent(name, "function name");
2441         } else if (isStatement) {
2442             // Nashorn extension: anonymous function statements
2443             if (env._no_syntax_extensions) {
2444                 expect(IDENT);
2445             }
2446         }
2447 
2448         // name is null, generate anonymous name
2449         boolean isAnonymous = false;
2450         if (name == null) {
2451             final String tmpName = "_L" + functionLine;
2452             name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
2453             isAnonymous = true;
2454         }
2455 
2456         expect(LPAREN);
2457         final List<IdentNode> parameters = formalParameterList();
2458         expect(RPAREN);
2459 
2460         FunctionNode functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL);
2461 
2462         if (isStatement) {
2463             if (topLevel) {
2464                 functionNode = functionNode.setFlag(lc, FunctionNode.IS_DECLARED);
2465             } else if (isStrictMode) {
2466                 throw error(JSErrorType.SYNTAX_ERROR, AbstractParser.message("strict.no.func.decl.here"), functionToken);
2467             } else if (env._function_statement == ScriptEnvironment.FunctionStatementBehavior.ERROR) {
2468                 throw error(JSErrorType.SYNTAX_ERROR, AbstractParser.message("no.func.decl.here"), functionToken);
2469             } else if (env._function_statement == ScriptEnvironment.FunctionStatementBehavior.WARNING) {
2470                 warning(JSErrorType.SYNTAX_ERROR, AbstractParser.message("no.func.decl.here.warn"), functionToken);
2471             }




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.parser;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.EVAL;
  29 import static jdk.nashorn.internal.codegen.CompilerConstants.ANON_FUNCTION_PREFIX;
  30 import static jdk.nashorn.internal.codegen.CompilerConstants.RUN_SCRIPT;
  31 import static jdk.nashorn.internal.parser.TokenType.ASSIGN;
  32 import static jdk.nashorn.internal.parser.TokenType.CASE;
  33 import static jdk.nashorn.internal.parser.TokenType.CATCH;
  34 import static jdk.nashorn.internal.parser.TokenType.COLON;
  35 import static jdk.nashorn.internal.parser.TokenType.COMMARIGHT;
  36 import static jdk.nashorn.internal.parser.TokenType.DECPOSTFIX;
  37 import static jdk.nashorn.internal.parser.TokenType.DECPREFIX;
  38 import static jdk.nashorn.internal.parser.TokenType.ELSE;
  39 import static jdk.nashorn.internal.parser.TokenType.EOF;
  40 import static jdk.nashorn.internal.parser.TokenType.EOL;
  41 import static jdk.nashorn.internal.parser.TokenType.FINALLY;
  42 import static jdk.nashorn.internal.parser.TokenType.FUNCTION;
  43 import static jdk.nashorn.internal.parser.TokenType.IDENT;
  44 import static jdk.nashorn.internal.parser.TokenType.IF;
  45 import static jdk.nashorn.internal.parser.TokenType.INCPOSTFIX;
  46 import static jdk.nashorn.internal.parser.TokenType.LBRACE;
  47 import static jdk.nashorn.internal.parser.TokenType.LPAREN;
  48 import static jdk.nashorn.internal.parser.TokenType.RBRACE;
  49 import static jdk.nashorn.internal.parser.TokenType.RBRACKET;


 372      */
 373     private Block newBlock() {
 374         return lc.push(new Block(token, Token.descPosition(token)));
 375     }
 376 
 377     /**
 378      * Set up a new function block.
 379      *
 380      * @param ident Name of function.
 381      * @return New block.
 382      */
 383     private FunctionNode newFunctionNode(final long startToken, final IdentNode ident, final List<IdentNode> parameters, final FunctionNode.Kind kind) {
 384         // Build function name.
 385         final StringBuilder sb = new StringBuilder();
 386 
 387         final FunctionNode parentFunction = lc.getCurrentFunction();
 388         if (parentFunction != null && !parentFunction.isProgram()) {
 389             sb.append(parentFunction.getName()).append('$');
 390         }
 391         
 392         assert ident.getName() != null;
 393         sb.append(ident.getName());
 394         
 395         final String name = namespace.uniqueName(sb.toString());
 396         assert parentFunction != null || name.equals(RUN_SCRIPT.symbolName())  : "name = " + name;// must not rename runScript().
 397 
 398         int flags = 0;
 399         if (parentFunction == null) {
 400             flags |= FunctionNode.IS_PROGRAM;
 401         }
 402         if (isStrictMode) {
 403             flags |= FunctionNode.IS_STRICT;
 404         }
 405         if (env._specialize_calls != null) {
 406             if (env._specialize_calls.contains(name)) {
 407                 flags |= FunctionNode.CAN_SPECIALIZE;
 408             }
 409         }
 410 
 411         // Start new block.
 412         FunctionNode functionNode =
 413             new FunctionNode(
 414                 source,


2433         final long functionToken = token;
2434         final int  functionLine  = line;
2435         // FUNCTION is tested in caller.
2436         next();
2437 
2438         IdentNode name = null;
2439 
2440         if (type == IDENT || isNonStrictModeIdent()) {
2441             name = getIdent();
2442             verifyStrictIdent(name, "function name");
2443         } else if (isStatement) {
2444             // Nashorn extension: anonymous function statements
2445             if (env._no_syntax_extensions) {
2446                 expect(IDENT);
2447             }
2448         }
2449 
2450         // name is null, generate anonymous name
2451         boolean isAnonymous = false;
2452         if (name == null) {
2453             final String tmpName = ANON_FUNCTION_PREFIX.symbolName() + functionLine;
2454             name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
2455             isAnonymous = true;
2456         }
2457 
2458         expect(LPAREN);
2459         final List<IdentNode> parameters = formalParameterList();
2460         expect(RPAREN);
2461 
2462         FunctionNode functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL);
2463 
2464         if (isStatement) {
2465             if (topLevel) {
2466                 functionNode = functionNode.setFlag(lc, FunctionNode.IS_DECLARED);
2467             } else if (isStrictMode) {
2468                 throw error(JSErrorType.SYNTAX_ERROR, AbstractParser.message("strict.no.func.decl.here"), functionToken);
2469             } else if (env._function_statement == ScriptEnvironment.FunctionStatementBehavior.ERROR) {
2470                 throw error(JSErrorType.SYNTAX_ERROR, AbstractParser.message("no.func.decl.here"), functionToken);
2471             } else if (env._function_statement == ScriptEnvironment.FunctionStatementBehavior.WARNING) {
2472                 warning(JSErrorType.SYNTAX_ERROR, AbstractParser.message("no.func.decl.here.warn"), functionToken);
2473             }