forked from wavefnd/Wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrivia.rs
More file actions
79 lines (67 loc) · 2.04 KB
/
trivia.rs
File metadata and controls
79 lines (67 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// This file is part of the Wave language project.
// Copyright (c) 2024–2026 Wave Foundation
// Copyright (c) 2024–2026 LunaStev and contributors
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
use super::Lexer;
impl<'a> Lexer<'a> {
pub(crate) fn skip_trivia(&mut self) {
loop {
self.skip_whitespace();
if self.is_at_end() {
break;
}
// line comment //
if self.peek() == '/' && self.peek_next() == '/' {
self.advance(); // '/'
self.advance(); // '/'
self.skip_comment();
continue;
}
// block comment /* */
if self.peek() == '/' && self.peek_next() == '*' {
self.advance(); // '/'
self.advance(); // '*'
self.skip_multiline_comment();
continue;
}
break;
}
}
pub(crate) fn skip_whitespace(&mut self) {
while !self.is_at_end() {
let c = self.peek();
match c {
' ' | '\r' | '\t' => { self.advance(); }
'\n' => { self.line += 1; self.advance(); }
_ => break,
}
}
}
pub(crate) fn skip_comment(&mut self) {
while !self.is_at_end() && self.peek() != '\n' {
self.advance();
}
}
pub(crate) fn skip_multiline_comment(&mut self) {
while !self.is_at_end() {
if self.peek() == '*' && self.peek_next() == '/' {
self.advance();
self.advance();
break;
}
if self.peek() == '\n' {
self.line += 1;
}
self.advance();
}
if self.is_at_end() {
panic!("Unterminated block comment");
}
}
}