145 * @return Top level function (script). |
145 * @return Top level function (script). |
146 */ |
146 */ |
147 public FunctionNode parse(final String scriptName) { |
147 public FunctionNode parse(final String scriptName) { |
148 try { |
148 try { |
149 stream = new TokenStream(); |
149 stream = new TokenStream(); |
150 lexer = new Lexer(source, stream, context._scripting); |
150 lexer = new Lexer(source, stream, context._scripting && !context._no_syntax_extensions); |
151 |
151 |
152 // Set up first token (skips opening EOL.) |
152 // Set up first token (skips opening EOL.) |
153 k = -1; |
153 k = -1; |
154 next(); |
154 next(); |
155 |
155 |
1063 // FOR tested in caller. |
1063 // FOR tested in caller. |
1064 next(); |
1064 next(); |
1065 |
1065 |
1066 // Nashorn extension: for each expression. |
1066 // Nashorn extension: for each expression. |
1067 // iterate property values rather than property names. |
1067 // iterate property values rather than property names. |
1068 if (type == IDENT && "each".equals(getValue())) { |
1068 if (!context._no_syntax_extensions && type == IDENT && "each".equals(getValue())) { |
1069 forNode.setIsForEach(); |
1069 forNode.setIsForEach(); |
1070 next(); |
1070 next(); |
1071 } |
1071 } |
1072 |
1072 |
1073 expect(LPAREN); |
1073 expect(LPAREN); |
2310 arguments = argumentList(); |
2310 arguments = argumentList(); |
2311 } else { |
2311 } else { |
2312 arguments = new ArrayList<>(); |
2312 arguments = new ArrayList<>(); |
2313 } |
2313 } |
2314 |
2314 |
2315 // This is to support the following interface impl. syntax: |
2315 // Nashorn extension: This is to support the following interface implementation |
|
2316 // syntax: |
2316 // |
2317 // |
2317 // var r = new java.lang.Runnable() { |
2318 // var r = new java.lang.Runnable() { |
2318 // run: function() { println("run"); } |
2319 // run: function() { println("run"); } |
2319 // }; |
2320 // }; |
2320 // |
2321 // |
2321 // The object literal following the "new Constructor()" expresssion |
2322 // The object literal following the "new Constructor()" expresssion |
2322 // is passed as an additional (last) argument to the constructor. |
2323 // is passed as an additional (last) argument to the constructor. |
2323 |
2324 |
2324 if (type == LBRACE) { |
2325 if (!context._no_syntax_extensions && type == LBRACE) { |
2325 arguments.add(objectLiteral()); |
2326 arguments.add(objectLiteral()); |
2326 } |
2327 } |
2327 |
2328 |
2328 final CallNode callNode = new CallNode(source, constructor.getToken(), finish, constructor, arguments); |
2329 final CallNode callNode = new CallNode(source, constructor.getToken(), finish, constructor, arguments); |
2329 if (isInWithBlock()) { |
2330 if (isInWithBlock()) { |
2473 IdentNode name = null; |
2474 IdentNode name = null; |
2474 |
2475 |
2475 if (type == IDENT || isNonStrictModeIdent()) { |
2476 if (type == IDENT || isNonStrictModeIdent()) { |
2476 name = getIdent(); |
2477 name = getIdent(); |
2477 verifyStrictIdent(name, "function name"); |
2478 verifyStrictIdent(name, "function name"); |
2478 } else if (isStatement && !context._anon_functions) { |
2479 } else if (isStatement) { |
2479 expect(IDENT); |
2480 // Nashorn extension: anonymous function statements |
|
2481 if (context._no_syntax_extensions || !context._anon_functions) { |
|
2482 expect(IDENT); |
|
2483 } |
2480 } |
2484 } |
2481 |
2485 |
2482 // name is null, generate anonymous name |
2486 // name is null, generate anonymous name |
2483 boolean isAnonymous = false; |
2487 boolean isAnonymous = false; |
2484 if (name == null) { |
2488 if (name == null) { |
2611 functionNode.setParameters(parameters); |
2615 functionNode.setParameters(parameters); |
2612 functionNode.setKind(kind); |
2616 functionNode.setKind(kind); |
2613 functionNode.setFirstToken(firstToken); |
2617 functionNode.setFirstToken(firstToken); |
2614 |
2618 |
2615 // Nashorn extension: expression closures |
2619 // Nashorn extension: expression closures |
2616 if (type != LBRACE) { |
2620 if (!context._no_syntax_extensions && type != LBRACE) { |
2617 /* |
2621 /* |
2618 * Example: |
2622 * Example: |
2619 * |
2623 * |
2620 * function square(x) x * x; |
2624 * function square(x) x * x; |
2621 * print(square(3)); |
2625 * print(square(3)); |