2018-05-01 23:04:40 -07:00
|
|
|
// JSON parser conforming to RFC8259.
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc8259
|
|
|
|
|
2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2018-06-06 08:24:36 -07:00
|
|
|
const debug = std.debug;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2018-05-01 23:04:40 -07:00
|
|
|
const mem = std.mem;
|
2018-10-26 11:59:58 -07:00
|
|
|
const maxInt = std.math.maxInt;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2019-10-10 20:22:18 -07:00
|
|
|
pub const WriteStream = @import("json/write_stream.zig").WriteStream;
|
|
|
|
|
2018-05-01 23:04:40 -07:00
|
|
|
// A single token slice into the parent string.
|
|
|
|
//
|
2018-06-01 09:23:07 -07:00
|
|
|
// Use `token.slice()` on the input at the current position to get the current slice.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Token = struct {
|
2018-05-03 00:16:06 -07:00
|
|
|
id: Id,
|
2018-05-01 23:04:40 -07:00
|
|
|
// How many bytes do we skip before counting
|
|
|
|
offset: u1,
|
|
|
|
// Whether string contains a \uXXXX sequence and cannot be zero-copied
|
2018-05-03 00:16:06 -07:00
|
|
|
string_has_escape: bool,
|
2018-05-01 23:04:40 -07:00
|
|
|
// Whether number is simple and can be represented by an integer (i.e. no `.` or `e`)
|
|
|
|
number_is_integer: bool,
|
|
|
|
// How many bytes from the current position behind the start of this token is.
|
|
|
|
count: usize,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Id = enum {
|
2018-05-03 00:16:06 -07:00
|
|
|
ObjectBegin,
|
|
|
|
ObjectEnd,
|
|
|
|
ArrayBegin,
|
|
|
|
ArrayEnd,
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
Null,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn init(id: Id, count: usize, offset: u1) Token {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Token{
|
2018-05-01 23:04:40 -07:00
|
|
|
.id = id,
|
|
|
|
.offset = offset,
|
2018-05-03 00:16:06 -07:00
|
|
|
.string_has_escape = false,
|
2018-05-01 23:04:40 -07:00
|
|
|
.number_is_integer = true,
|
|
|
|
.count = count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initString(count: usize, has_unicode_escape: bool) Token {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Token{
|
2018-05-03 00:16:06 -07:00
|
|
|
.id = Id.String,
|
2018-05-01 23:04:40 -07:00
|
|
|
.offset = 0,
|
2018-05-03 00:16:06 -07:00
|
|
|
.string_has_escape = has_unicode_escape,
|
2018-05-01 23:04:40 -07:00
|
|
|
.number_is_integer = true,
|
|
|
|
.count = count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initNumber(count: usize, number_is_integer: bool) Token {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Token{
|
2018-05-03 00:16:06 -07:00
|
|
|
.id = Id.Number,
|
2018-05-01 23:04:40 -07:00
|
|
|
.offset = 0,
|
2018-05-03 00:16:06 -07:00
|
|
|
.string_has_escape = false,
|
2018-05-01 23:04:40 -07:00
|
|
|
.number_is_integer = number_is_integer,
|
|
|
|
.count = count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// A marker token is a zero-length
|
2018-05-03 00:16:06 -07:00
|
|
|
pub fn initMarker(id: Id) Token {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Token{
|
2018-05-01 23:04:40 -07:00
|
|
|
.id = id,
|
|
|
|
.offset = 0,
|
2018-05-03 00:16:06 -07:00
|
|
|
.string_has_escape = false,
|
2018-05-01 23:04:40 -07:00
|
|
|
.number_is_integer = true,
|
|
|
|
.count = 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice into the underlying input string.
|
2018-10-15 15:23:47 -07:00
|
|
|
pub fn slice(self: Token, input: []const u8, i: usize) []const u8 {
|
2018-05-30 13:09:11 -07:00
|
|
|
return input[i + self.offset - self.count .. i + self.offset];
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A small streaming JSON parser. This accepts input one byte at a time and returns tokens as
|
|
|
|
// they are encountered. No copies or allocations are performed during parsing and the entire
|
|
|
|
// parsing state requires ~40-50 bytes of stack space.
|
|
|
|
//
|
|
|
|
// Conforms strictly to RFC8529.
|
2018-06-06 08:24:36 -07:00
|
|
|
//
|
|
|
|
// For a non-byte based wrapper, consider using TokenStream instead.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const StreamingParser = struct {
|
2018-05-01 23:04:40 -07:00
|
|
|
// Current state
|
|
|
|
state: State,
|
|
|
|
// How many bytes we have counted for the current token
|
|
|
|
count: usize,
|
|
|
|
// What state to follow after parsing a string (either property or value string)
|
|
|
|
after_string_state: State,
|
|
|
|
// What state to follow after parsing a value (either top-level or value end)
|
|
|
|
after_value_state: State,
|
|
|
|
// If we stopped now, would the complete parsed string to now be a valid json string
|
|
|
|
complete: bool,
|
|
|
|
// Current token flags to pass through to the next generated, see Token.
|
2018-05-03 00:16:06 -07:00
|
|
|
string_has_escape: bool,
|
2018-05-01 23:04:40 -07:00
|
|
|
number_is_integer: bool,
|
|
|
|
|
|
|
|
// Bit-stack for nested object/map literals (max 255 nestings).
|
|
|
|
stack: u256,
|
|
|
|
stack_used: u8,
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
const object_bit = 0;
|
|
|
|
const array_bit = 1;
|
2018-10-26 11:59:58 -07:00
|
|
|
const max_stack_size = maxInt(u8);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn init() StreamingParser {
|
|
|
|
var p: StreamingParser = undefined;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.reset();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn reset(p: *StreamingParser) void {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.TopLevelBegin;
|
|
|
|
p.count = 0;
|
|
|
|
// Set before ever read in main transition function
|
|
|
|
p.after_string_state = undefined;
|
2018-05-09 21:29:49 -07:00
|
|
|
p.after_value_state = State.ValueEnd; // handle end of values normally
|
2018-05-01 23:04:40 -07:00
|
|
|
p.stack = 0;
|
|
|
|
p.stack_used = 0;
|
|
|
|
p.complete = false;
|
2018-05-03 00:16:06 -07:00
|
|
|
p.string_has_escape = false;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = true;
|
|
|
|
}
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const State = enum {
|
2018-05-01 23:04:40 -07:00
|
|
|
// These must be first with these explicit values as we rely on them for indexing the
|
|
|
|
// bit-stack directly and avoiding a branch.
|
|
|
|
ObjectSeparator = 0,
|
|
|
|
ValueEnd = 1,
|
|
|
|
|
|
|
|
TopLevelBegin,
|
|
|
|
TopLevelEnd,
|
|
|
|
|
|
|
|
ValueBegin,
|
|
|
|
ValueBeginNoClosing,
|
|
|
|
|
|
|
|
String,
|
|
|
|
StringUtf8Byte3,
|
|
|
|
StringUtf8Byte2,
|
|
|
|
StringUtf8Byte1,
|
|
|
|
StringEscapeCharacter,
|
|
|
|
StringEscapeHexUnicode4,
|
|
|
|
StringEscapeHexUnicode3,
|
|
|
|
StringEscapeHexUnicode2,
|
|
|
|
StringEscapeHexUnicode1,
|
|
|
|
|
|
|
|
Number,
|
|
|
|
NumberMaybeDotOrExponent,
|
|
|
|
NumberMaybeDigitOrDotOrExponent,
|
|
|
|
NumberFractionalRequired,
|
|
|
|
NumberFractional,
|
|
|
|
NumberMaybeExponent,
|
|
|
|
NumberExponent,
|
|
|
|
NumberExponentDigitsRequired,
|
|
|
|
NumberExponentDigits,
|
|
|
|
|
|
|
|
TrueLiteral1,
|
|
|
|
TrueLiteral2,
|
|
|
|
TrueLiteral3,
|
|
|
|
|
|
|
|
FalseLiteral1,
|
|
|
|
FalseLiteral2,
|
|
|
|
FalseLiteral3,
|
|
|
|
FalseLiteral4,
|
|
|
|
|
|
|
|
NullLiteral1,
|
|
|
|
NullLiteral2,
|
|
|
|
NullLiteral3,
|
|
|
|
|
|
|
|
// Only call this function to generate array/object final state.
|
|
|
|
pub fn fromInt(x: var) State {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.assert(x == 0 or x == 1);
|
2018-05-03 00:16:06 -07:00
|
|
|
const T = @TagType(State);
|
2018-06-19 00:50:38 -07:00
|
|
|
return @intToEnum(State, @intCast(T, x));
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Error = error{
|
2018-05-01 23:04:40 -07:00
|
|
|
InvalidTopLevel,
|
|
|
|
TooManyNestedItems,
|
|
|
|
TooManyClosingItems,
|
|
|
|
InvalidValueBegin,
|
|
|
|
InvalidValueEnd,
|
|
|
|
UnbalancedBrackets,
|
|
|
|
UnbalancedBraces,
|
|
|
|
UnexpectedClosingBracket,
|
|
|
|
UnexpectedClosingBrace,
|
|
|
|
InvalidNumber,
|
|
|
|
InvalidSeparator,
|
|
|
|
InvalidLiteral,
|
|
|
|
InvalidEscapeCharacter,
|
|
|
|
InvalidUnicodeHexSymbol,
|
|
|
|
InvalidUtf8Byte,
|
|
|
|
InvalidTopLevelTrailing,
|
|
|
|
InvalidControlCharacter,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Give another byte to the parser and obtain any new tokens. This may (rarely) return two
|
|
|
|
// tokens. token2 is always null if token1 is null.
|
|
|
|
//
|
|
|
|
// There is currently no error recovery on a bad stream.
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn feed(p: *StreamingParser, c: u8, token1: *?Token, token2: *?Token) Error!void {
|
2018-05-09 21:29:49 -07:00
|
|
|
token1.* = null;
|
|
|
|
token2.* = null;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.count += 1;
|
|
|
|
|
|
|
|
// unlikely
|
|
|
|
if (try p.transition(c, token1)) {
|
|
|
|
_ = try p.transition(c, token2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a single transition on the state machine and return any possible token.
|
2018-06-06 08:24:36 -07:00
|
|
|
fn transition(p: *StreamingParser, c: u8, token: *?Token) Error!bool {
|
2018-05-01 23:04:40 -07:00
|
|
|
switch (p.state) {
|
|
|
|
State.TopLevelBegin => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= object_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ObjectSeparator;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ObjectBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'[' => {
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= array_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ValueEnd;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ArrayBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'-' => {
|
|
|
|
p.number_is_integer = true;
|
|
|
|
p.state = State.Number;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'0' => {
|
|
|
|
p.number_is_integer = true;
|
|
|
|
p.state = State.NumberMaybeDotOrExponent;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'1'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = true;
|
|
|
|
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'"' => {
|
|
|
|
p.state = State.String;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
// We don't actually need the following since after_value_state should override.
|
|
|
|
p.after_string_state = State.ValueEnd;
|
2018-05-03 00:16:06 -07:00
|
|
|
p.string_has_escape = false;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
't' => {
|
|
|
|
p.state = State.TrueLiteral1;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'f' => {
|
|
|
|
p.state = State.FalseLiteral1;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'n' => {
|
|
|
|
p.state = State.NullLiteral1;
|
|
|
|
p.after_value_state = State.TopLevelEnd;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidTopLevel;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.TopLevelEnd => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidTopLevelTrailing;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.ValueBegin => switch (c) {
|
|
|
|
// NOTE: These are shared in ValueEnd as well, think we can reorder states to
|
|
|
|
// be a bit clearer and avoid this duplication.
|
|
|
|
'}' => {
|
|
|
|
// unlikely
|
|
|
|
if (p.stack & 1 != object_bit) {
|
|
|
|
return error.UnexpectedClosingBracket;
|
|
|
|
}
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
return error.TooManyClosingItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.fromInt(p.stack & 1);
|
|
|
|
|
|
|
|
p.stack >>= 1;
|
|
|
|
p.stack_used -= 1;
|
|
|
|
|
|
|
|
switch (p.stack_used) {
|
|
|
|
0 => {
|
|
|
|
p.complete = true;
|
|
|
|
p.state = State.TopLevelEnd;
|
|
|
|
},
|
2018-06-07 22:43:13 -07:00
|
|
|
else => {
|
|
|
|
p.state = State.ValueEnd;
|
|
|
|
},
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ObjectEnd);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
']' => {
|
|
|
|
if (p.stack & 1 != array_bit) {
|
|
|
|
return error.UnexpectedClosingBrace;
|
|
|
|
}
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
return error.TooManyClosingItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.fromInt(p.stack & 1);
|
|
|
|
|
|
|
|
p.stack >>= 1;
|
|
|
|
p.stack_used -= 1;
|
|
|
|
|
|
|
|
switch (p.stack_used) {
|
|
|
|
0 => {
|
|
|
|
p.complete = true;
|
|
|
|
p.state = State.TopLevelEnd;
|
|
|
|
},
|
2018-06-07 22:43:13 -07:00
|
|
|
else => {
|
|
|
|
p.state = State.ValueEnd;
|
|
|
|
},
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ArrayEnd);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'{' => {
|
|
|
|
if (p.stack_used == max_stack_size) {
|
|
|
|
return error.TooManyNestedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= object_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ObjectSeparator;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ObjectBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'[' => {
|
|
|
|
if (p.stack_used == max_stack_size) {
|
|
|
|
return error.TooManyNestedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= array_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ValueEnd;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ArrayBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'-' => {
|
|
|
|
p.state = State.Number;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'0' => {
|
|
|
|
p.state = State.NumberMaybeDotOrExponent;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'1'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'"' => {
|
|
|
|
p.state = State.String;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
't' => {
|
|
|
|
p.state = State.TrueLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'f' => {
|
|
|
|
p.state = State.FalseLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'n' => {
|
|
|
|
p.state = State.NullLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidValueBegin;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: A bit of duplication here and in the following state, redo.
|
|
|
|
State.ValueBeginNoClosing => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
if (p.stack_used == max_stack_size) {
|
|
|
|
return error.TooManyNestedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= object_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ObjectSeparator;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ObjectBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'[' => {
|
|
|
|
if (p.stack_used == max_stack_size) {
|
|
|
|
return error.TooManyNestedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.stack <<= 1;
|
|
|
|
p.stack |= array_bit;
|
|
|
|
p.stack_used += 1;
|
|
|
|
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ValueEnd;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ArrayBegin);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'-' => {
|
|
|
|
p.state = State.Number;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'0' => {
|
|
|
|
p.state = State.NumberMaybeDotOrExponent;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'1'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'"' => {
|
|
|
|
p.state = State.String;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
't' => {
|
|
|
|
p.state = State.TrueLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'f' => {
|
|
|
|
p.state = State.FalseLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
|
|
|
'n' => {
|
|
|
|
p.state = State.NullLiteral1;
|
|
|
|
p.count = 0;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidValueBegin;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.ValueEnd => switch (c) {
|
|
|
|
',' => {
|
|
|
|
p.after_string_state = State.fromInt(p.stack & 1);
|
|
|
|
p.state = State.ValueBeginNoClosing;
|
|
|
|
},
|
|
|
|
']' => {
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
return error.UnbalancedBrackets;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = State.ValueEnd;
|
|
|
|
p.after_string_state = State.fromInt(p.stack & 1);
|
|
|
|
|
|
|
|
p.stack >>= 1;
|
|
|
|
p.stack_used -= 1;
|
|
|
|
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
p.complete = true;
|
|
|
|
p.state = State.TopLevelEnd;
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ArrayEnd);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
return error.UnbalancedBraces;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = State.ValueEnd;
|
|
|
|
p.after_string_state = State.fromInt(p.stack & 1);
|
|
|
|
|
|
|
|
p.stack >>= 1;
|
|
|
|
p.stack_used -= 1;
|
|
|
|
|
|
|
|
if (p.stack_used == 0) {
|
|
|
|
p.complete = true;
|
|
|
|
p.state = State.TopLevelEnd;
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initMarker(Token.Id.ObjectEnd);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidValueEnd;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.ObjectSeparator => switch (c) {
|
|
|
|
':' => {
|
|
|
|
p.state = State.ValueBegin;
|
|
|
|
p.after_string_state = State.ValueEnd;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x09, 0x0A, 0x0D, 0x20 => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// whitespace
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidSeparator;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.String => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
0x00...0x1F => {
|
2018-05-01 23:04:40 -07:00
|
|
|
return error.InvalidControlCharacter;
|
|
|
|
},
|
|
|
|
'"' => {
|
|
|
|
p.state = p.after_string_state;
|
|
|
|
if (p.after_value_state == State.TopLevelEnd) {
|
|
|
|
p.state = State.TopLevelEnd;
|
|
|
|
p.complete = true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initString(p.count - 1, p.string_has_escape);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
'\\' => {
|
|
|
|
p.state = State.StringEscapeCharacter;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0x20, 0x21, 0x23...0x5B, 0x5D...0x7F => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// non-control ascii
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0xC0...0xDF => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringUtf8Byte1;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0xE0...0xEF => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringUtf8Byte2;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
0xF0...0xFF => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringUtf8Byte3;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidUtf8Byte;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringUtf8Byte3 => switch (c >> 6) {
|
|
|
|
0b10 => p.state = State.StringUtf8Byte2,
|
|
|
|
else => return error.InvalidUtf8Byte,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringUtf8Byte2 => switch (c >> 6) {
|
|
|
|
0b10 => p.state = State.StringUtf8Byte1,
|
|
|
|
else => return error.InvalidUtf8Byte,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringUtf8Byte1 => switch (c >> 6) {
|
|
|
|
0b10 => p.state = State.String,
|
|
|
|
else => return error.InvalidUtf8Byte,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringEscapeCharacter => switch (c) {
|
|
|
|
// NOTE: '/' is allowed as an escaped character but it also is allowed
|
|
|
|
// as unescaped according to the RFC. There is a reported errata which suggests
|
|
|
|
// removing the non-escaped variant but it makes more sense to simply disallow
|
|
|
|
// it as an escape code here.
|
|
|
|
//
|
|
|
|
// The current JSONTestSuite tests rely on both of this behaviour being present
|
|
|
|
// however, so we default to the status quo where both are accepted until this
|
|
|
|
// is further clarified.
|
2018-05-28 17:23:55 -07:00
|
|
|
'"', '\\', '/', 'b', 'f', 'n', 'r', 't' => {
|
2018-05-03 00:16:06 -07:00
|
|
|
p.string_has_escape = true;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.String;
|
|
|
|
},
|
|
|
|
'u' => {
|
2018-05-03 00:16:06 -07:00
|
|
|
p.string_has_escape = true;
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringEscapeHexUnicode4;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidEscapeCharacter;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringEscapeHexUnicode4 => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9', 'A'...'F', 'a'...'f' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringEscapeHexUnicode3;
|
|
|
|
},
|
|
|
|
else => return error.InvalidUnicodeHexSymbol,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringEscapeHexUnicode3 => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9', 'A'...'F', 'a'...'f' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringEscapeHexUnicode2;
|
|
|
|
},
|
|
|
|
else => return error.InvalidUnicodeHexSymbol,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringEscapeHexUnicode2 => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9', 'A'...'F', 'a'...'f' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.StringEscapeHexUnicode1;
|
|
|
|
},
|
|
|
|
else => return error.InvalidUnicodeHexSymbol,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.StringEscapeHexUnicode1 => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9', 'A'...'F', 'a'...'f' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.String;
|
|
|
|
},
|
|
|
|
else => return error.InvalidUnicodeHexSymbol,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.Number => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
|
|
|
'0' => {
|
|
|
|
p.state = State.NumberMaybeDotOrExponent;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'1'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidNumber;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberMaybeDotOrExponent => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
|
|
|
'.' => {
|
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberFractionalRequired;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'e', 'E' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberExponent;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
p.state = p.after_value_state;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initNumber(p.count, p.number_is_integer);
|
2018-05-01 23:04:40 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberMaybeDigitOrDotOrExponent => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
|
|
|
'.' => {
|
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberFractionalRequired;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'e', 'E' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberExponent;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// another digit
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
p.state = p.after_value_state;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initNumber(p.count, p.number_is_integer);
|
2018-05-01 23:04:40 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberFractionalRequired => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.NumberFractional;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidNumber;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberFractional => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// another digit
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'e', 'E' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberExponent;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
p.state = p.after_value_state;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initNumber(p.count, p.number_is_integer);
|
2018-05-01 23:04:40 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberMaybeExponent => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'e', 'E' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.number_is_integer = false;
|
|
|
|
p.state = State.NumberExponent;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
p.state = p.after_value_state;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initNumber(p.count, p.number_is_integer);
|
2018-05-01 23:04:40 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberExponent => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'-', '+' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.complete = false;
|
|
|
|
p.state = State.NumberExponentDigitsRequired;
|
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
p.state = State.NumberExponentDigits;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidNumber;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberExponentDigitsRequired => switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
p.state = State.NumberExponentDigits;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidNumber;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NumberExponentDigits => {
|
|
|
|
p.complete = p.after_value_state == State.TopLevelEnd;
|
|
|
|
switch (c) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'0'...'9' => {
|
2018-05-01 23:04:40 -07:00
|
|
|
// another digit
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
p.state = p.after_value_state;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.initNumber(p.count, p.number_is_integer);
|
2018-05-01 23:04:40 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
State.TrueLiteral1 => switch (c) {
|
|
|
|
'r' => p.state = State.TrueLiteral2,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.TrueLiteral2 => switch (c) {
|
|
|
|
'u' => p.state = State.TrueLiteral3,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.TrueLiteral3 => switch (c) {
|
|
|
|
'e' => {
|
|
|
|
p.state = p.after_value_state;
|
|
|
|
p.complete = p.state == State.TopLevelEnd;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.init(Token.Id.True, p.count + 1, 1);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidLiteral;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.FalseLiteral1 => switch (c) {
|
|
|
|
'a' => p.state = State.FalseLiteral2,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.FalseLiteral2 => switch (c) {
|
|
|
|
'l' => p.state = State.FalseLiteral3,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.FalseLiteral3 => switch (c) {
|
|
|
|
's' => p.state = State.FalseLiteral4,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.FalseLiteral4 => switch (c) {
|
|
|
|
'e' => {
|
|
|
|
p.state = p.after_value_state;
|
|
|
|
p.complete = p.state == State.TopLevelEnd;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.init(Token.Id.False, p.count + 1, 1);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidLiteral;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NullLiteral1 => switch (c) {
|
|
|
|
'u' => p.state = State.NullLiteral2,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NullLiteral2 => switch (c) {
|
|
|
|
'l' => p.state = State.NullLiteral3,
|
|
|
|
else => return error.InvalidLiteral,
|
|
|
|
},
|
|
|
|
|
|
|
|
State.NullLiteral3 => switch (c) {
|
|
|
|
'l' => {
|
|
|
|
p.state = p.after_value_state;
|
|
|
|
p.complete = p.state == State.TopLevelEnd;
|
2018-05-09 21:29:49 -07:00
|
|
|
token.* = Token.init(Token.Id.Null, p.count + 1, 1);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.InvalidLiteral;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
// A small wrapper over a StreamingParser for full slices. Returns a stream of json Tokens.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const TokenStream = struct {
|
2018-06-06 08:24:36 -07:00
|
|
|
i: usize,
|
|
|
|
slice: []const u8,
|
|
|
|
parser: StreamingParser,
|
|
|
|
token: ?Token,
|
|
|
|
|
2019-10-27 13:52:28 -07:00
|
|
|
pub const Error = StreamingParser.Error || error{UnexpectedEndOfJson};
|
2019-10-27 02:45:54 -07:00
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn init(slice: []const u8) TokenStream {
|
2018-11-13 05:08:37 -08:00
|
|
|
return TokenStream{
|
2018-06-06 08:24:36 -07:00
|
|
|
.i = 0,
|
|
|
|
.slice = slice,
|
|
|
|
.parser = StreamingParser.init(),
|
|
|
|
.token = null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:52:28 -07:00
|
|
|
pub fn next(self: *TokenStream) Error!?Token {
|
2018-06-06 08:24:36 -07:00
|
|
|
if (self.token) |token| {
|
2019-06-22 15:54:27 -07:00
|
|
|
const copy = token;
|
2018-06-06 08:24:36 -07:00
|
|
|
self.token = null;
|
2019-06-22 15:54:27 -07:00
|
|
|
return copy;
|
2018-06-06 08:24:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var t1: ?Token = undefined;
|
|
|
|
var t2: ?Token = undefined;
|
|
|
|
|
|
|
|
while (self.i < self.slice.len) {
|
|
|
|
try self.parser.feed(self.slice[self.i], &t1, &t2);
|
|
|
|
self.i += 1;
|
|
|
|
|
|
|
|
if (t1) |token| {
|
|
|
|
self.token = t2;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:42:07 -08:00
|
|
|
if (self.parser.complete) {
|
2019-10-27 02:45:54 -07:00
|
|
|
return null;
|
|
|
|
} else {
|
2019-10-27 13:52:28 -07:00
|
|
|
return error.UnexpectedEndOfJson;
|
2018-06-06 08:24:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn checkNext(p: *TokenStream, id: Token.Id) void {
|
2018-06-09 20:42:14 -07:00
|
|
|
const token = (p.next() catch unreachable).?;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.assert(token.id == id);
|
|
|
|
}
|
|
|
|
|
2018-11-19 13:04:17 -08:00
|
|
|
test "json.token" {
|
2018-06-06 08:24:36 -07:00
|
|
|
const s =
|
|
|
|
\\{
|
|
|
|
\\ "Image": {
|
|
|
|
\\ "Width": 800,
|
|
|
|
\\ "Height": 600,
|
|
|
|
\\ "Title": "View from 15th Floor",
|
|
|
|
\\ "Thumbnail": {
|
|
|
|
\\ "Url": "http://www.example.com/image/481989943",
|
|
|
|
\\ "Height": 125,
|
|
|
|
\\ "Width": 100
|
|
|
|
\\ },
|
|
|
|
\\ "Animated" : false,
|
|
|
|
\\ "IDs": [116, 943, 234, 38793]
|
|
|
|
\\ }
|
|
|
|
\\}
|
|
|
|
;
|
|
|
|
|
|
|
|
var p = TokenStream.init(s);
|
|
|
|
|
|
|
|
checkNext(&p, Token.Id.ObjectBegin);
|
|
|
|
checkNext(&p, Token.Id.String); // Image
|
|
|
|
checkNext(&p, Token.Id.ObjectBegin);
|
|
|
|
checkNext(&p, Token.Id.String); // Width
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.String); // Height
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.String); // Title
|
|
|
|
checkNext(&p, Token.Id.String);
|
|
|
|
checkNext(&p, Token.Id.String); // Thumbnail
|
|
|
|
checkNext(&p, Token.Id.ObjectBegin);
|
|
|
|
checkNext(&p, Token.Id.String); // Url
|
|
|
|
checkNext(&p, Token.Id.String);
|
|
|
|
checkNext(&p, Token.Id.String); // Height
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.String); // Width
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.ObjectEnd);
|
|
|
|
checkNext(&p, Token.Id.String); // Animated
|
|
|
|
checkNext(&p, Token.Id.False);
|
|
|
|
checkNext(&p, Token.Id.String); // IDs
|
|
|
|
checkNext(&p, Token.Id.ArrayBegin);
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.Number);
|
|
|
|
checkNext(&p, Token.Id.ArrayEnd);
|
|
|
|
checkNext(&p, Token.Id.ObjectEnd);
|
|
|
|
checkNext(&p, Token.Id.ObjectEnd);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect((try p.next()) == null);
|
2018-06-06 08:24:36 -07:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:04:40 -07:00
|
|
|
// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
|
|
|
|
// be able to decode the string even if this returns true.
|
|
|
|
pub fn validate(s: []const u8) bool {
|
2018-06-06 08:24:36 -07:00
|
|
|
var p = StreamingParser.init();
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
for (s) |c, i| {
|
|
|
|
var token1: ?Token = undefined;
|
|
|
|
var token2: ?Token = undefined;
|
|
|
|
|
2018-06-07 22:43:13 -07:00
|
|
|
p.feed(c, &token1, &token2) catch |err| {
|
2018-05-01 23:04:40 -07:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.complete;
|
|
|
|
}
|
|
|
|
|
2018-11-19 13:04:17 -08:00
|
|
|
test "json.validate" {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(validate("{}"));
|
2018-06-07 22:43:13 -07:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:04:40 -07:00
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const ArenaAllocator = std.heap.ArenaAllocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
2019-09-03 14:53:05 -07:00
|
|
|
const StringHashMap = std.StringHashMap;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const ValueTree = struct {
|
2018-05-01 23:04:40 -07:00
|
|
|
arena: ArenaAllocator,
|
|
|
|
root: Value,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn deinit(self: *ValueTree) void {
|
2018-05-01 23:04:40 -07:00
|
|
|
self.arena.deinit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-03 14:53:05 -07:00
|
|
|
pub const ObjectMap = StringHashMap(Value);
|
2019-10-10 20:22:18 -07:00
|
|
|
pub const Array = ArrayList(Value);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Value = union(enum) {
|
2018-05-01 23:04:40 -07:00
|
|
|
Null,
|
|
|
|
Bool: bool,
|
|
|
|
Integer: i64,
|
|
|
|
Float: f64,
|
|
|
|
String: []const u8,
|
2019-10-10 20:22:18 -07:00
|
|
|
Array: Array,
|
2018-05-01 23:04:40 -07:00
|
|
|
Object: ObjectMap,
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
pub fn dump(self: Value) void {
|
|
|
|
switch (self) {
|
2018-05-01 23:04:40 -07:00
|
|
|
Value.Null => {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("null");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Bool => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Integer => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Float => |inner| {
|
2019-06-30 03:25:03 -07:00
|
|
|
debug.warn("{:.5}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.String => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\"{}\"", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Array => |inner| {
|
|
|
|
var not_first = false;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("[");
|
2018-05-01 23:04:40 -07:00
|
|
|
for (inner.toSliceConst()) |value| {
|
|
|
|
if (not_first) {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn(",");
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
not_first = true;
|
2018-05-03 00:16:06 -07:00
|
|
|
value.dump();
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("]");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Object => |inner| {
|
|
|
|
var not_first = false;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{{");
|
2018-05-01 23:04:40 -07:00
|
|
|
var it = inner.iterator();
|
|
|
|
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
if (not_first) {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn(",");
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
not_first = true;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\"{}\":", entry.key);
|
2018-05-03 00:16:06 -07:00
|
|
|
entry.value.dump();
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("}}");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
pub fn dumpIndent(self: Value, indent: usize) void {
|
2018-05-01 23:04:40 -07:00
|
|
|
if (indent == 0) {
|
2018-05-03 00:16:06 -07:00
|
|
|
self.dump();
|
2018-05-01 23:04:40 -07:00
|
|
|
} else {
|
2018-05-03 00:16:06 -07:00
|
|
|
self.dumpIndentLevel(indent, 0);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
fn dumpIndentLevel(self: Value, indent: usize, level: usize) void {
|
|
|
|
switch (self) {
|
2018-05-01 23:04:40 -07:00
|
|
|
Value.Null => {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("null");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Bool => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Integer => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Float => |inner| {
|
2019-06-30 03:25:03 -07:00
|
|
|
debug.warn("{:.5}", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.String => |inner| {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\"{}\"", inner);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Array => |inner| {
|
|
|
|
var not_first = false;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("[\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
for (inner.toSliceConst()) |value| {
|
|
|
|
if (not_first) {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn(",\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
not_first = true;
|
|
|
|
padSpace(level + indent);
|
2018-05-03 00:16:06 -07:00
|
|
|
value.dumpIndentLevel(indent, level + indent);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
padSpace(level);
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("]");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
Value.Object => |inner| {
|
|
|
|
var not_first = false;
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("{{\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
var it = inner.iterator();
|
|
|
|
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
if (not_first) {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn(",\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
not_first = true;
|
|
|
|
padSpace(level + indent);
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\"{}\": ", entry.key);
|
2018-05-03 00:16:06 -07:00
|
|
|
entry.value.dumpIndentLevel(indent, level + indent);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("\n");
|
2018-05-01 23:04:40 -07:00
|
|
|
padSpace(level);
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn("}}");
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn padSpace(indent: usize) void {
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < indent) : (i += 1) {
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.warn(" ");
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A non-stream JSON parser which constructs a tree of Value's.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Parser = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
allocator: *Allocator,
|
2018-05-01 23:04:40 -07:00
|
|
|
state: State,
|
|
|
|
copy_strings: bool,
|
|
|
|
// Stores parent nodes and un-combined Values.
|
2019-10-10 20:22:18 -07:00
|
|
|
stack: Array,
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const State = enum {
|
2018-05-01 23:04:40 -07:00
|
|
|
ObjectKey,
|
|
|
|
ObjectValue,
|
|
|
|
ArrayValue,
|
|
|
|
Simple,
|
|
|
|
};
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn init(allocator: *Allocator, copy_strings: bool) Parser {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Parser{
|
2018-05-01 23:04:40 -07:00
|
|
|
.allocator = allocator,
|
|
|
|
.state = State.Simple,
|
|
|
|
.copy_strings = copy_strings,
|
2019-10-10 20:22:18 -07:00
|
|
|
.stack = Array.init(allocator),
|
2018-05-01 23:04:40 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn deinit(p: *Parser) void {
|
2018-05-03 00:26:24 -07:00
|
|
|
p.stack.deinit();
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn reset(p: *Parser) void {
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.Simple;
|
2018-05-03 00:26:24 -07:00
|
|
|
p.stack.shrink(0);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
pub fn parse(p: *Parser, input: []const u8) !ValueTree {
|
|
|
|
var s = TokenStream.init(input);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
var arena = ArenaAllocator.init(p.allocator);
|
|
|
|
errdefer arena.deinit();
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
while (try s.next()) |token| {
|
|
|
|
try p.transition(&arena.allocator, input, s.i - 1, token);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:24:36 -07:00
|
|
|
debug.assert(p.stack.len == 1);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
return ValueTree{
|
2018-05-01 23:04:40 -07:00
|
|
|
.arena = arena,
|
2018-05-03 00:26:24 -07:00
|
|
|
.root = p.stack.at(0),
|
2018-05-01 23:04:40 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even though p.allocator exists, we take an explicit allocator so that allocation state
|
|
|
|
// can be cleaned up on error correctly during a `parse` on call.
|
2018-10-15 15:23:47 -07:00
|
|
|
fn transition(p: *Parser, allocator: *Allocator, input: []const u8, i: usize, token: Token) !void {
|
2018-05-01 23:04:40 -07:00
|
|
|
switch (p.state) {
|
|
|
|
State.ObjectKey => switch (token.id) {
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ObjectEnd => {
|
2018-05-03 00:26:24 -07:00
|
|
|
if (p.stack.len == 1) {
|
2018-05-01 23:04:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:26:24 -07:00
|
|
|
var value = p.stack.pop();
|
2018-11-26 21:52:32 -08:00
|
|
|
try p.pushToParent(&value);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.String => {
|
2018-05-03 00:26:24 -07:00
|
|
|
try p.stack.append(try p.parseString(allocator, token, input, i));
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectValue;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
unreachable;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
State.ObjectValue => {
|
2018-05-03 00:26:24 -07:00
|
|
|
var object = &p.stack.items[p.stack.len - 2].Object;
|
|
|
|
var key = p.stack.items[p.stack.len - 1].String;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
switch (token.id) {
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ObjectBegin => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ArrayBegin => {
|
2019-10-10 20:22:18 -07:00
|
|
|
try p.stack.append(Value{ .Array = Array.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ArrayValue;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.String => {
|
2018-05-01 23:04:40 -07:00
|
|
|
_ = try object.put(key, try p.parseString(allocator, token, input, i));
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Number => {
|
2018-05-01 23:04:40 -07:00
|
|
|
_ = try object.put(key, try p.parseNumber(token, input, i));
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.True => {
|
2018-11-13 05:08:37 -08:00
|
|
|
_ = try object.put(key, Value{ .Bool = true });
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.False => {
|
2018-11-13 05:08:37 -08:00
|
|
|
_ = try object.put(key, Value{ .Bool = false });
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Null => {
|
2018-05-01 23:04:40 -07:00
|
|
|
_ = try object.put(key, Value.Null);
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-06-07 22:43:13 -07:00
|
|
|
Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
|
2018-05-01 23:04:40 -07:00
|
|
|
unreachable;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
State.ArrayValue => {
|
2018-05-03 00:26:24 -07:00
|
|
|
var array = &p.stack.items[p.stack.len - 1].Array;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
switch (token.id) {
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ArrayEnd => {
|
2018-05-03 00:26:24 -07:00
|
|
|
if (p.stack.len == 1) {
|
2018-05-01 23:04:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:26:24 -07:00
|
|
|
var value = p.stack.pop();
|
2018-11-26 21:52:32 -08:00
|
|
|
try p.pushToParent(&value);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ObjectBegin => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ArrayBegin => {
|
2019-10-10 20:22:18 -07:00
|
|
|
try p.stack.append(Value{ .Array = Array.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ArrayValue;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.String => {
|
2018-05-01 23:04:40 -07:00
|
|
|
try array.append(try p.parseString(allocator, token, input, i));
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Number => {
|
2018-05-01 23:04:40 -07:00
|
|
|
try array.append(try p.parseNumber(token, input, i));
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.True => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try array.append(Value{ .Bool = true });
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.False => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try array.append(Value{ .Bool = false });
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Null => {
|
2018-05-01 23:04:40 -07:00
|
|
|
try array.append(Value.Null);
|
|
|
|
},
|
2018-06-07 22:43:13 -07:00
|
|
|
Token.Id.ObjectEnd => {
|
2018-05-01 23:04:40 -07:00
|
|
|
unreachable;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
State.Simple => switch (token.id) {
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ObjectBegin => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.ArrayBegin => {
|
2019-10-10 20:22:18 -07:00
|
|
|
try p.stack.append(Value{ .Array = Array.init(allocator) });
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ArrayValue;
|
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.String => {
|
2018-05-03 00:26:24 -07:00
|
|
|
try p.stack.append(try p.parseString(allocator, token, input, i));
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Number => {
|
2018-05-03 00:26:24 -07:00
|
|
|
try p.stack.append(try p.parseNumber(token, input, i));
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.True => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try p.stack.append(Value{ .Bool = true });
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.False => {
|
2018-11-13 05:08:37 -08:00
|
|
|
try p.stack.append(Value{ .Bool = false });
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-03 00:16:06 -07:00
|
|
|
Token.Id.Null => {
|
2018-05-03 00:26:24 -07:00
|
|
|
try p.stack.append(Value.Null);
|
2018-05-01 23:04:40 -07:00
|
|
|
},
|
2018-05-28 17:23:55 -07:00
|
|
|
Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
|
2018-05-01 23:04:40 -07:00
|
|
|
unreachable;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 21:52:32 -08:00
|
|
|
fn pushToParent(p: *Parser, value: *const Value) !void {
|
|
|
|
switch (p.stack.toSlice()[p.stack.len - 1]) {
|
2018-05-01 23:04:40 -07:00
|
|
|
// Object Parent -> [ ..., object, <key>, value ]
|
|
|
|
Value.String => |key| {
|
2018-05-03 00:26:24 -07:00
|
|
|
_ = p.stack.pop();
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-05-03 00:26:24 -07:00
|
|
|
var object = &p.stack.items[p.stack.len - 1].Object;
|
2018-11-26 21:52:32 -08:00
|
|
|
_ = try object.put(key, value.*);
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ObjectKey;
|
|
|
|
},
|
|
|
|
// Array Parent -> [ ..., <array>, value ]
|
2018-11-26 21:52:32 -08:00
|
|
|
Value.Array => |*array| {
|
|
|
|
try array.append(value.*);
|
2018-05-01 23:04:40 -07:00
|
|
|
p.state = State.ArrayValue;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
unreachable;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
fn parseString(p: *Parser, allocator: *Allocator, token: Token, input: []const u8, i: usize) !Value {
|
2018-05-01 23:04:40 -07:00
|
|
|
// TODO: We don't strictly have to copy values which do not contain any escape
|
|
|
|
// characters if flagged with the option.
|
|
|
|
const slice = token.slice(input, i);
|
2019-11-05 16:42:07 -08:00
|
|
|
return Value{ .String = try mem.dupe(allocator, u8, slice) };
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
fn parseNumber(p: *Parser, token: Token, input: []const u8, i: usize) !Value {
|
2018-05-01 23:04:40 -07:00
|
|
|
return if (token.number_is_integer)
|
2018-11-13 05:08:37 -08:00
|
|
|
Value{ .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
|
2018-05-01 23:04:40 -07:00
|
|
|
else
|
2019-02-14 20:32:13 -08:00
|
|
|
Value{ .Float = try std.fmt.parseFloat(f64, token.slice(input, i)) };
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-19 13:04:17 -08:00
|
|
|
test "json.parser.dynamic" {
|
2018-06-06 08:24:36 -07:00
|
|
|
var p = Parser.init(debug.global_allocator, false);
|
2018-05-03 00:26:24 -07:00
|
|
|
defer p.deinit();
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
const s =
|
2018-05-09 21:29:49 -07:00
|
|
|
\\{
|
|
|
|
\\ "Image": {
|
|
|
|
\\ "Width": 800,
|
|
|
|
\\ "Height": 600,
|
|
|
|
\\ "Title": "View from 15th Floor",
|
|
|
|
\\ "Thumbnail": {
|
|
|
|
\\ "Url": "http://www.example.com/image/481989943",
|
|
|
|
\\ "Height": 125,
|
|
|
|
\\ "Width": 100
|
|
|
|
\\ },
|
|
|
|
\\ "Animated" : false,
|
2018-11-18 10:14:37 -08:00
|
|
|
\\ "IDs": [116, 943, 234, 38793],
|
2019-02-13 03:12:00 -08:00
|
|
|
\\ "ArrayOfObject": [{"n": "m"}],
|
|
|
|
\\ "double": 1.3412
|
2018-05-09 21:29:49 -07:00
|
|
|
\\ }
|
|
|
|
\\}
|
|
|
|
;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
|
|
|
var tree = try p.parse(s);
|
2018-05-03 00:26:24 -07:00
|
|
|
defer tree.deinit();
|
|
|
|
|
2018-05-01 23:04:40 -07:00
|
|
|
var root = tree.root;
|
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
var image = root.Object.get("Image").?.value;
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
const width = image.Object.get("Width").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(width.Integer == 800);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
const height = image.Object.get("Height").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(height.Integer == 600);
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
const title = image.Object.get("Title").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, title.String, "View from 15th Floor"));
|
2018-05-01 23:04:40 -07:00
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
const animated = image.Object.get("Animated").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(animated.Bool == false);
|
2018-11-18 10:14:37 -08:00
|
|
|
|
|
|
|
const array_of_object = image.Object.get("ArrayOfObject").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(array_of_object.Array.len == 1);
|
2018-11-18 10:14:37 -08:00
|
|
|
|
|
|
|
const obj0 = array_of_object.Array.at(0).Object.get("n").?.value;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, obj0.String, "m"));
|
2019-02-13 03:12:00 -08:00
|
|
|
|
|
|
|
const double = image.Object.get("double").?.value;
|
|
|
|
testing.expect(double.Float == 1.3412);
|
2018-05-01 23:04:40 -07:00
|
|
|
}
|
2019-04-29 11:02:19 -07:00
|
|
|
|
|
|
|
test "import more json tests" {
|
|
|
|
_ = @import("json/test.zig");
|
2019-10-27 12:49:42 -07:00
|
|
|
_ = @import("json/write_stream.zig");
|
2019-04-29 11:02:19 -07:00
|
|
|
}
|
2019-10-10 20:22:18 -07:00
|
|
|
|
|
|
|
test "write json then parse it" {
|
|
|
|
var out_buffer: [1000]u8 = undefined;
|
|
|
|
|
|
|
|
var slice_out_stream = std.io.SliceOutStream.init(&out_buffer);
|
|
|
|
const out_stream = &slice_out_stream.stream;
|
|
|
|
var jw = WriteStream(@typeOf(out_stream).Child, 4).init(out_stream);
|
|
|
|
|
|
|
|
try jw.beginObject();
|
|
|
|
|
|
|
|
try jw.objectField("f");
|
|
|
|
try jw.emitBool(false);
|
|
|
|
|
|
|
|
try jw.objectField("t");
|
|
|
|
try jw.emitBool(true);
|
|
|
|
|
|
|
|
try jw.objectField("int");
|
|
|
|
try jw.emitNumber(i32(1234));
|
|
|
|
|
|
|
|
try jw.objectField("array");
|
|
|
|
try jw.beginArray();
|
|
|
|
|
|
|
|
try jw.arrayElem();
|
|
|
|
try jw.emitNull();
|
|
|
|
|
|
|
|
try jw.arrayElem();
|
|
|
|
try jw.emitNumber(f64(12.34));
|
|
|
|
|
|
|
|
try jw.endArray();
|
|
|
|
|
|
|
|
try jw.objectField("str");
|
|
|
|
try jw.emitString("hello");
|
|
|
|
|
|
|
|
try jw.endObject();
|
|
|
|
|
|
|
|
var mem_buffer: [1024 * 20]u8 = undefined;
|
|
|
|
const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;
|
|
|
|
var parser = Parser.init(allocator, false);
|
|
|
|
const tree = try parser.parse(slice_out_stream.getWritten());
|
|
|
|
|
|
|
|
testing.expect(tree.root.Object.get("f").?.value.Bool == false);
|
|
|
|
testing.expect(tree.root.Object.get("t").?.value.Bool == true);
|
|
|
|
testing.expect(tree.root.Object.get("int").?.value.Integer == 1234);
|
|
|
|
testing.expect(tree.root.Object.get("array").?.value.Array.at(0).Null == {});
|
|
|
|
testing.expect(tree.root.Object.get("array").?.value.Array.at(1).Float == 12.34);
|
|
|
|
testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
|
|
|
|
}
|
2019-10-27 02:45:54 -07:00
|
|
|
|
2019-10-27 13:41:52 -07:00
|
|
|
test "parsing empty string gives appropriate error" {
|
2019-10-27 02:45:54 -07:00
|
|
|
var p = Parser.init(debug.global_allocator, false);
|
|
|
|
defer p.deinit();
|
2019-10-27 13:52:28 -07:00
|
|
|
testing.expectError(error.UnexpectedEndOfJson, p.parse(""));
|
2019-10-27 02:45:54 -07:00
|
|
|
}
|