Newer
Older
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
use crate::lexer::ScriptTokenType;
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct PositionState {
offset: usize,
line: usize,
column: usize,
}
impl Default for PositionState {
fn default() -> Self {
Self {
offset: 0,
column: 0,
line: 1,
}
}
}
impl PositionState {
pub fn advance(&mut self, amount: usize) {
self.offset += amount;
self.column += amount;
}
pub fn new_line(&mut self) {
self.offset += 1;
self.line += 1;
self.column = 0;
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct ScanningState {
lexeme_start: usize,
lexeme_current: usize,
}
impl ScanningState {
pub fn new(idx: usize) -> Self {
ScanningState {
lexeme_start: idx,
lexeme_current: idx,
}
}
pub fn with_width(idx: usize, width: usize) -> Self {
ScanningState {
lexeme_start: idx,
lexeme_current: idx + width,
}
}
pub fn advance(&mut self, amount: usize) {
self.lexeme_current += amount;
}
pub fn start(&self) -> usize {
self.lexeme_start
}
pub fn current(&self) -> usize {
self.lexeme_current
}
}
#[derive(Clone, Copy, Debug)]
pub struct Scanner<'a> {
source: &'a str,
position: PositionState,
scan_state: ScanningState,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ScannerErrorKind<'a> {
UnexpectedEof,
UnexpectedCharacter { span: TokenSpan<'a> },
}
#[derive(Clone, Debug, PartialEq)]
pub struct ScannerError<'a> {
pub kind: ScannerErrorKind<'a>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TokenSpan<'a> {
pub position: PositionState,
pub length: usize,
pub source: &'a str,
}
impl<'a> From<Scanner<'a>> for TokenSpan<'a> {
fn from(value: Scanner<'a>) -> TokenSpan<'a> {
TokenSpan {
source: value.source,
position: value.position,
length: value
.scan_state
.lexeme_current
.saturating_sub(value.scan_state.lexeme_start),
}
}
}
#[derive(Clone, Debug)]
pub struct ScannerToken<'a> {
pub location: TokenSpan<'a>,
pub token: ScriptTokenType<'a>,
}
pub type ScannerResult<'a> = Result<ScannerToken<'a>, ScannerError<'a>>;
impl<'a> Scanner<'a> {
pub fn new(source: &str) -> Scanner {
Scanner {
source,
position: PositionState::default(),
scan_state: ScanningState::default(),
}
}
pub fn is_finished(&self) -> bool {
self.position.offset == self.source.len()
}
pub fn scan_token(&mut self) -> Result<ScannerToken<'a>, ScannerError> {
if self.is_finished() {
Ok(ScannerToken {
token: ScriptTokenType::Eof,
location: TokenSpan::from(*self),
})
} else {
Err(ScannerError {
kind: ScannerErrorKind::UnexpectedEof,
})
}
}
}