1 #ifndef TNT_LEX_H_INCLUDED
2 #define TNT_LEX_H_INCLUDED
3 
4 /*
5  * Redistribution and use in source and binary forms, with or
6  * without modification, are permitted provided that the following
7  * conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above
10  *    copyright notice, this list of conditions and the
11  *    following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials
16  *    provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /* token id's */
34 
35 enum {
36 	TNT_TK_ERROR = -1,
37 	TNT_TK_EOF = 0,
38 	TNT_TK_NONE = 1000,
39 	TNT_TK_NUM32,
40 	TNT_TK_NUM64,
41 	TNT_TK_ID,
42 	TNT_TK_KEY,
43 	TNT_TK_TABLE,
44 	TNT_TK_PUNCT,
45 	TNT_TK_STRING,
46 	TNT_TK_PING,
47 	TNT_TK_UPDATE,
48 	TNT_TK_SET,
49 	TNT_TK_WHERE,
50 	TNT_TK_SPLICE,
51 	TNT_TK_DELETE,
52 	TNT_TK_FROM,
53 	TNT_TK_INSERT,
54 	TNT_TK_REPLACE,
55 	TNT_TK_INTO,
56 	TNT_TK_VALUES,
57 	TNT_TK_SELECT,
58 	TNT_TK_LIMIT,
59 	TNT_TK_CALL,
60 	TNT_TK_OR,
61 	TNT_TK_AND,
62 	TNT_TK_CUSTOM = 2000
63 };
64 
65 /* keyword descriptor */
66 
67 struct tnt_lex_keyword {
68 	char *name;
69 	int size;
70 	int tk;
71 };
72 
73 /* token object */
74 
75 struct tnt_tk {
76 	int tk;
77 	union {
78 		int32_t i32;
79 		int64_t i64;
80 		struct tnt_utf8 s;
81 	} v;
82 	int line, col;
83 	SLIST_ENTRY(tnt_tk) next;
84 	STAILQ_ENTRY(tnt_tk) nextq;
85 };
86 
87 /* token object accessors */
88 
89 #define TNT_TK_S(TK) (&(TK)->v.s)
90 #define TNT_TK_I32(TK) ((TK)->v.i32)
91 #define TNT_TK_I64(TK) ((TK)->v.i64)
92 
93 /* lexer object */
94 
95 struct tnt_lex {
96 	struct tnt_utf8 buf;
97 	struct tnt_lex_keyword *keywords;
98 	size_t pos;
99 	int line, col;
100 	int count;
101 	SLIST_HEAD(,tnt_tk) stack;
102 	int countq;
103 	STAILQ_HEAD(,tnt_tk) q;
104 	bool idonly;
105 	char *error;
106 };
107 
108 bool tnt_lex_init(struct tnt_lex *l, struct tnt_lex_keyword *keywords,
109 		  unsigned char *buf, size_t size);
110 void tnt_lex_free(struct tnt_lex *l);
111 
112 char *tnt_lex_nameof(struct tnt_lex *l, int tk);
113 void tnt_lex_idonly(struct tnt_lex *l, bool on);
114 
115 void tnt_lex_push(struct tnt_lex *l, struct tnt_tk *tk);
116 int tnt_lex(struct tnt_lex *l, struct tnt_tk **tk);
117 
118 #endif /* TNT_LEX_H_INCLUDED */
119