Home > Mobile >  Rust pest parser fail
Rust pest parser fail

Time:08-11

I'm learning pest parser written in Rust and I need to parse a file:

{ISomething as Something, IOther as Other}

I wrote a rule:

LBrace = { "{" }
RBrace = { "}" }
Comma = { "," }
As = { "as" }
symbolAliases = { LBrace ~ importAliases ~ ( Comma ~ importAliases )* ~ RBrace }
importAliases = { Identifier ~ (As ~ Identifier)? }
Identifier = { IdentifierStart ~ IdentifierPart* }
IdentifierStart = _{ 'a'..'z' | 'A'..'Z' | "$" | "_" }
IdentifierPart = _{ 'a'..'z' | 'A'..'Z' | '0'..'9' | "$" | "_" }

But the parser throws an error:

thread 'main' panicked at 'unsuccessful parse: Error { variant: ParsingError { positives: [As, RBrace, Comma], negatives: [] }, location: Pos(11), line_col: Pos((1, 12)), path: None, line: "{ISomething as Something, IOther as Other}", continued_line: None }', src\main.rs:18:10
stack backtrace:

Help me figure out what the problem is.

CodePudding user response:

You're not parsing any whitespace, that's your problem. It's expecting something like {ISomethingasSomething,IOtherasOther}. You can add a whitespace rule and add it in sensible places:

LBrace = { "{" }
RBrace = { "}" }
Comma = { "," }
As = { ws  ~ "as" ~ ws  }
//     ^^^^^------^^^^^
// an "as" token should probably be surrounded by whitespace to make sense

symbolAliases = { LBrace ~ importAliases ~ ( Comma ~ importAliases )* ~ RBrace }
importAliases = { ws* ~ Identifier ~ (As ~ Identifier)? }
//                ^^^^^

Identifier = { IdentifierStart ~ IdentifierPart* }
IdentifierStart = _{ 'a'..'z' | 'A'..'Z' | "$" | "_" }
IdentifierPart = _{ 'a'..'z' | 'A'..'Z' | '0'..'9' | "$" | "_" }

// whitespace rule defined here (probably want to add tabs and so on, too):
ws = _{ " " }

That's the least amount to make it parse, but you'll probably want it around braces and add some more fine-grained rules.

OR: you can add them implicitly, depending on your needs.

  • Related