1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #pragma once #include<string> #include"Error.h" #include<iostream>
namespace Cliu {
namespace json {
class Scanner { public: Scanner(const std::string& source) : source_(source), current_(0) {}
Scanner(std::string&& source) : source_(std::move(source)), current_(0) {} enum class JsonTokenType { BEGIN_OBJECT, END_OBJECT,
VALUE_SEPARATOR, NAME_SEPARATOR,
VALUE_STRING, VALUE_NUMBER,
LITERAL_TRUE, LITERAL_FALSE, LITERAL_NULL,
BEGIN_ARRAY, END_ARRAY,
END_OF_SOURCE,
ERROR
};
JsonTokenType Scan();
void Rollback();
friend std::ostream& operator<<(std::ostream& os, const JsonTokenType& type) { switch (type) { case JsonTokenType::BEGIN_ARRAY: os << "["; break; case JsonTokenType::END_ARRAY: os << "]"; break; case JsonTokenType::BEGIN_OBJECT: os << "{"; break; case JsonTokenType::END_OBJECT: os << "}"; break; case JsonTokenType::NAME_SEPARATOR: os << ":"; break; case JsonTokenType::VALUE_SEPARATOR: os << ","; break; case JsonTokenType::VALUE_NUMBER: os << "number"; break; case JsonTokenType::VALUE_STRING: os << "string"; break; case JsonTokenType::LITERAL_TRUE: os << "true"; break; case JsonTokenType::LITERAL_FALSE: os << "false"; break; case JsonTokenType::LITERAL_NULL: os << "null"; break; case JsonTokenType::END_OF_SOURCE: os << "EOF"; break; default: break; } return os; }
float GetNumberValue() { return value_number_; };
const std::string& GetStringValue() { return value_string_; };
private: bool IsAtEnd(); char Advance(); void ScanTrue(); void ScanFalse(); void ScanNull(); void ScanString(); void ScanNumber();
char Peek(); char PeekNext(); bool IsDigit(char c);
private: std::string source_; size_t current_; size_t prev_pos_;
float value_number_; std::string value_string_;
};
}
}
|