userspace/kp_lex.h - ktap

Data types defined

Macros defined

Source code

  1. /*
  2. * Lexical analyzer.
  3. *
  4. * Copyright (C) 2012-2014 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
  5. *
  6. * Adapted from luajit and lua interpreter.
  7. * Copyright (C) 2005-2014 Mike Pall.
  8. * Copyright (C) 1994-2008 Lua.org, PUC-Rio.
  9. */

  10. #ifndef _KTAP_LEX_H
  11. #define _KTAP_LEX_H

  12. #include <stdarg.h>
  13. #include "../include/err.h"
  14. #include "../include/ktap_bc.h"
  15. #include "kp_util.h"

  16. /* ktap lexer tokens. */
  17. #define TKDEF(_, __) \
  18.     _(trace) _(trace_end) _(argstr) _(probename) _(ffi) \
  19.     _(arg0)_(arg1) _(arg2) _(arg3) _(arg4) _(arg5) _(arg6) _(arg7) \
  20.     _(arg8) _(arg9) _(profile) _(tick) \
  21.     _(pid) _(tid) _(uid) _(cpu) _(execname) __(incr, +=) \
  22.     __(and, &&) _(break) _(do) _(else) _(elseif) _(end) _(false) \
  23.     _(for) _(function) _(goto) _(if) _(in) __(local, var) _(nil) \
  24.     __(not, !) __(or, ||) \
  25.     _(repeat) _(return) _(then) _(true) _(until) _(while) \
  26.     __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) \
  27.     __(ne, !=) __(label, ::) __(number, <number>) __(name, <name>) \
  28.     __(string, <string>) __(eof, <eof>)

  29. enum {
  30.     TK_OFS = 256,
  31. #define TKENUM1(name)        TK_##name,
  32. #define TKENUM2(name, sym)    TK_##name,
  33.     TKDEF(TKENUM1, TKENUM2)
  34. #undef TKENUM1
  35. #undef TKENUM2
  36.     TK_RESERVED = TK_while - TK_OFS
  37. };

  38. typedef int LexChar;    /* Lexical character. Unsigned ext. from char. */
  39. typedef int LexToken;    /* Lexical token. */

  40. /* Combined bytecode ins/line. Only used during bytecode generation. */
  41. typedef struct BCInsLine {
  42.     BCIns ins;        /* Bytecode instruction. */
  43.     BCLine line;        /* Line number for this bytecode. */
  44. } BCInsLine;

  45. /* Info for local variables. Only used during bytecode generation. */
  46. typedef struct VarInfo {
  47.     ktap_str_t *name;    /* Local variable name or goto/label name. */
  48.     BCPos startpc;    /* First point where the local variable is active. */
  49.     BCPos endpc;    /* First point where the local variable is dead. */
  50.     uint8_t slot;    /* Variable slot. */
  51.     uint8_t info;    /* Variable/goto/label info. */
  52. } VarInfo;

  53. /* lexer state. */
  54. typedef struct LexState {
  55.     struct FuncState *fs;    /* Current FuncState. Defined in kp_parse.c. */
  56.     ktap_val_t tokval;    /* Current token value. */
  57.     ktap_val_t lookaheadval;/* Lookahead token value. */
  58.     const char *p;    /* Current position in input buffer. */
  59.     const char *pe;    /* End of input buffer. */
  60.     LexChar c;        /* Current character. */
  61.     LexToken tok;        /* Current token. */
  62.     LexToken lookahead;    /* Lookahead token. */
  63.     SBuf sb;        /* String buffer for tokens. */
  64.     BCLine linenumber;    /* Input line counter. */
  65.     BCLine lastline;    /* Line of last token. */
  66.     ktap_str_t *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.     int sizevstack;    /* Size of variable stack. */
  71.     int vtop;    /* Top of variable stack. */
  72.     BCInsLine *bcstack;/* Stack for bytecode instructions/line numbers. */
  73.     int sizebcstack;/* Size of bytecode stack. */
  74.     uint32_t level;    /* Syntactical nesting level. */
  75. } LexState;

  76. int kp_lex_setup(LexState *ls, const char *str);
  77. void kp_lex_cleanup(LexState *ls);
  78. void kp_lex_next(LexState *ls);
  79. void kp_lex_read_string_until(LexState *ls, int c);
  80. LexToken kp_lex_lookahead(LexState *ls);
  81. const char *kp_lex_token2str(LexState *ls, LexToken tok);
  82. void kp_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...);
  83. void kp_lex_init(void);

  84. #endif