1/*
2** Lexical analyzer.
3** Copyright (C) 2005-2014 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
23enum {
24 TK_OFS = 256,
25#define TKENUM1(name) TK_##name,
26#define TKENUM2(name, sym) TK_##name,
27TKDEF(TKENUM1, TKENUM2)
28#undef TKENUM1
29#undef TKENUM2
30 TK_RESERVED = TK_while - TK_OFS
31};
32
33typedef int LexToken;
34
35/* Combined bytecode ins/line. Only used during bytecode generation. */
36typedef struct BCInsLine {
37 BCIns ins; /* Bytecode instruction. */
38 BCLine line; /* Line number for this bytecode. */
39} BCInsLine;
40
41/* Info for local variables. Only used during bytecode generation. */
42typedef struct VarInfo {
43 GCRef name; /* Local variable name or goto/label name. */
44 BCPos startpc; /* First point where the local variable is active. */
45 BCPos endpc; /* First point where the local variable is dead. */
46 uint8_t slot; /* Variable slot. */
47 uint8_t info; /* Variable/goto/label info. */
48} VarInfo;
49
50/* Lua lexer state. */
51typedef struct LexState {
52 struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */
53 struct lua_State *L; /* Lua state. */
54 TValue tokenval; /* Current token value. */
55 TValue lookaheadval; /* Lookahead token value. */
56 int current; /* Current character (charint). */
57 LexToken token; /* Current token. */
58 LexToken lookahead; /* Lookahead token. */
59 MSize n; /* Bytes left in input buffer. */
60 const char *p; /* Current position in input buffer. */
61 SBuf sb; /* String buffer for tokens. */
62 lua_Reader rfunc; /* Reader callback. */
63 void *rdata; /* Reader callback data. */
64 BCLine linenumber; /* Input line counter. */
65 BCLine lastline; /* Line of last token. */
66 GCstr *chunkname; /* Current chunk name (interned string). */
67 const char *chunkarg; /* Chunk name argument. */
68 const char *mode; /* Allow loading bytecode (b) and/or source text (t). */
69 VarInfo *vstack; /* Stack for names and extents of local variables. */
70 MSize sizevstack; /* Size of variable stack. */
71 MSize vtop; /* Top of variable stack. */
72 BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */
73 MSize sizebcstack; /* Size of bytecode stack. */
74 uint32_t level; /* Syntactical nesting level. */
75} LexState;
76
77LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls);
78LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls);
79LJ_FUNC void lj_lex_next(LexState *ls);
80LJ_FUNC LexToken lj_lex_lookahead(LexState *ls);
81LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken token);
82LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...);
83LJ_FUNC void lj_lex_init(lua_State *L);
84
85#endif
86