OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/parser.h" | 5 #include "vm/parser.h" |
6 | 6 |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/compiler_stats.h" | 10 #include "vm/compiler_stats.h" |
(...skipping 4070 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4081 library_.AddObject(func, accessor_name); | 4081 library_.AddObject(func, accessor_name); |
4082 } else { | 4082 } else { |
4083 library_.ReplaceObject(func, accessor_name); | 4083 library_.ReplaceObject(func, accessor_name); |
4084 } | 4084 } |
4085 } | 4085 } |
4086 | 4086 |
4087 | 4087 |
4088 // TODO(hausner): Remove support for old library definition syntax. | 4088 // TODO(hausner): Remove support for old library definition syntax. |
4089 void Parser::ParseLibraryNameObsoleteSyntax() { | 4089 void Parser::ParseLibraryNameObsoleteSyntax() { |
4090 if ((script_.kind() == RawScript::kLibraryTag) && | 4090 if ((script_.kind() == RawScript::kLibraryTag) && |
4091 (CurrentToken() != Token::kLIBRARY)) { | 4091 (CurrentToken() != Token::kLEGACY_LIBRARY)) { |
4092 // Handle error case early to get consistent error message. | 4092 // Handle error case early to get consistent error message. |
4093 ExpectToken(Token::kLIBRARY); | 4093 ExpectToken(Token::kLEGACY_LIBRARY); |
4094 } | 4094 } |
4095 if (CurrentToken() == Token::kLIBRARY) { | 4095 if (CurrentToken() == Token::kLEGACY_LIBRARY) { |
4096 ConsumeToken(); | 4096 ConsumeToken(); |
4097 ExpectToken(Token::kLPAREN); | 4097 ExpectToken(Token::kLPAREN); |
4098 if (CurrentToken() != Token::kSTRING) { | 4098 if (CurrentToken() != Token::kSTRING) { |
4099 ErrorMsg("library name expected"); | 4099 ErrorMsg("library name expected"); |
4100 } | 4100 } |
4101 const String& name = *CurrentLiteral(); | 4101 const String& name = *CurrentLiteral(); |
4102 ConsumeToken(); | 4102 ConsumeToken(); |
4103 ExpectToken(Token::kRPAREN); | 4103 ExpectToken(Token::kRPAREN); |
4104 ExpectToken(Token::kSEMICOLON); | 4104 ExpectToken(Token::kSEMICOLON); |
4105 library_.SetName(name); | 4105 library_.SetName(name); |
(...skipping 16 matching lines...) Expand all Loading... |
4122 Error& prev_error = Error::Handle(); | 4122 Error& prev_error = Error::Handle(); |
4123 prev_error ^= Api::UnwrapHandle(result); | 4123 prev_error ^= Api::UnwrapHandle(result); |
4124 AppendErrorMsg(prev_error, token_pos, "library handler failed"); | 4124 AppendErrorMsg(prev_error, token_pos, "library handler failed"); |
4125 } | 4125 } |
4126 return result; | 4126 return result; |
4127 } | 4127 } |
4128 | 4128 |
4129 | 4129 |
4130 // TODO(hausner): Remove support for old library definition syntax. | 4130 // TODO(hausner): Remove support for old library definition syntax. |
4131 void Parser::ParseLibraryImportObsoleteSyntax() { | 4131 void Parser::ParseLibraryImportObsoleteSyntax() { |
4132 while (CurrentToken() == Token::kIMPORT) { | 4132 while (CurrentToken() == Token::kLEGACY_IMPORT) { |
4133 const intptr_t import_pos = TokenPos(); | 4133 const intptr_t import_pos = TokenPos(); |
4134 ConsumeToken(); | 4134 ConsumeToken(); |
4135 ExpectToken(Token::kLPAREN); | 4135 ExpectToken(Token::kLPAREN); |
4136 if (CurrentToken() != Token::kSTRING) { | 4136 if (CurrentToken() != Token::kSTRING) { |
4137 ErrorMsg("library url expected"); | 4137 ErrorMsg("library url expected"); |
4138 } | 4138 } |
4139 const String& url = *CurrentLiteral(); | 4139 const String& url = *CurrentLiteral(); |
4140 ConsumeToken(); | 4140 ConsumeToken(); |
4141 String& prefix = String::Handle(); | 4141 String& prefix = String::Handle(); |
4142 if (CurrentToken() == Token::kCOMMA) { | 4142 if (CurrentToken() == Token::kCOMMA) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4189 library_prefix = LibraryPrefix::New(prefix, import); | 4189 library_prefix = LibraryPrefix::New(prefix, import); |
4190 library_.AddObject(library_prefix, prefix); | 4190 library_.AddObject(library_prefix, prefix); |
4191 } | 4191 } |
4192 } | 4192 } |
4193 } | 4193 } |
4194 } | 4194 } |
4195 | 4195 |
4196 | 4196 |
4197 // TODO(hausner): Remove support for old library definition syntax. | 4197 // TODO(hausner): Remove support for old library definition syntax. |
4198 void Parser::ParseLibraryIncludeObsoleteSyntax() { | 4198 void Parser::ParseLibraryIncludeObsoleteSyntax() { |
4199 while (CurrentToken() == Token::kSOURCE) { | 4199 while (CurrentToken() == Token::kLEGACY_SOURCE) { |
4200 const intptr_t source_pos = TokenPos(); | 4200 const intptr_t source_pos = TokenPos(); |
4201 ConsumeToken(); | 4201 ConsumeToken(); |
4202 ExpectToken(Token::kLPAREN); | 4202 ExpectToken(Token::kLPAREN); |
4203 if (CurrentToken() != Token::kSTRING) { | 4203 if (CurrentToken() != Token::kSTRING) { |
4204 ErrorMsg("source url expected"); | 4204 ErrorMsg("source url expected"); |
4205 } | 4205 } |
4206 const String& url = *CurrentLiteral(); | 4206 const String& url = *CurrentLiteral(); |
4207 ConsumeToken(); | 4207 ConsumeToken(); |
4208 ExpectToken(Token::kRPAREN); | 4208 ExpectToken(Token::kRPAREN); |
4209 ExpectToken(Token::kSEMICOLON); | 4209 ExpectToken(Token::kSEMICOLON); |
4210 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, | 4210 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, |
4211 source_pos, | 4211 source_pos, |
4212 url); | 4212 url); |
4213 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); | 4213 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); |
4214 CallLibraryTagHandler(kSourceTag, source_pos, canon_url); | 4214 CallLibraryTagHandler(kSourceTag, source_pos, canon_url); |
4215 } | 4215 } |
4216 } | 4216 } |
4217 | 4217 |
4218 | 4218 |
4219 void Parser::ParseLibraryName() { | 4219 void Parser::ParseLibraryName() { |
4220 ASSERT(IsLiteral("library")); | 4220 ASSERT(CurrentToken() == Token::kLIBRARY); |
4221 ConsumeToken(); | 4221 ConsumeToken(); |
4222 // TODO(hausner): Exact syntax of library name still unclear: identifier, | 4222 // TODO(hausner): Exact syntax of library name still unclear: identifier, |
4223 // qualified identifier or even multiple dots allowed? For now we just | 4223 // qualified identifier or even multiple dots allowed? For now we just |
4224 // accept simple identifiers. | 4224 // accept simple identifiers. |
4225 const String& lib_name = *ExpectIdentifier("library name expected"); | 4225 const String& lib_name = *ExpectIdentifier("library name expected"); |
4226 library_.SetName(lib_name); | 4226 library_.SetName(lib_name); |
4227 ExpectSemicolon(); | 4227 ExpectSemicolon(); |
4228 } | 4228 } |
4229 | 4229 |
4230 | 4230 |
4231 void Parser::ParseIdentList(GrowableObjectArray* names) { | 4231 void Parser::ParseIdentList(GrowableObjectArray* names) { |
4232 while (IsIdentifier()) { | 4232 while (IsIdentifier()) { |
4233 names->Add(*CurrentLiteral()); | 4233 names->Add(*CurrentLiteral()); |
4234 ConsumeToken(); // Identifier. | 4234 ConsumeToken(); // Identifier. |
4235 if (CurrentToken() != Token::kCOMMA) { | 4235 if (CurrentToken() != Token::kCOMMA) { |
4236 return; | 4236 return; |
4237 } | 4237 } |
4238 ConsumeToken(); // Comma. | 4238 ConsumeToken(); // Comma. |
4239 } | 4239 } |
4240 } | 4240 } |
4241 | 4241 |
4242 | 4242 |
4243 void Parser::ParseLibraryImportExport() { | 4243 void Parser::ParseLibraryImportExport() { |
4244 bool is_import = IsLiteral("import"); | 4244 bool is_import = (CurrentToken() == Token::kIMPORT); |
4245 bool is_export = IsLiteral("export"); | 4245 bool is_export = (CurrentToken() == Token::kEXPORT); |
4246 ASSERT(is_import || is_export); | 4246 ASSERT(is_import || is_export); |
4247 const intptr_t import_pos = TokenPos(); | 4247 const intptr_t import_pos = TokenPos(); |
4248 ConsumeToken(); | 4248 ConsumeToken(); |
4249 if (CurrentToken() != Token::kSTRING) { | 4249 if (CurrentToken() != Token::kSTRING) { |
4250 ErrorMsg("library url expected"); | 4250 ErrorMsg("library url expected"); |
4251 } | 4251 } |
4252 const String& url = *CurrentLiteral(); | 4252 const String& url = *CurrentLiteral(); |
4253 if (url.Length() == 0) { | 4253 if (url.Length() == 0) { |
4254 ErrorMsg("library url expected"); | 4254 ErrorMsg("library url expected"); |
4255 } | 4255 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4345 void Parser::ParseLibraryDefinition() { | 4345 void Parser::ParseLibraryDefinition() { |
4346 TRACE_PARSER("ParseLibraryDefinition"); | 4346 TRACE_PARSER("ParseLibraryDefinition"); |
4347 | 4347 |
4348 // Handle the script tag. | 4348 // Handle the script tag. |
4349 if (CurrentToken() == Token::kSCRIPTTAG) { | 4349 if (CurrentToken() == Token::kSCRIPTTAG) { |
4350 // Nothing to do for script tags except to skip them. | 4350 // Nothing to do for script tags except to skip them. |
4351 ConsumeToken(); | 4351 ConsumeToken(); |
4352 } | 4352 } |
4353 | 4353 |
4354 // TODO(hausner): Remove support for old library definition syntax. | 4354 // TODO(hausner): Remove support for old library definition syntax. |
4355 if ((CurrentToken() == Token::kLIBRARY) || | 4355 if ((CurrentToken() == Token::kLEGACY_LIBRARY) || |
4356 (CurrentToken() == Token::kIMPORT) || | 4356 (CurrentToken() == Token::kLEGACY_IMPORT) || |
4357 (CurrentToken() == Token::kSOURCE)) { | 4357 (CurrentToken() == Token::kLEGACY_SOURCE)) { |
4358 ParseLibraryNameObsoleteSyntax(); | 4358 ParseLibraryNameObsoleteSyntax(); |
4359 ParseLibraryImportObsoleteSyntax(); | 4359 ParseLibraryImportObsoleteSyntax(); |
4360 ParseLibraryIncludeObsoleteSyntax(); | 4360 ParseLibraryIncludeObsoleteSyntax(); |
4361 // Core lib has not been explicitly imported, so we implicitly | 4361 // Core lib has not been explicitly imported, so we implicitly |
4362 // import it here. | 4362 // import it here. |
4363 if (!library_.ImportsCorelib()) { | 4363 if (!library_.ImportsCorelib()) { |
4364 Library& core_lib = Library::Handle(Library::CoreLibrary()); | 4364 Library& core_lib = Library::Handle(Library::CoreLibrary()); |
4365 ASSERT(!core_lib.IsNull()); | 4365 ASSERT(!core_lib.IsNull()); |
4366 const Namespace& core_ns = Namespace::Handle( | 4366 const Namespace& core_ns = Namespace::Handle( |
4367 Namespace::New(core_lib, Array::Handle(), Array::Handle())); | 4367 Namespace::New(core_lib, Array::Handle(), Array::Handle())); |
4368 library_.AddImport(core_ns); | 4368 library_.AddImport(core_ns); |
4369 } | 4369 } |
4370 return; | 4370 return; |
4371 } | 4371 } |
4372 | 4372 |
4373 // We may read metadata tokens that are part of the toplevel | 4373 // We may read metadata tokens that are part of the toplevel |
4374 // declaration that follows the library definitions. Therefore, we | 4374 // declaration that follows the library definitions. Therefore, we |
4375 // need to remember the position of the last token that was | 4375 // need to remember the position of the last token that was |
4376 // successfully consumed. | 4376 // successfully consumed. |
4377 intptr_t metadata_pos = TokenPos(); | 4377 intptr_t metadata_pos = TokenPos(); |
4378 SkipMetadata(); | 4378 SkipMetadata(); |
4379 if (IsLiteral("library")) { | 4379 if (CurrentToken() == Token::kLIBRARY) { |
4380 ParseLibraryName(); | 4380 ParseLibraryName(); |
4381 metadata_pos = TokenPos(); | 4381 metadata_pos = TokenPos(); |
4382 SkipMetadata(); | 4382 SkipMetadata(); |
4383 } else if (script_.kind() == RawScript::kLibraryTag) { | 4383 } else if (script_.kind() == RawScript::kLibraryTag) { |
4384 ErrorMsg("library name definition expected"); | 4384 ErrorMsg("library name definition expected"); |
4385 } | 4385 } |
4386 while (IsLiteral("import") || IsLiteral("export")) { | 4386 while ((CurrentToken() == Token::kIMPORT) || |
| 4387 (CurrentToken() == Token::kEXPORT)) { |
4387 ParseLibraryImportExport(); | 4388 ParseLibraryImportExport(); |
4388 metadata_pos = TokenPos(); | 4389 metadata_pos = TokenPos(); |
4389 SkipMetadata(); | 4390 SkipMetadata(); |
4390 } | 4391 } |
4391 // Core lib has not been explicitly imported, so we implicitly | 4392 // Core lib has not been explicitly imported, so we implicitly |
4392 // import it here. | 4393 // import it here. |
4393 if (!library_.ImportsCorelib()) { | 4394 if (!library_.ImportsCorelib()) { |
4394 Library& core_lib = Library::Handle(Library::CoreLibrary()); | 4395 Library& core_lib = Library::Handle(Library::CoreLibrary()); |
4395 ASSERT(!core_lib.IsNull()); | 4396 ASSERT(!core_lib.IsNull()); |
4396 const Namespace& core_ns = Namespace::Handle( | 4397 const Namespace& core_ns = Namespace::Handle( |
4397 Namespace::New(core_lib, Array::Handle(), Array::Handle())); | 4398 Namespace::New(core_lib, Array::Handle(), Array::Handle())); |
4398 library_.AddImport(core_ns); | 4399 library_.AddImport(core_ns); |
4399 } | 4400 } |
4400 while (IsLiteral("part")) { | 4401 while (CurrentToken() == Token::kPART) { |
4401 ParseLibraryPart(); | 4402 ParseLibraryPart(); |
4402 metadata_pos = TokenPos(); | 4403 metadata_pos = TokenPos(); |
4403 SkipMetadata(); | 4404 SkipMetadata(); |
4404 } | 4405 } |
4405 if (IsLiteral("library") || IsLiteral("import") || IsLiteral("export")) { | |
4406 ErrorMsg("unexpected token '%s'", CurrentLiteral()->ToCString()); | |
4407 } | |
4408 SetPosition(metadata_pos); | 4406 SetPosition(metadata_pos); |
4409 } | 4407 } |
4410 | 4408 |
4411 | 4409 |
4412 void Parser::ParsePartHeader() { | 4410 void Parser::ParsePartHeader() { |
4413 intptr_t metadata_pos = TokenPos(); | 4411 intptr_t metadata_pos = TokenPos(); |
4414 SkipMetadata(); | 4412 SkipMetadata(); |
4415 // TODO(hausner): Once support for old #source directive is removed | 4413 // TODO(hausner): Once support for old #source directive is removed |
4416 // from the compiler, add an error message here if we don't find | 4414 // from the compiler, add an error message here if we don't find |
4417 // a 'part of' directive. | 4415 // a 'part of' directive. |
4418 if (IsLiteral("part")) { | 4416 if (CurrentToken() == Token::kPART) { |
4419 ConsumeToken(); | 4417 ConsumeToken(); |
4420 if (!IsLiteral("of")) { | 4418 if (!IsLiteral("of")) { |
4421 ErrorMsg("'part of' expected"); | 4419 ErrorMsg("'part of' expected"); |
4422 } | 4420 } |
4423 ConsumeToken(); | 4421 ConsumeToken(); |
4424 // TODO(hausner): Exact syntax of library name still unclear: identifier, | 4422 // TODO(hausner): Exact syntax of library name still unclear: identifier, |
4425 // qualified identifier or even multiple dots allowed? For now we just | 4423 // qualified identifier or even multiple dots allowed? For now we just |
4426 // accept simple identifiers. | 4424 // accept simple identifiers. |
4427 // The VM is not required to check that the library name matches the | 4425 // The VM is not required to check that the library name matches the |
4428 // name of the current library, so we ignore it. | 4426 // name of the current library, so we ignore it. |
(...skipping 5439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9868 void Parser::SkipQualIdent() { | 9866 void Parser::SkipQualIdent() { |
9869 ASSERT(IsIdentifier()); | 9867 ASSERT(IsIdentifier()); |
9870 ConsumeToken(); | 9868 ConsumeToken(); |
9871 if (CurrentToken() == Token::kPERIOD) { | 9869 if (CurrentToken() == Token::kPERIOD) { |
9872 ConsumeToken(); // Consume the kPERIOD token. | 9870 ConsumeToken(); // Consume the kPERIOD token. |
9873 ExpectIdentifier("identifier expected after '.'"); | 9871 ExpectIdentifier("identifier expected after '.'"); |
9874 } | 9872 } |
9875 } | 9873 } |
9876 | 9874 |
9877 } // namespace dart | 9875 } // namespace dart |
OLD | NEW |