Reviewers: arv, rossberg,
Description:
Simplify error message logic in ParseImportNames
The new logic ensures that the error messages are the same in the
"import { <reserved word> }" and "import { foo as <reserved ord> }"
cases.
Also prepares ParseImportNames for returning both the import and local
names to ParseImportClause.
BUG=v8:1569
LOG=n
Please review this at https://codereview.chromium.org/952863006/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+51, -35 lines):
M src/parser.h
M src/parser.cc
A + test/message/import-as-eval.js
A test/message/import-as-eval.out
A + test/message/import-as-reserved-word.js
A + test/message/import-as-reserved-word.out
A + test/message/import-eval.js
A test/message/import-eval.out
A + test/message/import-reserved-word.js
A + test/message/import-reserved-word.out
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index
e50a5efdcbde94f5bd4a49c2e4108cb31192a2eb..f15418f6b9ab253c7b09d4ed5bdb5ac86d3255b3
100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1349,7 +1349,8 @@ void* Parser::ParseExportClause(ZoneList<const
AstRawString*>* export_names,
}
-void* Parser::ParseNamedImports(ZoneList<const AstRawString*>* names,
+void* Parser::ParseNamedImports(ZoneList<const AstRawString*>*
import_names,
+ ZoneList<const AstRawString*>* local_names,
bool* ok) {
// NamedImports :
// '{' '}'
@@ -1366,27 +1367,26 @@ void* Parser::ParseNamedImports(ZoneList<const
AstRawString*>* names,
Expect(Token::LBRACE, CHECK_OK);
- Token::Value name_tok;
- while ((name_tok = peek()) != Token::RBRACE) {
- const AstRawString* name = ParseIdentifierName(CHECK_OK);
- const AstRawString* import_name = NULL;
+ while (peek() != Token::RBRACE) {
+ const AstRawString* import_name = ParseIdentifierName(CHECK_OK);
+ const AstRawString* local_name = import_name;
// In the presence of 'as', the left-side of the 'as' can
// be any IdentifierName. But without 'as', it must be a valid
- // BindingIdentiifer.
+ // BindingIdentifier.
if (CheckContextualKeyword(CStrVector("as"))) {
- import_name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
- } else if (!Token::IsIdentifier(name_tok, STRICT, false)) {
+ local_name = ParseIdentifierName(CHECK_OK);
+ }
+ if (!Token::IsIdentifier(scanner()->current_token(), STRICT, false)) {
*ok = false;
- ReportMessageAt(scanner()->location(), "unexpected_reserved");
+ ReportMessage("unexpected_reserved");
return NULL;
- } else if (IsEvalOrArguments(name)) {
+ } else if (IsEvalOrArguments(local_name)) {
*ok = false;
- ReportMessageAt(scanner()->location(), "strict_eval_arguments");
+ ReportMessage("strict_eval_arguments");
return NULL;
}
- // TODO(ES6): Return the import_name as well as the name.
- names->Add(name, zone());
- USE(import_name);
+ import_names->Add(import_name, zone());
+ local_names->Add(local_name, zone());
if (peek() == Token::RBRACE) break;
Expect(Token::COMMA, CHECK_OK);
}
@@ -1419,8 +1419,10 @@ Statement* Parser::ParseImportDeclaration(bool* ok) {
// 'import' ModuleSpecifier ';'
if (tok == Token::STRING) {
- ParseModuleSpecifier(CHECK_OK);
+ Literal* module = ParseModuleSpecifier(CHECK_OK);
ExpectSemicolon(CHECK_OK);
+ // TODO(ES6): Add module to the requested modules of scope_->module().
+ USE(module);
return factory()->NewEmptyStatement(pos);
}
@@ -1432,7 +1434,8 @@ Statement* Parser::ParseImportDeclaration(bool* ok) {
}
const AstRawString* module_instance_binding = NULL;
- ZoneList<const AstRawString*> names(1, zone());
+ ZoneList<const AstRawString*> local_names(1, zone());
+ ZoneList<const AstRawString*> import_names(1, zone());
if (imported_default_binding == NULL || Check(Token::COMMA)) {
switch (peek()) {
case Token::MUL: {
@@ -1444,7 +1447,7 @@ Statement* Parser::ParseImportDeclaration(bool* ok) {
}
case Token::LBRACE:
- ParseNamedImports(&names, CHECK_OK);
+ ParseNamedImports(&import_names, &local_names, CHECK_OK);
break;
default:
@@ -1468,7 +1471,9 @@ Statement* Parser::ParseImportDeclaration(bool* ok) {
// TODO(ES6): Add an appropriate declaration.
}
- for (int i = 0; i < names.length(); ++i) {
+ const int length = import_names.length();
+ DCHECK_EQ(length, local_names.length());
+ for (int i = 0; i < length; ++i) {
// TODO(ES6): Add an appropriate declaration for each name
}
Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index
26255b28cb4b947128401a77db1fb321113d2edf..a65a863793f05f1289fe961464034112e318b5fb
100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -713,7 +713,8 @@ class Parser : public ParserBase<ParserTraits> {
ZoneList<Scanner::Location>* export_locations,
ZoneList<const AstRawString*>* local_names,
Scanner::Location* reserved_loc, bool* ok);
- void* ParseNamedImports(ZoneList<const AstRawString*>* names, bool* ok);
+ void* ParseNamedImports(ZoneList<const AstRawString*>* import_names,
+ ZoneList<const AstRawString*>* local_names,
bool* ok);
Statement* ParseStatement(ZoneList<const AstRawString*>* labels, bool*
ok);
Statement* ParseSubStatement(ZoneList<const AstRawString*>* labels,
bool* ok);
Statement* ParseFunctionDeclaration(ZoneList<const AstRawString*>* names,
Index: test/message/import-as-eval.js
diff --git a/test/mjsunit/harmony/modules.js
b/test/message/import-as-eval.js
similarity index 81%
copy from test/mjsunit/harmony/modules.js
copy to test/message/import-as-eval.js
index
e56880500ba359914bbdd7223b43335eac3cc038..66adc32cbe435b8cbaf451e1373520a5edea2663
100644
--- a/test/mjsunit/harmony/modules.js
+++ b/test/message/import-as-eval.js
@@ -4,5 +4,4 @@
//
// MODULE
-export let a = 42;
-assertEquals(42, a);
+import { foo as eval } from "mod";
Index: test/message/import-as-eval.out
diff --git a/test/message/import-as-eval.out
b/test/message/import-as-eval.out
new file mode 100644
index
0000000000000000000000000000000000000000..622f7fe9e187d544b9d20e2e5a1cd6c175e4bd9e
--- /dev/null
+++ b/test/message/import-as-eval.out
@@ -0,0 +1,7 @@
+# Copyright 2015 the V8 project authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+*%(basename)s:7: SyntaxError: Unexpected eval or arguments in strict mode
+import { foo as eval } from "mod";
+ ^^^^
+SyntaxError: Unexpected eval or arguments in strict mode
Index: test/message/import-as-reserved-word.js
diff --git a/test/mjsunit/harmony/modules.js
b/test/message/import-as-reserved-word.js
similarity index 81%
copy from test/mjsunit/harmony/modules.js
copy to test/message/import-as-reserved-word.js
index
e56880500ba359914bbdd7223b43335eac3cc038..562699d45f84a8c2f65f38b1d6d7d5b7de147bf5
100644
--- a/test/mjsunit/harmony/modules.js
+++ b/test/message/import-as-reserved-word.js
@@ -4,5 +4,4 @@
//
// MODULE
-export let a = 42;
-assertEquals(42, a);
+import { foo as import } from "mod";
Index: test/message/import-as-reserved-word.out
diff --git a/test/message/export-duplicate.out
b/test/message/import-as-reserved-word.out
similarity index 51%
copy from test/message/export-duplicate.out
copy to test/message/import-as-reserved-word.out
index
e88779f5805370e47565c87126674e4b934d7c19..1ee8d41c1afa3a77b0dd971ea4652a70a567317c
100644
--- a/test/message/export-duplicate.out
+++ b/test/message/import-as-reserved-word.out
@@ -1,7 +1,7 @@
# Copyright 2015 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-*%(basename)s:9: SyntaxError: Duplicate export of 'a'
-export { a, b };
- ^
-SyntaxError: Duplicate export of 'a'
+*%(basename)s:7: SyntaxError: Unexpected reserved word
+import { foo as import } from "mod";
+ ^^^^^^
+SyntaxError: Unexpected reserved word
Index: test/message/import-eval.js
diff --git a/test/mjsunit/harmony/modules.js b/test/message/import-eval.js
similarity index 81%
copy from test/mjsunit/harmony/modules.js
copy to test/message/import-eval.js
index
e56880500ba359914bbdd7223b43335eac3cc038..8ab35baef672a45d6a449fd328941dc4ff9a00eb
100644
--- a/test/mjsunit/harmony/modules.js
+++ b/test/message/import-eval.js
@@ -4,5 +4,4 @@
//
// MODULE
-export let a = 42;
-assertEquals(42, a);
+import { eval } from "mod";
Index: test/message/import-eval.out
diff --git a/test/message/import-eval.out b/test/message/import-eval.out
new file mode 100644
index
0000000000000000000000000000000000000000..148662a28ca9e2864b5c1d6a393c7fa0bda3d667
--- /dev/null
+++ b/test/message/import-eval.out
@@ -0,0 +1,7 @@
+# Copyright 2015 the V8 project authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+*%(basename)s:7: SyntaxError: Unexpected eval or arguments in strict mode
+import { eval } from "mod";
+ ^^^^
+SyntaxError: Unexpected eval or arguments in strict mode
Index: test/message/import-reserved-word.js
diff --git a/test/mjsunit/harmony/modules.js
b/test/message/import-reserved-word.js
similarity index 81%
copy from test/mjsunit/harmony/modules.js
copy to test/message/import-reserved-word.js
index
e56880500ba359914bbdd7223b43335eac3cc038..1fd7ba291e7f64b41c183223bff35381b6621e27
100644
--- a/test/mjsunit/harmony/modules.js
+++ b/test/message/import-reserved-word.js
@@ -4,5 +4,4 @@
//
// MODULE
-export let a = 42;
-assertEquals(42, a);
+import { import } from "mod";
Index: test/message/import-reserved-word.out
diff --git a/test/message/export-duplicate-as.out
b/test/message/import-reserved-word.out
similarity index 54%
copy from test/message/export-duplicate-as.out
copy to test/message/import-reserved-word.out
index
1726d9491a39fab654093e6b6cda30d265cc015b..5b990e9e590aa6088f1fe5a058eac16f88a238f0
100644
--- a/test/message/export-duplicate-as.out
+++ b/test/message/import-reserved-word.out
@@ -1,7 +1,7 @@
# Copyright 2015 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-*%(basename)s:9: SyntaxError: Duplicate export of 'c'
-export { a, b as c };
- ^
-SyntaxError: Duplicate export of 'c'
+*%(basename)s:7: SyntaxError: Unexpected reserved word
+import { import } from "mod";
+ ^^^^^^
+SyntaxError: Unexpected reserved word
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.