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 3504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3515 ConsumeToken(); | 3515 ConsumeToken(); |
3516 ExpectToken(Token::kRPAREN); | 3516 ExpectToken(Token::kRPAREN); |
3517 ExpectToken(Token::kSEMICOLON); | 3517 ExpectToken(Token::kSEMICOLON); |
3518 library_.SetName(name); | 3518 library_.SetName(name); |
3519 } | 3519 } |
3520 } | 3520 } |
3521 | 3521 |
3522 | 3522 |
3523 Dart_Handle Parser::CallLibraryTagHandler(Dart_LibraryTag tag, | 3523 Dart_Handle Parser::CallLibraryTagHandler(Dart_LibraryTag tag, |
3524 intptr_t token_pos, | 3524 intptr_t token_pos, |
3525 const String& url, | 3525 const String& url) { |
3526 const Array& import_map) { | |
3527 Isolate* isolate = Isolate::Current(); | 3526 Isolate* isolate = Isolate::Current(); |
3528 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); | 3527 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); |
3529 if (handler == NULL) { | 3528 if (handler == NULL) { |
3530 ErrorMsg(token_pos, "no library handler registered"); | 3529 ErrorMsg(token_pos, "no library handler registered"); |
3531 } | 3530 } |
3532 Dart_Handle result = handler(tag, | 3531 Dart_Handle result = handler(tag, |
3533 Api::NewHandle(isolate, library_.raw()), | 3532 Api::NewHandle(isolate, library_.raw()), |
3534 Api::NewHandle(isolate, url.raw()), | 3533 Api::NewHandle(isolate, url.raw())); |
3535 Api::NewHandle(isolate, import_map.raw())); | |
3536 if (Dart_IsError(result)) { | 3534 if (Dart_IsError(result)) { |
3537 Error& prev_error = Error::Handle(); | 3535 Error& prev_error = Error::Handle(); |
3538 prev_error ^= Api::UnwrapHandle(result); | 3536 prev_error ^= Api::UnwrapHandle(result); |
3539 AppendErrorMsg(prev_error, token_pos, "library handler failed"); | 3537 AppendErrorMsg(prev_error, token_pos, "library handler failed"); |
3540 } | 3538 } |
3541 return result; | 3539 return result; |
3542 } | 3540 } |
3543 | 3541 |
3544 | 3542 |
3545 void Parser::ParseLibraryImport() { | 3543 void Parser::ParseLibraryImport() { |
(...skipping 19 matching lines...) Expand all Loading... |
3565 } | 3563 } |
3566 prefix = CurrentLiteral()->raw(); | 3564 prefix = CurrentLiteral()->raw(); |
3567 // TODO(asiva): Need to also check that prefix is not a reserved keyword. | 3565 // TODO(asiva): Need to also check that prefix is not a reserved keyword. |
3568 if (!Scanner::IsIdent(prefix)) { | 3566 if (!Scanner::IsIdent(prefix)) { |
3569 ErrorMsg("prefix should be an identifier"); | 3567 ErrorMsg("prefix should be an identifier"); |
3570 } | 3568 } |
3571 ConsumeToken(); | 3569 ConsumeToken(); |
3572 } | 3570 } |
3573 ExpectToken(Token::kRPAREN); | 3571 ExpectToken(Token::kRPAREN); |
3574 ExpectToken(Token::kSEMICOLON); | 3572 ExpectToken(Token::kSEMICOLON); |
3575 const Array& import_map = Array::Handle(library_.import_map()); | |
3576 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, | 3573 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, |
3577 import_pos, | 3574 import_pos, |
3578 url, | 3575 url); |
3579 import_map); | |
3580 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); | 3576 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); |
3581 // Lookup the library URL. | 3577 // Lookup the library URL. |
3582 Library& library = Library::Handle(Library::LookupLibrary(canon_url)); | 3578 Library& library = Library::Handle(Library::LookupLibrary(canon_url)); |
3583 if (library.IsNull()) { | 3579 if (library.IsNull()) { |
3584 // Call the library tag handler to load the library. | 3580 // Call the library tag handler to load the library. |
3585 CallLibraryTagHandler(kImportTag, import_pos, canon_url, import_map); | 3581 CallLibraryTagHandler(kImportTag, import_pos, canon_url); |
3586 // If the library tag handler succeded without registering the | 3582 // If the library tag handler succeded without registering the |
3587 // library we create an empty library to import. | 3583 // library we create an empty library to import. |
3588 library = Library::LookupLibrary(canon_url); | 3584 library = Library::LookupLibrary(canon_url); |
3589 if (library.IsNull()) { | 3585 if (library.IsNull()) { |
3590 library = Library::New(canon_url); | 3586 library = Library::New(canon_url); |
3591 library.Register(); | 3587 library.Register(); |
3592 } | 3588 } |
3593 } | 3589 } |
3594 // Add the import to the library. | 3590 // Add the import to the library. |
3595 if (prefix.IsNull() || (prefix.Length() == 0)) { | 3591 if (prefix.IsNull() || (prefix.Length() == 0)) { |
3596 library_.AddImport(library); | 3592 library_.AddImport(library); |
3597 } else { | 3593 } else { |
3598 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); | 3594 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); |
3599 library_prefix = library_.LookupLocalLibraryPrefix(prefix); | 3595 library_prefix = library_.LookupLocalLibraryPrefix(prefix); |
3600 if (!library_prefix.IsNull()) { | 3596 if (!library_prefix.IsNull()) { |
3601 library_prefix.AddLibrary(library); | 3597 library_prefix.AddLibrary(library); |
3602 } else { | 3598 } else { |
3603 library_prefix = LibraryPrefix::New(prefix, library); | 3599 library_prefix = LibraryPrefix::New(prefix, library); |
3604 library_.AddObject(library_prefix, prefix); | 3600 library_.AddObject(library_prefix, prefix); |
3605 } | 3601 } |
3606 } | 3602 } |
3607 } | 3603 } |
3608 } | 3604 } |
3609 | 3605 |
3610 | 3606 |
3611 void Parser::ParseLibraryInclude() { | 3607 void Parser::ParseLibraryInclude() { |
3612 TRACE_PARSER("ParseLibraryInclude"); | 3608 TRACE_PARSER("ParseLibraryInclude"); |
3613 const Array& import_map = Array::Handle(library_.import_map()); | |
3614 while (CurrentToken() == Token::kSOURCE) { | 3609 while (CurrentToken() == Token::kSOURCE) { |
3615 const intptr_t source_pos = token_index_; | 3610 const intptr_t source_pos = token_index_; |
3616 ConsumeToken(); | 3611 ConsumeToken(); |
3617 ExpectToken(Token::kLPAREN); | 3612 ExpectToken(Token::kLPAREN); |
3618 if (CurrentToken() != Token::kSTRING) { | 3613 if (CurrentToken() != Token::kSTRING) { |
3619 ErrorMsg("source url expected"); | 3614 ErrorMsg("source url expected"); |
3620 } | 3615 } |
3621 const String& url = *ParseImportStringLiteral(); | 3616 const String& url = *ParseImportStringLiteral(); |
3622 ExpectToken(Token::kRPAREN); | 3617 ExpectToken(Token::kRPAREN); |
3623 ExpectToken(Token::kSEMICOLON); | 3618 ExpectToken(Token::kSEMICOLON); |
3624 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, | 3619 Dart_Handle handle = CallLibraryTagHandler(kCanonicalizeUrl, |
3625 source_pos, | 3620 source_pos, |
3626 url, | 3621 url); |
3627 import_map); | |
3628 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); | 3622 const String& canon_url = String::CheckedHandle(Api::UnwrapHandle(handle)); |
3629 CallLibraryTagHandler(kSourceTag, source_pos, canon_url, import_map); | 3623 CallLibraryTagHandler(kSourceTag, source_pos, canon_url); |
3630 } | 3624 } |
3631 } | 3625 } |
3632 | 3626 |
3633 | 3627 |
3634 void Parser::ParseLibraryResource() { | 3628 void Parser::ParseLibraryResource() { |
3635 TRACE_PARSER("ParseLibraryResource"); | 3629 TRACE_PARSER("ParseLibraryResource"); |
3636 while (CurrentToken() == Token::kRESOURCE) { | 3630 while (CurrentToken() == Token::kRESOURCE) { |
3637 // Currently the VM does ignore #resource library tags. They are only used | 3631 // Currently the VM does ignore #resource library tags. They are only used |
3638 // by the IDE. | 3632 // by the IDE. |
3639 ConsumeToken(); | 3633 ConsumeToken(); |
(...skipping 3549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7189 return var_or_field; | 7183 return var_or_field; |
7190 } | 7184 } |
7191 | 7185 |
7192 | 7186 |
7193 // Resolve variables used in an import string literal. | 7187 // Resolve variables used in an import string literal. |
7194 // If the variable name cannot be resolved issue an error message. | 7188 // If the variable name cannot be resolved issue an error message. |
7195 // Currently we only resolve against the global map which is passed in | 7189 // Currently we only resolve against the global map which is passed in |
7196 // when the script is loaded. | 7190 // when the script is loaded. |
7197 RawString* Parser::ResolveImportVar(intptr_t ident_pos, const String& ident) { | 7191 RawString* Parser::ResolveImportVar(intptr_t ident_pos, const String& ident) { |
7198 TRACE_PARSER("ResolveImportVar"); | 7192 TRACE_PARSER("ResolveImportVar"); |
7199 String& map_name = String::Handle(library_.LookupImportMap(ident)); | 7193 const Array& import_map = |
7200 if (!map_name.IsNull()) { | 7194 Array::Handle(Isolate::Current()->object_store()->import_map()); |
7201 return map_name.raw(); | 7195 if (!import_map.IsNull()) { |
| 7196 intptr_t length = import_map.Length(); |
| 7197 intptr_t index = 0; |
| 7198 String& name = String::Handle(); |
| 7199 while (index < (length - 1)) { |
| 7200 name ^= import_map.At(index); |
| 7201 if (name.Equals(ident)) { |
| 7202 name ^= import_map.At(index + 1); |
| 7203 return name.raw(); |
| 7204 } |
| 7205 index += 2; |
| 7206 } |
7202 } | 7207 } |
7203 ErrorMsg(ident_pos, "import variable '%s' has not been defined", | 7208 ErrorMsg(ident_pos, "import variable '%s' has not been defined", |
7204 ident.ToCString()); | 7209 ident.ToCString()); |
7205 return String::null(); | 7210 return String::null(); |
7206 } | 7211 } |
7207 | 7212 |
7208 | 7213 |
7209 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and | 7214 // Parses type = [ident "."] ident ["<" type { "," type } ">"], then resolve and |
7210 // finalize it according to the given type finalization mode. | 7215 // finalize it according to the given type finalization mode. |
7211 RawAbstractType* Parser::ParseType( | 7216 RawAbstractType* Parser::ParseType( |
(...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8441 void Parser::SkipQualIdent() { | 8446 void Parser::SkipQualIdent() { |
8442 ASSERT(IsIdentifier()); | 8447 ASSERT(IsIdentifier()); |
8443 ConsumeToken(); | 8448 ConsumeToken(); |
8444 if (CurrentToken() == Token::kPERIOD) { | 8449 if (CurrentToken() == Token::kPERIOD) { |
8445 ConsumeToken(); // Consume the kPERIOD token. | 8450 ConsumeToken(); // Consume the kPERIOD token. |
8446 ExpectIdentifier("identifier expected after '.'"); | 8451 ExpectIdentifier("identifier expected after '.'"); |
8447 } | 8452 } |
8448 } | 8453 } |
8449 | 8454 |
8450 } // namespace dart | 8455 } // namespace dart |
OLD | NEW |