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