我有一个Pest语法示例:
WHITESPACE = _{ " " }
identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
int_literal = { DECIMAL_NUMBER+ }
assignment_op = { ":=" }
formula = { (identifier ~ assignment_op ~ int_literal) | int_literal }
file = { formula ~ EOI }病虫害派生出:
extern crate pest_derive;
extern crate from_pest;
extern crate pest_ast;
extern crate pest;
mod parser {
#[derive(Parser)]
#[grammar = "talk/formula.pest"]
pub struct Parser;
}
mod ast {
use super::parser::Rule;
use pest::Span;
fn span_into_str(span: Span) -> &str {
span.as_str()
}
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::int_literal))]
pub struct IntLiteral {
#[pest_ast(outer(with(span_into_str), with(str::parse::<i64>), with(Result::unwrap)))]
pub value: i64
}
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::identifier))]
pub struct Identifier {
#[pest_ast(inner(with(span_into_str), with(String::from)))]
pub value: String
}
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::assignment_op))]
pub struct AssignmentOp {
}
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::formula))]
pub enum Formula {
Assignment {
lvalue: Identifier,
a: AssignmentOp, // can I skip this?
rvalue: IntLiteral,
},
IntLiteral {
rvalue: IntLiteral,
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::ast::*;
use pest::Parser;
use from_pest::FromPest;
#[test]
fn test_formula0() {
let source = "a := 12";
let mut parse_tree = parser::Parser::parse(parser::Rule::formula, source).unwrap();
println!("parse tree = {:#?}", parse_tree);
let syntax_tree: Formula = Formula::from_pest(&mut parse_tree).expect("infallible");
println!("syntax tree = {:#?}", syntax_tree);
}
}运行测试,我会感到infallible: NoMatch的恐慌。
()终端组相匹配吗?:=,我不需要确切地知道AssignmentExpression { lvalue, rvalue }是被使用的。发布于 2021-05-08 21:08:20
我在害虫-最后一期#8中找到了一个例子。语法规则:
seq = { a ~ b ~ c }
choice = { a | b | c }
compund_seq = { a ~ (b | c) }
compound_choice = { (a ~ b) | (b ~ c) }
assign = { (a|b|c) ~ "=" ~ number }
assigns = { (assign ~ ",")* ~ assign ~ ","? }对应代码:
enum choice<'pest>{
struct _1(a<'pest>),
struct _2(b<'pest>),
struct _3(c<'pest>),
}
struct compound_seq<'pest>(
#[pest_ast(outer)] Span<'pest>,
a<'pest>,
enum _2 {
struct _1(b<'pest>),
struct _2(c<'pest>),
},
);
enum compound_choice<'pest>{
struct _1(
#[pest_ast(outer)] Span<'pest>,
a<'pest>,
b<'pest>,
),
struct _2(
#[pest_ast(outer)] Span<'pest>,
b<'pest>,
c<'pest>,
),
}
struct assign<'pest>(
#[pest_ast(outer)] Span<'pest>,
enum _1 {
struct _1(a<'pest>),
struct _2(b<'pest>),
struct _3(c<'pest>),
},
number<'pest>,
);
struct assigns<'pest>(
#[pest_ast(outer)] Span<'pest>,
Vec<struct _1(assign<'pest>)>,
assign<'pest>,
);一旦我知道我在正确的轨道上,我就发现了我的代码中的错误,与所问的问题完全无关。Identifier规则应该使用outer span而不是inner。
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::identifier))]
pub struct Identifier {
#[pest_ast(outer(with(span_into_str), with(String::from)))]
pub value: String
}最有用的调试工具是打印Identifier规则生成的原始语法树:
#[test]
fn test_identifier() {
let source = "foobar";
let mut parse_tree = parser::Parser::parse(parser::Rule::identifier, source).unwrap();
println!("parse tree = {:#?}", parse_tree);
let syntax_tree: Identifier = Identifier::from_pest(&mut parse_tree).expect("infallible");
println!("syntax tree = {:#?}", syntax_tree);
assert_eq!(syntax_tree.value, "foobar".to_string());
}为了让struct编译,我还必须删除enum中的Formula:
#[derive(Debug, FromPest)]
#[pest_ast(rule(Rule::formula))]
pub enum Formula {
Assignment {
lvalue: Identifier,
// a: AssignmentOp,
rvalue: IntLiteral,
},
OrTest {
or_test: IntLiteral,
}
}问题的答案:
pest甚至支持用字段派生枚举变体吗?
是的,上面的例子。
我是否可以将枚举变体与括号内的一组终端进行匹配?
还没有回答。这对我没用。
我可以跳过一些终端机吗?如果我最终得到了一个:= { lvalue,rvalue },我就不需要确切地知道使用了AssignmentExpression。
pest-ast与害虫产生的树一起工作。为了跳过某件事,在源语法中使它成为一条沉默的规则。
https://stackoverflow.com/questions/67365593
复制相似问题