prism_parser/grammar/
mod.rs

1use crate::grammar::escaped_string::EscapedString;
2use crate::grammar::rule_action::RuleAction;
3use crate::grammar::serde_leak::*;
4use serde::{Deserialize, Serialize};
5
6pub mod action_result;
7pub mod apply_action;
8pub mod escaped_string;
9pub mod from_action_result;
10pub mod rule_action;
11pub mod serde_leak;
12
13#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
14pub struct GrammarFile<'arn, 'grm> {
15    #[serde(borrow, with = "leak_slice")]
16    pub rules: &'arn [Rule<'arn, 'grm>],
17}
18
19#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
20pub struct Rule<'arn, 'grm> {
21    pub name: &'grm str,
22    pub adapt: bool,
23    #[serde(with = "leak_slice")]
24    pub args: &'arn [&'grm str],
25    #[serde(borrow, with = "leak_slice")]
26    pub blocks: &'arn [Block<'arn, 'grm>],
27}
28
29#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
30pub struct Block<'arn, 'grm> {
31    pub name: &'grm str,
32    pub adapt: bool,
33    #[serde(borrow, with = "leak_slice")]
34    pub constructors: &'arn [AnnotatedRuleExpr<'arn, 'grm>],
35}
36
37#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
38pub struct AnnotatedRuleExpr<'arn, 'grm>(
39    #[serde(borrow, with = "leak_slice")] pub &'arn [RuleAnnotation<'grm>],
40    #[serde(borrow)] pub RuleExpr<'arn, 'grm>,
41);
42
43#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
44pub struct CharClass<'arn> {
45    pub neg: bool,
46    #[serde(borrow, with = "leak_slice")]
47    pub ranges: &'arn [(char, char)],
48}
49
50impl CharClass<'_> {
51    pub fn contains(&self, c: char) -> bool {
52        self.ranges.iter().any(|range| range.0 <= c && c <= range.1) ^ self.neg
53    }
54}
55
56#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)]
57pub enum RuleAnnotation<'grm> {
58    #[serde(borrow)]
59    Error(EscapedString<'grm>),
60    DisableLayout,
61    EnableLayout,
62    DisableRecovery,
63    EnableRecovery,
64}
65
66#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
67pub enum RuleExpr<'arn, 'grm> {
68    RunVar(
69        &'grm str,
70        #[serde(with = "leak_slice")] &'arn [RuleExpr<'arn, 'grm>],
71    ),
72    CharClass(CharClass<'arn>),
73    Literal(EscapedString<'grm>),
74    Repeat {
75        #[serde(with = "leak")]
76        expr: &'arn Self,
77        min: u64,
78        max: Option<u64>,
79        #[serde(with = "leak")]
80        delim: &'arn Self,
81    },
82    Sequence(#[serde(with = "leak_slice")] &'arn [RuleExpr<'arn, 'grm>]),
83    Choice(#[serde(with = "leak_slice")] &'arn [RuleExpr<'arn, 'grm>]),
84    NameBind(&'grm str, #[serde(with = "leak")] &'arn Self),
85    Action(#[serde(with = "leak")] &'arn Self, RuleAction<'arn, 'grm>),
86    SliceInput(#[serde(with = "leak")] &'arn Self),
87    PosLookahead(#[serde(with = "leak")] &'arn Self),
88    NegLookahead(#[serde(with = "leak")] &'arn Self),
89    This,
90    Next,
91    AtAdapt(&'grm str, &'grm str),
92    Guid,
93}