Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Unified Diff: src/scanner.cc

Issue 7039037: Create stand-alone json parser (including scanner). (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/scanner.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/scanner.cc
===================================================================
--- src/scanner.cc (revision 7898)
+++ src/scanner.cc (working copy)
@@ -342,244 +342,4 @@
}
-// ----------------------------------------------------------------------------
-// JsonScanner
-
-JsonScanner::JsonScanner(UnicodeCache* unicode_cache)
- : Scanner(unicode_cache) { }
-
-
-void JsonScanner::Initialize(UC16CharacterStream* source) {
- source_ = source;
- Init();
- // Skip initial whitespace.
- SkipJsonWhiteSpace();
- // Preload first token as look-ahead.
- ScanJson();
-}
-
-
-Token::Value JsonScanner::Next() {
- // BUG 1215673: Find a thread safe way to set a stack limit in
- // pre-parse mode. Otherwise, we cannot safely pre-parse from other
- // threads.
- current_ = next_;
- // Check for stack-overflow before returning any tokens.
- ScanJson();
- return current_.token;
-}
-
-
-bool JsonScanner::SkipJsonWhiteSpace() {
- int start_position = source_pos();
- // JSON WhiteSpace is tab, carrige-return, newline and space.
- while (c0_ == ' ' || c0_ == '\n' || c0_ == '\r' || c0_ == '\t') {
- Advance();
- }
- return source_pos() != start_position;
-}
-
-
-void JsonScanner::ScanJson() {
- next_.literal_chars = NULL;
- Token::Value token;
- do {
- // Remember the position of the next token
- next_.location.beg_pos = source_pos();
- switch (c0_) {
- case '\t':
- case '\r':
- case '\n':
- case ' ':
- Advance();
- token = Token::WHITESPACE;
- break;
- case '{':
- Advance();
- token = Token::LBRACE;
- break;
- case '}':
- Advance();
- token = Token::RBRACE;
- break;
- case '[':
- Advance();
- token = Token::LBRACK;
- break;
- case ']':
- Advance();
- token = Token::RBRACK;
- break;
- case ':':
- Advance();
- token = Token::COLON;
- break;
- case ',':
- Advance();
- token = Token::COMMA;
- break;
- case '"':
- token = ScanJsonString();
- break;
- case '-':
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- token = ScanJsonNumber();
- break;
- case 't':
- token = ScanJsonIdentifier("true", Token::TRUE_LITERAL);
- break;
- case 'f':
- token = ScanJsonIdentifier("false", Token::FALSE_LITERAL);
- break;
- case 'n':
- token = ScanJsonIdentifier("null", Token::NULL_LITERAL);
- break;
- default:
- if (c0_ < 0) {
- Advance();
- token = Token::EOS;
- } else {
- Advance();
- token = Select(Token::ILLEGAL);
- }
- }
- } while (token == Token::WHITESPACE);
-
- next_.location.end_pos = source_pos();
- next_.token = token;
-}
-
-
-Token::Value JsonScanner::ScanJsonString() {
- ASSERT_EQ('"', c0_);
- Advance();
- LiteralScope literal(this);
- while (c0_ != '"') {
- // Check for control character (0x00-0x1f) or unterminated string (<0).
- if (c0_ < 0x20) return Token::ILLEGAL;
- if (c0_ != '\\') {
- AddLiteralCharAdvance();
- } else {
- Advance();
- switch (c0_) {
- case '"':
- case '\\':
- case '/':
- AddLiteralChar(c0_);
- break;
- case 'b':
- AddLiteralChar('\x08');
- break;
- case 'f':
- AddLiteralChar('\x0c');
- break;
- case 'n':
- AddLiteralChar('\x0a');
- break;
- case 'r':
- AddLiteralChar('\x0d');
- break;
- case 't':
- AddLiteralChar('\x09');
- break;
- case 'u': {
- uc32 value = 0;
- for (int i = 0; i < 4; i++) {
- Advance();
- int digit = HexValue(c0_);
- if (digit < 0) {
- return Token::ILLEGAL;
- }
- value = value * 16 + digit;
- }
- AddLiteralChar(value);
- break;
- }
- default:
- return Token::ILLEGAL;
- }
- Advance();
- }
- }
- literal.Complete();
- Advance();
- return Token::STRING;
-}
-
-
-Token::Value JsonScanner::ScanJsonNumber() {
- LiteralScope literal(this);
- bool negative = false;
-
- if (c0_ == '-') {
- AddLiteralCharAdvance();
- negative = true;
- }
- if (c0_ == '0') {
- AddLiteralCharAdvance();
- // Prefix zero is only allowed if it's the only digit before
- // a decimal point or exponent.
- if ('0' <= c0_ && c0_ <= '9') return Token::ILLEGAL;
- } else {
- int i = 0;
- int digits = 0;
- if (c0_ < '1' || c0_ > '9') return Token::ILLEGAL;
- do {
- i = i * 10 + c0_ - '0';
- digits++;
- AddLiteralCharAdvance();
- } while (c0_ >= '0' && c0_ <= '9');
- if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) {
- number_ = (negative ? -i : i);
- return Token::NUMBER;
- }
- }
- if (c0_ == '.') {
- AddLiteralCharAdvance();
- if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL;
- do {
- AddLiteralCharAdvance();
- } while (c0_ >= '0' && c0_ <= '9');
- }
- if (AsciiAlphaToLower(c0_) == 'e') {
- AddLiteralCharAdvance();
- if (c0_ == '-' || c0_ == '+') AddLiteralCharAdvance();
- if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL;
- do {
- AddLiteralCharAdvance();
- } while (c0_ >= '0' && c0_ <= '9');
- }
- literal.Complete();
- ASSERT_NOT_NULL(next_.literal_chars);
- number_ = StringToDouble(unicode_cache_,
- next_.literal_chars->ascii_literal(),
- NO_FLAGS, // Hex, octal or trailing junk.
- OS::nan_value());
- return Token::NUMBER;
-}
-
-
-Token::Value JsonScanner::ScanJsonIdentifier(const char* text,
- Token::Value token) {
- LiteralScope literal(this);
- while (*text != '\0') {
- if (c0_ != *text) return Token::ILLEGAL;
- Advance();
- text++;
- }
- if (unicode_cache_->IsIdentifierPart(c0_)) return Token::ILLEGAL;
- literal.Complete();
- return token;
-}
-
-
} } // namespace v8::internal
« no previous file with comments | « src/scanner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698