| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #pragma once
- #include "pocketpy/objects/sourcedata.hpp"
- #include "pocketpy/objects/error.hpp"
- #include <variant>
- namespace pkpy{
- typedef uint8_t TokenIndex;
- constexpr const char* kTokens[] = {
- "is not", "not in", "yield from",
- "@eof", "@eol", "@sof",
- "@id", "@num", "@str", "@fstr", "@long", "@bytes", "@imag",
- "@indent", "@dedent",
- /*****************************************/
- "+", "+=", "-", "-=", // (INPLACE_OP - 1) can get '=' removed
- "*", "*=", "/", "/=", "//", "//=", "%", "%=",
- "&", "&=", "|", "|=", "^", "^=",
- "<<", "<<=", ">>", ">>=",
- /*****************************************/
- ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}",
- "**", "=", ">", "<", "..", "...", "->", "@", "==", "!=", ">=", "<=",
- "++", "--", "~",
- /** KW_BEGIN **/
- "class", "import", "as", "def", "lambda", "pass", "del", "from", "with", "yield",
- "None", "in", "is", "and", "or", "not", "True", "False", "global", "try", "except", "finally",
- "while", "for", "if", "elif", "else", "break", "continue", "return", "assert", "raise"
- };
- using TokenValue = std::variant<std::monostate, i64, f64, Str>;
- const int kTokenCount = sizeof(kTokens) / sizeof(kTokens[0]);
- constexpr TokenIndex TK(const char token[]) {
- for(int k=0; k<kTokenCount; k++){
- const char* i = kTokens[k];
- const char* j = token;
- while(*i && *j && *i == *j) { i++; j++;}
- if(*i == *j) return k;
- }
- return 255;
- }
- inline constexpr bool is_raw_string_used(TokenIndex t){
- return t == TK("@id") || t == TK("@long");
- }
- #define TK_STR(t) kTokens[t]
- const std::map<std::string_view, TokenIndex> kTokenKwMap = [](){
- std::map<std::string_view, TokenIndex> map;
- for(int k=TK("class"); k<kTokenCount; k++) map[kTokens[k]] = k;
- return map;
- }();
- struct Token{
- TokenIndex type;
- const char* start;
- int length;
- int line;
- int brackets_level;
- TokenValue value;
- Str str() const { return Str(start, length);}
- std::string_view sv() const { return std::string_view(start, length);}
- };
- // https://docs.python.org/3/reference/expressions.html#operator-precedence
- enum Precedence {
- PREC_LOWEST,
- PREC_LAMBDA, // lambda
- PREC_TERNARY, // ?:
- PREC_LOGICAL_OR, // or
- PREC_LOGICAL_AND, // and
- PREC_LOGICAL_NOT, // not
- /* https://docs.python.org/3/reference/expressions.html#comparisons
- * Unlike C, all comparison operations in Python have the same priority,
- * which is lower than that of any arithmetic, shifting or bitwise operation.
- * Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics.
- */
- PREC_COMPARISION, // < > <= >= != ==, in / is / is not / not in
- PREC_BITWISE_OR, // |
- PREC_BITWISE_XOR, // ^
- PREC_BITWISE_AND, // &
- PREC_BITWISE_SHIFT, // << >>
- PREC_TERM, // + -
- PREC_FACTOR, // * / % // @
- PREC_UNARY, // - not ~
- PREC_EXPONENT, // **
- PREC_PRIMARY, // f() x[] a.b 1:2
- PREC_HIGHEST,
- };
- enum StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES };
- struct Lexer {
- VM* vm;
- std::shared_ptr<SourceData> src;
- const char* token_start;
- const char* curr_char;
- int current_line = 1;
- vector<Token> nexts;
- stack_no_copy<int, small_vector_2<int, 8>> indents;
- int brackets_level = 0;
- char peekchar() const{ return *curr_char; }
- bool match_n_chars(int n, char c0);
- bool match_string(const char* s);
- int eat_spaces();
- bool eat_indentation();
- char eatchar();
- char eatchar_include_newline();
- int eat_name();
- void skip_line_comment();
- bool matchchar(char c);
- void add_token(TokenIndex type, TokenValue value={});
- void add_token_2(char c, TokenIndex one, TokenIndex two);
- Str eat_string_until(char quote, bool raw);
- void eat_string(char quote, StringType type);
- void eat_number();
- bool lex_one_token();
- /***** Error Reporter *****/
- [[noreturn]] void throw_err(StrName type, Str msg);
- [[noreturn]] void throw_err(StrName type, Str msg, int lineno, const char* cursor);
- [[noreturn]] void SyntaxError(Str msg){ throw_err("SyntaxError", msg); }
- [[noreturn]] void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); }
- [[noreturn]] void IndentationError(Str msg){ throw_err("IndentationError", msg); }
-
- Lexer(VM* vm, std::shared_ptr<SourceData> src);
- vector<Token> run();
- };
- enum class IntParsingResult{
- Success,
- Failure,
- Overflow,
- };
- IntParsingResult parse_uint(std::string_view text, i64* out, int base);
- } // namespace pkpy
|