src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java

Print this page




  60 import java.util.Collections;
  61 import java.util.Deque;
  62 import java.util.HashMap;
  63 import java.util.HashSet;
  64 import java.util.Iterator;
  65 import java.util.List;
  66 import java.util.Map;
  67 import jdk.internal.dynalink.support.NameCodec;
  68 import jdk.nashorn.internal.codegen.CompilerConstants;
  69 import jdk.nashorn.internal.codegen.Namespace;
  70 import jdk.nashorn.internal.ir.AccessNode;
  71 import jdk.nashorn.internal.ir.BaseNode;
  72 import jdk.nashorn.internal.ir.BinaryNode;
  73 import jdk.nashorn.internal.ir.Block;
  74 import jdk.nashorn.internal.ir.BlockStatement;
  75 import jdk.nashorn.internal.ir.BreakNode;
  76 import jdk.nashorn.internal.ir.CallNode;
  77 import jdk.nashorn.internal.ir.CaseNode;
  78 import jdk.nashorn.internal.ir.CatchNode;
  79 import jdk.nashorn.internal.ir.ContinueNode;

  80 import jdk.nashorn.internal.ir.EmptyNode;

  81 import jdk.nashorn.internal.ir.Expression;
  82 import jdk.nashorn.internal.ir.ExpressionStatement;
  83 import jdk.nashorn.internal.ir.ForNode;
  84 import jdk.nashorn.internal.ir.FunctionNode;
  85 import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
  86 import jdk.nashorn.internal.ir.IdentNode;
  87 import jdk.nashorn.internal.ir.IfNode;
  88 import jdk.nashorn.internal.ir.IndexNode;
  89 import jdk.nashorn.internal.ir.JoinPredecessorExpression;
  90 import jdk.nashorn.internal.ir.LabelNode;
  91 import jdk.nashorn.internal.ir.LiteralNode;
  92 import jdk.nashorn.internal.ir.Node;
  93 import jdk.nashorn.internal.ir.ObjectNode;
  94 import jdk.nashorn.internal.ir.PropertyKey;
  95 import jdk.nashorn.internal.ir.PropertyNode;
  96 import jdk.nashorn.internal.ir.ReturnNode;
  97 import jdk.nashorn.internal.ir.RuntimeNode;
  98 import jdk.nashorn.internal.ir.Statement;
  99 import jdk.nashorn.internal.ir.SwitchNode;
 100 import jdk.nashorn.internal.ir.TernaryNode;


 809                                         getValue(statement.getToken());
 810                                     }
 811 
 812                                     // verify that function name as well as parameter names
 813                                     // satisfy strict mode restrictions.
 814                                     verifyStrictIdent(function.getIdent(), "function name");
 815                                     for (final IdentNode param : function.getParameters()) {
 816                                         verifyStrictIdent(param, "function parameter");
 817                                     }
 818                                 }
 819                             } else if (Context.DEBUG) {
 820                                 final int flag = FunctionNode.getDirectiveFlag(directive);
 821                                 if (flag != 0) {
 822                                     final ParserContextFunctionNode function = lc.getCurrentFunction();
 823                                     function.setFlag(flag);
 824                                 }
 825                             }
 826                         }
 827                     }
 828                 } catch (final Exception e) {


 829                     //recover parsing
 830                     recover(e);



 831                 }
 832 
 833                 // No backtracking from here on.
 834                 stream.commit(k);
 835             }
 836         } finally {
 837             isStrictMode = oldStrictMode;
 838         }
 839     }
 840 
 841     /**
 842      * Statement :
 843      *      Block
 844      *      VariableStatement
 845      *      EmptyStatement
 846      *      ExpressionStatement
 847      *      IfStatement
 848      *      IterationStatement
 849      *      ContinueStatement
 850      *      BreakStatement


 967 
 968             expressionStatement();
 969             break;
 970         }
 971     }
 972 
 973     private void addPropertyFunctionStatement(final PropertyFunction propertyFunction) {
 974         final FunctionNode fn = propertyFunction.functionNode;
 975         functionDeclarations.add(new ExpressionStatement(fn.getLineNumber(), fn.getToken(), finish, fn));
 976     }
 977 
 978     /**
 979      * block :
 980      *      { StatementList? }
 981      *
 982      * see 12.1
 983      *
 984      * Parse a statement block.
 985      */
 986     private void block() {
 987         appendStatement(new BlockStatement(line, getBlock(true)));
 988     }
 989 
 990     /**
 991      * StatementList :
 992      *      Statement
 993      *      StatementList Statement
 994      *
 995      * See 12.1
 996      *
 997      * Parse a list of statements.
 998      */
 999     private void statementList() {
1000         // Accumulate statements until end of list. */
1001 loop:
1002         while (type != EOF) {
1003             switch (type) {
1004             case EOF:
1005             case CASE:
1006             case DEFAULT:
1007             case RBRACE:


1322 
1323             // Set the for body.
1324             body = getStatement();
1325         } finally {
1326             lc.pop(forNode);
1327         }
1328 
1329         if (vars != null) {
1330             for (final VarNode var : vars) {
1331                 appendStatement(var);
1332             }
1333         }
1334         if (body != null) {
1335             appendStatement(new ForNode(forLine, forToken, body.getFinish(), body, (forNode.getFlags() | flags), init, test, modify));
1336         }
1337         if (outer != null) {
1338             restoreBlock(outer);
1339             appendStatement(new BlockStatement(startLine, new Block(
1340                     outer.getToken(),
1341                     body.getFinish(),
1342                     outer.getStatements())));
1343         }
1344     }
1345 
1346     /**
1347      * ...IterationStatement :
1348      *           ...
1349      *           while ( Expression ) Statement
1350      *           ...
1351      *
1352      * See 12.6
1353      *
1354      * Parse while statement.
1355      */
1356     private void whileStatement() {
1357         // Capture WHILE token.
1358         final long whileToken = token;
1359         final int whileLine = line;
1360         // WHILE tested in caller.
1361         next();
1362 


1866             Block finallyStatements = null;
1867 
1868             if (type == FINALLY) {
1869                 next();
1870                 finallyStatements = getBlock(true);
1871             }
1872 
1873             // Need at least one catch or a finally.
1874             if (catchBlocks.isEmpty() && finallyStatements == null) {
1875                 throw error(AbstractParser.message("missing.catch.or.finally"), tryToken);
1876             }
1877 
1878             final TryNode tryNode = new TryNode(tryLine, tryToken, finish, tryBody, catchBlocks, finallyStatements);
1879             // Add try.
1880             assert lc.peek() == outer;
1881             appendStatement(tryNode);
1882         } finally {
1883             restoreBlock(outer);
1884         }
1885 
1886         appendStatement(new BlockStatement(startLine, new Block(tryToken, finish, outer.getFlags(), outer.getStatements())));
1887     }
1888 
1889     /**
1890      * DebuggerStatement :
1891      *      debugger ;
1892      *
1893      * See 12.15
1894      *
1895      * Parse debugger statement.
1896      */
1897     private void  debuggerStatement() {
1898         // Capture DEBUGGER token.
1899         final int  debuggerLine  = line;
1900         final long debuggerToken = token;
1901         // DEBUGGER tested in caller.
1902         next();
1903         endOfLine();
1904         appendStatement(new ExpressionStatement(debuggerLine, debuggerToken, finish, new RuntimeNode(debuggerToken, finish, RuntimeNode.Request.DEBUGGER, Collections.<Expression>emptyList())));
1905     }
1906 
1907     /**
1908      * PrimaryExpression :
1909      *      this
1910      *      Identifier
1911      *      Literal
1912      *      ArrayLiteral
1913      *      ObjectLiteral
1914      *      ( Expression )
1915      *
1916      *  See 11.1
1917      *
1918      * Parse primary expression.
1919      * @return Expression node.
1920      */
1921     @SuppressWarnings("fallthrough")
1922     private Expression primaryExpression() {
1923         // Capture first token.
1924         final int  primaryLine  = line;




  60 import java.util.Collections;
  61 import java.util.Deque;
  62 import java.util.HashMap;
  63 import java.util.HashSet;
  64 import java.util.Iterator;
  65 import java.util.List;
  66 import java.util.Map;
  67 import jdk.internal.dynalink.support.NameCodec;
  68 import jdk.nashorn.internal.codegen.CompilerConstants;
  69 import jdk.nashorn.internal.codegen.Namespace;
  70 import jdk.nashorn.internal.ir.AccessNode;
  71 import jdk.nashorn.internal.ir.BaseNode;
  72 import jdk.nashorn.internal.ir.BinaryNode;
  73 import jdk.nashorn.internal.ir.Block;
  74 import jdk.nashorn.internal.ir.BlockStatement;
  75 import jdk.nashorn.internal.ir.BreakNode;
  76 import jdk.nashorn.internal.ir.CallNode;
  77 import jdk.nashorn.internal.ir.CaseNode;
  78 import jdk.nashorn.internal.ir.CatchNode;
  79 import jdk.nashorn.internal.ir.ContinueNode;
  80 import jdk.nashorn.internal.ir.DebuggerNode;
  81 import jdk.nashorn.internal.ir.EmptyNode;
  82 import jdk.nashorn.internal.ir.ErrorNode;
  83 import jdk.nashorn.internal.ir.Expression;
  84 import jdk.nashorn.internal.ir.ExpressionStatement;
  85 import jdk.nashorn.internal.ir.ForNode;
  86 import jdk.nashorn.internal.ir.FunctionNode;
  87 import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
  88 import jdk.nashorn.internal.ir.IdentNode;
  89 import jdk.nashorn.internal.ir.IfNode;
  90 import jdk.nashorn.internal.ir.IndexNode;
  91 import jdk.nashorn.internal.ir.JoinPredecessorExpression;
  92 import jdk.nashorn.internal.ir.LabelNode;
  93 import jdk.nashorn.internal.ir.LiteralNode;
  94 import jdk.nashorn.internal.ir.Node;
  95 import jdk.nashorn.internal.ir.ObjectNode;
  96 import jdk.nashorn.internal.ir.PropertyKey;
  97 import jdk.nashorn.internal.ir.PropertyNode;
  98 import jdk.nashorn.internal.ir.ReturnNode;
  99 import jdk.nashorn.internal.ir.RuntimeNode;
 100 import jdk.nashorn.internal.ir.Statement;
 101 import jdk.nashorn.internal.ir.SwitchNode;
 102 import jdk.nashorn.internal.ir.TernaryNode;


 811                                         getValue(statement.getToken());
 812                                     }
 813 
 814                                     // verify that function name as well as parameter names
 815                                     // satisfy strict mode restrictions.
 816                                     verifyStrictIdent(function.getIdent(), "function name");
 817                                     for (final IdentNode param : function.getParameters()) {
 818                                         verifyStrictIdent(param, "function parameter");
 819                                     }
 820                                 }
 821                             } else if (Context.DEBUG) {
 822                                 final int flag = FunctionNode.getDirectiveFlag(directive);
 823                                 if (flag != 0) {
 824                                     final ParserContextFunctionNode function = lc.getCurrentFunction();
 825                                     function.setFlag(flag);
 826                                 }
 827                             }
 828                         }
 829                     }
 830                 } catch (final Exception e) {
 831                     final int errorLine = line;
 832                     final long errorToken = token;
 833                     //recover parsing
 834                     recover(e);
 835                     final ErrorNode errorExpr = new ErrorNode(errorToken, finish);
 836                     final ExpressionStatement expressionStatement = new ExpressionStatement(errorLine, errorToken, finish, errorExpr);
 837                     appendStatement(expressionStatement);
 838                 }
 839 
 840                 // No backtracking from here on.
 841                 stream.commit(k);
 842             }
 843         } finally {
 844             isStrictMode = oldStrictMode;
 845         }
 846     }
 847 
 848     /**
 849      * Statement :
 850      *      Block
 851      *      VariableStatement
 852      *      EmptyStatement
 853      *      ExpressionStatement
 854      *      IfStatement
 855      *      IterationStatement
 856      *      ContinueStatement
 857      *      BreakStatement


 974 
 975             expressionStatement();
 976             break;
 977         }
 978     }
 979 
 980     private void addPropertyFunctionStatement(final PropertyFunction propertyFunction) {
 981         final FunctionNode fn = propertyFunction.functionNode;
 982         functionDeclarations.add(new ExpressionStatement(fn.getLineNumber(), fn.getToken(), finish, fn));
 983     }
 984 
 985     /**
 986      * block :
 987      *      { StatementList? }
 988      *
 989      * see 12.1
 990      *
 991      * Parse a statement block.
 992      */
 993     private void block() {
 994         appendStatement(new BlockStatement(line, getBlock(true), false));
 995     }
 996 
 997     /**
 998      * StatementList :
 999      *      Statement
1000      *      StatementList Statement
1001      *
1002      * See 12.1
1003      *
1004      * Parse a list of statements.
1005      */
1006     private void statementList() {
1007         // Accumulate statements until end of list. */
1008 loop:
1009         while (type != EOF) {
1010             switch (type) {
1011             case EOF:
1012             case CASE:
1013             case DEFAULT:
1014             case RBRACE:


1329 
1330             // Set the for body.
1331             body = getStatement();
1332         } finally {
1333             lc.pop(forNode);
1334         }
1335 
1336         if (vars != null) {
1337             for (final VarNode var : vars) {
1338                 appendStatement(var);
1339             }
1340         }
1341         if (body != null) {
1342             appendStatement(new ForNode(forLine, forToken, body.getFinish(), body, (forNode.getFlags() | flags), init, test, modify));
1343         }
1344         if (outer != null) {
1345             restoreBlock(outer);
1346             appendStatement(new BlockStatement(startLine, new Block(
1347                     outer.getToken(),
1348                     body.getFinish(),
1349                     outer.getStatements()), true));
1350         }
1351     }
1352 
1353     /**
1354      * ...IterationStatement :
1355      *           ...
1356      *           while ( Expression ) Statement
1357      *           ...
1358      *
1359      * See 12.6
1360      *
1361      * Parse while statement.
1362      */
1363     private void whileStatement() {
1364         // Capture WHILE token.
1365         final long whileToken = token;
1366         final int whileLine = line;
1367         // WHILE tested in caller.
1368         next();
1369 


1873             Block finallyStatements = null;
1874 
1875             if (type == FINALLY) {
1876                 next();
1877                 finallyStatements = getBlock(true);
1878             }
1879 
1880             // Need at least one catch or a finally.
1881             if (catchBlocks.isEmpty() && finallyStatements == null) {
1882                 throw error(AbstractParser.message("missing.catch.or.finally"), tryToken);
1883             }
1884 
1885             final TryNode tryNode = new TryNode(tryLine, tryToken, finish, tryBody, catchBlocks, finallyStatements);
1886             // Add try.
1887             assert lc.peek() == outer;
1888             appendStatement(tryNode);
1889         } finally {
1890             restoreBlock(outer);
1891         }
1892 
1893         appendStatement(new BlockStatement(startLine, new Block(tryToken, finish, outer.getFlags(), outer.getStatements()), true));
1894     }
1895 
1896     /**
1897      * DebuggerStatement :
1898      *      debugger ;
1899      *
1900      * See 12.15
1901      *
1902      * Parse debugger statement.
1903      */
1904     private void  debuggerStatement() {
1905         // Capture DEBUGGER token.
1906         final int  debuggerLine  = line;
1907         final long debuggerToken = token;
1908         // DEBUGGER tested in caller.
1909         next();
1910         endOfLine();
1911         appendStatement(new DebuggerNode(debuggerLine, debuggerToken, finish));
1912     }
1913 
1914     /**
1915      * PrimaryExpression :
1916      *      this
1917      *      Identifier
1918      *      Literal
1919      *      ArrayLiteral
1920      *      ObjectLiteral
1921      *      ( Expression )
1922      *
1923      *  See 11.1
1924      *
1925      * Parse primary expression.
1926      * @return Expression node.
1927      */
1928     @SuppressWarnings("fallthrough")
1929     private Expression primaryExpression() {
1930         // Capture first token.
1931         final int  primaryLine  = line;