parse, output the let statement

This commit is contained in:
Fábio Santos
2015-09-06 21:33:17 +01:00
committed by Richard van Velzen
parent 34685a6f55
commit dde9e293df
4 changed files with 33 additions and 5 deletions

View File

@@ -682,6 +682,10 @@ var AST_Var = DEFNODE("Var", null, {
$documentation: "A `var` statement"
}, AST_Definitions);
var AST_Let = DEFNODE("Let", null, {
$documentation: "A `let` statement"
}, AST_Definitions);
var AST_Const = DEFNODE("Const", null, {
$documentation: "A `const` statement"
}, AST_Definitions);

View File

@@ -994,6 +994,9 @@ function OutputStream(options) {
if (!avoid_semicolon)
output.semicolon();
});
DEFPRINT(AST_Let, function(self, output){
self._do_print(output, "let");
});
DEFPRINT(AST_Var, function(self, output){
self._do_print(output, "var");
});

View File

@@ -44,7 +44,7 @@
"use strict";
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in of instanceof new return switch throw try typeof var void while with';
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in of instanceof new return switch throw try typeof var let void while with';
var KEYWORDS_ATOM = 'false null true';
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield'
+ " " + KEYWORDS_ATOM + " " + KEYWORDS;
@@ -879,6 +879,9 @@ function parse($TEXT, options) {
case "var":
return tmp = var_(), semicolon(), tmp;
case "let":
return tmp = let_(), semicolon(), tmp;
case "const":
return tmp = const_(), semicolon(), tmp;
@@ -949,13 +952,15 @@ function parse($TEXT, options) {
expect("(");
var init = null;
if (!is("punc", ";")) {
init = is("keyword", "var")
? (next(), var_(true))
: expression(true, true);
init =
is("keyword", "var") ? (next(), var_(true)) :
is("keyword", "let") ? (next(), let_(true)) :
expression(true, true);
var is_in = is("operator", "in");
var is_of = is("keyword", "of");
if (is_in || is_of) {
if (init instanceof AST_Var && init.definitions.length > 1)
if ((init instanceof AST_Var || init instanceof AST_Let) &&
init.definitions.length > 1)
croak("Only one variable declaration allowed in for..in loop");
next();
if (is_in) {
@@ -1253,6 +1258,14 @@ function parse($TEXT, options) {
});
};
var let_ = function(no_in) {
return new AST_Let({
start : prev(),
definitions : vardefs(no_in, false),
end : prev()
});
};
var const_ = function() {
return new AST_Const({
start : prev(),

View File

@@ -0,0 +1,8 @@
let_statement: {
input: {
let x = 6;
}
expect_exact: "let x=6;"
}