// Parse a variable declaration statement // - might include several variable definitions separated by commas. // eg: var x=23, y, z=44; ast.Statement ParseVarDeclStatement(ParseContext ctx) { // Is it a variable declaration? if (t.token == Token.kw_var) { var bmk = t.GetBookmark(); t.Next(); // Parse the first varaible declaration var stmt = new ast.StatementVariableDeclaration(bmk); ParseVarDecl(ctx, stmt); // Parse other declarations while (t.SkipOptional(Token.comma)) { ParseVarDecl(ctx, stmt); } // End of statement return(stmt); } else { // Must be just a normal expression statement return(new ast.StatementExpression(t.GetBookmark(), ParseCompositeExpression(ctx))); } }
// Parse a single variable declaration void ParseVarDecl(ParseContext ctx, ast.StatementVariableDeclaration decl) { var bmk = t.GetBookmark(); // Variable name t.Require(Token.identifier); var name = t.identifier; t.Next(); // Optional initial value ast.Expression InitialValue = null; if (t.SkipOptional(Token.assign)) { InitialValue = ParseSingleExpression(ctx); } // Store it decl.AddDeclaration(bmk, name, InitialValue); }
// Parse a variable declaration statement // - might include several variable definitions separated by commas. // eg: var x=23, y, z=44; ast.Statement ParseVarDeclStatement(ParseContext ctx) { // Is it a variable declaration? if (t.token == Token.kw_var) { var bmk = t.GetBookmark(); t.Next(); // Parse the first varaible declaration var stmt = new ast.StatementVariableDeclaration(bmk); ParseVarDecl(ctx, stmt); // Parse other declarations while (t.SkipOptional(Token.comma)) { ParseVarDecl(ctx, stmt); } // End of statement return stmt; } else { // Must be just a normal expression statement return new ast.StatementExpression(t.GetBookmark(), ParseCompositeExpression(ctx)); } }