| 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/object.h" | 5 #include "vm/object.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
| 10 #include "vm/bigint_operations.h" | 10 #include "vm/bigint_operations.h" |
| (...skipping 4575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4586 void TokenStream::SetTokenObjects(const Array& value) const { | 4586 void TokenStream::SetTokenObjects(const Array& value) const { |
| 4587 StorePointer(&raw_ptr()->token_objects_, value.raw()); | 4587 StorePointer(&raw_ptr()->token_objects_, value.raw()); |
| 4588 } | 4588 } |
| 4589 | 4589 |
| 4590 | 4590 |
| 4591 void TokenStream::SetLength(intptr_t value) const { | 4591 void TokenStream::SetLength(intptr_t value) const { |
| 4592 raw_ptr()->length_ = Smi::New(value); | 4592 raw_ptr()->length_ = Smi::New(value); |
| 4593 } | 4593 } |
| 4594 | 4594 |
| 4595 | 4595 |
| 4596 RawString* TokenStream::PrivateKey() const { |
| 4597 return raw_ptr()->private_key_; |
| 4598 } |
| 4599 |
| 4600 |
| 4601 void TokenStream::SetPrivateKey(const String& value) const { |
| 4602 StorePointer(&raw_ptr()->private_key_, value.raw()); |
| 4603 } |
| 4604 |
| 4605 |
| 4596 RawString* TokenStream::GenerateSource() const { | 4606 RawString* TokenStream::GenerateSource() const { |
| 4597 Iterator iterator(*this, 0); | 4607 Iterator iterator(*this, 0); |
| 4598 GrowableObjectArray& literals = | 4608 const GrowableObjectArray& literals = |
| 4599 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); | 4609 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); |
| 4600 String& literal = String::Handle(); | 4610 const String& private_key = String::Handle(PrivateKey()); |
| 4611 intptr_t private_len = private_key.Length(); |
| 4612 |
| 4601 String& blank = String::Handle(String::New(" ")); | 4613 String& blank = String::Handle(String::New(" ")); |
| 4602 String& newline = String::Handle(String::New("\n")); | 4614 String& newline = String::Handle(String::New("\n")); |
| 4603 String& two_newlines = String::Handle(String::New("\n\n")); | 4615 String& two_newlines = String::Handle(String::New("\n\n")); |
| 4604 String& double_quotes = String::Handle(String::New("\"")); | 4616 String& double_quotes = String::Handle(String::New("\"")); |
| 4605 String& dollar = String::Handle(String::New("$")); | 4617 String& dollar = String::Handle(String::New("$")); |
| 4606 String& two_spaces = String::Handle(String::New(" ")); | 4618 String& two_spaces = String::Handle(String::New(" ")); |
| 4619 |
| 4620 Token::Kind curr = iterator.CurrentTokenKind(); |
| 4621 Token::Kind prev = Token::kILLEGAL; |
| 4622 // Handles used in the loop. |
| 4607 Object& obj = Object::Handle(); | 4623 Object& obj = Object::Handle(); |
| 4608 Token::Kind kind = iterator.CurrentTokenKind(); | 4624 String& literal = String::Handle(); |
| 4625 // Current indentation level. |
| 4609 int indent = 0; | 4626 int indent = 0; |
| 4610 while (kind != Token::kEOS) { | 4627 |
| 4628 while (curr != Token::kEOS) { |
| 4629 // Remember current values for this token. |
| 4611 obj = iterator.CurrentToken(); | 4630 obj = iterator.CurrentToken(); |
| 4612 literal = iterator.MakeLiteralToken(obj); | 4631 literal = iterator.MakeLiteralToken(obj); |
| 4613 if (kind == Token::kSTRING) { | 4632 // Advance to be able to use next token kind. |
| 4633 iterator.Advance(); |
| 4634 Token::Kind next = iterator.CurrentTokenKind(); |
| 4635 |
| 4636 // Handle the current token. |
| 4637 if (curr == Token::kSTRING) { |
| 4614 bool escape_quotes = false; | 4638 bool escape_quotes = false; |
| 4615 for (intptr_t i = 0; i < literal.Length(); i++) { | 4639 for (intptr_t i = 0; i < literal.Length(); i++) { |
| 4616 if (literal.CharAt(i) == '"') { | 4640 if (literal.CharAt(i) == '"') { |
| 4617 escape_quotes = true; | 4641 escape_quotes = true; |
| 4618 break; | 4642 break; |
| 4619 } | 4643 } |
| 4620 } | 4644 } |
| 4621 literals.Add(double_quotes); | 4645 if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) { |
| 4646 literals.Add(double_quotes); |
| 4647 } |
| 4622 if (escape_quotes) { | 4648 if (escape_quotes) { |
| 4623 literal = String::EscapeDoubleQuotes(literal); | 4649 literal = String::EscapeDoubleQuotes(literal); |
| 4624 literals.Add(literal); | 4650 literals.Add(literal); |
| 4625 } else { | 4651 } else { |
| 4626 literals.Add(literal); | 4652 literals.Add(literal); |
| 4627 } | 4653 } |
| 4628 literals.Add(double_quotes); | 4654 if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) { |
| 4629 } else if (kind == Token::kINTERPOL_VAR) { | 4655 literals.Add(double_quotes); |
| 4630 literals.Add(double_quotes); | 4656 } |
| 4657 } else if (curr == Token::kINTERPOL_VAR) { |
| 4631 literals.Add(dollar); | 4658 literals.Add(dollar); |
| 4632 literals.Add(literal); | 4659 literals.Add(literal); |
| 4633 literals.Add(double_quotes); | 4660 } else if (curr == Token::kIDENT) { |
| 4634 } else if (kind == Token::kINTERPOL_START) { | 4661 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) { |
| 4635 literals.Add(double_quotes); | 4662 literal = String::SubString(literal, 0, literal.Length() - private_len); |
| 4663 } |
| 4636 literals.Add(literal); | 4664 literals.Add(literal); |
| 4637 } else if (kind == Token::kINTERPOL_END) { | |
| 4638 literals.Add(literal); | |
| 4639 literals.Add(double_quotes); | |
| 4640 } else { | 4665 } else { |
| 4641 literals.Add(literal); | 4666 literals.Add(literal); |
| 4642 } | 4667 } |
| 4643 // Determine the separation text based on this current token. | 4668 // Determine the separation text based on this current token. |
| 4644 const String* separator = NULL; | 4669 const String* separator = NULL; |
| 4645 switch (kind) { | 4670 switch (curr) { |
| 4646 case Token::kLBRACE: | 4671 case Token::kLBRACE: |
| 4647 indent++; | 4672 indent++; |
| 4648 separator = &newline; | 4673 separator = &newline; |
| 4649 break; | 4674 break; |
| 4650 case Token::kRBRACE: | 4675 case Token::kRBRACE: |
| 4651 if (indent == 0) { | 4676 if (indent == 0) { |
| 4652 separator = &two_newlines; | 4677 separator = &two_newlines; |
| 4653 } else { | 4678 } else { |
| 4654 separator = &newline; | 4679 separator = &newline; |
| 4655 } | 4680 } |
| 4656 break; | 4681 break; |
| 4657 case Token::kSEMICOLON: | 4682 case Token::kSEMICOLON: |
| 4658 separator = &newline; | 4683 separator = &newline; |
| 4659 break; | 4684 break; |
| 4660 case Token::kPERIOD: | 4685 case Token::kPERIOD: |
| 4661 case Token::kLPAREN: | 4686 case Token::kLPAREN: |
| 4687 case Token::kLBRACK: |
| 4688 case Token::kTIGHTADD: |
| 4689 case Token::kINTERPOL_VAR: |
| 4690 case Token::kINTERPOL_START: |
| 4691 case Token::kINTERPOL_END: |
| 4662 break; | 4692 break; |
| 4663 default: | 4693 default: |
| 4664 separator = ␣ | 4694 separator = ␣ |
| 4665 break; | 4695 break; |
| 4666 } | 4696 } |
| 4667 // Advance the iterator. | |
| 4668 iterator.Advance(); | |
| 4669 kind = iterator.CurrentTokenKind(); | |
| 4670 // Determine whether the separation text needs to be updated based on the | 4697 // Determine whether the separation text needs to be updated based on the |
| 4671 // next token. | 4698 // next token. |
| 4672 switch (kind) { | 4699 switch (next) { |
| 4673 case Token::kRBRACE: | 4700 case Token::kRBRACE: |
| 4674 indent--; | 4701 indent--; |
| 4675 break; | 4702 break; |
| 4676 case Token::kSEMICOLON: | 4703 case Token::kSEMICOLON: |
| 4677 case Token::kPERIOD: | 4704 case Token::kPERIOD: |
| 4678 case Token::kCOMMA: | 4705 case Token::kCOMMA: |
| 4679 case Token::kLPAREN: | 4706 case Token::kLPAREN: |
| 4680 case Token::kRPAREN: | 4707 case Token::kRPAREN: |
| 4708 case Token::kLBRACK: |
| 4709 case Token::kRBRACK: |
| 4710 case Token::kINTERPOL_VAR: |
| 4711 case Token::kINTERPOL_START: |
| 4712 case Token::kINTERPOL_END: |
| 4681 separator = NULL; | 4713 separator = NULL; |
| 4682 break; | 4714 break; |
| 4715 case Token::kELSE: |
| 4716 separator = ␣ |
| 4683 default: | 4717 default: |
| 4684 // Do nothing. | 4718 // Do nothing. |
| 4685 break; | 4719 break; |
| 4686 } | 4720 } |
| 4721 // Update the few cases where both tokens need to be taken into account. |
| 4722 if (((curr == Token::kIF) || (curr == Token::kFOR)) && |
| 4723 (next == Token::kLPAREN)) { |
| 4724 separator = ␣ |
| 4725 } else if ((curr == Token::kASSIGN) && (next == Token::kLPAREN)) { |
| 4726 separator = & blank; |
| 4727 } else if ((curr == Token::kLBRACE) && (next == Token::kRBRACE)) { |
| 4728 separator = NULL; |
| 4729 } |
| 4687 if (separator != NULL) { | 4730 if (separator != NULL) { |
| 4688 literals.Add(*separator); | 4731 literals.Add(*separator); |
| 4689 if (separator == &newline) { | 4732 if (separator == &newline) { |
| 4690 for (int i = 0; i < indent; i++) { | 4733 for (int i = 0; i < indent; i++) { |
| 4691 literals.Add(two_spaces); | 4734 literals.Add(two_spaces); |
| 4692 } | 4735 } |
| 4693 } | 4736 } |
| 4694 } | 4737 } |
| 4738 // Setup for next iteration. |
| 4739 prev = curr; |
| 4740 curr = next; |
| 4695 } | 4741 } |
| 4696 const Array& source = Array::Handle(Array::MakeArray(literals)); | 4742 const Array& source = Array::Handle(Array::MakeArray(literals)); |
| 4697 return String::ConcatAll(source); | 4743 return String::ConcatAll(source); |
| 4698 } | 4744 } |
| 4699 | 4745 |
| 4700 | 4746 |
| 4701 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const { | 4747 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const { |
| 4702 Iterator iterator(*this, 0); | 4748 Iterator iterator(*this, 0); |
| 4703 intptr_t src_pos = 0; | 4749 intptr_t src_pos = 0; |
| 4704 Token::Kind kind = iterator.CurrentTokenKind(); | 4750 Token::Kind kind = iterator.CurrentTokenKind(); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4872 GrowableArray<intptr_t> literal_table_[kTableSize]; | 4918 GrowableArray<intptr_t> literal_table_[kTableSize]; |
| 4873 const GrowableObjectArray& token_objects_; | 4919 const GrowableObjectArray& token_objects_; |
| 4874 Object& token_obj_; | 4920 Object& token_obj_; |
| 4875 LiteralToken& literal_token_; | 4921 LiteralToken& literal_token_; |
| 4876 String& literal_str_; | 4922 String& literal_str_; |
| 4877 | 4923 |
| 4878 DISALLOW_COPY_AND_ASSIGN(CompressedTokenStreamData); | 4924 DISALLOW_COPY_AND_ASSIGN(CompressedTokenStreamData); |
| 4879 }; | 4925 }; |
| 4880 | 4926 |
| 4881 | 4927 |
| 4882 RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens) { | 4928 RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens, |
| 4929 const String& private_key) { |
| 4883 // Copy the relevant data out of the scanner into a compressed stream of | 4930 // Copy the relevant data out of the scanner into a compressed stream of |
| 4884 // tokens. | 4931 // tokens. |
| 4885 CompressedTokenStreamData data; | 4932 CompressedTokenStreamData data; |
| 4886 intptr_t len = tokens.length(); | 4933 intptr_t len = tokens.length(); |
| 4887 for (intptr_t i = 0; i < len; i++) { | 4934 for (intptr_t i = 0; i < len; i++) { |
| 4888 Scanner::TokenDescriptor token = tokens[i]; | 4935 Scanner::TokenDescriptor token = tokens[i]; |
| 4889 if (token.kind == Token::kIDENT) { // Identifier token. | 4936 if (token.kind == Token::kIDENT) { // Identifier token. |
| 4890 if (FLAG_compiler_stats) { | 4937 if (FLAG_compiler_stats) { |
| 4891 CompilerStats::num_ident_tokens_total += 1; | 4938 CompilerStats::num_ident_tokens_total += 1; |
| 4892 } | 4939 } |
| 4893 data.AddIdentToken(token.literal); | 4940 data.AddIdentToken(token.literal); |
| 4894 } else if (Token::NeedsLiteralToken(token.kind)) { // Literal token. | 4941 } else if (Token::NeedsLiteralToken(token.kind)) { // Literal token. |
| 4895 if (FLAG_compiler_stats) { | 4942 if (FLAG_compiler_stats) { |
| 4896 CompilerStats::num_literal_tokens_total += 1; | 4943 CompilerStats::num_literal_tokens_total += 1; |
| 4897 } | 4944 } |
| 4898 data.AddLiteralToken(token.kind, token.literal); | 4945 data.AddLiteralToken(token.kind, token.literal); |
| 4899 } else { // Keyword, pseudo keyword etc. | 4946 } else { // Keyword, pseudo keyword etc. |
| 4900 ASSERT(token.kind < Token::kNumTokens); | 4947 ASSERT(token.kind < Token::kNumTokens); |
| 4901 data.AddSimpleToken(token.kind); | 4948 data.AddSimpleToken(token.kind); |
| 4902 } | 4949 } |
| 4903 } | 4950 } |
| 4904 if (FLAG_compiler_stats) { | 4951 if (FLAG_compiler_stats) { |
| 4905 CompilerStats::num_tokens_total += len; | 4952 CompilerStats::num_tokens_total += len; |
| 4906 } | 4953 } |
| 4907 data.AddSimpleToken(Token::kEOS); // End of stream. | 4954 data.AddSimpleToken(Token::kEOS); // End of stream. |
| 4908 | 4955 |
| 4909 // Create and setup the token stream object. | 4956 // Create and setup the token stream object. |
| 4910 const TokenStream& result = TokenStream::Handle(New(data.Length())); | 4957 const TokenStream& result = TokenStream::Handle(New(data.Length())); |
| 4958 result.SetPrivateKey(private_key); |
| 4911 { | 4959 { |
| 4912 NoGCScope no_gc; | 4960 NoGCScope no_gc; |
| 4913 memmove(result.EntryAddr(0), data.GetStream(), data.Length()); | 4961 memmove(result.EntryAddr(0), data.GetStream(), data.Length()); |
| 4914 const Array& tokens = Array::Handle(Array::MakeArray(data.TokenObjects())); | 4962 const Array& tokens = Array::Handle(Array::MakeArray(data.TokenObjects())); |
| 4915 result.SetTokenObjects(tokens); | 4963 result.SetTokenObjects(tokens); |
| 4916 } | 4964 } |
| 4917 return result.raw(); | 4965 return result.raw(); |
| 4918 } | 4966 } |
| 4919 | 4967 |
| 4920 | 4968 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5104 const TokenStream& tkns = TokenStream::Handle(tokens()); | 5152 const TokenStream& tkns = TokenStream::Handle(tokens()); |
| 5105 if (!tkns.IsNull()) { | 5153 if (!tkns.IsNull()) { |
| 5106 // Already tokenized. | 5154 // Already tokenized. |
| 5107 return; | 5155 return; |
| 5108 } | 5156 } |
| 5109 | 5157 |
| 5110 // Get the source, scan and allocate the token stream. | 5158 // Get the source, scan and allocate the token stream. |
| 5111 TimerScope timer(FLAG_compiler_stats, &CompilerStats::scanner_timer); | 5159 TimerScope timer(FLAG_compiler_stats, &CompilerStats::scanner_timer); |
| 5112 const String& src = String::Handle(Source()); | 5160 const String& src = String::Handle(Source()); |
| 5113 Scanner scanner(src, private_key); | 5161 Scanner scanner(src, private_key); |
| 5114 set_tokens(TokenStream::Handle(TokenStream::New(scanner.GetStream()))); | 5162 set_tokens(TokenStream::Handle(TokenStream::New(scanner.GetStream(), |
| 5163 private_key))); |
| 5115 if (FLAG_compiler_stats) { | 5164 if (FLAG_compiler_stats) { |
| 5116 CompilerStats::src_length += src.Length(); | 5165 CompilerStats::src_length += src.Length(); |
| 5117 } | 5166 } |
| 5118 } | 5167 } |
| 5119 | 5168 |
| 5120 | 5169 |
| 5121 void Script::GetTokenLocation(intptr_t token_pos, | 5170 void Script::GetTokenLocation(intptr_t token_pos, |
| 5122 intptr_t* line, | 5171 intptr_t* line, |
| 5123 intptr_t* column) const { | 5172 intptr_t* column) const { |
| 5124 const String& src = String::Handle(Source()); | 5173 const String& src = String::Handle(Source()); |
| (...skipping 5920 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11045 const char* JSRegExp::ToCString() const { | 11094 const char* JSRegExp::ToCString() const { |
| 11046 const String& str = String::Handle(pattern()); | 11095 const String& str = String::Handle(pattern()); |
| 11047 const char* format = "JSRegExp: pattern=%s flags=%s"; | 11096 const char* format = "JSRegExp: pattern=%s flags=%s"; |
| 11048 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); | 11097 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); |
| 11049 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len + 1); | 11098 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 11050 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); | 11099 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); |
| 11051 return chars; | 11100 return chars; |
| 11052 } | 11101 } |
| 11053 | 11102 |
| 11054 } // namespace dart | 11103 } // namespace dart |
| OLD | NEW |