sq_lexer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //#ifdef USE_SQLEXER
  2. #include "sqpcheader.h"
  3. #include "sqobject.h"
  4. #include "sqcompiler.h"
  5. #include "sqstate.h"
  6. #include "sqvm.h"
  7. #include "sqlexer.h"
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h> /* for malloc */
  11. #include <assert.h> /* for a few sanity tests */
  12. static const SQChar SQ_LIBNAME[] = _SC("SQLexer");
  13. SQ_OPT_STRING_STRLEN();
  14. struct sq_lexer_st
  15. {
  16. SQLexer *lex;
  17. HSQOBJECT source;
  18. SQStrBufState buf;
  19. HSQUIRRELVM vm;
  20. };
  21. static const SQChar SQLEXER_Tag[] = _SC("sq_SQLexer_ctx");
  22. #define GET_SQLexer_INSTANCE() SQ_GET_INSTANCE(v, 1, sq_lexer_st, SQLEXER_Tag) \
  23. if(self == NULL) return sq_throwerror(v, _SC("SQLexer object already closed"));
  24. static SQRESULT SQLexer_release_hook(SQUserPointer p, SQInteger size, void */*ep*/)
  25. {
  26. sq_lexer_st *self = (sq_lexer_st*)p;
  27. if(self && self->lex)
  28. {
  29. sq_release(self->vm, &self->source);
  30. self->lex->~SQLexer();
  31. sq_free(self->lex, sizeof(SQLexer));
  32. self->lex = NULL;
  33. sq_free(self, sizeof(sq_lexer_st));
  34. }
  35. return 0;
  36. }
  37. static SQRESULT sq_SQLexer_reset_src(HSQUIRRELVM v, sq_lexer_st *self){
  38. SQ_FUNC_VARS_NO_TOP(v);
  39. SQ_GET_STRING(v, 2, src);
  40. sq_release(v, &self->source);
  41. sq_resetobject(&self->source);
  42. SQRESULT rc = sq_getstackobj(v, 2, &self->source);
  43. if(rc == SQ_OK)
  44. {
  45. sq_addref(v, &self->source);
  46. self->buf.buf = src;
  47. self->buf.ptr = 0;
  48. self->buf.size = src_size;
  49. }
  50. return 1;
  51. }
  52. static SQRESULT sq_SQLexer_constructor(HSQUIRRELVM v){
  53. sq_lexer_st *self = (sq_lexer_st*)sq_malloc(sizeof(sq_lexer_st));//sq_newuserdata(v, sizeof(sq_lexer_st));
  54. memset(self, 0, sizeof(*self));
  55. sq_SQLexer_reset_src(v, self);
  56. self->lex = (SQLexer*)sq_malloc(sizeof(SQLexer));
  57. new (self->lex) SQLexer();
  58. self->vm = v;
  59. SQBool want_comments = SQFalse;
  60. if(sq_gettop(v) > 2)
  61. {
  62. //we want comments returned by the lexer
  63. sq_getbool(v, 3, &want_comments);
  64. }
  65. self->lex->Init(v->_sharedstate, sq_strbuf_lexfeed, &self->buf, NULL, NULL, want_comments);
  66. sq_setinstanceup(v, 1, self);
  67. sq_setreleasehook(v,1, SQLexer_release_hook);
  68. return 1;
  69. }
  70. static SQRESULT sq_SQLexer_reset(HSQUIRRELVM v){
  71. SQ_FUNC_VARS_NO_TOP(v);
  72. GET_SQLexer_INSTANCE();
  73. sq_SQLexer_reset_src(v, self);
  74. self->lex->ResetReader(sq_strbuf_lexfeed, &self->buf, 1);
  75. return 1;
  76. }
  77. static SQRESULT sq_SQLexer_lasterror(HSQUIRRELVM v){
  78. SQ_FUNC_VARS_NO_TOP(v);
  79. GET_SQLexer_INSTANCE();
  80. sq_pushstring(v, self->lex->data->lasterror, -1);
  81. return 1;
  82. }
  83. static SQRESULT sq_SQLexer_longstr(HSQUIRRELVM v){
  84. SQ_FUNC_VARS_NO_TOP(v);
  85. GET_SQLexer_INSTANCE();
  86. sq_pushstring(v, &self->lex->data->longstr[0], self->lex->data->longstr.size());
  87. return 1;
  88. }
  89. static SQRESULT sq_SQLexer_tok2str(HSQUIRRELVM v){
  90. SQ_FUNC_VARS_NO_TOP(v);
  91. GET_SQLexer_INSTANCE();
  92. SQ_GET_INTEGER(v, 2, tok);
  93. const SQChar *tk_name = self->lex->Tok2Str(tok);
  94. if(tk_name) sq_pushstring(v, tk_name, -1);
  95. else sq_pushnull(v);
  96. return 1;
  97. }
  98. static SQRESULT sq_SQLexer_token_name(HSQUIRRELVM v){
  99. SQ_FUNC_VARS_NO_TOP(v);
  100. GET_SQLexer_INSTANCE();
  101. SQ_GET_INTEGER(v, 2, tok);
  102. const SQChar *tk_name = self->lex->GetTokenName(tok);
  103. if(tk_name) sq_pushstring(v, tk_name, -1);
  104. else sq_pushnull(v);
  105. return 1;
  106. }
  107. static SQRESULT sq_SQLexer_svalue(HSQUIRRELVM v){
  108. SQ_FUNC_VARS_NO_TOP(v);
  109. GET_SQLexer_INSTANCE();
  110. if(self->lex->data->svalue) sq_pushstring(v, self->lex->data->svalue, -1);
  111. else sq_pushnull(v);
  112. return 1;
  113. }
  114. static SQRESULT sq_SQLexer_nvalue(HSQUIRRELVM v){
  115. SQ_FUNC_VARS_NO_TOP(v);
  116. GET_SQLexer_INSTANCE();
  117. sq_pushinteger(v, self->lex->data->nvalue);
  118. return 1;
  119. }
  120. static SQRESULT sq_SQLexer_fvalue(HSQUIRRELVM v){
  121. SQ_FUNC_VARS_NO_TOP(v);
  122. GET_SQLexer_INSTANCE();
  123. sq_pushfloat(v, self->lex->data->fvalue);
  124. return 1;
  125. }
  126. static SQRESULT sq_SQLexer_prevtoken(HSQUIRRELVM v){
  127. SQ_FUNC_VARS_NO_TOP(v);
  128. GET_SQLexer_INSTANCE();
  129. sq_pushinteger(v, self->lex->data->prevtoken);
  130. return 1;
  131. }
  132. static SQRESULT sq_SQLexer_lasttokenline(HSQUIRRELVM v){
  133. SQ_FUNC_VARS_NO_TOP(v);
  134. GET_SQLexer_INSTANCE();
  135. sq_pushinteger(v, self->lex->data->lasttokenline);
  136. return 1;
  137. }
  138. static SQRESULT sq_SQLexer_lasttokencolumn(HSQUIRRELVM v){
  139. SQ_FUNC_VARS_NO_TOP(v);
  140. GET_SQLexer_INSTANCE();
  141. sq_pushinteger(v, self->lex->data->lasttokencolumn);
  142. return 1;
  143. }
  144. static SQRESULT sq_SQLexer_currentline(HSQUIRRELVM v){
  145. SQ_FUNC_VARS_NO_TOP(v);
  146. GET_SQLexer_INSTANCE();
  147. sq_pushinteger(v, self->lex->data->currentline);
  148. return 1;
  149. }
  150. static SQRESULT sq_SQLexer_currentcolumn(HSQUIRRELVM v){
  151. SQ_FUNC_VARS_NO_TOP(v);
  152. GET_SQLexer_INSTANCE();
  153. sq_pushinteger(v, self->lex->data->currentcolumn);
  154. return 1;
  155. }
  156. static SQRESULT sq_SQLexer_lex(HSQUIRRELVM v){
  157. SQ_FUNC_VARS_NO_TOP(v);
  158. GET_SQLexer_INSTANCE();
  159. sq_pushinteger(v, self->lex->Lex());
  160. return 1;
  161. }
  162. static SQRESULT sq_SQLexer_lookaheadlex(HSQUIRRELVM v){
  163. SQ_FUNC_VARS_NO_TOP(v);
  164. GET_SQLexer_INSTANCE();
  165. sq_pushinteger(v, self->lex->LookaheadLex());
  166. return 1;
  167. }
  168. static SQRESULT sq_SQLexer_first_enum_token(HSQUIRRELVM v){
  169. SQ_FUNC_VARS_NO_TOP(v);
  170. GET_SQLexer_INSTANCE();
  171. sq_pushinteger(v, TK_FIRST_ENUM_TOKEN);
  172. return 1;
  173. }
  174. static SQRESULT sq_SQLexer_last_enum_token(HSQUIRRELVM v){
  175. SQ_FUNC_VARS_NO_TOP(v);
  176. GET_SQLexer_INSTANCE();
  177. sq_pushinteger(v, TK_LAST_ENUM_TOKEN);
  178. return 1;
  179. }
  180. #define _DECL_SQLEXER_FUNC(name,nparams,pmask) {_SC(#name),sq_SQLexer_##name,nparams,pmask}
  181. static SQRegFunction SQLexer_obj_funcs[]={
  182. _DECL_SQLEXER_FUNC(constructor, -2, _SC(".sb")),
  183. _DECL_SQLEXER_FUNC(reset, 2, _SC(".s")),
  184. _DECL_SQLEXER_FUNC(tok2str, 2, _SC(".i")),
  185. _DECL_SQLEXER_FUNC(token_name, 2, _SC(".i")),
  186. _DECL_SQLEXER_FUNC(lasterror, 1, _SC(".")),
  187. _DECL_SQLEXER_FUNC(longstr, 1, _SC(".")),
  188. _DECL_SQLEXER_FUNC(svalue, 1, _SC(".")),
  189. _DECL_SQLEXER_FUNC(nvalue, 1, _SC(".")),
  190. _DECL_SQLEXER_FUNC(fvalue, 1, _SC(".")),
  191. _DECL_SQLEXER_FUNC(currentline, 1, _SC(".")),
  192. _DECL_SQLEXER_FUNC(currentcolumn, 1, _SC(".")),
  193. _DECL_SQLEXER_FUNC(prevtoken, 1, _SC(".")),
  194. _DECL_SQLEXER_FUNC(lasttokenline, 1, _SC(".")),
  195. _DECL_SQLEXER_FUNC(lasttokencolumn, 1, _SC(".")),
  196. _DECL_SQLEXER_FUNC(first_enum_token, 1, _SC(".")),
  197. _DECL_SQLEXER_FUNC(last_enum_token, 1, _SC(".")),
  198. _DECL_SQLEXER_FUNC(lex, 1, _SC(".")),
  199. _DECL_SQLEXER_FUNC(lookaheadlex, 1, _SC(".")),
  200. {0,0}
  201. };
  202. #undef _DECL_SQLEXER_FUNC
  203. typedef struct {
  204. const SQChar *Str;
  205. SQInteger Val;
  206. } KeyIntType, * KeyIntPtrType;
  207. static KeyIntType SQLexer_constants[] = {
  208. #define MK_CONST(c) {_SC(#c), c}
  209. //MK_CONST(SSL_SESSION_ID_SIZE),
  210. {0,0}
  211. };
  212. #ifdef __cplusplus
  213. extern "C" {
  214. #endif
  215. /* This defines a function that opens up your library. */
  216. SQRESULT sqext_register_SQLexer (HSQUIRRELVM v) {
  217. //add a namespace SQLexer
  218. sq_pushstring(v, SQ_LIBNAME, -1);
  219. sq_newclass(v,SQFalse);
  220. sq_settypetag(v,-1,(SQUserPointer)SQLEXER_Tag);
  221. sq_insert_reg_funcs(v, SQLexer_obj_funcs);
  222. //add constants
  223. KeyIntPtrType KeyIntPtr;
  224. for (KeyIntPtr = SQLexer_constants; KeyIntPtr->Str; KeyIntPtr++) {
  225. sq_pushstring(v, KeyIntPtr->Str, -1); //first the key
  226. sq_pushinteger(v, KeyIntPtr->Val); //then the value
  227. sq_newslot(v, -3, SQFalse); //store then
  228. }
  229. sq_newslot(v,-3,SQFalse); //add SQLexer table to the root table
  230. return SQ_OK;
  231. }
  232. #ifdef __cplusplus
  233. }
  234. //#endif //USE_SQLEXER
  235. #endif