aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-07 11:56:08 +0000
committerAleksey Kladov <[email protected]>2018-01-07 11:56:08 +0000
commit9e4052cc2ee12751ba94909ff479bd03df141ac4 (patch)
tree2e7c3a063369c5151fd851910c997e5d1020a164 /src
parent18f9e50b2d1aaf91992be9fd2f2a7e1866a943d3 (diff)
Test utils
Diffstat (limited to 'src')
-rw-r--r--src/parser/event_parser/grammar.rs26
-rw-r--r--src/tree/file_builder.rs9
2 files changed, 26 insertions, 9 deletions
diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs
index 79ef8b31c..e1f717714 100644
--- a/src/parser/event_parser/grammar.rs
+++ b/src/parser/event_parser/grammar.rs
@@ -8,7 +8,12 @@ pub fn file(p: &mut Parser) {
8 node(p, FILE, |p| { 8 node(p, FILE, |p| {
9 shebang(p); 9 shebang(p);
10 inner_attributes(p); 10 inner_attributes(p);
11 many(p, |p| skip_to_first(p, item_first, item)); 11 many(p, |p| {
12 skip_to_first(
13 p, item_first, item,
14 "expected item",
15 )
16 });
12 }) 17 })
13} 18}
14 19
@@ -84,19 +89,34 @@ fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
84} 89}
85 90
86 91
87fn skip_to_first<C, F>(p: &mut Parser, cond: C, f: F) -> bool 92fn skip_to_first<C, F>(p: &mut Parser, cond: C, f: F, message: &str) -> bool
88where 93where
89 C: Fn(&Parser) -> bool, 94 C: Fn(&Parser) -> bool,
90 F: FnOnce(&mut Parser), 95 F: FnOnce(&mut Parser),
91{ 96{
97 let mut skipped = false;
92 loop { 98 loop {
93 if cond(p) { 99 if cond(p) {
100 if skipped {
101 p.finish();
102 }
94 f(p); 103 f(p);
95 return true; 104 return true;
96 } 105 }
97 if p.bump().is_none() { 106 if p.is_eof() {
107 if skipped {
108 p.finish();
109 }
98 return false; 110 return false;
99 } 111 }
112 if !skipped {
113 p.start(ERROR);
114 p.error()
115 .message(message)
116 .emit();
117 }
118 p.bump().unwrap();
119 skipped = true;
100 } 120 }
101} 121}
102 122
diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs
index b07f4027b..37bd5b2c8 100644
--- a/src/tree/file_builder.rs
+++ b/src/tree/file_builder.rs
@@ -73,7 +73,9 @@ impl FileBuilder {
73 pub fn finish(self) -> File { 73 pub fn finish(self) -> File {
74 assert!( 74 assert!(
75 self.in_progress.is_empty(), 75 self.in_progress.is_empty(),
76 "some nodes in FileBuilder are unfinished" 76 "some nodes in FileBuilder are unfinished: {:?}",
77 self.in_progress.iter().map(|&(idx, _)| self.nodes[idx].kind)
78 .collect::<Vec<_>>()
77 ); 79 );
78 assert!( 80 assert!(
79 self.pos == (self.text.len() as u32).into(), 81 self.pos == (self.text.len() as u32).into(),
@@ -122,11 +124,6 @@ impl FileBuilder {
122 let idx = self.current_id(); 124 let idx = self.current_id();
123 &mut self.nodes[idx] 125 &mut self.nodes[idx]
124 } 126 }
125
126 fn current_sibling(&mut self) -> Option<&mut NodeData> {
127 let idx = self.in_progress.last().unwrap().1?;
128 Some(&mut self.nodes[idx])
129 }
130} 127}
131 128
132fn fill<T>(slot: &mut Option<T>, value: T) { 129fn fill<T>(slot: &mut Option<T>, value: T) {