代码拉取完成,页面将自动刷新
rust-peg
is a simple yet flexible parser generator that makes it easy to write robust parsers. Based on the Parsing Expression Grammar formalism, it provides a Rust macro that builds a recursive descent parser from a concise definition of the grammar.
&str
, &[u8]
, &[T]
or custom types implementing traitsrustc
error messages for errors in the grammar definition or the Rust
code embedded within itParse a comma-separated list of numbers surrounded by brackets into a Vec<u32>
:
peg::parser!{
grammar list_parser() for str {
rule number() -> u32
= n:$(['0'..='9']+) {? n.parse().or(Err("u32")) }
pub rule list() -> Vec<u32>
= "[" l:(number() ** ",") "]" { l }
}
}
pub fn main() {
assert_eq!(list_parser::list("[1,1,2,3,5,8]"), Ok(vec![1, 1, 2, 3, 5, 8]));
}
See the tests for more examples
Grammar rule syntax reference in rustdoc
crate | parser type | action code | integration | input type | precedence climbing | parameterized rules | streaming input |
---|---|---|---|---|---|---|---|
peg | PEG | in grammar | proc macro (block) |
&str , &[T] , custom |
Yes | Yes | No |
pest | PEG | external | proc macro (file) | &str |
Yes | No | No |
nom | combinators | in source | library |
&[u8] , custom |
No | Yes | Yes |
lalrpop | LR(1) | in grammar | build script | &str |
No | Yes | No |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。