summaryrefslogtreecommitdiff
path: root/tikzit/src/common/tikzparser.ym
blob: 1183f12dbe2f83ecfa063a797cfa9e7b5c7dd81a (plain)
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
171
172
173
174
175
176
177
178
179
180
181
%{
/*
 * Copyright 2010       Chris Heunen
 * Copyright 2010-2013  Aleks Kissinger
 * Copyright 2013       K. Johan Paulsson
 * Copyright 2013       Alex Merry <dev@randomguy3.me.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "Edge.h"

%}

%code requires {
#import "TikzGraphAssembler+Parser.h"
#import "GraphElementData.h"
#import "GraphElementProperty.h"
#import "Node.h"
struct noderef {
	Node *node;
	NSString *anchor;
};
}

%defines "common/tikzparser.h"
%pure-parser
%parse-param {TikzGraphAssembler *assembler}
%error-verbose


%union {
	NSPoint pt;
	NSString *nsstr;
	GraphElementProperty *prop;
	GraphElementData *data;
	Node *node;
	struct noderef noderef;
};

%{
#import "tikzlexer.h"
%}


%token BEGIN_TIKZPICTURE_CMD "\\begin{tikzpicture}"
%token END_TIKZPICTURE_CMD "\\end{tikzpicture}"
%token BEGIN_PGFONLAYER_CMD "\\begin{pgfonlayer}"
%token END_PGFONLAYER_CMD "\\end{pgfonlayer}"
%token DRAW_CMD "\\draw"
%token NODE_CMD "\\node"
%token PATH_CMD "\\path"
%token RECTANGLE "rectangle"
%token NODE "node"
%token AT "at"
%token TO "to"
%token SEMICOLON ";"
%token COMMA ","

%token LEFTPARENTHESIS "("
%token RIGHTPARENTHESIS ")"
%token LEFTBRACKET "["
%token RIGHTBRACKET "]"
%token FULLSTOP "."
%token EQUALS "="
%token <pt> COORD "co-ordinate"
%token <nsstr> PROPSTRING "key/value string"
%token <nsstr> REFSTRING "string"
%token <nsstr> DELIMITEDSTRING "{-delimited string"

%type<nsstr>   nodename
%type<nsstr>   optanchor
%type<nsstr>   val
%type<prop>    property
%type<data>    extraproperties
%type<data>    properties
%type<data>    optproperties
%type<node>    optedgenode
%type<noderef> noderef
%type<noderef> optnoderef

%%

tikzpicture: "\\begin{tikzpicture}" optproperties tikzcmds "\\end{tikzpicture}";
tikzcmds: tikzcmds tikzcmd | ;
tikzcmd: node | edge | boundingbox | ignore;

ignore: "\\begin{pgfonlayer}" DELIMITEDSTRING | "\\end{pgfonlayer}";

optproperties:
	"[" properties "]"
	{ $$ = $2; }
	| { $$ = nil; };
properties: property extraproperties
	{
		[$2 addObject:$1];
		$$ = $2;
	};
extraproperties:
	"," property extraproperties
	{
		[$3 addObject:$2];
		$$ = $3;
	}
	| { $$ = [GraphElementData data]; };
property:
	val "=" val
	{ $$ = [GraphElementProperty property:$1 withValue:$3]; }
	| val
	{ $$ = [GraphElementProperty atom:$1]; };
val: PROPSTRING { $$ = $1; } | DELIMITEDSTRING { $$ = $1; };

nodename: "(" REFSTRING ")" { $$ = $2; };
node: "\\node" optproperties nodename "at" COORD DELIMITEDSTRING ";"
	{
		Node *node = [Node node];
		[node setData:$2];
		[node setName:$3];
		[node setPoint:$5];
		[node setLabel:$6];
		[assembler addNodeToMap:node];
		[[assembler graph] addNode:node];
	};

optanchor:  { $$ = nil; } | "." REFSTRING { $$ = $2; };
noderef: "(" REFSTRING optanchor ")"
	{
		$$.node = [assembler nodeWithName:$2];
		$$.anchor = $3;
	};
optnoderef:
	noderef { $$ = $1; }
	| "(" ")" { $$.node = nil; $$.anchor = nil; }
optedgenode:
	{ $$ = nil; }
	| "node" optproperties DELIMITEDSTRING
	{
		$$ = [Node node];
		[$$ setData:$2];
		[$$ setLabel:$3];
	}
edge: "\\draw" optproperties noderef "to" optedgenode optnoderef ";"
	{
		Edge *edge = [Edge edge];
		[edge setData:$2];
		[edge setSource:$3.node];
		[edge setSourceAnchor:$3.anchor];
		[edge setEdgeNode:$5];
		if ($6.node) {
			[edge setTarget:$6.node];
			[edge setTargetAnchor:$6.anchor];
		} else {
			[edge setTarget:$3.node];
			[edge setTargetAnchor:$3.anchor];
		}
		[edge setAttributesFromData];
		[[assembler graph] addEdge:edge];
	};

ignoreprop: val | val "=" val;
ignoreprops: ignoreprop ignoreprops | ;
optignoreprops: "[" ignoreprops "]";
boundingbox:
	"\\path" optignoreprops COORD "rectangle" COORD ";"
	{
		[[assembler graph] setBoundingBox:NSRectAroundPoints($3, $5)];
	};

/* vi:ft=yacc:noet:ts=4:sts=4:sw=4
*/