PikaParser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #ifndef __PIKA_PARSER__H
  31. #define __PIKA_PARSER__H
  32. #include "PikaVM.h"
  33. #include "dataQueueObj.h"
  34. #include "dataStack.h"
  35. typedef QueueObj AST;
  36. typedef enum TokenType {
  37. TOKEN_strEnd = 0,
  38. TOKEN_symbol,
  39. TOKEN_keyword,
  40. TOKEN_operator,
  41. TOKEN_devider,
  42. TOKEN_literal,
  43. } TokenType;
  44. enum StmtType {
  45. STMT_reference,
  46. STMT_tuple,
  47. STMT_string,
  48. STMT_bytes,
  49. STMT_number,
  50. STMT_method,
  51. STMT_chain,
  52. STMT_operator,
  53. STMT_inhert,
  54. STMT_import,
  55. STMT_list,
  56. STMT_slice,
  57. STMT_dict,
  58. STMT_none,
  59. };
  60. typedef struct Asmer Asmer;
  61. struct Asmer {
  62. char* asm_code;
  63. uint8_t block_deepth_now;
  64. uint8_t is_new_line;
  65. char* line_pointer;
  66. };
  67. typedef enum _GenRuleValType {
  68. VAL_NONEVAL,
  69. VAL_DYNAMIC,
  70. VAL_STATIC_,
  71. } GenRuleValType;
  72. typedef struct GenRule {
  73. char* ins;
  74. GenRuleValType type;
  75. char* ast;
  76. char* val;
  77. } GenRule;
  78. typedef struct BlockState {
  79. Stack* stack;
  80. int deepth;
  81. } BlockState;
  82. typedef struct Parser Parser;
  83. typedef char* (*fn_parser_Ast2Target)(Parser* self, AST* ast);
  84. typedef char* (*fn_parser_Lines2Target)(Parser* self, char* sPyLines);
  85. #define _VAL_NEED_INIT -1
  86. #define PIKA_BLOCK_SPACE 4
  87. struct Parser {
  88. Args lineBuffs;
  89. Args genBuffs;
  90. BlockState blockState;
  91. int blockDeepthOrigin;
  92. fn_parser_Ast2Target fn_ast2Target;
  93. pika_bool isGenBytecode;
  94. ByteCodeFrame* bytecode_frame;
  95. uint8_t thisBlockDeepth;
  96. uint32_t label_pc;
  97. };
  98. typedef struct LexToken LexToken;
  99. struct LexToken {
  100. char* tokenStream;
  101. enum TokenType type;
  102. char* pyload;
  103. };
  104. typedef struct Cursor ParsetState;
  105. struct Cursor {
  106. char* tokenStream;
  107. uint16_t length;
  108. uint16_t iter_index;
  109. int8_t bracket_deepth;
  110. struct LexToken token1;
  111. struct LexToken token2;
  112. Arg* last_token;
  113. Args* iter_buffs;
  114. Args* buffs_p;
  115. PIKA_RES result;
  116. };
  117. char* Lexer_getTokenStream(Args* outBuffs, char* stmt);
  118. char* Lexer_printTokenStream(Args* outBuffs, char* tokenStream);
  119. char* pika_file2Asm(Args* outBuffs, char* filename);
  120. char* pika_lines2Asm(Args* outBuffs, char* multiLine);
  121. char* pika_lines2Array(char* lines);
  122. char* pika_line2Asm(Args* buffs_p, char* line, Stack* blockStack);
  123. AST* parser_line2Ast(Parser* self, char* line);
  124. char* parser_file2Doc(Parser* self, char* sPyFile);
  125. int parser_file2DocFile(Parser* self, char* sPyFile, char* sDocFile);
  126. char* parser_ast2Asm(Parser* self, AST* ast);
  127. char* parser_lines2Doc(Parser* self, char* sPyLines);
  128. char* parser_file2Doc(Parser* self, char* filename);
  129. AST* line2Ast(char* line);
  130. PIKA_RES pika_lines2Bytes(ByteCodeFrame* bf, char* py_lines);
  131. char* parser_line2Target(Parser* self, char* line);
  132. Parser* parser_create(void);
  133. int parser_deinit(Parser* parser);
  134. char* Cursor_popLastToken(Args* outBuffs, char** pStmt, char* str);
  135. char* Cursor_getCleanStmt(Args* outBuffs, char* cmd);
  136. uint8_t Cursor_count(char* stmt, TokenType type, char* pyload);
  137. uint8_t _Cursor_count(char* stmt,
  138. TokenType type,
  139. char* pyload,
  140. pika_bool bSkipbracket);
  141. AST* AST_parseStmt(AST* ast, char* stmt);
  142. AST* AST_create(void);
  143. char* AST_genAsm_top(AST* oAST, Args* outBuffs);
  144. int32_t AST_deinit(AST* ast);
  145. char* instructUnit_fromAsmLine(Args* outBuffs, char* pikaAsm);
  146. ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm);
  147. #define _Cursor_forEach(cursor) \
  148. _Cursor_beforeIter(&cursor); \
  149. for (int __i = 0; __i < cursor.length; __i++)
  150. #define Cursor_forEachExistPs(cursor, stmt) \
  151. /* init parserStage */ \
  152. _Cursor_init(&cursor); \
  153. _Cursor_parse(&cursor, stmt); \
  154. _Cursor_forEach(cursor)
  155. #define Cursor_forEach(cursor, stmt) \
  156. struct Cursor cursor; \
  157. Cursor_forEachExistPs(cursor, stmt)
  158. uint16_t TokenStream_getSize(char* tokenStream);
  159. Arg* arg_strAddIndent(Arg* aStrIn, int indent);
  160. Arg* arg_strAddIndentMuti(Arg* aStrIn, int indent);
  161. #endif
  162. #ifdef __cplusplus
  163. }
  164. #endif