|
@@ -9,6 +9,7 @@
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "Constants.h"
|
|
#include "Constants.h"
|
|
|
|
|
+#include "Utils.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
int line;
|
|
int line;
|
|
@@ -121,8 +122,9 @@ static const char* tokenizerAddNumber(TState* t, const char* s) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static char mapEscapeSequence(TState* t, const char* s) {
|
|
static char mapEscapeSequence(TState* t, const char* s) {
|
|
|
- static char map[][2] = {
|
|
|
|
|
- {'n', '\n'}, {'t', '\t'}, {'r', '\r'}, {'\\', '\\'}, {'#', '\0'}};
|
|
|
|
|
|
|
+ static char map[][2] = {{'n', '\n'}, {'t', '\t'}, {'r', '\r'},
|
|
|
|
|
+ {'\\', '\\'}, {'"', '"'}, {'\'', '\''},
|
|
|
|
|
+ {'#', '\0'}};
|
|
|
for(size_t i = 0; map[i][0] != '#'; i++) {
|
|
for(size_t i = 0; map[i][0] != '#'; i++) {
|
|
|
if(s[1] == map[i][0]) {
|
|
if(s[1] == map[i][0]) {
|
|
|
return map[i][1];
|
|
return map[i][1];
|
|
@@ -156,6 +158,33 @@ static const char* tokenizerAddString(TState* t, const char* s) {
|
|
|
return s;
|
|
return s;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static const char* tokenizerAddChar(TState* t, const char* s) {
|
|
|
|
|
+ UTF8 buffer = {};
|
|
|
|
|
+ while(true) {
|
|
|
|
|
+ char c = *(++s);
|
|
|
|
|
+ if(c == '\0') {
|
|
|
|
|
+ THROW_ERROR("Unclosed character");
|
|
|
|
|
+ } else if(c == '\'') {
|
|
|
|
|
+ s++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ } else if(buffer.length >= sizeof(buffer.data)) {
|
|
|
|
|
+ THROW_ERROR("Too long character");
|
|
|
|
|
+ } else if(buffer.length > 0 && !isUTF8Remainder(c)) {
|
|
|
|
|
+ THROW_ERROR("Invalid character");
|
|
|
|
|
+ } else if(c == '\\') {
|
|
|
|
|
+ c = mapEscapeSequence(t, s);
|
|
|
|
|
+ s++;
|
|
|
|
|
+ }
|
|
|
|
|
+ buffer.data[buffer.length++] = c;
|
|
|
|
|
+ }
|
|
|
|
|
+ i32 i = convertUTF8toUnicode(buffer);
|
|
|
|
|
+ tAddToken(t, TT_INT32);
|
|
|
|
|
+ if(bufferWriteI32(&t->tokenizer->buffer, i)) {
|
|
|
|
|
+ tTooMuchTokens(t);
|
|
|
|
|
+ }
|
|
|
|
|
+ return s;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
#define SIMPLE_TOKEN(ch, token) \
|
|
#define SIMPLE_TOKEN(ch, token) \
|
|
|
else if(c == ch) { \
|
|
else if(c == ch) { \
|
|
|
tAddToken(t, token); \
|
|
tAddToken(t, token); \
|
|
@@ -181,10 +210,12 @@ static void tParseLineString(TState* t, const char* s) {
|
|
|
s = tokenizerAddNumber(t, s);
|
|
s = tokenizerAddNumber(t, s);
|
|
|
} else if(c == '"') {
|
|
} else if(c == '"') {
|
|
|
s = tokenizerAddString(t, s);
|
|
s = tokenizerAddString(t, s);
|
|
|
|
|
+ } else if(c == '\'') {
|
|
|
|
|
+ s = tokenizerAddChar(t, s);
|
|
|
} else if(c == '\n' || c == '#') {
|
|
} else if(c == '\n' || c == '#') {
|
|
|
tAddToken(t, TT_NEWLINE);
|
|
tAddToken(t, TT_NEWLINE);
|
|
|
break;
|
|
break;
|
|
|
- } else if(c == ' ') {
|
|
|
|
|
|
|
+ } else if(c == ' ' || c == '\r') {
|
|
|
s++;
|
|
s++;
|
|
|
}
|
|
}
|
|
|
SIMPLE_TOKEN(',', TT_COMMA)
|
|
SIMPLE_TOKEN(',', TT_COMMA)
|