Skip to content

Commit e947ae0

Browse files
committed
Парсинг экранированных символов в строке
1 parent 6c093e5 commit e947ae0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

program.own

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,6 @@ println `extended word variable`
238238

239239
// Operator overloading
240240
def `::`(v1, v2) = string(v1) + string(v2)
241-
println 1 :: 2 :: 3
241+
println 1 :: 2 :: 3
242+
243+
println "\u042a"

src/com/annimon/ownlang/parser/Lexer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ private void tokenizeNumber() {
150150
private void tokenizeHexNumber() {
151151
clearBuffer();
152152
char current = peek(0);
153-
while (Character.isDigit(current) || isHexNumber(current)) {
153+
while (isHexNumber(current)) {
154154
buffer.append(current);
155155
current = next();
156156
}
157157
addToken(TokenType.HEX_NUMBER, buffer.toString());
158158
}
159159

160160
private static boolean isHexNumber(char current) {
161-
return "abcdef".indexOf(Character.toLowerCase(current)) != -1;
161+
return Character.isDigit(current)
162+
|| ('a' <= current && current <= 'f')
163+
|| ('A' <= current && current <= 'F');
162164
}
163165

164166
private void tokenizeOperator() {
@@ -236,7 +238,28 @@ private void tokenizeText() {
236238
case 'b': current = next(); buffer.append('\b'); continue;
237239
case 'f': current = next(); buffer.append('\f'); continue;
238240
case 'n': current = next(); buffer.append('\n'); continue;
241+
case 'r': current = next(); buffer.append('\r'); continue;
239242
case 't': current = next(); buffer.append('\t'); continue;
243+
case 'u': // http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.3
244+
int rollbackPosition = pos;
245+
while (current == 'u') current = next();
246+
int escapedValue = 0;
247+
for (int i = 12; i >= 0 && escapedValue != -1; i -= 4) {
248+
if (isHexNumber(current)) {
249+
escapedValue |= (Character.digit(current, 16) << i);
250+
} else {
251+
escapedValue = -1;
252+
}
253+
current = next();
254+
}
255+
if (escapedValue >= 0) {
256+
buffer.append((char) escapedValue);
257+
} else {
258+
// rollback
259+
buffer.append("\\u");
260+
pos = rollbackPosition;
261+
}
262+
continue;
240263
}
241264
buffer.append('\\');
242265
continue;

0 commit comments

Comments
 (0)