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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::{
LuaKind, LuaSyntaxToken,
kind::LuaTokenKind,
parser_error::{LuaParseError, LuaParseErrorKind},
};
pub fn string_token_value(token: &LuaSyntaxToken) -> Result<String, LuaParseError> {
match token.kind() {
LuaKind::Token(LuaTokenKind::TkString) => normal_string_value(token),
LuaKind::Token(LuaTokenKind::TkLongString) => long_string_value(token),
_ => unreachable!(),
}
}
fn long_string_value(token: &LuaSyntaxToken) -> Result<String, LuaParseError> {
let range = token.text_range();
let text = token.text();
if text.len() < 4 {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!("String too short"),
range,
));
}
let mut equal_num = 0;
let mut i = 0;
let mut chars = text.char_indices();
// check first char
if let Some((_, first_char)) = chars.next() {
if first_char != '[' {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!(
"Invalid long string start, expected '[', found '%{char}'",
char = first_char
),
range,
));
}
} else {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!("Invalid long string start, expected '[', found end of input"),
range,
));
}
for (idx, c) in chars.by_ref() {
// calc eq num
if c == '=' {
equal_num += 1;
} else if c == '[' {
i = idx + 1;
break;
} else {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!("Invalid long string start"),
range,
));
}
}
// check string len is enough
if text.len() < i + equal_num + 2 {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!(
"Invalid long string end, expected '%{eq}]'",
eq = "=".repeat(equal_num)
),
range,
));
}
// lua special rule for long string
if let Some((_, first_content_char)) = chars.next() {
if first_content_char == '\r' {
if let Some((_, next_char)) = chars.next() {
if next_char == '\n' {
i += 2;
} else {
i += 1;
}
}
} else if first_content_char == '\n' {
i += 1;
}
}
let content = &text[i..(text.len() - equal_num - 2)];
Ok(content.to_string())
}
fn normal_string_value(token: &LuaSyntaxToken) -> Result<String, LuaParseError> {
let text = token.text();
if text.len() < 2 {
return Ok(String::new());
}
let mut result = String::with_capacity(text.len() - 2);
let mut chars = text.chars().peekable();
let delimiter = chars.next().unwrap();
while let Some(c) = chars.next() {
match c {
'\\' => {
if let Some(next_char) = chars.next() {
match next_char {
'a' => result.push('\u{0007}'), // Bell
'b' => result.push('\u{0008}'), // Backspace
'f' => result.push('\u{000C}'), // Formfeed
'n' => result.push('\n'), // Newline
'r' => result.push('\r'), // Carriage return
't' => result.push('\t'), // Horizontal tab
'v' => result.push('\u{000B}'), // Vertical tab
'x' => {
// Hexadecimal escape sequence
let hex = chars.by_ref().take(2).collect::<String>();
if hex.len() == 2 && hex.chars().all(|c| c.is_ascii_hexdigit()) {
if let Ok(value) = u8::from_str_radix(&hex, 16) {
result.push(value as char);
}
} else {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!("Invalid hex escape sequence '\\x%{hex}'", hex = hex),
token.text_range(),
));
}
}
'u' => {
// Unicode escape sequence
if let Some('{') = chars.next() {
let unicode_hex =
chars.by_ref().take_while(|c| *c != '}').collect::<String>();
if let Ok(code_point) = u32::from_str_radix(&unicode_hex, 16) {
if let Some(unicode_char) = std::char::from_u32(code_point) {
result.push(unicode_char);
} else {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!(
"Invalid unicode escape sequence '\\u{{%{unicode_hex}}}'",
unicode_hex = unicode_hex
),
token.text_range(),
));
}
}
}
}
'0'..='9' => {
// Decimal escape sequence
let mut dec = String::new();
dec.push(next_char);
for _ in 0..2 {
if let Some(digit) = chars.peek() {
if digit.is_ascii_digit() {
dec.push(*digit);
} else {
break;
}
chars.next();
}
}
if let Ok(value) = dec.parse::<u8>() {
result.push(value as char);
}
}
'\\' | '\'' | '\"' => result.push(next_char),
'z' => {
// Skip whitespace
while let Some(c) = chars.peek() {
if !c.is_whitespace() {
break;
}
chars.next();
}
}
'\r' | '\n' => {
result.push(next_char);
}
_ => {
return Err(LuaParseError::new(
LuaParseErrorKind::SyntaxError,
&t!("Invalid escape sequence '\\%{char}'", char = next_char),
token.text_range(),
));
}
}
}
}
_ => {
if c == delimiter {
break;
}
result.push(c);
}
}
}
Ok(result)
}