首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在pest.rs中,pest-ast板条箱,我如何导出一个枚举字段?

在pest.rs中,pest-ast板条箱,我如何导出一个枚举字段?
EN

Stack Overflow用户
提问于 2021-05-03 08:15:01
回答 1查看 178关注 0票数 0

我有一个Pest语法示例:

代码语言:javascript
复制
WHITESPACE = _{ " " }
identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
int_literal = { DECIMAL_NUMBER+ }

assignment_op = { ":=" }
formula = { (identifier ~ assignment_op ~ int_literal) | int_literal }

file = { formula ~ EOI }

病虫害派生出:

代码语言:javascript
复制
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的恐慌。

  • pest甚至支持用字段派生枚举变体吗?
  • 我可以将枚举变体与括号内的()终端组相匹配吗?
  • 我可以跳过一些终端机吗?如果我最终得到了一个:=,我不需要确切地知道AssignmentExpression { lvalue, rvalue }是被使用的。
EN

回答 1

Stack Overflow用户

发布于 2021-05-08 21:08:20

我在害虫-最后一期#8中找到了一个例子。语法规则:

代码语言:javascript
复制
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 ~ ","? }

对应代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
#[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规则生成的原始语法树:

代码语言:javascript
复制
#[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

代码语言:javascript
复制
#[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与害虫产生的树一起工作。为了跳过某件事,在源语法中使它成为一条沉默的规则。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67365593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档