summaryrefslogtreecommitdiff
path: root/tikzit/src/common/TikzGraphAssembler.m
blob: 5e60b05cc5dbad59852cd8ed1c1287d284aa53ca (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
182
//
//  TikzGraphAssembler.m
//  TikZiT
//  
//  Copyright 2010 Aleks Kissinger. All rights reserved.
//  
//  
//  This file is part of TikZiT.
//  
//  TikZiT 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 3 of the License, or
//  (at your option) any later version.
//  
//  TikZiT 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 TikZiT.  If not, see <http://www.gnu.org/licenses/>.
//  

#import "TikzGraphAssembler.h"
#import "NSError+Tikzit.h"

extern int yyparse(void);
extern int yylex(void);  
extern int yy_scan_string(const char* yy_str);
extern void yy_delete_buffer(int b);
extern int yylex_destroy(void);


static NSLock *parseLock = nil;
static id currentAssembler = nil;

void yyerror(const char *str) {
	NSLog(@"Parse error: %s", str);
	if (currentAssembler != nil) {
		NSError *error = [NSError
			errorWithMessage:[NSString stringWithCString:str]
						code:TZ_ERR_PARSE];
		[currentAssembler invalidateWithError:error];
	}
}

int yywrap() {
	return 1;
}

@implementation TikzGraphAssembler

- (id)init {
	[super init];
	graph = nil;
	currentNode = nil;
	currentEdge = nil;
	nodeMap = nil;
	return self;
}

- (Graph*)graph { return graph; }
- (NSError*)lastError { return lastError; }

- (GraphElementData *)data {
	if (currentNode != nil) {
		return [currentNode data];
	} else if (currentEdge != nil) {
		return [currentEdge data];
	} else {
		return [graph data];
	}
}

- (Node*)currentNode { return currentNode; }
- (Edge*)currentEdge { return currentEdge; }

- (BOOL)parseTikz:(NSString *)tikz {
	return [self parseTikz:tikz forGraph:[Graph graph]];
}

- (BOOL)parseTikz:(NSString*)tikz forGraph:(Graph*)gr {
	[parseLock lock];
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	currentAssembler = self;
	
	// set the current graph
	if (graph != gr) {
		[graph release];
		graph = [gr retain];
	}
	
	// the node map keeps track of the mapping of names to nodes
	nodeMap = [[NSMutableDictionary alloc] init];
	
	// do the parsing
	yy_scan_string([tikz UTF8String]);
	yyparse();   
	yylex_destroy();
	
	[nodeMap release];
	nodeMap = nil;
	
	currentAssembler = nil;
	[pool drain];
	
	[parseLock unlock];
	
	return (graph != nil);
}

- (void)prepareNode {
	currentNode = [[Node alloc] init];
}

- (void)finishNode {
	if (currentEdge != nil) { // this is an edge node
		[currentEdge setEdgeNode:currentNode];
	} else { // this is a normal node
		[graph addNode:currentNode];
		[nodeMap setObject:currentNode forKey:[currentNode name]];
	}
	
	[currentNode release];
	currentNode = nil;
}

- (void)prepareEdge {
	currentEdge = [[Edge alloc] init];
}

- (void)finishEdge {
	[currentEdge setAttributesFromData];
	[graph addEdge:currentEdge];
	[currentEdge release];
	currentEdge = nil;
}

- (void)setEdgeSource:(NSString*)src target:(NSString*)targ {
	if (![targ isEqualToString:@""]) {
		[currentEdge setSource:[nodeMap objectForKey:src]];
		[currentEdge setTarget:[nodeMap objectForKey:targ]];
	} else {
		Node *s = [nodeMap objectForKey:src];
		[currentEdge setSource:s];
		[currentEdge setTarget:s];
	}
}

- (void)dealloc {
	[graph release];
	[lastError release];
	[super dealloc];
}

- (void)invalidate {
	[graph release];
	graph = nil;
	lastError = nil;
}

- (void)invalidateWithError:(NSError*)error {
	[self invalidate];
	lastError = [error retain];
}

+ (void)setup {
	parseLock = [[NSLock alloc] init];
}

+ (TikzGraphAssembler*)currentAssembler {
	return currentAssembler;
}

+ (TikzGraphAssembler*)assembler {
	return [[[TikzGraphAssembler alloc] init] autorelease];
}

@end

// vi:ft=objc:ts=4:noet:sts=4:sw=4