Просмотр исходного кода

Now on the exposed SQLexer accept a an optional table with the keywords to be used by the lex.

mingodad 8 лет назад
Родитель
Сommit
715e694405
3 измененных файлов с 31 добавлено и 10 удалено
  1. 29 8
      SquiLu/squirrel/sq_lexer.cpp
  2. 1 1
      SquiLu/squirrel/sqlexer.cpp
  3. 1 1
      SquiLu/squirrel/sqlexer.h

+ 29 - 8
SquiLu/squirrel/sq_lexer.cpp

@@ -6,6 +6,7 @@
 #include "sqstate.h"
 #include "sqvm.h"
 #include "sqlexer.h"
+#include "sqtable.h"
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>  /* for malloc */
@@ -15,10 +16,30 @@ static const SQChar SQ_LIBNAME[] = _SC("SQLexer");
 
 SQ_OPT_STRING_STRLEN();
 
+class CustomSQLexer : public SQLexer
+{
+public:
+    HSQUIRRELVM _vm;
+    SQInteger _kw_idx;
+
+    CustomSQLexer(HSQUIRRELVM vm, SQInteger kw_idx):
+        _vm(vm), _kw_idx(kw_idx){}
+
+    virtual SQTable * GetKeywords()
+    {
+        if(_vm && _kw_idx)
+        {
+            SQObjectPtr &tbl_var = stack_get(_vm, _kw_idx);
+            SQTable *tbl = _table(tbl_var)->Clone();
+            return tbl;
+        }
+        return SQLexer::GetKeywords();
+    }
+};
 
 struct sq_lexer_st
 {
-    SQLexer *lex;
+    CustomSQLexer *lex;
     HSQOBJECT source;
     SQStrBufState buf;
     HSQUIRRELVM vm;
@@ -35,8 +56,8 @@ static SQRESULT SQLexer_release_hook(SQUserPointer p, SQInteger size, void */*ep
 	if(self && self->lex)
     {
         sq_release(self->vm, &self->source);
-        self->lex->~SQLexer();
-        sq_free(self->lex, sizeof(SQLexer));
+        self->lex->~CustomSQLexer();
+        sq_free(self->lex, sizeof(CustomSQLexer));
         self->lex = NULL;
         sq_free(self, sizeof(sq_lexer_st));
     }
@@ -61,17 +82,17 @@ static SQRESULT sq_SQLexer_reset_src(HSQUIRRELVM v, sq_lexer_st *self){
 }
 
 static SQRESULT sq_SQLexer_constructor(HSQUIRRELVM v){
-
+    SQInteger _top = sq_gettop(v);
 	sq_lexer_st *self = (sq_lexer_st*)sq_malloc(sizeof(sq_lexer_st));//sq_newuserdata(v, sizeof(sq_lexer_st));
 	memset(self, 0, sizeof(*self));
 
     sq_SQLexer_reset_src(v, self);
-    self->lex = (SQLexer*)sq_malloc(sizeof(SQLexer));
-    new (self->lex) SQLexer();
+    self->lex = (CustomSQLexer*)sq_malloc(sizeof(CustomSQLexer));
+    new (self->lex) CustomSQLexer(v, ((_top > 3) ? 4 : 0));
     self->vm = v;
 
     SQBool want_comments = SQFalse;
-    if(sq_gettop(v) > 2)
+    if(_top > 2)
     {
         //we want comments returned by the lexer
         sq_getbool(v, 3, &want_comments);
@@ -232,7 +253,7 @@ static SQRESULT sq_SQLexer_last_enum_token(HSQUIRRELVM v){
 #define _DECL_SQLEXER_FUNC(name,nparams,pmask) {_SC(#name),sq_SQLexer_##name,nparams,pmask}
 static SQRegFunction SQLexer_obj_funcs[]={
 
-	_DECL_SQLEXER_FUNC(constructor, -2, _SC(".sb")),
+	_DECL_SQLEXER_FUNC(constructor, -2, _SC(".sbt")),
 	_DECL_SQLEXER_FUNC(reset, 2, _SC(".s")),
 	_DECL_SQLEXER_FUNC(tok2str, 2, _SC(".i")),
 	_DECL_SQLEXER_FUNC(token_name, 2, _SC(".i")),

+ 1 - 1
SquiLu/squirrel/sqlexer.cpp

@@ -701,7 +701,7 @@ try_again:
                 {
                     //false positive append all chars till here and continue
                     APPEND_CHAR(ndelim);
-                    for(int j=0; j < i; ++j) APPEND_CHAR(cpp_delimin[j]); //recover already eaten chars from buffer
+                    for(size_t j=0; j < i; ++j) APPEND_CHAR(cpp_delimin[j]); //recover already eaten chars from buffer
                     APPEND_CHAR(CUR_CHAR); //append the last one that mismatch
                     if(CUR_CHAR == _SC('\n')) data->currentline++;
                     NEXT();

+ 1 - 1
SquiLu/squirrel/sqlexer.h

@@ -73,7 +73,7 @@ struct SQLexer
 	SQInteger Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,
                 CompilerErrorFunc efunc,void *ed, SQBool want_comments=SQFalse);
 	SQInteger ResetReader(SQLEXREADFUNC rg, SQUserPointer up, SQInteger line);
-	SQTable * GetKeywords();
+	virtual SQTable * GetKeywords();
 	SQInteger Error(const SQChar *err, ...);
 	SQInteger Lex();
 	SQInteger LookaheadLex();