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

Add an option to the lexer to parse strings with single and or double quotes

mingodad 6 лет назад
Родитель
Сommit
e40dc0fa7e
3 измененных файлов с 18 добавлено и 6 удалено
  1. 11 3
      SquiLu/squirrel/sq_lexer.cpp
  2. 4 2
      SquiLu/squirrel/sqlexer.cpp
  3. 3 1
      SquiLu/squirrel/sqlexer.h

+ 11 - 3
SquiLu/squirrel/sq_lexer.cpp

@@ -91,7 +91,7 @@ static SQRESULT sq_SQLexer_constructor(HSQUIRRELVM v){
 
 
     sq_SQLexer_reset_src(v, self);
     sq_SQLexer_reset_src(v, self);
     self->lex = (CustomSQLexer*)sq_malloc(sizeof(CustomSQLexer));
     self->lex = (CustomSQLexer*)sq_malloc(sizeof(CustomSQLexer));
-    new (self->lex) CustomSQLexer(v, ((_top > 3) ? 4 : 0));
+    new (self->lex) CustomSQLexer(v, ((_top > 4) ? 5 : 0));
     self->vm = v;
     self->vm = v;
 
 
     SQBool want_comments = SQFalse;
     SQBool want_comments = SQFalse;
@@ -101,7 +101,15 @@ static SQRESULT sq_SQLexer_constructor(HSQUIRRELVM v){
         sq_getbool(v, 3, &want_comments);
         sq_getbool(v, 3, &want_comments);
     }
     }
 
 
-    self->lex->Init(v->_sharedstate, sq_strbuf_lexfeed, &self->buf, NULL, NULL, want_comments);
+    SQBool want_stringSingleAndDoubleQuotes = SQFalse;
+    if(_top > 3)
+    {
+        //we want comments returned by the lexer
+        sq_getbool(v, 4, &want_stringSingleAndDoubleQuotes);
+    }
+
+    self->lex->Init(v->_sharedstate, sq_strbuf_lexfeed, &self->buf, NULL, NULL,
+                    want_comments, want_stringSingleAndDoubleQuotes);
 
 
     sq_setinstanceup(v, 1, self);
     sq_setinstanceup(v, 1, self);
     sq_setreleasehook(v,1, SQLexer_release_hook);
     sq_setreleasehook(v,1, SQLexer_release_hook);
@@ -274,7 +282,7 @@ static SQRESULT sq_SQLexer_readcount(HSQUIRRELVM v){
 #define _DECL_SQLEXER_FUNC(name,nparams,pmask) {_SC(#name),sq_SQLexer_##name,nparams,pmask,false}
 #define _DECL_SQLEXER_FUNC(name,nparams,pmask) {_SC(#name),sq_SQLexer_##name,nparams,pmask,false}
 static SQRegFunction SQLexer_obj_funcs[]={
 static SQRegFunction SQLexer_obj_funcs[]={
 
 
-	_DECL_SQLEXER_FUNC(constructor, -2, _SC(".sbt")),
+	_DECL_SQLEXER_FUNC(constructor, -2, _SC(".sbbt")),
 	_DECL_SQLEXER_FUNC(reset, 2, _SC(".s")),
 	_DECL_SQLEXER_FUNC(reset, 2, _SC(".s")),
 	_DECL_SQLEXER_FUNC(tok2str, 2, _SC(".i")),
 	_DECL_SQLEXER_FUNC(tok2str, 2, _SC(".i")),
 	_DECL_SQLEXER_FUNC(token_name, 2, _SC(".i")),
 	_DECL_SQLEXER_FUNC(token_name, 2, _SC(".i")),

+ 4 - 2
SquiLu/squirrel/sqlexer.cpp

@@ -27,9 +27,11 @@ SQLexer::~SQLexer()
 }
 }
 
 
 SQInteger SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg,
 SQInteger SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg,
-                        SQUserPointer up,CompilerErrorFunc efunc,void *ed, SQBool want_comments)
+                        SQUserPointer up,CompilerErrorFunc efunc,void *ed,
+                        SQBool want_comments, SQBool want_stringSingleAndDoubleQuotes)
 {
 {
     _want_comments = want_comments;
     _want_comments = want_comments;
+    _want_stringSingleAndDoubleQuotes = want_stringSingleAndDoubleQuotes;
     data = &_data;
     data = &_data;
     _data_lookahead.currentline = -1;
     _data_lookahead.currentline = -1;
 	_errfunc = efunc;
 	_errfunc = efunc;
@@ -787,7 +789,7 @@ try_again:
 	}
 	}
 	TERMINATE_BUFFER();
 	TERMINATE_BUFFER();
 	SQInteger len = data->longstr.size()-1;
 	SQInteger len = data->longstr.size()-1;
-	if(ndelim == _SC('\'')) {
+	if(ndelim == _SC('\'') && !_want_stringSingleAndDoubleQuotes) {
 		if(len == 0) return Error(_SC("empty constant"));
 		if(len == 0) return Error(_SC("empty constant"));
 		if(len > 1) return Error(_SC("constant too long"));
 		if(len > 1) return Error(_SC("constant too long"));
 		data->nvalue = data->longstr[0];
 		data->nvalue = data->longstr[0];

+ 3 - 1
SquiLu/squirrel/sqlexer.h

@@ -77,7 +77,8 @@ struct SQLexer
 	SQLexer();
 	SQLexer();
 	virtual ~SQLexer();
 	virtual ~SQLexer();
 	SQInteger Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,
 	SQInteger Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,
-                CompilerErrorFunc efunc,void *ed, SQBool want_comments=SQFalse);
+                CompilerErrorFunc efunc,void *ed, SQBool want_comments=SQFalse,
+                SQBool want_stringSingleAndDoubleQuotes=SQFalse);
 	SQInteger ResetReader(SQLEXREADFUNC rg, SQUserPointer up, SQInteger line);
 	SQInteger ResetReader(SQLEXREADFUNC rg, SQUserPointer up, SQInteger line);
 	virtual SQTable * GetKeywords();
 	virtual SQTable * GetKeywords();
 	SQInteger Error(const SQChar *err, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
 	SQInteger Error(const SQChar *err, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
@@ -112,6 +113,7 @@ public:
 	CompilerErrorFunc _errfunc;
 	CompilerErrorFunc _errfunc;
 	void *_errtarget;
 	void *_errtarget;
 	SQBool _want_comments;
 	SQBool _want_comments;
+	SQBool _want_stringSingleAndDoubleQuotes;
 };
 };
 
 
 #endif
 #endif