fix a bug

This commit is contained in:
blueloveTH 2022-11-16 02:11:54 +08:00
parent e310239b05
commit 7979697f78
3 changed files with 18 additions and 8 deletions

View File

@ -176,8 +176,6 @@ public:
parser->current = parser->nextToken(); parser->current = parser->nextToken();
//_Str _info = parser->current.info(); printf("%s\n", (const char*)_info); //_Str _info = parser->current.info(); printf("%s\n", (const char*)_info);
if(parser->current.type == TK("(")) parser->ignoreIndent += 1;
if(parser->current.type == TK(")")) parser->ignoreIndent -= 1;
while (parser->peekChar() != '\0') { while (parser->peekChar() != '\0') {
parser->token_start = parser->current_char; parser->token_start = parser->current_char;
@ -246,6 +244,7 @@ public:
if (isdigit(c)) { if (isdigit(c)) {
eatNumber(); eatNumber();
} else if (isalpha(c) || c=='_') { } else if (isalpha(c) || c=='_') {
// 可以支持中文编程
if(c == 'f'){ if(c == 'f'){
if(parser->matchChar('\'')) {eatString('\'', true); return;} if(parser->matchChar('\'')) {eatString('\'', true); return;}
if(parser->matchChar('"')) {eatString('"', true); return;} if(parser->matchChar('"')) {eatString('"', true); return;}
@ -274,11 +273,9 @@ public:
} }
void consume(_TokenType expected) { void consume(_TokenType expected) {
lexToken(); if (!match(expected)){
Token prev = parser->previous;
if (prev.type != expected){
_StrStream ss; _StrStream ss;
ss << "expected '" << TK_STR(expected) << "', but got '" << TK_STR(prev.type) << "'"; ss << "expected '" << TK_STR(expected) << "', but got '" << TK_STR(peek()) << "'";
syntaxError(ss.str()); syntaxError(ss.str());
} }
} }

View File

@ -103,7 +103,8 @@ struct Parser {
std::queue<Token> nexts; std::queue<Token> nexts;
std::stack<int> indents; std::stack<int> indents;
int ignoreIndent = 0; int brackets_level_0 = 0;
int brackets_level_1 = 0;
Token nextToken(){ Token nextToken(){
if(nexts.empty()) return makeErrToken(); if(nexts.empty()) return makeErrToken();
@ -139,7 +140,7 @@ struct Parser {
} }
bool eatIndentation(){ bool eatIndentation(){
if(ignoreIndent > 0) return true; if(brackets_level_0 > 0 || brackets_level_1 > 0) return true;
int spaces = eatSpaces(); int spaces = eatSpaces();
// https://docs.python.org/3/reference/lexical_analysis.html#indentation // https://docs.python.org/3/reference/lexical_analysis.html#indentation
if(spaces > indents.top()){ if(spaces > indents.top()){
@ -227,6 +228,14 @@ struct Parser {
// Initialize the next token as the type. // Initialize the next token as the type.
void setNextToken(_TokenType type, PyVar value=nullptr) { void setNextToken(_TokenType type, PyVar value=nullptr) {
switch(type){
case TK("("): brackets_level_0++; break;
case TK(")"): brackets_level_0--; break;
case TK("["): brackets_level_1++; break;
case TK("]"): brackets_level_1--; break;
}
nexts.push( Token{ nexts.push( Token{
type, type,
token_start, token_start,

View File

@ -1,3 +1,7 @@
[
1,2,3
]
import ink import ink
print('Once upon a time...') print('Once upon a time...')