From dbd620bbaf7f28728c2686737b6a1453caaebd25 Mon Sep 17 00:00:00 2001 From: Alex Merry Date: Fri, 22 Mar 2013 22:26:12 +0000 Subject: Rewrite the lexer to be context-aware It turns out Flex can do modal lexing. This means that we can switch mode for optional properties, and also for co-ordinates. As a result, the parser is much simpler and doesn't keel over all the time on valid input. --- tikzit/src/common/tikzlexer.lm | 104 +++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 31 deletions(-) (limited to 'tikzit/src/common/tikzlexer.lm') diff --git a/tikzit/src/common/tikzlexer.lm b/tikzit/src/common/tikzlexer.lm index a9a2e51..3fd7b53 100644 --- a/tikzit/src/common/tikzlexer.lm +++ b/tikzit/src/common/tikzlexer.lm @@ -1,5 +1,10 @@ %option nounput %option yylineno +%s props +%s xcoord +%s ycoord +%s noderef +FLOAT \-?[0-9]*(\.[0-9]+)? %{ // @@ -7,6 +12,9 @@ // TikZiT // // Copyright 2010 Chris Heunen. All rights reserved. +// Copyright 2012 Aleks Kissinger +// Copyright 2012 KJ +// Copyright 2013 Alex Merry // // // This file is part of TikZiT. @@ -45,48 +53,82 @@ extern int tokenpos; tokenpos = 0; yyless(1); } -[ ]+ { tokenpos += yyleng; } /* ignore whitespace */; -[\t]+ { tokenpos += 4*yyleng; } /* ignore whitespace */; -\\begin { tokenpos += yyleng; return LATEXBEGIN; } -\\end { tokenpos += yyleng; return LATEXEND; } -\{tikzpicture\} { tokenpos += yyleng; return TIKZPICTURE; } -\{pgfonlayer\} { tokenpos += yyleng; return PGFONLAYER; } -\( { tokenpos += yyleng; return LEFTPARENTHESIS; } -\) { tokenpos += yyleng; return RIGHTPARENTHESIS; } -\[ { tokenpos += yyleng; return LEFTBRACKET; } -\] { tokenpos += yyleng; return RIGHTBRACKET; } -; { tokenpos += yyleng; return SEMICOLON; } -, { tokenpos += yyleng; return COMMA; } -\. { tokenpos += yyleng; return FULLSTOP; } -= { tokenpos += yyleng; return EQUALS; } -\\draw { tokenpos += yyleng; return DRAW; } -to { tokenpos += yyleng; return TO; } -\\node { tokenpos += yyleng; return NODE; } -\\path { tokenpos += yyleng; return PATH; } -node { tokenpos += yyleng; return ALTNODE; } +[ ]+ { 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; } -[0-9]+ { - tokenpos += yyleng; - yylval.nsstr=[NSString stringWithUTF8String:yytext]; - return NATURALNUMBER; +\([ ]*{FLOAT}[ ]*,[ ]*{FLOAT}[ ]*\) { + tokenpos += 1; + yyless(1); + BEGIN(xcoord); +} +{FLOAT} { + tokenpos += yyleng; + yylval.pt.x=(float)strtod(yytext,NULL); + BEGIN(ycoord); +} +, { tokenpos += yyleng; } +{FLOAT} { + tokenpos += yyleng; + yylval.pt.y=(float)strtod(yytext,NULL); +} +\) { + tokenpos += yyleng; + BEGIN(INITIAL); + return COORD; } -(\-?[0-9]*\.[0-9]+)|(\-?[0-9]+) { - tokenpos += yyleng; + /* when we see "[", change parsing mode */ +\[ /*syntaxhlfix]*/ { + tokenpos += yyleng; + BEGIN(props); + return LEFTBRACKET; +} += { tokenpos += yyleng; return EQUALS; } +, { tokenpos += yyleng; return COMMA; } +[^=,\{\] \t]([^=,\{\]]*[^=,\{\] \t])? { + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; - return REALNUMBER; + return PROPSTRING; +} +\] { + tokenpos += yyleng; + BEGIN(INITIAL); + return RIGHTBRACKET; } -\\?[a-zA-Z<>\-\'][a-zA-Z<>\-\'0-9]* { //' - tokenpos += yyleng; +\( { + tokenpos += yyleng; + BEGIN(noderef); + return LEFTPARENTHESIS; +} +\. { + tokenpos += yyleng; + return FULLSTOP; +} +[^\.\{\)]+ { + tokenpos += yyleng; yylval.nsstr=[NSString stringWithUTF8String:yytext]; - return LWORD; + return REFSTRING; +} +\) { + tokenpos += yyleng; + BEGIN(INITIAL); + return RIGHTPARENTHESIS; } - -\{ { +\{ { NSMutableString *buf = [NSMutableString string]; unsigned int brace_depth = 1; unsigned int escape = 0; -- cgit v1.2.3