-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParser.c
More file actions
152 lines (129 loc) · 4.14 KB
/
Parser.c
File metadata and controls
152 lines (129 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <assert.h>
#include <lauxlib.h>
#include <lualib.h>
#include "Parser.h"
#include "stack_debug.h"
#include "Statement.h"
#include "node_visitor.h"
#include "gsp_base.h"
#include "gsp_node.h"
#include "gsp_list.h"
#include "gsp_sourcetoken.h"
#include "gsp_sqlparser.h"
static const char* ParserMetatable = "sqlparser.Parser";
/* 建仓一个名为ParserMetatable的全局table变量,填充Parser_methods函数表 */
int Parser_register_on_luaopen(lua_State *L){
if(0 == luaL_newmetatable(L, ParserMetatable)){
luaL_error(L, "userdata key already has the key tname");
}
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
if(!lua_istable(L, -1)){
luaL_error(L,"must table");
}
luaL_register(L, NULL, Parser_object_metatable);
return 1;
}
/* 创建对你Pasrer,同时把全局的ParserMetatable函数表设置给它 */
int Parser_new(lua_State *L){
int vendor = luaL_checkinteger(L, 1);
Parser *p = (Parser *)lua_newuserdata(L, sizeof(Parser));
p->vendor = vendor;
if( gsp_parser_create((gsp_dbvendor) vendor, &p->_parser) != 0){
luaL_error(L, "gsp_parser_create(vendor=%d) faild.", vendor);
return 0;
}
assert(lua_isuserdata (L, -1));
luaL_getmetatable(L, ParserMetatable);
lua_setmetatable(L, -2);
return 1;
}
/* Deallocate Parser object */
int Parser_free(lua_State *L) {
Parser *p = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
if (p->_parser != NULL){
// printf("Parser_free(gsp_parser_free())\n");
gsp_parser_free(p->_parser);
p->_parser = NULL;
}
return 0;
}
int Parser_tostring(lua_State *L){
Parser *p = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
lua_pushfstring(L, "%s(vendor=%d)", ParserMetatable, p->vendor);
return 1;
}
int Parser_check_syntax(lua_State *L){
int rc;
Parser *p = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
const char *query = luaL_checkstring(L, 2);
if (query == NULL || strlen(query)==0){
luaL_error(L, "args 1:sql can not be empty string.");
return 0;
}
rc = gsp_check_syntax(p->_parser, query);
lua_pushnumber(L, rc);
return 1;
}
int Parser_tokenize(lua_State *L){
char * token_str;
struct gsp_sourcetoken *token;
int i, rc;
int token_cnt;
Parser *parser = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
char *query = luaL_checkstring(L, 2);
rc = gsp_tokenize(parser->_parser, query);
if (rc != 0) {
luaL_error(L, "tokenize() failed, rc=[%d]", rc);
return 0;
}
token_cnt = parser->_parser->number_of_token;
lua_newtable(L);
for(i=0; i< token_cnt; i++){
token = &parser->_parser->sourcetokenlist[i];
// TODO: any unicode handling?
token_str = gsp_token_text(token);
if (!token_str) {
lua_gettop(L); // drop stack top ?
luaL_error(L, "tokenize() invalid token");
return 0;
}
lua_pushstring(L, token_str);
free(token_str);
lua_rawseti (L, -2, i+1);
}
return 1;
}
// get nth statement
// Parser.get_statement(n)
int Parser_get_statement(lua_State *L) {
int n = -1;
gsp_sql_statement *stmt;
Statement *statement;
Parser *parser = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
n = luaL_checkinteger(L, 2);
if (parser->_parser == NULL || n < 0 || n >= parser->_parser->nStatement) {
luaL_error(L, "get_statement() index out of bounds");
return 0;
}
stmt = &parser->_parser->pStatement[n];
if (stmt->parseTree == NULL && stmt->stmt == NULL) {
luaL_error(L, "Invalid syntax");
return 0;
}
Statement_FromStatement(L, stmt);
return statement != NULL ? 1 : 0;
}
// Parser.get_statement_count()
int Parser_get_statement_count(lua_State *L){
Parser *parser = (Parser *)luaL_checkudata(L, 1, ParserMetatable);
if (parser==NULL || parser->_parser == NULL){
return 0;
}
lua_pushnumber(L, (double)parser->_parser->nStatement);
return 1;
}