Commit a06579ec authored by Benoit Barbot's avatar Benoit Barbot
Browse files

improve parser

parent 061a47bb
......@@ -237,14 +237,16 @@ let parse_file file add_node add_arc =
try
let nodelist,arclist = TikzParser.main TikzLexer.token lexbuf in
let nl = Hashtbl.create 10 in
List.iter (fun (pos,name,content) ->
let k = add_node (content,[]) (from_tikz pos) in
List.iter (fun (pos,at,name,content) ->
let k = add_node (content,at) (from_tikz pos) in
Hashtbl.add nl name k
) nodelist;
List.iter (fun (al,start,fin,pl) ->
let s2 = Hashtbl.find nl start
and fin2 = Hashtbl.find nl fin in
let pl2 = List.map (fun pos -> `Point (from_tikz pos)) pl in
let pl2 = List.map (function `Point pos -> `Point (from_tikz pos)
| `ControlPoint pos -> `ControlPoint (from_tikz pos)
| `Text t -> `Text t) pl in
add_arc (al,pl2) s2 fin2) arclist
with
......@@ -252,3 +254,5 @@ let parse_file file add_node add_arc =
Printf.fprintf stderr "%a: Parsing error: unexpected token:'%s'\n"
print_position lexbuf (Lexing.lexeme lexbuf);
failwith "Fail to parse Tikz file format"
......@@ -20,8 +20,7 @@ rule token = parse
| "'" {PRIME}
| '(' {LPAR}
| ')' {RPAR}
| '}' {RBRAK}
| '{' {LBRAK}
| '{' { read_tex_content (Buffer.create 17) lexbuf}
| '\\' {BACKSLASH}
| '[' {LSQBRAK}
| ']' {RSQBRAK}
......@@ -33,7 +32,8 @@ rule token = parse
| "controls" {CONTROLS}
| "begin" {BEGIN}
| "end" {END}
| "tikzpicture" {TIKZPICTURE}
| "and" {AND}
| "{tikzpicture}" {TIKZPICTURE}
| "node" {NODE}
| "draw" {DRAW}
| "path" {PATH}
......@@ -58,5 +58,19 @@ and read_string buf = parse
}
| _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) }
| eof { raise (SyntaxError ("String is not terminated")) }
and read_xml buf = shortest
| eof { raise (SyntaxError ("XML is not terminated")) }
and read_tex_content buf = parse
| '}' { TEXCONTENT (Buffer.contents buf) }
| '\\' '/' { Buffer.add_char buf '/'; read_tex_content buf lexbuf }
| '\\' '\\' { Buffer.add_char buf '\\'; read_tex_content buf lexbuf }
| '\\' 'b' { Buffer.add_char buf '\b'; read_tex_content buf lexbuf }
| '\\' 'f' { Buffer.add_char buf '\012'; read_tex_content buf lexbuf }
| '\\' 'n' { Buffer.add_char buf '\n'; read_tex_content buf lexbuf }
| '\\' 'r' { Buffer.add_char buf '\r'; read_tex_content buf lexbuf }
| '\\' 't' { Buffer.add_char buf '\t'; read_tex_content buf lexbuf }
| [^ '}' '\\']+
{ Buffer.add_string buf (Lexing.lexeme lexbuf);
read_tex_content buf lexbuf
}
| _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) }
| eof { raise (SyntaxError ("String is not terminated")) }
......@@ -5,31 +5,31 @@
%token <int> INT
%token <float> FLOAT
%token <string> STRING
%token <string> TEXCONTENT
%token LPAR RPAR
%token BACKSLASH
%token SEMICOLON COLON PRIME COMMA QMARK
%token LBRAK RBRAK
%token LSQBRAK RSQBRAK
%token EOF
%token BEGIN END
%token BEGIN END AND
%token TIKZPICTURE
%token NODE DRAW AT PATH CONTROLS
%token PATHDELIM CONTROLDELIM
%start main
%type <(((float*float)*string*string) list)*((string list*string*string*(float*float) list ) list)> main
%type <(((float*float)*(string list)*string*string) list)*((string list*string*string*(DrawingGeom.path_elem) list ) list)> main
%%
main:
BACKSLASH BEGIN LBRAK TIKZPICTURE RBRAK
BACKSLASH BEGIN TIKZPICTURE
body
LBRAK TIKZPICTURE RBRAK
{ $6 } ;
TIKZPICTURE
{ $4 } ;
body:
BACKSLASH NODE node body {
let ((x,y),name,content) = $3 in
Printf.printf "\\node (%f,%f) %s %s;\n" x y name content;
let ((x,y),at,name,content) = $3 in
Printf.printf "\\node[] (%f,%f) %s %s;\n" x y name content;
let n,a = $4 in
$3 :: n , a
}
......@@ -44,17 +44,27 @@ let (al,start,fin,pl) = $3 in
| BACKSLASH END { [],[] };
node:
AT LPAR FLOAT COMMA FLOAT RPAR LSQBRAK attribute_list RSQBRAK LPAR STRING RPAR LBRAK STRING RBRAK SEMICOLON { ($3,$5),$11,$14 };
AT LPAR FLOAT COMMA FLOAT RPAR LSQBRAK attribute_list RSQBRAK LPAR STRING RPAR TEXCONTENT SEMICOLON { ($3,$5),$8,$11,$13 };
draw:
LSQBRAK attribute_list RSQBRAK LPAR STRING RPAR PATHDELIM pathlist SEMICOLON {
let pl,fin = $8 in ($2,$5,fin,pl) }
LSQBRAK attribute_list RSQBRAK LPAR STRING RPAR pathlist SEMICOLON {
let pl,fin = $7 in ($2,$5,fin,pl) }
pathlist:
LPAR FLOAT COMMA FLOAT RPAR PATHDELIM pathlist { let l,f = $7 in (($2,$4)::l,f) }
| LPAR STRING RPAR { [],$2 }
PATHDELIM LPAR FLOAT COMMA FLOAT RPAR pathlist
{ let l,f = $7 in (`Point ($3,$5)::l,f) }
| CONTROLDELIM CONTROLS LPAR FLOAT COMMA FLOAT RPAR AND LPAR FLOAT COMMA FLOAT RPAR CONTROLDELIM pathlist2
{ let l,f = $15 in
`ControlPoint ($4,$6)::`ControlPoint ($10,$12)::l,f }
| PATHDELIM LPAR STRING RPAR { [],$3 }
| NODE TEXCONTENT pathlist {
let l,f = $3 in (`Text (0.5,$2)::l,f) }
pathlist2:
LPAR FLOAT COMMA FLOAT RPAR pathlist { let l,f = $6 in (`Point ($2,$4)::l,f) }
| NODE TEXCONTENT pathlist2 {
let l,f = $3 in (`Text (0.5,$2)::l,f) }
| LPAR STRING RPAR { [],$2 }
attribute_list:
| attribute { [$1] }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment