Skip to content
Newer
Older
100644 132 lines (112 sloc) 3.6 KB
00a8c43 @beelsebob MapCSS fun times
authored
1 //
2 // OSPMapCSSParser.m
3 // OpenStreetPad
4 //
5 // Created by Thomas Davie on 29/10/2011.
6 // Copyright (c) 2011 Thomas Davie. All rights reserved.
7 //
8
9 #import "OSPMapCSSParser.h"
10
11 #import "CoreParse.h"
12
13 @interface OSPMapCSSParser () <CPTokeniserDelegate>
14
15 @property (readwrite, strong) CPTokeniser *tokeniser;
16 @property (readwrite, strong) CPParser *parser;
17
18 @end
19
20 @implementation OSPMapCSSParser
21 {
22 NSCharacterSet *symbolsSet;
23 int nestingDepth;
24 BOOL justTokenisedObject;
25 BOOL inRange;
26 }
27
28 @synthesize tokeniser;
29 @synthesize parser;
30
31 - (id)init
32 {
33 self = [super init];
34
35 if (nil != self)
36 {
37 symbolsSet = [NSCharacterSet characterSetWithCharactersInString:@"*[]{}().,;@|-!=<>:!#%"];
38
39 NSDictionary *pt = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"parser" ofType:@"osp"]];
40 [self setTokeniser:[pt objectForKey:@"tokeniser"]];
41 [self setParser:[pt objectForKey:@"parser"]];
42 [[self tokeniser] setDelegate:self];
43 }
44
45 return self;
46 }
47
48 - (OSPMapCSSStyleSheet *)parse:(NSString *)mapCSS
49 {
50 CPTokenStream *stream = [[CPTokenStream alloc] init];
7eea4e1 @beelsebob Added the ability to write OSM XML format. Fixes #23
authored
51 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^()
d4ea996 @beelsebob Git rid of uses of NSThread.
authored
52 {
53 @autoreleasepool
54 {
55 [[self tokeniser] tokenise:mapCSS into:stream];
56 }
57 });
00a8c43 @beelsebob MapCSS fun times
authored
58 return [[OSPMapCSSStyleSheet alloc] initWithRules:[[self parser] parse:stream]];
59 }
60
61 - (BOOL)tokeniser:(CPTokeniser *)tokeniser shouldConsumeToken:(CPToken *)token
62 {
63 NSString *name = [token name];
64 if ([name isEqualToString:@"{"] || [name isEqualToString:@"["])
65 {
66 nestingDepth++;
67 }
68 else if ([name isEqualToString:@"}"] || [name isEqualToString:@"]"])
69 {
70 nestingDepth--;
71 }
72 else if ([name isEqualToString:@"|z"])
73 {
74 inRange = YES;
75 }
76 else if (inRange && ![token isKindOfClass:[CPNumberToken class]] && ![name isEqualToString:@"-"])
77 {
78 inRange = NO;
79 }
80 else if (inRange && [token isKindOfClass:[CPNumberToken class]])
81 {
82 return [[(CPNumberToken *)token number] floatValue] >= 0;
83 }
84 else if ([token isKindOfClass:[CPKeywordToken class]])
85 {
86 return (0 == nestingDepth ||
87 [symbolsSet characterIsMember:[name characterAtIndex:0]] ||
88 [name isEqualToString:@"eval"] ||
3a80d75 @beelsebob Added support for RichardF's new tag('x') syntax.
authored
89 [name isEqualToString:@"tag"] ||
00a8c43 @beelsebob MapCSS fun times
authored
90 [name isEqualToString:@"url"] ||
91 [name isEqualToString:@"set"] ||
92 [name isEqualToString:@"pt"] ||
93 [name isEqualToString:@"px"] ||
94 [name isEqualToString:@"rgb"] ||
95 [name isEqualToString:@"rgba"]);
96 }
97
98 return YES;
99 }
100
101 - (NSArray *)tokeniser:(CPTokeniser *)tokeniser willProduceToken:(CPToken *)token
102 {
103 NSString *name = [token name];
104 if ([token isKindOfClass:[CPWhiteSpaceToken class]])
105 {
106 if (justTokenisedObject)
107 {
108 return [NSArray arrayWithObject:token];
109 }
110 else
111 {
112 return [NSArray array];
113 }
114 }
115
116 justTokenisedObject = NO;
117 if ([name isEqualToString:@"Comment"])
118 {
119 return [NSArray array];
120 }
121
122 if ([name isEqualToString:@"node"] || [name isEqualToString:@"way" ] || [name isEqualToString:@"relation"] ||
123 [name isEqualToString:@"area"] || [name isEqualToString:@"line"] || [name isEqualToString:@"canvas"] || [name isEqualToString:@"*"])
124 {
125 justTokenisedObject = YES;
126 }
127
128 return [NSArray arrayWithObject:token];
129 }
130
131 @end
Something went wrong with that request. Please try again.