lj_lex.h (3367B)
1 /* 2 ** Lexical analyzer. 3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h 4 */ 5 6 #ifndef _LJ_LEX_H 7 #define _LJ_LEX_H 8 9 #include <stdarg.h> 10 11 #include "lj_obj.h" 12 #include "lj_err.h" 13 14 #if LJ_53 15 #define TKDEF_BITWISE(_,__) \ 16 __(idiv, <idiv>) __(shl, <<) __(shr, >>) 17 #else 18 #define TKDEF_BITWISE(_,__) 19 #endif 20 21 /* Lua lexer tokens. */ 22 #define TKDEF(_, __) \ 23 _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \ 24 _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \ 25 _(repeat) _(return) _(then) _(true) _(until) _(while) \ 26 TKDEF_BITWISE(_,__) \ 27 __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \ 28 __(label, ::) __(number, <number>) __(name, <name>) __(string, <string>) \ 29 __(eof, <eof>) 30 31 enum { 32 TK_OFS = 256, 33 #define TKENUM1(name) TK_##name, 34 #define TKENUM2(name, sym) TK_##name, 35 TKDEF(TKENUM1, TKENUM2) 36 #undef TKENUM1 37 #undef TKENUM2 38 TK_RESERVED = TK_while - TK_OFS 39 }; 40 41 typedef int LexChar; /* Lexical character. Unsigned ext. from char. */ 42 typedef int LexToken; /* Lexical token. */ 43 44 /* Combined bytecode ins/line. Only used during bytecode generation. */ 45 typedef struct BCInsLine { 46 BCIns ins; /* Bytecode instruction. */ 47 BCLine line; /* Line number for this bytecode. */ 48 } BCInsLine; 49 50 /* Info for local variables. Only used during bytecode generation. */ 51 typedef struct VarInfo { 52 GCRef name; /* Local variable name or goto/label name. */ 53 BCPos startpc; /* First point where the local variable is active. */ 54 BCPos endpc; /* First point where the local variable is dead. */ 55 uint8_t slot; /* Variable slot. */ 56 uint8_t info; /* Variable/goto/label info. */ 57 } VarInfo; 58 59 /* Lua lexer state. */ 60 typedef struct LexState { 61 struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ 62 struct lua_State *L; /* Lua state. */ 63 TValue tokval; /* Current token value. */ 64 TValue lookaheadval; /* Lookahead token value. */ 65 const char *p; /* Current position in input buffer. */ 66 const char *pe; /* End of input buffer. */ 67 LexChar c; /* Current character. */ 68 LexToken tok; /* Current token. */ 69 LexToken lookahead; /* Lookahead token. */ 70 SBuf sb; /* String buffer for tokens. */ 71 lua_Reader rfunc; /* Reader callback. */ 72 void *rdata; /* Reader callback data. */ 73 BCLine linenumber; /* Input line counter. */ 74 BCLine lastline; /* Line of last token. */ 75 GCstr *chunkname; /* Current chunk name (interned string). */ 76 GCstr *env; /* Literal "_ENV" (interned string). */ 77 const char *chunkarg; /* Chunk name argument. */ 78 const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ 79 VarInfo *vstack; /* Stack for names and extents of local variables. */ 80 MSize sizevstack; /* Size of variable stack. */ 81 MSize vtop; /* Top of variable stack. */ 82 BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */ 83 MSize sizebcstack; /* Size of bytecode stack. */ 84 uint32_t level; /* Syntactical nesting level. */ 85 } LexState; 86 87 LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls); 88 LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls); 89 LJ_FUNC void lj_lex_next(LexState *ls); 90 LJ_FUNC LexToken lj_lex_lookahead(LexState *ls); 91 LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken tok); 92 LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...); 93 LJ_FUNC void lj_lex_init(lua_State *L); 94 95 #endif