lj_lex.h 3.3 KB

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