bnf_rules/
lib.rs

1pub extern crate bnf_rules_macro;
2pub extern crate bnf_rules_parser;
3
4#[cfg(test)]
5mod tests {
6    mod test_expr {
7        mod bnf_rules {
8            pub extern crate bnf_rules_macro;
9            pub extern crate bnf_rules_parser;
10        }
11
12        crate::bnf_rules_macro::bnf_rules!(
13            #[generate_code = true]
14
15            source   ::= [ expr ]
16            expr     ::= factor { "+" factor }
17            factor   ::= "-" primary | primary
18            primary  ::= "(" expr ")" | number
19            number   ::= r"\d+" // regex
20        );
21
22        #[test]
23        fn test() {
24            // A function named "parse_source" is automatically generated.
25            let node: ASTNode = parse_source("10 + (200 + -10)").unwrap();
26            dbg!(&node);
27
28            assert_eq!(
29                node,
30                NonTerminal {
31                    internal_symbol_id: None,
32                    symbol_name: "source".to_string(),
33                    children: [NonTerminal {
34                        internal_symbol_id: None,
35                        symbol_name: "expr".to_string(),
36                        children: [
37                            NonTerminal {
38                                internal_symbol_id: None,
39                                symbol_name: "factor".to_string(),
40                                children: [NonTerminal {
41                                    internal_symbol_id: None,
42                                    symbol_name: "primary".to_string(),
43                                    children: [NonTerminal {
44                                        internal_symbol_id: None,
45                                        symbol_name: "number".to_string(),
46                                        children: [Terminal {
47                                            internal_symbol_id: None,
48                                            text: "10".to_string(),
49                                            position: TokenPosition {
50                                                start_position: 0,
51                                                text_length: 2,
52                                                line: 1,
53                                                column: 1,
54                                            },
55                                        },]
56                                        .to_vec(),
57                                        position: TokenPosition {
58                                            start_position: 0,
59                                            text_length: 2,
60                                            line: 1,
61                                            column: 1,
62                                        },
63                                    },]
64                                    .to_vec(),
65                                    position: TokenPosition {
66                                        start_position: 0,
67                                        text_length: 2,
68                                        line: 1,
69                                        column: 1,
70                                    },
71                                },]
72                                .to_vec(),
73                                position: TokenPosition {
74                                    start_position: 0,
75                                    text_length: 2,
76                                    line: 1,
77                                    column: 1,
78                                },
79                            },
80                            Terminal {
81                                internal_symbol_id: None,
82                                text: "+".to_string(),
83                                position: TokenPosition {
84                                    start_position: 3,
85                                    text_length: 1,
86                                    line: 1,
87                                    column: 4,
88                                },
89                            },
90                            NonTerminal {
91                                internal_symbol_id: None,
92                                symbol_name: "factor".to_string(),
93                                children: [NonTerminal {
94                                    internal_symbol_id: None,
95                                    symbol_name: "primary".to_string(),
96                                    children: [
97                                        Terminal {
98                                            internal_symbol_id: None,
99                                            text: "(".to_string(),
100                                            position: TokenPosition {
101                                                start_position: 5,
102                                                text_length: 1,
103                                                line: 1,
104                                                column: 6,
105                                            },
106                                        },
107                                        NonTerminal {
108                                            internal_symbol_id: None,
109                                            symbol_name: "expr".to_string(),
110                                            children: [
111                                                NonTerminal {
112                                                    internal_symbol_id: None,
113                                                    symbol_name: "factor".to_string(),
114                                                    children: [NonTerminal {
115                                                        internal_symbol_id: None,
116                                                        symbol_name: "primary".to_string(),
117                                                        children: [NonTerminal {
118                                                            internal_symbol_id: None,
119                                                            symbol_name: "number".to_string(),
120                                                            children: [Terminal {
121                                                                internal_symbol_id: None,
122                                                                text: "200".to_string(),
123                                                                position: TokenPosition {
124                                                                    start_position: 6,
125                                                                    text_length: 3,
126                                                                    line: 1,
127                                                                    column: 7,
128                                                                },
129                                                            },]
130                                                            .to_vec(),
131                                                            position: TokenPosition {
132                                                                start_position: 6,
133                                                                text_length: 3,
134                                                                line: 1,
135                                                                column: 7,
136                                                            },
137                                                        },]
138                                                        .to_vec(),
139                                                        position: TokenPosition {
140                                                            start_position: 6,
141                                                            text_length: 3,
142                                                            line: 1,
143                                                            column: 7,
144                                                        },
145                                                    },]
146                                                    .to_vec(),
147                                                    position: TokenPosition {
148                                                        start_position: 6,
149                                                        text_length: 3,
150                                                        line: 1,
151                                                        column: 7,
152                                                    },
153                                                },
154                                                Terminal {
155                                                    internal_symbol_id: None,
156                                                    text: "+".to_string(),
157                                                    position: TokenPosition {
158                                                        start_position: 10,
159                                                        text_length: 1,
160                                                        line: 1,
161                                                        column: 11,
162                                                    },
163                                                },
164                                                NonTerminal {
165                                                    internal_symbol_id: None,
166                                                    symbol_name: "factor".to_string(),
167                                                    children: [
168                                                        Terminal {
169                                                            internal_symbol_id: None,
170                                                            text: "-".to_string(),
171                                                            position: TokenPosition {
172                                                                start_position: 12,
173                                                                text_length: 1,
174                                                                line: 1,
175                                                                column: 13,
176                                                            },
177                                                        },
178                                                        NonTerminal {
179                                                            internal_symbol_id: None,
180                                                            symbol_name: "primary".to_string(),
181                                                            children: [NonTerminal {
182                                                                internal_symbol_id: None,
183                                                                symbol_name: "number".to_string(),
184                                                                children: [Terminal {
185                                                                    internal_symbol_id: None,
186                                                                    text: "10".to_string(),
187                                                                    position: TokenPosition {
188                                                                        start_position: 13,
189                                                                        text_length: 2,
190                                                                        line: 1,
191                                                                        column: 14,
192                                                                    },
193                                                                },]
194                                                                .to_vec(),
195                                                                position: TokenPosition {
196                                                                    start_position: 13,
197                                                                    text_length: 2,
198                                                                    line: 1,
199                                                                    column: 14,
200                                                                },
201                                                            },]
202                                                            .to_vec(),
203                                                            position: TokenPosition {
204                                                                start_position: 13,
205                                                                text_length: 2,
206                                                                line: 1,
207                                                                column: 14,
208                                                            },
209                                                        },
210                                                    ]
211                                                    .to_vec(),
212                                                    position: TokenPosition {
213                                                        start_position: 12,
214                                                        text_length: 3,
215                                                        line: 1,
216                                                        column: 13,
217                                                    },
218                                                },
219                                            ]
220                                            .to_vec(),
221                                            position: TokenPosition {
222                                                start_position: 6,
223                                                text_length: 9,
224                                                line: 1,
225                                                column: 7,
226                                            },
227                                        },
228                                        Terminal {
229                                            internal_symbol_id: None,
230                                            text: ")".to_string(),
231                                            position: TokenPosition {
232                                                start_position: 15,
233                                                text_length: 1,
234                                                line: 1,
235                                                column: 16,
236                                            },
237                                        },
238                                    ]
239                                    .to_vec(),
240                                    position: TokenPosition {
241                                        start_position: 5,
242                                        text_length: 11,
243                                        line: 1,
244                                        column: 6,
245                                    },
246                                },]
247                                .to_vec(),
248                                position: TokenPosition {
249                                    start_position: 5,
250                                    text_length: 11,
251                                    line: 1,
252                                    column: 6,
253                                },
254                            },
255                        ]
256                        .to_vec(),
257                        position: TokenPosition {
258                            start_position: 0,
259                            text_length: 16,
260                            line: 1,
261                            column: 1,
262                        },
263                    },]
264                    .to_vec(),
265                    position: TokenPosition {
266                        start_position: 0,
267                        text_length: 16,
268                        line: 1,
269                        column: 1,
270                    },
271                }
272            );
273        }
274    }
275
276    mod test_quotes {
277        mod bnf_rules {
278            pub extern crate bnf_rules_macro;
279            pub extern crate bnf_rules_parser;
280        }
281
282        crate::bnf_rules_macro::bnf_rules!(
283            source       ::= quotedstring
284            quotedstring ::= r#""[^"]*""#
285        );
286
287        #[test]
288        fn test() {
289            let node = parse_source(r#""Hello, world!""#).unwrap();
290            dbg!(node);
291        }
292    }
293}