syntax icon indicating copy to clipboard operation
syntax copied to clipboard

Make generated parsers thread-safe

Open DmitrySoshnikov opened this issue 8 years ago • 1 comments

Currently "global" variables like yytext, yyleng, $$, etc. are implemented either as module-level variables (in JS), or static class variables (in some plugins, C#, PHP, etc).

This makes parsing several files in parallel (e.g. in threads) impossible, since they mutate the same "global" data. We should make them instance variables, and always export a class for Tokenizer and the Parser (instead of a singleton instance which just access the global or static class variables).

This will require rewriting global code in handles like:

yytext = yytext.slice(1, -1); 

to an instance property access. Currently we already rewrite it to static props access in some plugins):

Yyparse.yytext = Yyparse.yytext.slice(1, -1); 

Eventually should be instance prop (specific to each plugin) instead of static one:

this.yytext = this.yytext.slice(1, -1); 

DmitrySoshnikov avatar Jan 12 '17 19:01 DmitrySoshnikov

You might look at the lemon parser used by SQLite for techniques to fix this. It’s been thread-safe since it was initially written.

It’s also in use at http://pikchr.org

snicolai avatar Oct 03 '20 02:10 snicolai