Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow commas and semicolons between fields in constant messages #738

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions protobuf-parse/src/pure/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ impl fmt::Display for ProtobufConstant {
impl ProtobufConstantMessage {
pub fn format(&self) -> String {
let mut s = String::new();
write!(s, "{{").unwrap();
write!(s, "{{ ").unwrap();
for (n, v) in &self.fields {
match v {
ProtobufConstant::Message(m) => write!(s, "{} {}", n, m.format()).unwrap(),
v => write!(s, "{}: {}", n, v.format()).unwrap(),
v => write!(s, "{}: {} ", n, v.format()).unwrap(),
}
}
write!(s, "}}").unwrap();
Expand Down
35 changes: 35 additions & 0 deletions protobuf-parse/src/pure/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ impl<'a> Parser<'a> {
while !self.tokenizer.lookahead_is_symbol('}')? {
let n = self.next_message_constant_field_name()?;
let v = self.next_field_value()?;

// Consume the comma or semicolon if present. Commas and semicolons
// between message fields are optional, all these are valid:
//
// {foo: 1,bar: 2,baz: 3,}
// {foo: 1;bar: 2;baz: 3;}
// {foo: 1 bar: 2 baz: 3}
// {foo: 1,bar: 2;baz: 3}
// {foo: 1,bar: 2 baz: 3}
//
self.tokenizer.next_symbol_if_in(&[',', ';'])?;

r.fields.insert(n, v);
}
self.tokenizer
Expand Down Expand Up @@ -1336,6 +1348,29 @@ mod test {
assert_eq!("10", mess.t.options[0].value.format());
}

#[test]
fn test_field_options() {
let msg = r#" (my_opt).my_field = {foo: 1 bar: 2} "#;
let opt = parse(msg, |p| p.next_field_option());
assert_eq!(r#"{ foo: 1 bar: 2 }"#, opt.value.format());

let msg = r#" (my_opt).my_field = {foo: 1; bar:2;} "#;
let opt = parse(msg, |p| p.next_field_option());
assert_eq!(r#"{ foo: 1 bar: 2 }"#, opt.value.format());

let msg = r#" (my_opt).my_field = {foo: 1, bar: 2} "#;
let opt = parse(msg, |p| p.next_field_option());
assert_eq!(r#"{ foo: 1 bar: 2 }"#, opt.value.format());

let msg = r#" (my_opt).my_field = "foo" "#;
let opt = parse(msg, |p| p.next_field_option());
assert_eq!(r#""foo""#, opt.value.format());

let msg = r#" (my_opt) = { my_field: "foo"} "#;
let opt = parse(msg, |p| p.next_field_option());
assert_eq!(r#"{ my_field: "foo" }"#, opt.value.format());
}

#[test]
fn test_message() {
let msg = r#"message ReferenceData
Expand Down
13 changes: 9 additions & 4 deletions protobuf-support/src/lexer/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,16 @@ impl<'a> Tokenizer<'a> {
Ok(())
}

pub fn next_symbol_if_eq(&mut self, symbol: char) -> TokenizerResult<bool> {
Ok(self.next_token_if(|token| match token {
&Token::Symbol(c) if c == symbol => true,
pub fn next_symbol_if_in(&mut self, symbols: &[char]) -> TokenizerResult<bool> {
self.next_token_if(|token| match token {
Token::Symbol(c) if symbols.contains(c) => true,
_ => false,
})? != None)
})
.map(|token| token.is_some())
}

pub fn next_symbol_if_eq(&mut self, symbol: char) -> TokenizerResult<bool> {
self.next_symbol_if_in(&[symbol])
}

pub fn next_symbol_expect_eq(
Expand Down
Loading