From 8cde489ab6c4169fb03d810447c18eea0d0eaa14 Mon Sep 17 00:00:00 2001 From: Alex Merry Date: Sat, 23 Mar 2013 03:30:19 +0000 Subject: Make the parser/lexer reentrant No more locking! Also, the interface for TikzGraphAssembler is much simpler. Changes to OSX code are completely untested. --- tikzit/src/common/tikzlexer.lm | 114 ++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 59 deletions(-) (limited to 'tikzit/src/common/tikzlexer.lm') diff --git a/tikzit/src/common/tikzlexer.lm b/tikzit/src/common/tikzlexer.lm index ad99445..3e2e0ed 100644 --- a/tikzit/src/common/tikzlexer.lm +++ b/tikzit/src/common/tikzlexer.lm @@ -1,11 +1,3 @@ -%option nounput -%option yylineno -%s props -%s xcoord -%s ycoord -%s noderef -FLOAT \-?[0-9]*(\.[0-9]+)? - %{ /* * Copyright 2010 Chris Heunen @@ -28,96 +20,100 @@ FLOAT \-?[0-9]*(\.[0-9]+)? */ #import -#ifdef __APPLE__ -#include "y.tab.h" -#else -#include "tikzparser.h" -#endif - -extern char linebuff[500]; -extern int lineno; -extern int tokenpos; +#import "tikzparser.h" %} + +%option reentrant bison-bridge 8bit +%option nounput +%option yylineno +%option noyywrap +%option header-file="common/tikzlexer.h" + +%s props +%s xcoord +%s ycoord +%s noderef + +FLOAT \-?[0-9]*(\.[0-9]+)? + %% -%\n /* ignore end of line */; -\n.* { strncpy(linebuff, yytext+1, 500); - linebuff[499] = 0; // ensure null-terminated - lineno++; - tokenpos = 0; - yyless(1); - } -[ ]+ { tokenpos += yyleng; } /* ignore whitespace */; -[\t]+ { tokenpos += 8*yyleng; } /* ignore whitespace */; -\\begin\{tikzpicture\} { tokenpos += yyleng; return BEGIN_TIKZPICTURE_CMD; } -\\end\{tikzpicture\} { tokenpos += yyleng; return END_TIKZPICTURE_CMD; } -\\begin\{pgfonlayer\} { tokenpos += yyleng; return BEGIN_PGFONLAYER_CMD; } -\\end\{pgfonlayer\} { tokenpos += yyleng; return END_PGFONLAYER_CMD; } -\\draw { tokenpos += yyleng; return DRAW_CMD; } -\\node { tokenpos += yyleng; return NODE_CMD; } -\\path { tokenpos += yyleng; return PATH_CMD; } -rectangle { tokenpos += yyleng; return RECTANGLE; } -node { tokenpos += yyleng; return NODE; } -at { tokenpos += yyleng; return AT; } -to { tokenpos += yyleng; return TO; } -; { tokenpos += yyleng; return SEMICOLON; } + +\n.* { + [yyextra newLineStarted:yytext+1]; + yyless(1); +} +[ ]+ { [yyextra incrementPosBy:yyleng]; } /* ignore whitespace */; +[\t]+ { [yyextra incrementPosBy:8*yyleng]; } /* ignore whitespace */; +\\begin\{tikzpicture\} { [yyextra incrementPosBy:yyleng]; return BEGIN_TIKZPICTURE_CMD; } +\\end\{tikzpicture\} { [yyextra incrementPosBy:yyleng]; return END_TIKZPICTURE_CMD; } +\\begin\{pgfonlayer\} { [yyextra incrementPosBy:yyleng]; return BEGIN_PGFONLAYER_CMD; } +\\end\{pgfonlayer\} { [yyextra incrementPosBy:yyleng]; return END_PGFONLAYER_CMD; } +\\draw { [yyextra incrementPosBy:yyleng]; return DRAW_CMD; } +\\node { [yyextra incrementPosBy:yyleng]; return NODE_CMD; } +\\path { [yyextra incrementPosBy:yyleng]; return PATH_CMD; } +rectangle { [yyextra incrementPosBy:yyleng]; return RECTANGLE; } +node { [yyextra incrementPosBy:yyleng]; return NODE; } +at { [yyextra incrementPosBy:yyleng]; return AT; } +to { [yyextra incrementPosBy:yyleng]; return TO; } +; { [yyextra incrementPosBy:yyleng]; return SEMICOLON; } \([ ]*{FLOAT}[ ]*,[ ]*{FLOAT}[ ]*\) { - tokenpos += 1; + [yyextra incrementPosBy:1]; yyless(1); BEGIN(xcoord); } {FLOAT} { - tokenpos += yyleng; - yylval.pt.x=(float)strtod(yytext,NULL); + [yyextra incrementPosBy:yyleng]; + yylval->pt.x=(float)strtod(yytext,NULL); BEGIN(ycoord); } -, { tokenpos += yyleng; } +, { [yyextra incrementPosBy:yyleng]; } {FLOAT} { - tokenpos += yyleng; - yylval.pt.y=(float)strtod(yytext,NULL); + [yyextra incrementPosBy:yyleng]; + yylval->pt.y=(float)strtod(yytext,NULL); } \) { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; BEGIN(INITIAL); return COORD; } /* when we see "[", change parsing mode */ \[ /*syntaxhlfix]*/ { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; BEGIN(props); return LEFTBRACKET; } -= { tokenpos += yyleng; return EQUALS; } -, { tokenpos += yyleng; return COMMA; } += { [yyextra incrementPosBy:yyleng]; return EQUALS; } +, { [yyextra incrementPosBy:yyleng]; return COMMA; } [^=,\{\] \t]([^=,\{\]]*[^=,\{\] \t])? { - tokenpos += yyleng; - yylval.nsstr=[NSString stringWithUTF8String:yytext]; + [yyextra incrementPosBy:yyleng]; + yylval->nsstr=[NSString stringWithUTF8String:yytext]; return PROPSTRING; } \] { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; BEGIN(INITIAL); return RIGHTBRACKET; } \( { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; BEGIN(noderef); return LEFTPARENTHESIS; } \. { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; return FULLSTOP; } [^\.\{\)]+ { - tokenpos += yyleng; - yylval.nsstr=[NSString stringWithUTF8String:yytext]; + [yyextra incrementPosBy:yyleng]; + yylval->nsstr=[NSString stringWithUTF8String:yytext]; return REFSTRING; } \) { - tokenpos += yyleng; + [yyextra incrementPosBy:yyleng]; BEGIN(INITIAL); return RIGHTPARENTHESIS; } @@ -127,7 +123,7 @@ to { tokenpos += yyleng; return TO; } unsigned int brace_depth = 1; unsigned int escape = 0; while (1) { - char c = input(); + char c = input(yyscanner); // eof reached before closing brace if (c == '\0' || c == EOF) yyterminate(); @@ -147,8 +143,8 @@ to { tokenpos += yyleng; return TO; } NSString *s = [buf copy]; yyleng += 1 + [buf length]; [s autorelease]; - yylval.nsstr = s; - tokenpos += yyleng; + yylval->nsstr = s; + [yyextra incrementPosBy:yyleng]; return DELIMITEDSTRING; } -- cgit v1.2.3