My Project
NPLParser.h
1 #pragma once
2 
3 #include <vector>
4 
5 namespace NPL
6 {
7  using namespace std;
8 
9  /* semantics information */
10  struct SemInfo
11  {
12  public:
13  double r;
14  string ts;
15  };
16 
17  struct Token
18  {
19  int token;
20  SemInfo seminfo;
21  };
22 
23  struct Zio {
24  size_t n; /* bytes still unread */
25  const char *p; /* current position in buffer */
26 
27  Zio(){};
28  Zio(const char *input, size_t nSize):p(input), n(nSize){};
29  };
30 
32  struct LexState
33  {
34  int current; /* current character (charint) */
35  int linenumber; /* input line counter */
36  int lastline; /* line of last token `consumed' */
37  Token t; /* current token */
38  Token lookahead; /* look ahead token */
39  Zio* z; /* input stream */
40  vector<char> buff; /* buffer for tokens */
41  int nestlevel; /* level of nested non-terminals */
43  bool bSucceed;
44  };
45 
47  class NPLLex
48  {
49  public:
50  /* end of stream */
51  static const int EOZ = -1;
52  static const int MAX_INT = 65530;
53  static const int FIRST_RESERVED = 257;
54 
55  /* maximum number of chars that can be read without checking buffer size */
56  static const int MAXNOCHECK = 5;
57  /* extra space to allocate when growing buffer */
58  static const int EXTRABUFF = 32;
59 
60  /* maximum length of a reserved word */
61  static const int TOKEN_LEN = (sizeof("function")/sizeof(char));
62 
63  enum RESERVED {
64  /* terminal symbols denoted by reserved words */
65  TK_AND = FIRST_RESERVED, TK_BREAK,
66  TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
67  TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
68  TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
69  /* other terminal symbols */
70  TK_NAME, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
71  TK_STRING, TK_EOS
72  };
73  /* number of reserved words */
74  static const int NUM_RESERVED = (int)TK_WHILE-FIRST_RESERVED+1;
75 
76  private:
77  LexState m_lexState;
78  Zio m_zio;
79  public:
80 
87  LexState * SetInput(const char* input, int nLen);
88  static int luaX_lex (LexState *LS, SemInfo *seminfo);
89  LexState * GetState(){return &m_lexState;};
90 
91  static const char* FormatString(const char * zFormat,...);
92  static void luaX_errorline (LexState *ls, const char *s, const char *token, int line);
93  static void luaX_error (LexState *ls, const char *s, const char *token);
94  static void luaX_syntaxerror (LexState *ls, const char *msg);
95  static void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
96  static void luaX_lexerror (LexState *ls, const char *s, int token);
97  static const char *luaX_token2str (LexState *ls, int token);
98 
99 
100  private:
102  // static lex functions
104  static inline int next(LexState *LS)
105  {
106  return LS->current = ((LS->z)->n--)>0 ? (int)((unsigned char)(*(LS->z)->p++)) : EOZ;
107  }
108 
109  static void checkbuffer(LexState *LS, int len)
110  {
111  if (((len)+MAXNOCHECK)*sizeof(char) > LS->buff.size())
112  {
113  if (len < 500)
114  LS->buff.resize(len+EXTRABUFF);
115  else
116  {
117  // added by LiXizhi 2007.6.20: in case the file contains super large string, such as a base64 encoded file of 2MB, we will double the size instead using a fixed length.
118  LS->buff.resize(len*2);
119  }
120  }
121  }
122  static void save(LexState *LS, char c, int& l)
123  {
124  LS->buff[l++] = (char)c;
125  }
126  static void save_and_next(LexState *LS, int& l)
127  {
128  save(LS, LS->current, l);
129  next(LS);
130  }
131 
132  static void ThrowError(LexState *ls, const char* errorMsg);
133 
134  static void inclinenumber (LexState *LS);
135  static void read_long_string (LexState *LS, SemInfo *seminfo);
136  static void read_string (LexState *LS, int del, SemInfo *seminfo);
137  static int readname (LexState *LS);
138 
139  /* LUA_NUMBER */
140  static void read_numeral (LexState *LS, int comma, SemInfo *seminfo);
141 
142  };
143 
147  class NPLParser
148  {
149  public:
150  NPLParser(void);
151  ~NPLParser(void);
152 
153 
154  /*
155  ** maximum number of syntactical nested non-terminals: Not too big,
156  ** or may overflow the C stack...
157  */
158  static const int LUA_MAXPARSERLEVEL = 200;
159 
160  public:
161 
168  static bool IsMsgData(const char* input, int nLen);
169 
171  static bool IsPureData(const char* input, int nLen);
172 
174  static bool IsPureTable(const char* input, int nLen);
175 
177  static bool CheckPureDataBlock(LexState *ls);
178 
184  static bool IsIdentifier(const char* str, int nLength);
185 
186  static void enterlevel(LexState *ls)
187  {
188  if (++(ls)->nestlevel > LUA_MAXPARSERLEVEL)
189  NPLLex::luaX_syntaxerror(ls, "too many syntax levels");
190  }
191 
192  static void leavelevel(LexState *ls)
193  {
194  ((ls)->nestlevel--);
195  }
196 
197  static void next (LexState *ls);
198  static void lookahead (LexState *ls);
199  static void error_expected (LexState *ls, int token);
200  static int testnext (LexState *ls, int c);
201  static void check (LexState *ls, int c);
202  static void check_condition(LexState *ls,void* c,const char* msg);
203  static void check_match (LexState *ls, int what, int who, int where);
204  };
205 }
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
lex state
Definition: NPLParser.h:32
for lexer for NPL files
Definition: NPLParser.h:47
Definition: NPLParser.h:23
parser for NPL files.
Definition: NPLParser.h:147
bool bSucceed
true if no error
Definition: NPLParser.h:43
Definition: NPLParser.h:17
Definition: NPLParser.h:10