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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::lexer::Token;
use crate::lexer::rules::{MatchResult, LexerRule, TokenError};
use crate::lexer::rules::strmatcher::StrMatcher;
#[derive(Clone)]
pub struct LineCommentRule {
state: (bool, bool),
comment: char
}
impl LineCommentRule {
pub fn new(comment: char) -> Self {
LineCommentRule { comment, state: (false, false) }
}
fn match_state(&self, state: (bool, bool)) -> MatchResult {
match state {
(_, false) => MatchResult::CompleteMatch,
(true, _) => MatchResult::CompleteMatch,
(false, true) => MatchResult::NoMatch,
}
}
fn next_state(&self, state: (bool, bool), next: char) -> (bool, bool) {
let (start, end) = state;
if !start {
if next != self.comment {
return (false, true);
}
return (true, false);
}
if start && !end {
if next == '\n' {
return (true, true);
}
return (true, false);
}
(false, true)
}
}
impl LexerRule for LineCommentRule {
fn reset(&mut self) {
self.state = (false, false);
}
fn current_state(&self) -> MatchResult {
self.match_state(self.state)
}
fn try_match(&mut self, _prev: Option<char>, next: char) -> MatchResult {
let state = self.next_state(self.state, next);
let match_result = self.match_state(state);
if match_result.is_match() {
self.state = state;
}
match_result
}
fn get_token(&self) -> Result<Token, TokenError> {
debug_assert!(self.current_state().is_complete_match());
Ok(Token::Comment)
}
}
#[derive(Clone)]
pub struct BlockCommentRule {
nestlevel: u32,
start: StrMatcher<'static>,
end: StrMatcher<'static>,
}
impl BlockCommentRule {
pub fn new(start: &'static str, end: &'static str) -> Self {
BlockCommentRule {
nestlevel: 0,
start: StrMatcher::case_sensitive(start),
end: StrMatcher::case_sensitive(end),
}
}
}
impl LexerRule for BlockCommentRule {
fn reset(&mut self) {
self.nestlevel = 0;
self.start.reset();
self.end.reset();
}
fn current_state(&self) -> MatchResult {
if self.nestlevel > 0 {
return MatchResult::IncompleteMatch;
}
if self.end.last_match_result().is_complete_match() {
return MatchResult::CompleteMatch;
}
self.start.last_match_result()
}
fn try_match(&mut self, _prev: Option<char>, next: char) -> MatchResult {
let start_result = self.start.try_match(next);
if start_result.is_complete_match() {
self.nestlevel += 1;
self.start.reset();
return MatchResult::IncompleteMatch;
}
if self.nestlevel > 0 {
let end_result = self.end.try_match(next);
if end_result.is_complete_match() {
self.nestlevel -= 1;
if self.nestlevel > 0 {
self.end.reset();
} else {
return MatchResult::CompleteMatch;
}
}
if !start_result.is_match() {
self.start.reset();
}
if !end_result.is_match() {
self.end.reset();
}
return MatchResult::IncompleteMatch;
}
start_result
}
fn get_token(&self) -> Result<Token, TokenError> {
debug_assert!(self.current_state().is_complete_match());
Ok(Token::Comment)
}
}