Skip to content

Unexpected parsing errors on unquoted strings #60

@andjsrk

Description

@andjsrk

Description

toon-rust/src/decode/parser.rs

Lines 1125 to 1175 in c22f0ae

Token::Integer(i) => {
let val = *i;
self.advance()?;
// If followed by string tokens, treat the whole value as a string
if let Token::String(..) = &self.current_token {
let mut accumulated = val.to_string();
while let Token::String(next, _) = &self.current_token {
accumulated.push(' ');
accumulated.push_str(next);
self.advance()?;
}
Ok(Value::String(accumulated))
} else {
Ok(Number::from(val).into())
}
}
Token::Number(n) => {
let val = *n;
self.advance()?;
// If followed by string tokens, treat the whole value as a string
if let Token::String(..) = &self.current_token {
let mut accumulated = val.to_string();
while let Token::String(next, _) = &self.current_token {
accumulated.push(' ');
accumulated.push_str(next);
self.advance()?;
}
Ok(Value::String(accumulated))
} else if val.is_finite() && val.fract() == 0.0 && val.abs() <= i64::MAX as f64 {
Ok(Number::from(val as i64).into())
} else {
Ok(Number::from_f64(val)
.ok_or_else(|| ToonError::InvalidInput(format!("Invalid number: {val}")))?
.into())
}
}
Token::String(s, _) => {
// Tabular fields can have multiple string tokens joined with spaces
let mut accumulated = s.clone();
self.advance()?;
while let Token::String(next, _) = &self.current_token {
if !accumulated.is_empty() {
accumulated.push(' ');
}
accumulated.push_str(next);
self.advance()?;
}
Ok(Value::String(accumulated))
}

Currently, the parser confuses "tokens that can be part of unquoted strings" and "string tokens", leading to weird parsing error.

Reproduction Steps

  1. Call the decoder with:
    Case 1:
    arr[1]{a}:
      1 null
    
    Case 2:
    arr[1]{a}:
      a 1
    
  2. See error.

Expected Behavior

Case 1: Successfully parsed to { "arr": [{ "a": "1 null" }] } (JSON for convenience)
Case 2: Successfully parsed to { "arr": [{ "a": "a 1" }] } (JSON for convenience)

Actual Behavior

Case 1: Parse error at line 2, column 8: Expected newline after tabular row 1
Case 2: Parse error at line 2, column 5: Expected newline after tabular row 1

Environment

  • Package version: 0.4.4
  • Runtime: Rust 1.96.0 (nightly)
  • OS: Windows 11

This bug is probably environment independent. (I have checked the source code)

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions