소스 검색

feat: 支持括号内无视换行

支持使用括号内无视换行
支持\无视comment
SongZihuan 4 년 전
부모
커밋
15b1ad09f9
9개의 변경된 파일71개의 추가작업 그리고 11개의 파일을 삭제
  1. 3 0
      include/lexical.h
  2. 1 0
      include/syntax.h
  3. 2 1
      include/token.h
  4. 0 2
      main.c
  5. 10 0
      parser/__grammar.c
  6. 16 4
      parser/grammar.c
  7. 2 0
      parser/include/__grammar.h
  8. 1 0
      parser/lexical.c
  9. 36 4
      parser/syntax.c

+ 3 - 0
include/lexical.h

@@ -8,6 +8,9 @@ struct LexFile{
         bool is_back;
         int p;
     } back;
+    struct {
+        int enter;  // 若计数为0则不忽略enter
+    } filter_data;
     long int line;
 };
 

+ 1 - 0
include/syntax.h

@@ -9,5 +9,6 @@ void varMather(int p, LexMather *mather);
 void stringMather(int p, LexMather *mather);
 void strMather(int p, LexMather *mather, const char *dest_p);
 void charMather(int p, LexMather *mather, int dest_p);
+void spaceMather(int p, LexMather *mather);
 
 #endif //VIRTUALMATH_SYNTAX_H

+ 2 - 1
include/token.h

@@ -89,8 +89,9 @@
 #define MATHER_FROM 75
 #define MATHER_ASSERT 76
 #define MATHER_LAMBDA 77
+#define MATHER_NOTENTER 78
 
-#define MATHER_MAX 78
+#define MATHER_MAX 79
 
 // 从-6开始是为了避开status的特殊值,尽管这并没有什么影响
 #define COMMAND -6

+ 0 - 2
main.c

@@ -25,11 +25,9 @@ int main(int argc, char *argv[]) {
  * 官方函数
  * 官方类
  * for 循环
- * with as 语句
  * goto 语句
  * label 标签
  * yield 语句
- * 括号无视换行
  * \ 无视换行
  * # 注释设定
  */

+ 10 - 0
parser/__grammar.c

@@ -257,6 +257,7 @@ bool parserParameter(PASERSSIGNATURE, Parameter **pt, bool is_formal, bool is_li
         s_4,  // name_args模式
     } status;
 
+    lexEnter(pm, true);
     if (is_dict && !is_list)
         status = s_2;  // is_formal关闭对only_value的支持
     else
@@ -336,11 +337,20 @@ bool parserParameter(PASERSSIGNATURE, Parameter **pt, bool is_formal, bool is_li
         freeToken(tmp, false);
     }
     *pt = new_pt;
+    lexEnter(pm, false);
     return true;
 
     error_:
     freeToken(tmp, true);
     freeParameter(new_pt, true);
     *pt = NULL;
+    lexEnter(pm, false);
     return false;
 }
+
+void lexEnter(ParserMessage *pm, bool lock){
+    if (lock)
+        pm->tm->file->filter_data.enter ++;
+    else
+        pm->tm->file->filter_data.enter --;
+}

+ 16 - 4
parser/grammar.c

