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 3512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3523 ConsumeToken(); | 3523 ConsumeToken(); |
3524 ExpectToken(Token::kRPAREN); | 3524 ExpectToken(Token::kRPAREN); |
3525 ExpectToken(Token::kSEMICOLON); | 3525 ExpectToken(Token::kSEMICOLON); |
3526 library_.SetName(name); | 3526 library_.SetName(name); |
3527 } | 3527 } |
3528 } | 3528 } |
3529 | 3529 |
3530 | 3530 |
3531 Dart_Handle Parser::CallLibraryTagHandler(Dart_LibraryTag tag, | 3531 Dart_Handle Parser::CallLibraryTagHandler(Dart_LibraryTag tag, |
3532 intptr_t token_pos, | 3532 intptr_t token_pos, |
3533 const String& url, | 3533 const String& url) { |
3534 const Array& import_map) { | |
3535 Isolate* isolate = Isolate::Current(); | 3534 Isolate* isolate = Isolate::Current(); |
3536 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); | 3535 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); |
3537 if (handler == NULL) { | 3536 if (handler == NULL) { |
3538 ErrorMsg(token_pos, "no library handler registered"); | 3537 ErrorMsg(token_pos, "no library handler registered"); |
3539 } | 3538 } |
3540 Dart_Handle result = handler(tag, | 3539 Dart_Handle result = handler(tag, |
3541 Api::NewHandle(isolate, library_.raw()), | 3540 Api::NewHandle(isolate, library_.raw()), |
3542 Api::NewHandle(isolate, url.raw()), | 3541 Api::NewHandle(isolate, url.raw())); |
3543 Api::NewHandle(isolate, import_map.raw())); | |
3544 if (Dart_IsError(result)) { | 3542 if (Dart_IsError(result)) { |
3545 Error& prev_error = Error::Handle(); | 3543 Error& prev_error = Error::Handle(); |
3546 prev_error ^= Api::UnwrapHandle(result); | 3544 prev_error ^= Api::UnwrapHandle(result); |
3547 AppendErrorMsg(prev_error, token_pos, "library handler failed"); | 3545 AppendErrorMsg(prev_error, token_pos, "library handler failed"); |
3548 } | 3546 } |
3549 return result; | 3547 return result; |
3550 } | 3548 } |
3551 | 3549 |
3552 | 3550 |
3553 void Parser::ParseLibraryImport() { | 3551 void Parser::ParseLibraryImport() { |
(...skipping 19 matching lines...) Expand all Loading... |
3573 } | 3571 } |
3574 prefix = CurrentLiteral()->raw(); | 3572 prefix = CurrentLiteral()->raw(); |
3575 // TODO(asiva): Need to also check that prefix is not a reserved keyword. | 3573 // TODO(asiva): Need to also check that prefix is not a reserved keyword. |
3576 if (!Scanner::IsIdent(prefix)) { | 3574 if (!Scanner::IsIdent(prefix)) { |
3577 ErrorMsg("prefix should be an identifier"); | 3575 ErrorMsg("prefix should be an identifier"); |
3578 } | 3576 } |
3579 ConsumeToken(); | 3577 ConsumeToken(); |
3580 } | 3578 } |
3581 ExpectToken(Token::kRPAREN); | 3579 ExpectToken(Token::kRPAREN); |
3582 ExpectToken(Token::kSEMICOLON); | 3580 ExpectToken(Token::kSEMICOLON); |
3583 const Array& import_map = Array::Handle(library_.import_map()); | |
3584 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, | 3581 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, |
3585 import_pos, | 3582 import_pos, |
3586 url, | 3583 url); |
3587 import_map); | |
3588 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); | 3584 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); |
3589 // Lookup the library URL. | 3585 // Lookup the library URL. |
3590 Library& library = Library::Handle(Library::LookupLibrary(canon_url)); | 3586 Library& library = Library::Handle(Library::LookupLibrary(canon_url)); |
3591 if (library.IsNull()) { | 3587 if (library.IsNull()) { |
3592 // Call the library tag handler to load the library. | 3588 // Call the library tag handler to load the library. |
3593 CallLibraryTagHandler(kImportTag, import_pos, canon_url, import_map); | 3589 CallLibraryTagHandler(kImportTag, import_pos, canon_url); |
3594 // If the library tag handler succeded without registering the | 3590 // If the library tag handler succeded without registering the |
3595 // library we create an empty library to import. | 3591 // library we create an empty library to import. |
3596 library = Library::LookupLibrary(canon_url); | 3592 library = Library::LookupLibrary(canon_url); |
3597 if (library.IsNull()) { | 3593 if (library.IsNull()) { |
3598 library = Library::New(canon_url); | 3594 library = Library::New(canon_url); |
3599 library.Register(); | 3595 library.Register(); |
3600 } | 3596 } |
3601 } | 3597 } |
3602 // Add the import to the library. | 3598 // Add the import to the library. |
3603 if (prefix.IsNull() || (prefix.Length() == 0)) { | 3599 if (prefix.IsNull() || (prefix.Length() == 0)) { |
3604 library_.AddImport(library); | 3600 library_.AddImport(library); |
3605 } else { | 3601 } else { |
3606 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); | 3602 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); |
3607 library_prefix = library_.LookupLocalLibraryPrefix(prefix); | 3603 library_prefix = library_.LookupLocalLibraryPrefix(prefix); |
3608 if (!library_prefix.IsNull()) { | 3604 if (!library_prefix.IsNull()) { |
3609 library_prefix.AddLibrary(library); | 3605 library_prefix.AddLibrary(library); |
3610 } else { | 3606 } else { |
3611 library_prefix = LibraryPrefix::New(prefix, library); | 3607 library_prefix = LibraryPrefix::New(prefix, library); |
3612 library_.AddObject(library_prefix, prefix); | 3608 library_.AddObject(library_prefix, prefix); |
3613 } | 3609 } |
3614 } | 3610 } |
3615 } | 3611 } |
3616 } | 3612 } |
3617 | 3613 |
3618 | 3614 |
3619 void Parser::ParseLibraryInclude() { | 3615 void Parser::ParseLibraryInclude() { |
3620 TRACE_PARSER("ParseLibraryInclude"); | 3616 TRACE_PARSER("ParseLibraryInclude"); |
3621 const Array& import_map = Array::Handle(library_.import_map()); | |
3622 while (CurrentToken() == Token::kSOURCE) { | 3617 while (CurrentToken() == Token::kSOURCE) { |
3623 const intptr_t source_pos = token_index_; | 3618 const intptr_t source_pos = token_index_; |
3624 ConsumeToken(); | 3619 ConsumeToken(); |
3625 ExpectToken(Token::kLPAREN); | 3620 ExpectToken(Token::kLPAREN); |
3626 if (CurrentToken() != Token::kSTRING) { | 3621 if (CurrentToken() != Token::kSTRING) { |
3627 ErrorMsg("source url expected"); | 3622 ErrorMsg("source url expected"); |
3628 } | 3623 } |
3629 const String& url = *ParseImportStringLiteral(); | 3624 const String& url = *ParseImportStringLiteral(); |
3630 ExpectToken(Token::kRPAREN); | 3625 ExpectToken(Token::kRPAREN); |
3631 ExpectToken(Token::kSEMICOLON); | 3626 ExpectToken(Token::kSEMICOLON); |
3632 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, | 3627 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, |
3633 source_pos, | 3628 source_pos, |
3634 url, | 3629 url); |
3635 import_map); | |
3636 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); | 3630 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); |
3637 CallLibraryTagHandler(kSourceTag, source_pos, canon_url, import_map); | 3631 CallLibraryTagHandler(kSourceTag, source_pos, canon_url); |
3638 } | 3632 } |
3639 } | 3633 } |
3640 | 3634 |
3641 | 3635 |
3642 void Parser::ParseLibraryResource() { | 3636 void Parser::ParseLibraryResource() { |
3643 TRACE_PARSER("ParseLibraryResource"); | 3637 TRACE_PARSER("ParseLibraryResource"); |
3644 while (CurrentToken() == Token::kRESOURCE) { | 3638 while (CurrentToken() == Token::kRESOURCE) { |
3645 // Currently the VM does ignore #resource library tags. They are only used | 3639 // Currently the VM does ignore #resource library tags. They are only used |
3646 // by the IDE. | 3640 // by the IDE. |
3647 ConsumeToken(); | 3641 ConsumeToken(); |
(...skipping 3561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7209 return var_or_field; | 7203 return var_or_field; |
7210 } | 7204 } |
7211 | 7205 |
7212 | 7206 |
7213 // Resolve variables used in an import string literal. | 7207 // Resolve variables used in an import string literal. |
7214 // If the variable name cannot be resolved issue an error message. | 7208 // If the variable name cannot be resolved issue an error message. |
7215 // Currently we only resolve against the global map which is passed in | 7209 // Currently we only resolve against the global map which is passed in |
7216 // when the script is loaded. | 7210 // when the script is loaded. |
7217 RawString* Parser::ResolveImportVar(intptr_t ident_pos, const String& ident) { | 7211 RawString* Parser::ResolveImportVar(intptr_t ident_pos, const String& ident) { |
7218 TRACE_PARSER("ResolveImportVar"); | 7212 TRACE_PARSER("ResolveImportVar"); |
7219 String& map_name = String::Handle(library_.LookupImportMap(ident)); | 7213 const Array& import_map = |
7220 if (!map_name.IsNull()) { | 7214 Array::Handle(Isolate::Current()->object_store()->import_map()); |
7221 return map_name.raw(); | 7215 if (!import_map.IsNull()) { |
| 7216 intptr_t length = import_map.Length(); |
| 7217 intptr_t index = 0; |
| 7218 String& name = String::Handle(); |
| 7219 while (index < (length - 1)) { |
| 7220 name ^= import_map.At(index); |
| 7221 if (name.Equals(ident)) { |
| 7222 name ^= import_map.At(index + 1); |
| 7223 return name.raw(); |
| 7224 } |
| 7225 index += 2; |
| 7226 } |
7222 } | 7227 } |
7223 ErrorMsg(ident_pos, "import variable '%s' has not been defined", | 7228 ErrorMsg(ident_pos, "import variable '%s' has not been defined", |
7224 ident.ToCString()); | 7229 ident.ToCString()); |
7225 return String::null(); | 7230 return String::null(); |
7226 } | 7231 } |
7227 | 7232 |
7228 | 7233 |
7229 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and | 7234 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and |
7230 // finalize it according to the given type finalization mode. | 7235 // finalize it according to the given type finalization mode. |
7231 RawAbstractType* Parser::ParseType( | 7236 RawAbstractType* Parser::ParseType( |
(...skipping 1247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8479 void Parser::SkipQualIdent() { | 8484 void Parser::SkipQualIdent() { |
8480 ASSERT(IsIdentifier()); | 8485 ASSERT(IsIdentifier()); |
8481 ConsumeToken(); | 8486 ConsumeToken(); |
8482 if (CurrentToken() == Token::kPERIOD) { | 8487 if (CurrentToken() == Token::kPERIOD) { |
8483 ConsumeToken(); // Consume the kPERIOD token. | 8488 ConsumeToken(); // Consume the kPERIOD token. |
8484 ExpectIdentifier("identifier expected after '.'"); | 8489 ExpectIdentifier("identifier expected after '.'"); |
8485 } | 8490 } |
8486 } | 8491 } |
8487 | 8492 |
8488 } // namespace dart | 8493 } // namespace dart |
OLD | NEW |