1 // pest. The Elegant Parser
2 // Copyright (c) 2018 Dragoș Tiselice
3 //
4 // Licensed under the Apache License, Version 2.0
5 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
6 // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. All files in the project carrying such notice may not be copied,
8 // modified, or distributed except according to those terms.
9 
10 #![cfg_attr(not(feature = "std"), no_std)]
11 extern crate alloc;
12 use alloc::{format, vec::Vec};
13 
14 #[macro_use]
15 extern crate pest;
16 #[macro_use]
17 extern crate pest_derive;
18 
19 #[derive(Parser)]
20 #[grammar = "../tests/lists.pest"]
21 struct ListsParser;
22 
23 #[test]
item()24 fn item() {
25     parses_to! {
26         parser: ListsParser,
27         input: "- a",
28         rule: Rule::lists,
29         tokens: [
30             item(2, 3)
31         ]
32     };
33 }
34 
35 #[test]
items()36 fn items() {
37     parses_to! {
38         parser: ListsParser,
39         input: "- a\n- b",
40         rule: Rule::lists,
41         tokens: [
42             item(2, 3),
43             item(6, 7)
44         ]
45     };
46 }
47 
48 #[test]
children()49 fn children() {
50     parses_to! {
51         parser: ListsParser,
52         input: "  - b",
53         rule: Rule::children,
54         tokens: [
55             children(0, 5, [
56                 item(4, 5)
57             ])
58         ]
59     };
60 }
61 
62 #[test]
nested_item()63 fn nested_item() {
64     parses_to! {
65         parser: ListsParser,
66         input: "- a\n  - b",
67         rule: Rule::lists,
68         tokens: [
69             item(2, 3),
70             children(4, 9, [
71                 item(8, 9)
72             ])
73         ]
74     };
75 }
76 
77 #[test]
nested_items()78 fn nested_items() {
79     parses_to! {
80         parser: ListsParser,
81         input: "- a\n  - b\n  - c",
82         rule: Rule::lists,
83         tokens: [
84             item(2, 3),
85             children(4, 15, [
86                 item(8, 9),
87                 item(14, 15)
88             ])
89         ]
90     };
91 }
92 
93 #[test]
nested_two_levels()94 fn nested_two_levels() {
95     parses_to! {
96         parser: ListsParser,
97         input: "- a\n  - b\n    - c",
98         rule: Rule::lists,
99         tokens: [
100             item(2, 3),
101             children(4, 17, [
102                 item(8, 9),
103                 children(10, 17, [
104                     item(16, 17)
105                 ])
106             ])
107         ]
108     };
109 }
110 
111 #[test]
nested_then_not()112 fn nested_then_not() {
113     parses_to! {
114         parser: ListsParser,
115         input: "- a\n  - b\n- c",
116         rule: Rule::lists,
117         tokens: [
118             item(2, 3),
119             children(4, 9, [
120                 item(8, 9)
121             ]),
122             item(12, 13)
123         ]
124     };
125 }
126