Reviewers: Lasse Reichstein,
Description:
Make 'module' a context-sensitive keyword.
Baseline: http://codereview.chromium.org/9401008/
[email protected]
BUG=
TEST=
Please review this at https://chromiumcodereview.appspot.com/9422001/
SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge
Affected files:
M src/heap.h
M src/parser.cc
M src/scanner.cc
M src/token.h
M test/mjsunit/harmony/module-parsing.js
Index: src/heap.h
diff --git a/src/heap.h b/src/heap.h
index
4aba9136e72694905af1d931d042b3522565cf04..eca8dd2ed55bb0092ff2956930e1801a6229fb6f
100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -177,6 +177,7 @@ namespace internal {
V(eval_symbol, "eval") \
V(function_symbol, "function") \
V(length_symbol, "length") \
+ V(module_symbol, "module") \
V(name_symbol, "name") \
V(native_symbol, "native") \
V(null_symbol, "null") \
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index
add250771c7383195e5be5ed43692a52c244ab01..006bf30f442f2dea34154588449ed2752d9d3fd7
100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1188,14 +1188,28 @@ Statement*
Parser::ParseModuleElement(ZoneStringList* labels,
case Token::LET:
case Token::CONST:
return ParseVariableStatement(kModuleElement, ok);
- case Token::MODULE:
- return ParseModuleDeclaration(ok);
case Token::IMPORT:
return ParseImportDeclaration(ok);
case Token::EXPORT:
return ParseExportDeclaration(ok);
- default:
- return ParseStatement(labels, ok);
+ default: {
+ Statement* stmt = ParseStatement(labels, ok);
+ // Handle 'module' as a context-sensitive keyword.
+ if (FLAG_harmony_modules &&
+ peek() == Token::IDENTIFIER &&
+ !scanner().HasAnyLineTerminatorBeforeNext() &&
+ stmt != NULL) {
+ ExpressionStatement* estmt = stmt->AsExpressionStatement();
+ if (estmt != NULL &&
+ estmt->expression()->AsVariableProxy() != NULL &&
+ estmt->expression()->AsVariableProxy()->name()->Equals(
+ isolate()->heap()->module_symbol()) &&
+ !scanner().literal_contains_escapes()) {
+ return ParseModuleDeclaration(ok);
+ }
+ }
+ return stmt;
+ }
}
}
@@ -1206,7 +1220,6 @@ Block* Parser::ParseModuleDeclaration(bool* ok) {
// Create new block with one expected declaration.
Block* block = factory()->NewBlock(NULL, 1, true);
- Expect(Token::MODULE, CHECK_OK);
Handle<String> name = ParseIdentifier(CHECK_OK);
// top_scope_->AddDeclaration(
// factory()->NewModuleDeclaration(proxy, module, top_scope_));
@@ -2180,8 +2193,17 @@ Statement*
Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
return ParseNativeDeclaration(ok);
}
- // Parsed expression statement.
- ExpectSemicolon(CHECK_OK);
+ // Parsed expression statement, or the context-sensitive 'module'
keyword.
+ // Only expect semicolon in the former case.
+ if (!FLAG_harmony_modules ||
+ peek() != Token::IDENTIFIER ||
+ scanner().HasAnyLineTerminatorBeforeNext() ||
+ expr->AsVariableProxy() == NULL ||
+ !expr->AsVariableProxy()->name()->Equals(
+ isolate()->heap()->module_symbol()) ||
+ scanner().literal_contains_escapes()) {
+ ExpectSemicolon(CHECK_OK);
+ }
return factory()->NewExpressionStatement(expr);
}
Index: src/scanner.cc
diff --git a/src/scanner.cc b/src/scanner.cc
index
42a1c2bc31e93cd0cd859d1fc461bb03b9732585..72768b381b4b085e5a0281093b44b76b92a38b31
100755
--- a/src/scanner.cc
+++ b/src/scanner.cc
@@ -850,9 +850,6 @@ uc32 Scanner::ScanIdentifierUnicodeEscape() {
KEYWORD_GROUP('l') \
KEYWORD("let", harmony_scoping \
? Token::LET : Token::FUTURE_STRICT_RESERVED_WORD) \
- KEYWORD_GROUP('m') \
- KEYWORD("module", harmony_modules \
- ? Token::MODULE : Token::IDENTIFIER) \
KEYWORD_GROUP('n') \
KEYWORD("new", Token::NEW) \
KEYWORD("null", Token::NULL_LITERAL) \
Index: src/token.h
diff --git a/src/token.h b/src/token.h
index
b305c88a30fd3a852d8452468910b4495249dc47..1eeb60d876607173885bed61a64e279fd08a6a93
100644
--- a/src/token.h
+++ b/src/token.h
@@ -173,7 +173,6 @@ namespace internal {
K(EXPORT, "export", 0) \
K(IMPORT, "import", 0) \
K(LET, "let", 0) \
- K(MODULE, "module", 0) \
\
/* Illegal token - not able to scan. */ \
T(ILLEGAL, "ILLEGAL", 0) \
Index: test/mjsunit/harmony/module-parsing.js
diff --git a/test/mjsunit/harmony/module-parsing.js
b/test/mjsunit/harmony/module-parsing.js
index
ac398636da852a2f1daaedf7187c892ee5eac38a..5a5e82fdb17f34d4a0d18882a4002b58c1b0a862
100644
--- a/test/mjsunit/harmony/module-parsing.js
+++ b/test/mjsunit/harmony/module-parsing.js
@@ -63,18 +63,28 @@ module E3 = E1.F
// Check that ASI does not interfere.
-module
-X
+module X
{
let x
}
-module
-Y
+module Y
=
X
-module
-Z
+module Z
at
"file://local"
+
+
+// Check that 'module' still works as an identifier.
+
+var module
+module = {}
+module["a"] = 6
+function module() {}
+function f(module) { return module }
+try {} catch (module) {}
+
+module
+v = 20
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev