aboutsummaryrefslogtreecommitdiff
path: root/src/parser/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/parser.rs')
-rw-r--r--src/parser/parser.rs193
1 files changed, 0 insertions, 193 deletions
diff --git a/src/parser/parser.rs b/src/parser/parser.rs
deleted file mode 100644
index 7c8e47cb6..000000000
--- a/src/parser/parser.rs
+++ /dev/null
@@ -1,193 +0,0 @@
1use super::Event;
2use super::input::{InputPosition, ParserInput};
3use SyntaxKind::{self, EOF, TOMBSTONE};
4
5pub(crate) struct Marker {
6 pos: u32,
7}
8
9impl Marker {
10 pub fn complete(self, p: &mut Parser, kind: SyntaxKind) -> CompleteMarker {
11 match self.event(p) {
12 &mut Event::Start {
13 kind: ref mut slot, ..
14 } => {
15 *slot = kind;
16 }
17 _ => unreachable!(),
18 }
19 p.event(Event::Finish);
20 let result = CompleteMarker { pos: self.pos };
21 ::std::mem::forget(self);
22 result
23 }
24
25 pub fn abandon(self, p: &mut Parser) {
26 let idx = self.pos as usize;
27 if idx == p.events.len() - 1 {
28 match p.events.pop() {
29 Some(Event::Start {
30 kind: TOMBSTONE,
31 forward_parent: None,
32 }) => (),
33 _ => unreachable!(),
34 }
35 }
36 ::std::mem::forget(self);
37 }
38
39 fn event<'p>(&self, p: &'p mut Parser) -> &'p mut Event {
40 &mut p.events[self.idx()]
41 }
42
43 fn idx(&self) -> usize {
44 self.pos as usize
45 }
46}
47
48impl Drop for Marker {
49 fn drop(&mut self) {
50 if !::std::thread::panicking() {
51 panic!("Each marker should be eithe completed or abandoned");
52 }
53 }
54}
55
56pub(crate) struct CompleteMarker {
57 pos: u32,
58}
59
60impl CompleteMarker {
61 pub(crate) fn precede(self, p: &mut Parser) -> Marker {
62 let m = p.start();
63 match p.events[self.pos as usize] {
64 Event::Start {
65 ref mut forward_parent,
66 ..
67 } => {
68 *forward_parent = Some(m.pos - self.pos);
69 }
70 _ => unreachable!(),
71 }
72 m
73 }
74}
75
76pub(crate) struct TokenSet {
77 pub tokens: &'static [SyntaxKind],
78}
79
80impl TokenSet {
81 pub fn contains(&self, kind: SyntaxKind) -> bool {
82 self.tokens.contains(&kind)
83 }
84}
85
86#[macro_export]
87macro_rules! token_set {
88 ($($t:ident),*) => {
89 TokenSet {
90 tokens: &[$($t),*],
91 }
92 };
93
94 ($($t:ident),* ,) => {
95 token_set!($($t),*)
96 };
97}
98
99pub(crate) struct Parser<'t> {
100 inp: &'t ParserInput<'t>,
101
102 pos: InputPosition,
103 events: Vec<Event>,
104}
105
106impl<'t> Parser<'t> {
107 pub(crate) fn new(inp: &'t ParserInput<'t>) -> Parser<'t> {
108 Parser {
109 inp,
110
111 pos: InputPosition::new(),
112 events: Vec::new(),
113 }
114 }
115
116 pub(crate) fn into_events(self) -> Vec<Event> {
117 assert_eq!(self.current(), EOF);
118 self.events
119 }
120
121 pub(crate) fn start(&mut self) -> Marker {
122 let m = Marker {
123 pos: self.events.len() as u32,
124 };
125 self.event(Event::Start {
126 kind: TOMBSTONE,
127 forward_parent: None,
128 });
129 m
130 }
131
132 pub(crate) fn error<'p, T: Into<String>>(&'p mut self, msg: T) -> ErrorBuilder<'p, 't> {
133 ErrorBuilder::new(self, msg.into())
134 }
135
136 pub(crate) fn bump(&mut self) {
137 let kind = self.current();
138 if kind == EOF {
139 return;
140 }
141 self.pos += 1;
142 self.event(Event::Token {
143 kind,
144 n_raw_tokens: 1,
145 });
146 }
147
148 pub(crate) fn bump_remap(&mut self, kind: SyntaxKind) {
149 if self.current() == EOF {
150 // TODO: panic!?
151 return;
152 }
153 self.pos += 1;
154 self.event(Event::Token {
155 kind,
156 n_raw_tokens: 1,
157 });
158 }
159
160 pub(crate) fn nth(&self, n: u32) -> SyntaxKind {
161 self.inp.kind(self.pos + n)
162 }
163
164 pub(crate) fn at_kw(&self, t: &str) -> bool {
165 self.inp.text(self.pos) == t
166 }
167
168 pub(crate) fn current(&self) -> SyntaxKind {
169 self.nth(0)
170 }
171
172 fn event(&mut self, event: Event) {
173 self.events.push(event)
174 }
175}
176
177pub(crate) struct ErrorBuilder<'p, 't: 'p> {
178 message: String,
179 parser: &'p mut Parser<'t>,
180}
181
182impl<'p, 't: 'p> Drop for ErrorBuilder<'p, 't> {
183 fn drop(&mut self) {
184 let message = ::std::mem::replace(&mut self.message, String::new());
185 self.parser.event(Event::Error { message });
186 }
187}
188
189impl<'t, 'p> ErrorBuilder<'p, 't> {
190 fn new(parser: &'p mut Parser<'t>, message: String) -> Self {
191 ErrorBuilder { message, parser }
192 }
193}