aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event.rs29
-rw-r--r--src/parser/grammar/items/mod.rs82
-rw-r--r--src/parser/input.rs4
-rw-r--r--src/parser/parser.rs16
4 files changed, 103 insertions, 28 deletions
diff --git a/src/parser/event.rs b/src/parser/event.rs
index fd6bdc086..e97350c89 100644
--- a/src/parser/event.rs
+++ b/src/parser/event.rs
@@ -1,4 +1,4 @@
1use {File, FileBuilder, Sink, SyntaxKind, Token}; 1use {File, FileBuilder, Sink, SyntaxKind, TextUnit, Token};
2use syntax_kinds::TOMBSTONE; 2use syntax_kinds::TOMBSTONE;
3use super::is_insignificant; 3use super::is_insignificant;
4 4
@@ -120,19 +120,26 @@ pub(super) fn to_file(text: String, tokens: &[Token], events: Vec<Event>) -> Fil
120 builder.finish_internal() 120 builder.finish_internal()
121 } 121 }
122 &Event::Token { 122 &Event::Token {
123 kind: _, 123 kind,
124 mut n_raw_tokens, 124 mut n_raw_tokens,
125 } => loop { 125 } => {
126 let token = tokens[idx]; 126 // FIXME: currently, we attach whitespace to some random node
127 if !is_insignificant(token.kind) { 127 // this should be done in a sensible manner instead
128 n_raw_tokens -= 1; 128 loop {
129 let token = tokens[idx];
130 if !is_insignificant(token.kind) {
131 break;
132 }
133 builder.leaf(token.kind, token.len);
134 idx += 1
129 } 135 }
130 idx += 1; 136 let mut len = TextUnit::new(0);
131 builder.leaf(token.kind, token.len); 137 for _ in 0..n_raw_tokens {
132 if n_raw_tokens == 0 { 138 len += tokens[idx].len;
133 break; 139 idx += 1;
134 } 140 }
135 }, 141 builder.leaf(kind, len);
142 }
136 &Event::Error { ref message } => builder.error().message(message.clone()).emit(), 143 &Event::Error { ref message } => builder.error().message(message.clone()).emit(),
137 } 144 }
138 } 145 }
diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs
index 3612802e1..37f2ab132 100644
--- a/src/parser/grammar/items/mod.rs
+++ b/src/parser/grammar/items/mod.rs
@@ -81,22 +81,76 @@ fn item(p: &mut Parser) {
81 CONST_ITEM 81 CONST_ITEM
82 } 82 }
83 }, 83 },
84 // TODO: auto trait 84 UNSAFE_KW => {
85 // test unsafe_trait
86 // unsafe trait T {}
87 UNSAFE_KW if la == TRAIT_KW => {
88 p.bump(); 85 p.bump();
89 traits::trait_item(p); 86 let la = p.nth(1);
90 TRAIT_ITEM 87 match p.current() {
91 } 88 // test unsafe_trait
92 // TODO: default impl 89 // unsafe trait T {}
93 // test unsafe_impl 90 TRAIT_KW => {
94 // unsafe impl Foo {} 91 traits::trait_item(p);
95 UNSAFE_KW if la == IMPL_KW => { 92 TRAIT_ITEM
96 p.bump(); 93 }
97 traits::impl_item(p); 94
98 IMPL_ITEM 95 // test unsafe_auto_trait
96 // unsafe auto trait T {}
97 IDENT if p.at_kw("auto") && la == TRAIT_KW => {
98 p.bump_remap(AUTO_KW);
99 traits::trait_item(p);
100 TRAIT_ITEM
101 }
102
103 // test unsafe_impl
104 // unsafe impl Foo {}
105 IMPL_KW => {
106 traits::impl_item(p);
107 IMPL_ITEM
108 }
109
110 // test unsafe_default_impl
111 // unsafe default impl Foo {}
112 IDENT if p.at_kw("default") && la == IMPL_KW => {
113 p.bump_remap(DEFAULT_KW);
114 traits::impl_item(p);
115 IMPL_ITEM
116 }
117
118 // test unsafe_extern_fn
119 // unsafe extern "C" fn foo() {}
120 EXTERN_KW => {
121 abi(p);
122 if !p.at(FN_KW) {
123 item.abandon(p);
124 p.error().message("expected function").emit();
125 return;
126 }
127 fn_item(p);
128 FN_ITEM
129 }
130
131 // test unsafe_fn
132 // unsafe fn foo() {}
133 FN_KW => {
134 fn_item(p);
135 FN_ITEM
136 }
137
138 t => {
139 item.abandon(p);
140 let message = "expected `trait`, `impl` or `fn`";
141
142 // test unsafe_block_in_mod
143 // fn foo(){} unsafe { } fn bar(){}
144 if t == L_CURLY {
145 error_block(p, message);
146 } else {
147 p.error().message(message).emit();
148 }
149 return;
150 }
151 }
99 } 152 }
153
100 MOD_KW => { 154 MOD_KW => {
101 mod_item(p); 155 mod_item(p);
102 MOD_ITEM 156 MOD_ITEM
diff --git a/src/parser/input.rs b/src/parser/input.rs
index 162b9ef5f..4d74ac1b1 100644
--- a/src/parser/input.rs
+++ b/src/parser/input.rs
@@ -46,9 +46,7 @@ impl<'t> ParserInput<'t> {
46 if !(idx < self.tokens.len()) { 46 if !(idx < self.tokens.len()) {
47 return ""; 47 return "";
48 } 48 }
49 let start_offset = self.start_offsets[idx]; 49 let range = TextRange::from_len(self.start_offsets[idx], self.tokens[idx].len);
50 let end_offset = self.tokens[idx].len;
51 let range = TextRange::from_to(start_offset, end_offset);
52 &self.text[range] 50 &self.text[range]
53 } 51 }
54} 52}
diff --git a/src/parser/parser.rs b/src/parser/parser.rs
index bb775c4a5..752d532d0 100644
--- a/src/parser/parser.rs
+++ b/src/parser/parser.rs
@@ -145,10 +145,26 @@ impl<'t> Parser<'t> {
145 }); 145 });
146 } 146 }
147 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
148 pub(crate) fn nth(&self, n: u32) -> SyntaxKind { 160 pub(crate) fn nth(&self, n: u32) -> SyntaxKind {
149 self.inp.kind(self.pos + n) 161 self.inp.kind(self.pos + n)
150 } 162 }
151 163
164 pub(crate) fn at_kw(&self, t: &str) -> bool {
165 self.inp.text(self.pos) == t
166 }
167
152 pub(crate) fn current(&self) -> SyntaxKind { 168 pub(crate) fn current(&self) -> SyntaxKind {
153 self.nth(0) 169 self.nth(0)
154 } 170 }