Browse Source

fix a bug

blueloveTH 3 years ago
parent
commit
7979697f78
3 changed files with 18 additions and 8 deletions
  1. 3 6
      src/compiler.h
  2. 11 2
      src/parser.h
  3. 4 0
      tests/6.py

+ 3 - 6
src/compiler.h

@@ -176,8 +176,6 @@ public:
         parser->current = parser->nextToken();
 
         //_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') {
             parser->token_start = parser->current_char;
@@ -246,6 +244,7 @@ public:
                     if (isdigit(c)) {
                         eatNumber();
                     } else if (isalpha(c) || c=='_') {
+                        // 可以支持中文编程
                         if(c == 'f'){
                             if(parser->matchChar('\'')) {eatString('\'', true); return;}
                             if(parser->matchChar('"')) {eatString('"', true); return;}
@@ -274,11 +273,9 @@ public:
     }
 
     void consume(_TokenType expected) {
-        lexToken();
-        Token prev = parser->previous;
-        if (prev.type != expected){
+        if (!match(expected)){
             _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());
         }
     }

+ 11 - 2
src/parser.h

@@ -103,7 +103,8 @@ struct Parser {
     std::queue<Token> nexts;
     std::stack<int> indents;
 
-    int ignoreIndent = 0;
+    int brackets_level_0 = 0;
+    int brackets_level_1 = 0;
 
     Token nextToken(){
         if(nexts.empty()) return makeErrToken();
@@ -139,7 +140,7 @@ struct Parser {
     }
 
     bool eatIndentation(){
-        if(ignoreIndent > 0) return true;
+        if(brackets_level_0 > 0 || brackets_level_1 > 0) return true;
         int spaces = eatSpaces();
         // https://docs.python.org/3/reference/lexical_analysis.html#indentation
         if(spaces > indents.top()){
@@ -227,6 +228,14 @@ struct Parser {
 
     // Initialize the next token as the type.
     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{
             type,
             token_start,

+ 4 - 0
tests/6.py

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