forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_parser.mly
executable file
·170 lines (139 loc) · 3.72 KB
/
dot_parser.mly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**************************************************************************/
/* */
/* Ocamlgraph: a generic graph library for OCaml */
/* Copyright (C) 2004-2010 */
/* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles */
/* */
/* This software is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public */
/* License version 2.1, with the special exception on linking */
/* described in file LICENSE. */
/* */
/* This software is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* */
/**************************************************************************/
/* $Id:$ */
/* DOT parser, following http://www.graphviz.org/doc/info/lang.html */
%{
open Dot_ast
open Parsing
let compass_pt = function
| Ident "n" -> N
| Ident "ne" -> Ne
| Ident "e" -> E
| Ident "se" -> Se
| Ident "s" -> S
| Ident "sw" -> Sw
| Ident "w" -> W
| Ident "nw" -> Nw
| _ -> invalid_arg "compass_pt"
%}
%token <Dot_ast.id> ID
%token COLON COMMA EQUAL SEMICOLON EDGEOP
%token STRICT GRAPH DIGRAPH LBRA RBRA LSQ RSQ NODE EDGE SUBGRAPH EOF
%nonassoc ID
%nonassoc LBRA
%type <Dot_ast.file> file
%start file
%%
file:
| strict_opt graph_or_digraph id_opt LBRA stmt_list RBRA EOF
{ { strict = $1; digraph = $2; id = $3; stmts = $5 } }
;
strict_opt:
| /* epsilon */ { false }
| STRICT { true }
;
graph_or_digraph:
| GRAPH { false }
| DIGRAPH { true }
;
stmt_list:
| /* epsilon */ { [] }
| list1_stmt { $1 }
;
list1_stmt:
| stmt semicolon_opt { [$1] }
| stmt semicolon_opt list1_stmt { $1 :: $3 }
;
semicolon_opt:
| /* epsilon */ { () }
| SEMICOLON { () }
;
stmt:
| node_stmt { $1 }
| edge_stmt { $1 }
| attr_stmt { $1 }
| ID EQUAL ID { Equal ($1, $3) }
| subgraph { Subgraph $1 }
;
node_stmt:
| node_id attr_list_opt { Node_stmt ($1, $2) }
;
edge_stmt:
| node edge_rhs attr_list_opt { Edge_stmt ($1, $2, $3) }
;
attr_stmt:
| GRAPH attr_list { Attr_graph $2 }
| NODE attr_list { Attr_node $2 }
| EDGE attr_list { Attr_edge $2 }
;
edge_rhs:
| EDGEOP node edge_rhs_opt { $2 :: $3 }
;
edge_rhs_opt:
| /* epsilon */ { [] }
| EDGEOP node edge_rhs_opt { $2 :: $3 }
;
node:
| node_id { NodeId $1 }
| subgraph { NodeSub $1 }
;
node_id:
| ID port_opt { $1, $2 }
;
port_opt:
| /* epsilon */ { None }
| port { Some $1 }
;
port:
| COLON ID { try PortC (compass_pt $2)
with Invalid_argument _ -> PortId ($2, None) }
| COLON ID COLON ID
{ let cp =
try compass_pt $4 with Invalid_argument _ -> raise Parse_error
in
PortId ($2, Some cp) }
;
attr_list_opt:
| /* epsilon */ { [] }
| attr_list { $1 }
;
attr_list:
| LSQ a_list RSQ { [$2] }
| LSQ a_list RSQ attr_list { $2 :: $4 }
;
id_opt:
| /* epsilon */ { None }
| ID { Some $1 }
;
a_list:
| equality comma_opt { [$1] }
| equality comma_opt a_list { $1 :: $3 }
;
equality:
| ID { $1, None }
| ID EQUAL ID { $1, Some $3 }
;
comma_opt:
| /* epsilon */ { () }
| COMMA { () }
;
subgraph:
| SUBGRAPH ID { SubgraphId $2 }
| SUBGRAPH ID LBRA stmt_list RBRA { SubgraphDef (Some $2, $4) }
| SUBGRAPH LBRA stmt_list RBRA { SubgraphDef (None, $3) }
| LBRA stmt_list RBRA { SubgraphDef (None, $2) }
;