This commit is contained in:
blueloveTH 2023-01-04 02:27:05 +08:00
parent 43f70499a3
commit a942b90f78
3 changed files with 61 additions and 4 deletions

View File

@ -115,11 +115,43 @@ public:
} }
_Str eatStringUntil(char quote, bool raw) { _Str eatStringUntil(char quote, bool raw) {
bool quote3 = false;
std::string_view sv = parser->lookahead(2);
if(sv.size() == 2 && sv[0] == quote && sv[1] == quote) {
quote3 = true;
parser->eatChar();
parser->eatChar();
}
std::vector<char> buff; std::vector<char> buff;
while (true) { while (true) {
char c = parser->eatCharIncludeNewLine(); char c = parser->eatCharIncludeNewLine();
if (c == quote) break; if (c == quote){
if (c == '\0' || c == '\n') syntaxError("EOL while scanning string literal"); if(quote3){
sv = parser->lookahead(2);
if(sv.size() == 2 && sv[0] == quote && sv[1] == quote) {
parser->eatChar();
parser->eatChar();
break;
}
buff.push_back(c);
} else {
break;
}
}
if (c == '\0'){
if(quote3 && parser->src->mode == SINGLE_MODE){
throw NeedMoreLines(false);
}
syntaxError("EOL while scanning string literal");
}
if (c == '\n'){
if(!quote3) syntaxError("EOL while scanning string literal");
else{
buff.push_back(c);
continue;
}
}
if (!raw && c == '\\') { if (!raw && c == '\\') {
switch (parser->eatCharIncludeNewLine()) { switch (parser->eatCharIncludeNewLine()) {
case '"': buff.push_back('"'); break; case '"': buff.push_back('"'); break;
@ -128,7 +160,6 @@ public:
case 'n': buff.push_back('\n'); break; case 'n': buff.push_back('\n'); break;
case 'r': buff.push_back('\r'); break; case 'r': buff.push_back('\r'); break;
case 't': buff.push_back('\t'); break; case 't': buff.push_back('\t'); break;
case '\n': case '\r': break;
default: syntaxError("invalid escape character"); default: syntaxError("invalid escape character");
} }
} else { } else {

View File

@ -124,6 +124,15 @@ struct Parser {
return *current_char; return *current_char;
} }
std::string_view lookahead(int n){
const char* c = current_char;
for(int i=0; i<n; i++){
if(*c == '\0') return std::string_view(current_char, i);
c++;
}
return std::string_view(current_char, n);
}
char peekNextChar() { char peekNextChar() {
if (peekChar() == '\0') return '\0'; if (peekChar() == '\0') return '\0';
return *(current_char + 1); return *(current_char + 1);

View File

@ -4,4 +4,21 @@ assert a == '1\\232\\\\\\13'
b = r'\' b = r'\'
assert len(b) == 3 assert len(b) == 3
assert b == '\\' assert b == '\\'
s = '''asdasd
asds1321321321测试测试
'''
assert s == 'asdasd\nasds1321321321测试测试\n'
s = r'''asdasd
asds1321321321测试\测试'''
assert s == 'asdasd\nasds1321321321测试\\测试'
s = f'''->->{s}<-<-
{123}
'''
assert s == '->->asdasd\nasds1321321321测试\\测试<-<-\n123\n'