Skip to content

Commit 329b66f

Browse files
dourouc05Drvi
andauthored
Fix parsing of multiline string literals for default field values
Co-authored-by: Tomáš Drvoštěp <tomas.drvostep@gmail.com>
1 parent 97f55ed commit 329b66f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/parsing/proto_options.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ function _parse_option_value(ps) # TODO: proper value parsing with validation
1515
str_val = val(readtoken(ps))
1616
# C-style string literals spanning multiple lines
1717
if nk == Tokens.STRING_LIT && nnk == Tokens.STRING_LIT
18+
iob = IOBuffer()
19+
write(iob, str_val)
1820
while peekkind(ps) == Tokens.STRING_LIT
19-
str_val = string(@view(str_val[begin:end-1]), val(readtoken(ps)))
21+
seek(iob, position(iob) - 1)
22+
write(iob, @view(val(readtoken(ps))[begin+1:end]))
2023
end
24+
str_val = String(take!(iob))
2125
end
2226
return has_minus ? string("-", str_val) : str_val
2327
end
@@ -94,4 +98,4 @@ function _parse_option!(ps::ParserState, options::Dict{String,Union{String,Dict{
9498
options[option_name] = _parse_option_value(ps)
9599
end
96100
return nothing
97-
end
101+
end

test/test_parser.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ end
146146
@test p.definitions["A"].fields[1].type.reference_type == Parsers.MESSAGE
147147
end
148148

149+
@testset "Single nested non-empty message proto file with default value" begin
150+
s, p, ctx = translate_simple_proto("message A { optional string a = 1 [ default = \"b\" ]; }")
151+
152+
@test haskey(p.definitions, "A")
153+
@test p.definitions["A"] isa Parsers.MessageType
154+
@test p.definitions["A"].fields[1].name == "a"
155+
@test p.definitions["A"].fields[1].type isa Parsers.StringType
156+
@test p.definitions["A"].fields[1].options["default"] == "\"b\""
157+
end
158+
159+
@testset "Single nested non-empty message proto file with default value on several lines" begin
160+
s, p, ctx = translate_simple_proto("message A { optional string a = 1 [ default = \"b;\"\n\t\"\"\n\t\"c\" ]; }")
161+
162+
@test haskey(p.definitions, "A")
163+
@test p.definitions["A"] isa Parsers.MessageType
164+
@test p.definitions["A"].fields[1].name == "a"
165+
@test p.definitions["A"].fields[1].type isa Parsers.StringType
166+
@test p.definitions["A"].fields[1].options["default"] == "\"b;c\""
167+
end
168+
149169
@testset "Single self-referential message proto file" begin
150170
s, p, ctx = translate_simple_proto("message A { optional A a = 1; }")
151171

0 commit comments

Comments
 (0)