| 24 | { |
| 25 | |
| 26 | struct JSONParser |
| 27 | { |
| 28 | JSONParser (String::CharPointerType text) : startLocation (text), currentLocation (text) {} |
| 29 | |
| 30 | String::CharPointerType startLocation, currentLocation; |
| 31 | |
| 32 | struct ErrorException |
| 33 | { |
| 34 | String message; |
| 35 | int line = 1, column = 1; |
| 36 | |
| 37 | String getDescription() const { return String (line) + ":" + String (column) + ": error: " + message; } |
| 38 | Result getResult() const { return Result::fail (getDescription()); } |
| 39 | }; |
| 40 | |
| 41 | [[noreturn]] void throwError (juce::String message, String::CharPointerType location) |
| 42 | { |
| 43 | ErrorException e; |
| 44 | e.message = std::move (message); |
| 45 | |
| 46 | for (auto i = startLocation; i < location && ! i.isEmpty(); ++i) |
| 47 | { |
| 48 | ++e.column; |
| 49 | if (*i == '\n') { e.column = 1; e.line++; } |
| 50 | } |
| 51 | |
| 52 | throw e; |
| 53 | } |
| 54 | |
| 55 | void skipWhitespace() { currentLocation = currentLocation.findEndOfWhitespace(); } |
| 56 | juce_wchar readChar() { return currentLocation.getAndAdvance(); } |
| 57 | juce_wchar peekChar() const { return *currentLocation; } |
| 58 | bool matchIf (char c) { if (peekChar() == (juce_wchar) c) { ++currentLocation; return true; } return false; } |
| 59 | bool isEOF() const { return peekChar() == 0; } |
| 60 | |
| 61 | bool matchString (const char* t) |
| 62 | { |
| 63 | while (*t != 0) |
| 64 | if (! matchIf (*t++)) |
| 65 | return false; |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | var parseObjectOrArray() |
| 71 | { |
| 72 | skipWhitespace(); |
| 73 | |
| 74 | if (matchIf ('{')) return parseObject(); |
| 75 | if (matchIf ('[')) return parseArray(); |
| 76 | |
| 77 | if (! isEOF()) |
| 78 | throwError ("Expected '{' or '['", currentLocation); |
| 79 | |
| 80 | return {}; |
| 81 | } |
| 82 | |
| 83 | String parseString (const juce_wchar quoteChar) |
no outgoing calls
no test coverage detected