Skip to content

Commit

Permalink
Fix parsing of multiline string literals for default field values
Browse files Browse the repository at this point in the history
Co-authored-by: Tomáš Drvoštěp <[email protected]>
  • Loading branch information
dourouc05 and Drvi authored Jan 2, 2024
1 parent 97f55ed commit 329b66f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/parsing/proto_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ function _parse_option_value(ps) # TODO: proper value parsing with validation
str_val = val(readtoken(ps))
# C-style string literals spanning multiple lines
if nk == Tokens.STRING_LIT && nnk == Tokens.STRING_LIT
iob = IOBuffer()
write(iob, str_val)
while peekkind(ps) == Tokens.STRING_LIT
str_val = string(@view(str_val[begin:end-1]), val(readtoken(ps)))
seek(iob, position(iob) - 1)
write(iob, @view(val(readtoken(ps))[begin+1:end]))
end
str_val = String(take!(iob))
end
return has_minus ? string("-", str_val) : str_val
end
Expand Down Expand Up @@ -94,4 +98,4 @@ function _parse_option!(ps::ParserState, options::Dict{String,Union{String,Dict{
options[option_name] = _parse_option_value(ps)
end
return nothing
end
end
20 changes: 20 additions & 0 deletions test/test_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ end
@test p.definitions["A"].fields[1].type.reference_type == Parsers.MESSAGE
end

@testset "Single nested non-empty message proto file with default value" begin
s, p, ctx = translate_simple_proto("message A { optional string a = 1 [ default = \"b\" ]; }")

@test haskey(p.definitions, "A")
@test p.definitions["A"] isa Parsers.MessageType
@test p.definitions["A"].fields[1].name == "a"
@test p.definitions["A"].fields[1].type isa Parsers.StringType
@test p.definitions["A"].fields[1].options["default"] == "\"b\""
end

@testset "Single nested non-empty message proto file with default value on several lines" begin
s, p, ctx = translate_simple_proto("message A { optional string a = 1 [ default = \"b;\"\n\t\"\"\n\t\"c\" ]; }")

@test haskey(p.definitions, "A")
@test p.definitions["A"] isa Parsers.MessageType
@test p.definitions["A"].fields[1].name == "a"
@test p.definitions["A"].fields[1].type isa Parsers.StringType
@test p.definitions["A"].fields[1].options["default"] == "\"b;c\""
end

@testset "Single self-referential message proto file" begin
s, p, ctx = translate_simple_proto("message A { optional A a = 1; }")

Expand Down

0 comments on commit 329b66f

Please sign in to comment.