Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef V8_ASMJS_ASM_LEXER_H_ | |
| 6 #define V8_ASMJS_ASM_LEXER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 #include <unordered_map> | |
| 11 | |
| 12 #include "src/asmjs/asm-names.h" | |
| 13 #include "src/base/logging.h" | |
| 14 #include "src/globals.h" | |
| 15 | |
| 16 namespace v8 { | |
| 17 namespace internal { | |
| 18 | |
| 19 class Utf16CharacterStream; | |
| 20 | |
| 21 class AsmJsLexer { | |
|
marja
2017/03/15 12:34:50
This class could use a comment explaining the lang
bradn
2017/03/16 00:21:47
Done.
| |
| 22 public: | |
| 23 typedef int32_t token_t; | |
| 24 | |
| 25 AsmJsLexer(); | |
|
marja
2017/03/15 12:34:50
Also, it's a bit confusing that we have Scanner (n
bradn
2017/03/16 00:21:47
Renamed
| |
| 26 // Pick the stream to parse (must be called before anything else). | |
| 27 void SetStream(std::unique_ptr<Utf16CharacterStream> stream); | |
| 28 | |
| 29 // Get current token. | |
| 30 token_t Token() const { return token_; } | |
| 31 // Advance to the next token. | |
| 32 void Next(); | |
| 33 // Back up by one token (NOTE: Can only be used to back up one, and doesn't | |
| 34 // work with IdentifierString()). | |
| 35 void Rewind(); | |
| 36 // Get raw string for current indentifier. | |
| 37 // NOTE: Doesn't work with Rewind(). | |
|
vogelheim
2017/03/15 12:07:41
I strongly prefer DCHECKs to a comment, as those a
bradn
2017/03/16 00:21:47
Done.
| |
| 38 const std::string& GetIdentifierString() const { return identifier_string_; } | |
| 39 // Check if we just passed a newline (doesn't appear in token stream). | |
| 40 bool IsPrecededByNewline() const { return preceded_by_newline_; } | |
| 41 | |
| 42 #if DEBUG | |
| 43 // Debug only method to go from a token back to its name. | |
| 44 // Slow, only use for debugging. | |
| 45 std::string Name(token_t token) const; | |
| 46 #endif | |
| 47 | |
| 48 // Get current position (to use with Seek). | |
| 49 int GetPosition() const; | |
| 50 // Restores old position (token after that position). | |
| 51 void Seek(int pos); | |
| 52 | |
| 53 // Select whether identifiers are resolved in global or local scope, | |
| 54 // and which scope new identifiers are added to. | |
| 55 void EnterLocalScope() { in_local_scope_ = true; } | |
| 56 void EnterGlobalScope() { in_local_scope_ = false; } | |
| 57 // Drop all current local identifiers. | |
| 58 void ResetLocals(); | |
| 59 | |
| 60 // Methods to check if a token is an identifier and which scope. | |
| 61 bool IsLocal() const { return IsLocal(Token()); } | |
| 62 bool IsGlobal() const { return IsGlobal(Token()); } | |
| 63 static bool IsLocal(token_t token) { return token <= kLocalsStart; } | |
| 64 static bool IsGlobal(token_t token) { return token >= kGlobalsStart; } | |
| 65 // Methods to find the index position of an identifier (count starting from | |
| 66 // 0 for each scope separately). | |
| 67 static size_t LocalIndex(token_t token) { | |
| 68 DCHECK(IsLocal(token)); | |
| 69 return -(token - kLocalsStart); | |
| 70 } | |
| 71 static size_t GlobalIndex(token_t token) { | |
| 72 DCHECK(IsGlobal(token)); | |
| 73 return token - kGlobalsStart; | |
| 74 } | |
| 75 | |
| 76 // Methods to check if the current token is an asm.js "number" (contains a | |
| 77 // dot) or an "unsigned" (a number without a dot). | |
| 78 bool IsUnsigned() const { return Token() == kUnsigned; } | |
| 79 uint64_t AsUnsigned() const { return unsigned_value_; } | |
| 80 bool IsDouble() const { return Token() == kDouble; } | |
| 81 double AsDouble() const { return double_value_; } | |
| 82 | |
| 83 // clang-format off | |
| 84 enum { | |
| 85 // [-10000 .. -10000-kMaxIdentifierCount) :: Local identifiers | |
| 86 // [-10000 .. -1) :: Builtin tokens like keywords | |
| 87 // (also includes some special | |
| 88 // ones like end of input) | |
| 89 // 0 .. 255 :: Single char tokens | |
| 90 // 256 .. 256+kMaxIdentifierCount :: Global identifiers | |
| 91 kLocalsStart = -10000, | |
| 92 #define V(name, _junk1, _junk2, _junk3) kToken_##name, | |
| 93 STDLIB_MATH_FUNCTION_LIST(V) | |
| 94 STDLIB_ARRAY_TYPE_LIST(V) | |
| 95 #undef V | |
| 96 #define V(name) kToken_##name, | |
| 97 STDLIB_OTHER_LIST(V) | |
| 98 STDLIB_MATH_VALUE_LIST(V) | |
| 99 KEYWORD_NAME_LIST(V) | |
| 100 #undef V | |
| 101 #define V(rawname, name) kToken_##name, | |
| 102 LONG_SYMBOL_NAME_LIST(V) | |
| 103 #undef V | |
| 104 | |
| 105 kEndOfInput = -1, | |
| 106 kParseError = -2, | |
| 107 kUnsigned = -3, | |
| 108 kDouble = -4, | |
| 109 kGlobalsStart = 256, | |
| 110 }; | |
| 111 // clang-format on | |
| 112 | |
| 113 private: | |
| 114 std::unique_ptr<Utf16CharacterStream> stream_; | |
| 115 token_t token_; | |
| 116 token_t preceding_token_; | |
| 117 token_t next_token_; | |
| 118 bool rewind_; | |
| 119 std::string identifier_string_; | |
| 120 bool in_local_scope_; | |
| 121 std::unordered_map<std::string, token_t> local_names_; | |
| 122 std::unordered_map<std::string, token_t> global_names_; | |
| 123 std::unordered_map<std::string, token_t> property_names_; | |
| 124 int global_count_; | |
| 125 double double_value_; | |
| 126 uint64_t unsigned_value_; | |
| 127 bool preceded_by_newline_; | |
| 128 | |
| 129 // Consume multiple characters. | |
| 130 void ConsumeIdentifier(uc32 ch); | |
| 131 void ConsumeNumber(uc32 ch); | |
| 132 void ConsumeCComment(); | |
| 133 void ConsumeCPPComment(); | |
| 134 void ConsumeString(uc32 quote); | |
| 135 void ConsumeCompareOrShift(uc32 ch); | |
| 136 | |
| 137 // Classify character categories. | |
| 138 bool IsIdentifierStart(uc32 ch); | |
| 139 bool IsIdentifierPart(uc32 ch); | |
| 140 bool IsNumberStart(uc32 ch); | |
| 141 }; | |
| 142 | |
| 143 } // namespace internal | |
| 144 } // namespace v8 | |
| 145 #endif | |
| OLD | NEW |