Merge pull request #3541 from xackus/language_server
* fix json parser crashing on empty input * make implicit cast of tagged unions to enums easier to find in docsmaster
commit
b37c009683
|
@ -2778,6 +2778,7 @@ test "simple union" {
|
|||
This turns the union into a <em>tagged</em> union, which makes it eligible
|
||||
to use with {#link|switch#} expressions. One can use {#link|@TagType#} to
|
||||
obtain the enum type from the union type.
|
||||
Tagged unions implicitly cast to their enum {#link|Implicit Cast: unions and enums#}
|
||||
</p>
|
||||
{#code_begin|test#}
|
||||
const std = @import("std");
|
||||
|
@ -2805,6 +2806,14 @@ test "switch on tagged union" {
|
|||
test "@TagType" {
|
||||
assert(@TagType(ComplexType) == ComplexTypeTag);
|
||||
}
|
||||
|
||||
test "implicit cast to enum" {
|
||||
const c1 = ComplexType{ .Ok = 42 };
|
||||
const c2 = ComplexType.NotOk;
|
||||
|
||||
assert(c1 == .Ok);
|
||||
assert(c2 == .NotOk);
|
||||
}
|
||||
{#code_end#}
|
||||
<p>In order to modify the payload of a tagged union in a switch expression,
|
||||
place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:
|
||||
|
|
|
@ -867,6 +867,8 @@ pub const TokenStream = struct {
|
|||
parser: StreamingParser,
|
||||
token: ?Token,
|
||||
|
||||
pub const Error = StreamingParser.Error || error{UnexpectedEndOfJson};
|
||||
|
||||
pub fn init(slice: []const u8) TokenStream {
|
||||
return TokenStream{
|
||||
.i = 0,
|
||||
|
@ -876,7 +878,7 @@ pub const TokenStream = struct {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn next(self: *TokenStream) !?Token {
|
||||
pub fn next(self: *TokenStream) Error!?Token {
|
||||
if (self.token) |token| {
|
||||
const copy = token;
|
||||
self.token = null;
|
||||
|
@ -896,16 +898,11 @@ pub const TokenStream = struct {
|
|||
}
|
||||
}
|
||||
|
||||
if (self.i > self.slice.len) {
|
||||
try self.parser.feed(' ', &t1, &t2);
|
||||
self.i += 1;
|
||||
|
||||
if (t1) |token| {
|
||||
return token;
|
||||
}
|
||||
if(self.parser.complete){
|
||||
return null;
|
||||
} else {
|
||||
return error.UnexpectedEndOfJson;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1456,3 +1453,9 @@ test "write json then parse it" {
|
|||
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"));
|
||||
}
|
||||
|
||||
test "parsing empty string gives appropriate error" {
|
||||
var p = Parser.init(debug.global_allocator, false);
|
||||
defer p.deinit();
|
||||
testing.expectError(error.UnexpectedEndOfJson, p.parse(""));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue