我正在尝试解析如下所示的行
MyTupleComponent str, str使用语法
cname = _{ (ASCII_ALPHANUMERIC | "_")+ }
ints = { "i8" | "i16" | "i32" | "i64" | "i128" | "isize" }
uints = { "u8" | "u16" | "u32" | "u64" | "u128" | "usize" }
strings = { "str" | "String" }
types = { strings | ints | uints }
tuple_component = { cname ~ (types ~ ("," ~ types)?)+ }但最终结果是
Err(Error { variant: ParsingError { positives: [types], negatives: [] }, location: Pos(20), line_col: Pos((1, 21)), path: None, line: "MyTupleComponent str, str", continued_line: None })有人知道为什么规则不能正确匹配吗?
发布于 2021-05-03 15:51:40
你可以走两条路:
正如@ZachThompson指出的,定义WHITESPACE。如果这样做,请将cname设置为原子的,以防止它捕获字母数字和空格。
下面的测试语法看起来不错:
WHITESPACE = _{ " " }
cname = @{ (ASCII_ALPHANUMERIC | "_")+ }
ints = { "i8" | "i16" | "i32" | "i64" | "i128" | "isize" }
uints = { "u8" | "u16" | "u32" | "u64" | "u128" | "usize" }
strings = { "str" | "String" }
types = { strings | ints | uints }
tuple_component = { cname ~ (types ~ ("," ~ types)?)+ }
file = { tuple_component ~ EOI }否则,您可以手动考虑空间。这种方法也可以,但它不能随着语法的增长而扩展。
附注:您是否打算解析像MyTupleComponent str, str str, str这样的表达式,而不使用逗号将第二个元组与第一个元组分开?它目前可以很好地解析。您可能希望将该规则简化为
tuple_component = { cname ~ types ~ ("," ~ types)* }https://stackoverflow.com/questions/67216538
复制相似问题