@@ -32,7 +32,8 @@ void freeParserMessage(ParserMessage *pm, bool self) {
 void parserCommandList(PASERSSIGNATURE, bool global, Statement *st) {
     int token_type;
     char *command_message = global ? "ERROR from command list(get parserCommand)" : NULL;
-
+    int save_enter = pm->tm->file->filter_data.enter;
+    pm->tm->file->filter_data.enter = 0;
     while (true){
         token_type = readBackToken(pm);
         if (token_type == MATHER_EOF){
@@ -67,7 +68,9 @@ void parserCommandList(PASERSSIGNATURE, bool global, Statement *st) {
             freeToken(command_token, false);
         }
     }
-    return_: return;
+    return_:
+    pm->tm->file->filter_data.enter = save_enter;
+    return;
 }
 
 /**
@@ -1079,7 +1082,9 @@ void parserBaseValue(PASERSSIGNATURE){
     else if (MATHER_LB == value_token->token_type){
         int tmp;
         Statement *tmp_st = NULL;
+        lexEnter(pm, true);
         tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RB, &tmp_st, "base value");
+        lexEnter(pm, false);
         if (tmp == 0){
             freeToken(value_token, true);
             syntaxError(pm, syntax_error, value_token->line, 1, "Don't get operation from Base Value");
@@ -1108,7 +1113,10 @@ void parserBaseValue(PASERSSIGNATURE){
         }
     }
     else if (MATHER_LP == value_token->token_type){
-        int tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RP, &st, "base value");
+        int tmp;
+        lexEnter(pm, true);
+        tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RP, &st, "base value");
+        lexEnter(pm, false);
         if (tmp == 0){
             freeToken(value_token, true);
             syntaxError(pm, syntax_error, value_token->line, 1, "Don't get operation from Base Value");
@@ -1122,7 +1130,11 @@ void parserBaseValue(PASERSSIGNATURE){
     }
     else if (MATHER_LC == value_token->token_type){
         Parameter *pt = NULL;
-        if (!parserParameter(CALLPASERSSIGNATURE, &pt, false, false, true, MATHER_COMMA, MATHER_COLON)) {
+        int parser_status;
+        lexEnter(pm, true);
+        parser_status = parserParameter(CALLPASERSSIGNATURE, &pt, false, false, true, MATHER_COMMA, MATHER_COLON);
+        lexEnter(pm, false);
+        if (!parser_status) {
             freeToken(value_token, true);
             syntaxError(pm, syntax_error, value_token->line, 1, "Don't get a dict parameter");
             goto return_;

+ 2 - 0
parser/include/__grammar.h

@@ -60,4 +60,6 @@ void twoOperation(PASERSSIGNATURE, PasersFunction callBack, GetSymbolFunction ge
 void tailOperation(PASERSSIGNATURE, PasersFunction callBack, TailFunction tailFunction, int call_type, int self_type,
                    char *call_name, char *self_name);
 
+void lexEnter(ParserMessage *pm, bool lock);
+
 #endif //VIRTUALMATH___GRAMMAR_H

+ 1 - 0
parser/lexical.c

@@ -31,6 +31,7 @@ LexFile *makeLexFile(char *dir){
     tmp->back.is_back = false;
     tmp->back.p = EOF;
     tmp->line = 1;
+    tmp->filter_data.enter = 0;
     return tmp;
 }
 

+ 36 - 4
parser/syntax.c

@@ -147,6 +147,26 @@ void charMather(int p, LexMather *mather, int dest_p){
         mather->status = LEXMATHER_MISTAKE;
 }
 
+/**
+ * 匹配空白符号
+ * @param p
+ * @param mather
+ */
+void spaceMather(int p, LexMather *mather){
+    if (mather->status == LEXMATHER_START || mather->status == LEXMATHER_ING)
+        if (isspace(p) && p != '\n'){
+            mather->str = memStrCharcpy(mather->str, 1, true, true, p);
+            mather->len ++;
+            mather->status = LEXMATHER_ING;
+        }
+        else if (mather->status == LEXMATHER_ING)
+            mather->status = LEXMATHER_END;
+        else
+            mather->status = LEXMATHER_MISTAKE;
+    else
+        mather->status = LEXMATHER_MISTAKE;
+}
+
 /**
  * 开始匹配,返回的int即checkoutMather返回的值(匹配成功的匹配器的索引)
  * @param file
@@ -161,9 +181,9 @@ int getMatherStatus(LexFile *file, LexMathers *mathers) {
         numberMather(p ,mathers->mathers[MATHER_NUMBER]);
         stringMather(p ,mathers->mathers[MATHER_STRING]);
         varMather(p ,mathers->mathers[MATHER_VAR]);
+        spaceMather(p ,mathers->mathers[MATHER_SPACE]);
         charMatherMacro(MATHER_EOF, EOF);
         charMatherMacro(MATHER_ENTER, '\n');
-        charMatherMacro(MATHER_SPACE, ' ');
 
         strMatherMacro(MATHER_IF, "if");  // 条件判断
         strMatherMacro(MATHER_ELIF, "elif");  // 条件循环
@@ -248,6 +268,7 @@ int getMatherStatus(LexFile *file, LexMathers *mathers) {
         strMatherMacro(MATHER_FROM, "from");
         strMatherMacro(MATHER_ASSERT, "assert");
         strMatherMacro(MATHER_LAMBDA, "lambda");
+        strMatherMacro(MATHER_NOTENTER, "\\\n");
 
         status = checkoutMather(mathers, MATHER_MAX);
     }
@@ -255,6 +276,14 @@ int getMatherStatus(LexFile *file, LexMathers *mathers) {
     return status;
 }
 
+int lexFilter(LexFile *file, int status){
+    if (status == MATHER_SPACE || status == MATHER_NOTENTER)
+        return -1;
+    if (file->filter_data.enter != 0 && status == MATHER_ENTER)
+        return -1;
+    return status;
+}
+
 /**
  * getMatherStatus的高级封装,若匹配到空格则自动忽略(再次匹配)
  * @param file
@@ -263,14 +292,17 @@ int getMatherStatus(LexFile *file, LexMathers *mathers) {
  */
 Token *getToken(LexFile *file, LexMathers *mathers) {
     int status = MATHER_SPACE;
-    Token *tmp;
-    while (status == MATHER_SPACE)
+    int filter;
+    Token *tmp = NULL;
+
+    while ((filter = lexFilter(file, status)) == -1)
         status = getMatherStatus(file, mathers);
+
     if (status == -2){
         tmp = makeLexToken(MATHER_ERROR_, NULL, NULL, file->line);
         goto return_;
     }
-    tmp = makeLexToken(status, mathers->mathers[status]->str, mathers->mathers[status]->second_str, file->line);
+    tmp = makeLexToken(filter, mathers->mathers[status]->str, mathers->mathers[status]->second_str, file->line);
     return_:
     return tmp;
 }