├── COSEigen ├── COSEigen.h └── COSEigen.m ├── COSLayout ├── COSLayout.h ├── COSLayout.m ├── COSLayoutLex.c ├── COSLayoutLex.h ├── COSLayoutParser.c └── COSLayoutParser.h ├── COSObserver ├── COSObserver.h └── COSObserver.m ├── LICENSE └── README.md /COSEigen/COSEigen.h: -------------------------------------------------------------------------------- 1 | // COSEigen.h 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import 27 | 28 | typedef void (*COS_IMP) (void); 29 | typedef void (*COS_IMP_V) (id, SEL, ...); 30 | typedef id (*COS_IMP_I) (id, SEL, ...); 31 | 32 | 33 | @interface COSEigen : NSObject 34 | 35 | + (instancetype)eigenForObject:(NSObject *)object; 36 | 37 | - (void)setMethod:(SEL)sel types:(const char *)types block:(id)block; 38 | - (COS_IMP)superImp:(SEL)sel; 39 | 40 | @end 41 | 42 | 43 | NS_INLINE 44 | COSEigen *COSEigenMake(NSObject *object) { 45 | return [COSEigen eigenForObject:object]; 46 | } 47 | -------------------------------------------------------------------------------- /COSEigen/COSEigen.m: -------------------------------------------------------------------------------- 1 | // COSEigen.m 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import "COSEigen.h" 27 | #import 28 | #import 29 | 30 | 31 | @interface COSEigen () 32 | 33 | @property (atomic, unsafe_unretained) Class eigenClass; 34 | 35 | - (instancetype)initWithEigenClass:(Class)eigenClass; 36 | 37 | - (void)dispose; 38 | 39 | @end 40 | 41 | 42 | @interface COSEigenSlots : NSObject 43 | 44 | + (instancetype)eigenSlotsOfObject:(NSObject *)object; 45 | 46 | @property (atomic, readonly) NSMutableArray *slots; 47 | @property (atomic, assign) OSSpinLock *deallocLock; 48 | 49 | - (void)addEigen:(COSEigen *)eigen; 50 | 51 | @end 52 | 53 | 54 | static SEL deallocSel = NULL; 55 | static const void *classKey = &classKey; 56 | 57 | NS_INLINE 58 | Class cos_class(id self, SEL _cmd) { 59 | return objc_getAssociatedObject(self, classKey); 60 | } 61 | 62 | NS_INLINE 63 | BOOL cos_responds_to_selector(id self, SEL _cmd, SEL sel) { 64 | return class_respondsToSelector(object_getClass(self), sel); 65 | } 66 | 67 | NS_INLINE 68 | Class cos_create_eigen_class(NSObject *object) { 69 | Class eigenClass = Nil; 70 | 71 | char *clsname = NULL; 72 | static const char *fmt = "COSEigen_%s_%p_%u"; 73 | Class class = cos_class(object, NULL); 74 | 75 | while (eigenClass == Nil) { 76 | if (asprintf(&clsname, fmt, class_getName(class), object, arc4random()) > 0) { 77 | eigenClass = objc_allocateClassPair(object_getClass(object), clsname, 0); 78 | free(clsname); 79 | } 80 | } 81 | 82 | objc_registerClassPair(eigenClass); 83 | 84 | return eigenClass; 85 | } 86 | 87 | NS_INLINE 88 | void cos_dispose_eigen_class(Class eigenClass) { 89 | unsigned int count = 0; 90 | Method *methods = class_copyMethodList(eigenClass, &count); 91 | 92 | while (count--) { 93 | imp_removeBlock(method_getImplementation(methods[count])); 94 | } 95 | 96 | if (methods != NULL) free(methods); 97 | 98 | objc_disposeClassPair(eigenClass); 99 | } 100 | 101 | 102 | @implementation COSEigenSlots 103 | 104 | @synthesize slots = _slots; 105 | 106 | + (void)initialize { 107 | deallocSel = sel_registerName("dealloc"); 108 | } 109 | 110 | + (instancetype)eigenSlotsOfObject:(NSObject *)object { 111 | static const void *eigenSlotsKey = &eigenSlotsKey; 112 | 113 | COSEigenSlots *eigenSlots = objc_getAssociatedObject(object, eigenSlotsKey); 114 | 115 | if (eigenSlots) return eigenSlots; 116 | 117 | eigenSlots = [[COSEigenSlots alloc] init]; 118 | 119 | objc_setAssociatedObject(object, eigenSlotsKey, eigenSlots, OBJC_ASSOCIATION_RETAIN); 120 | objc_setAssociatedObject(object, classKey, [object class], OBJC_ASSOCIATION_ASSIGN); 121 | 122 | Class rootEigenClass = cos_create_eigen_class(object); 123 | IMP superDeallocImp = class_getMethodImplementation(object_getClass(object), deallocSel); 124 | OSSpinLock *deallocLock = (OSSpinLock *)malloc(sizeof(OSSpinLock)); 125 | 126 | eigenSlots.deallocLock = deallocLock; 127 | 128 | *deallocLock = OS_SPINLOCK_INIT; 129 | 130 | class_addMethod(rootEigenClass, deallocSel, imp_implementationWithBlock(^(void *self) { 131 | OSSpinLockLock(deallocLock); 132 | ((void(*)(id, SEL))superDeallocImp)((__bridge id)self, deallocSel); 133 | OSSpinLockUnlock(deallocLock); 134 | }), "v@:"); 135 | 136 | [eigenSlots addEigen:[[COSEigen alloc] initWithEigenClass:rootEigenClass]]; 137 | 138 | object_setClass(object, rootEigenClass); 139 | 140 | return eigenSlots; 141 | } 142 | 143 | - (NSMutableArray *)slots { 144 | @synchronized(self) { 145 | return _slots ?: (_slots = [[NSMutableArray alloc] init]); 146 | } 147 | } 148 | 149 | - (void)addEigen:(COSEigen *)eigen { 150 | [self.slots addObject:eigen]; 151 | } 152 | 153 | - (void)dealloc { 154 | NSArray *slots = [self.slots copy]; 155 | OSSpinLock *deallocLock = self.deallocLock; 156 | 157 | dispatch_queue_t queue = [NSThread isMainThread] ? 158 | dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) : 159 | dispatch_get_main_queue(); 160 | 161 | dispatch_async(queue, ^{ 162 | OSSpinLockLock(deallocLock); 163 | for (COSEigen *eigen in slots.reverseObjectEnumerator) { 164 | [eigen dispose]; 165 | } 166 | OSSpinLockUnlock(deallocLock); 167 | free(deallocLock); 168 | }); 169 | } 170 | 171 | @end 172 | 173 | 174 | @implementation COSEigen 175 | 176 | + (instancetype)eigenForObject:(NSObject *)object { 177 | COSEigen *eigen = nil; 178 | 179 | if (object) { 180 | COSEigenSlots *eigenSlots = [COSEigenSlots eigenSlotsOfObject:object]; 181 | 182 | Class eigenClass = cos_create_eigen_class(object); 183 | 184 | class_addMethod(eigenClass, @selector(class), (IMP)cos_class, "#@:"); 185 | class_addMethod(eigenClass, @selector(respondsToSelector:), (IMP)cos_responds_to_selector, "c@::"); 186 | 187 | eigen = [[COSEigen alloc] initWithEigenClass:eigenClass]; 188 | 189 | [eigenSlots addEigen:eigen]; 190 | 191 | object_setClass(object, eigenClass); 192 | } 193 | 194 | return eigen; 195 | } 196 | 197 | - (instancetype)initWithEigenClass:(Class)eigenClass { 198 | if ((self = [super init])) { 199 | self.eigenClass = eigenClass; 200 | } 201 | 202 | return self; 203 | } 204 | 205 | - (void)setMethod:(SEL)sel types:(const char *)types block:(id)block { 206 | unsigned int count; 207 | Method *methods = class_copyMethodList(self.eigenClass, &count); 208 | 209 | while (count--) { 210 | Method method = methods[count]; 211 | if (sel_isEqual(method_getName(method), sel)) { 212 | imp_removeBlock(method_getImplementation(method)); 213 | break; 214 | } 215 | } 216 | 217 | if (methods != NULL) free(methods); 218 | 219 | class_replaceMethod(self.eigenClass, sel, imp_implementationWithBlock(block), types); 220 | } 221 | 222 | - (COS_IMP)superImp:(SEL)sel { 223 | Class superCls = class_getSuperclass(self.eigenClass); 224 | 225 | return (COS_IMP)method_getImplementation(class_getInstanceMethod(superCls, sel)); 226 | } 227 | 228 | - (void)dispose { 229 | cos_dispose_eigen_class(self.eigenClass); 230 | } 231 | 232 | @end 233 | -------------------------------------------------------------------------------- /COSLayout/COSLayout.h: -------------------------------------------------------------------------------- 1 | // COSLayout.h 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import 27 | #import 28 | 29 | 30 | @interface COSLayout : NSObject 31 | 32 | + (instancetype)layoutOfView:(UIView *)view; 33 | 34 | - (void)addRule:(NSString *)format, ...; 35 | - (void)addRule:(NSString *)format args:(va_list)args; 36 | - (void)addRule:(NSString *)format arguments:(NSArray *)arguments; 37 | 38 | @end 39 | 40 | 41 | @interface UIView (COSLayout) 42 | 43 | @property (nonatomic, strong, readonly) COSLayout *coslayout; 44 | 45 | @end 46 | 47 | 48 | @protocol COSCGFloatProtocol 49 | 50 | - (CGFloat)cos_CGFloatValue; 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /COSLayout/COSLayout.m: -------------------------------------------------------------------------------- 1 | // COSLayout.m 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import "COSLayout.h" 27 | #import "COSLayoutParser.h" 28 | 29 | #import 30 | 31 | #define COS_STREQ(a, b) (strcmp(a, b) == 0) 32 | 33 | @class COSLayoutRule; 34 | 35 | typedef CGFloat(^COSFloatBlock)(UIView *); 36 | typedef CGFloat(^COSCoordBlock)(COSLayoutRule *); 37 | 38 | typedef NS_ENUM(NSInteger, COSLayoutDir) { 39 | COSLayoutDirv = 1, 40 | COSLayoutDirh 41 | }; 42 | 43 | static const void *COSLayoutKey = &COSLayoutKey; 44 | static const void *COSLayoutDriverKey = &COSLayoutDriverKey; 45 | 46 | static NSMutableSet *swizzledDriverClasses = nil; 47 | static NSMutableSet *swizzledLayoutClasses = nil; 48 | 49 | static NSString *COSLayoutCycleExceptionName = @"COSLayoutCycleException"; 50 | static NSString *COSLayoutCycleExceptionDesc = @"Layout can not be solved because of cycle"; 51 | 52 | static NSString *COSLayoutSyntaxExceptionName = @"COSLayoutSyntaxException"; 53 | static NSString *COSLayoutSyntaxExceptionDesc = @"Layout rule has a syntax error"; 54 | 55 | 56 | @interface COSCoord : NSObject 57 | 58 | + (instancetype)nilCoord; 59 | 60 | + (instancetype)coordWithFloat:(CGFloat)value; 61 | + (instancetype)coordWithPercentage:(CGFloat)percentage; 62 | + (instancetype)coordWithPercentage:(CGFloat)percentage dir:(COSLayoutDir)dir; 63 | + (instancetype)coordWithBlock:(COSCoordBlock)block; 64 | 65 | @property (nonatomic, strong) NSMutableSet *dependencies; 66 | @property (nonatomic, copy) COSCoordBlock block; 67 | 68 | - (instancetype)add:(COSCoord *)other; 69 | - (instancetype)sub:(COSCoord *)other; 70 | - (instancetype)mul:(COSCoord *)other; 71 | - (instancetype)div:(COSCoord *)other; 72 | 73 | - (BOOL)valid; 74 | 75 | @end 76 | 77 | 78 | @interface COSCoords : NSObject 79 | 80 | + (instancetype)coordsOfView:(UIView *)view; 81 | 82 | @end 83 | 84 | 85 | @interface COSLayoutRule : NSObject 86 | 87 | + (instancetype)layoutRuleWithView:(UIView *)view 88 | name:(NSString *)name 89 | coord:(COSCoord *)coord 90 | dir:(COSLayoutDir)dir; 91 | 92 | @property (nonatomic, weak) UIView *view; 93 | @property (nonatomic, copy) NSString *name; 94 | @property (nonatomic, strong) COSCoord *coord; 95 | @property (nonatomic, assign) COSLayoutDir dir; 96 | 97 | - (CGFloat)floatValue; 98 | 99 | - (BOOL)valid; 100 | 101 | @end 102 | 103 | 104 | @interface COSLayoutRuleHub : NSObject 105 | 106 | @property (nonatomic, readonly) NSMutableArray *vRules; 107 | @property (nonatomic, readonly) NSMutableArray *hRules; 108 | 109 | - (void)vAddRule:(COSLayoutRule *)rule; 110 | - (void)hAddRule:(COSLayoutRule *)rule; 111 | 112 | @end 113 | 114 | 115 | @interface COSLayout () 116 | 117 | @property (nonatomic, weak) UIView *view; 118 | 119 | @property (nonatomic, strong) COSLayoutRuleHub *ruleHub; 120 | @property (nonatomic, strong) NSMutableDictionary *ruleMap; 121 | 122 | @property (nonatomic, strong) COSLayoutRule *wRule; 123 | @property (nonatomic, strong) COSLayoutRule *hRule; 124 | 125 | @property (nonatomic, strong) COSCoord *w; 126 | @property (nonatomic, strong) COSCoord *h; 127 | 128 | @property (nonatomic, strong) COSCoord *minw; 129 | @property (nonatomic, strong) COSCoord *maxw; 130 | 131 | @property (nonatomic, strong) COSCoord *minh; 132 | @property (nonatomic, strong) COSCoord *maxh; 133 | 134 | @property (nonatomic, strong) COSCoord *tt; 135 | @property (nonatomic, strong) COSCoord *tb; 136 | 137 | @property (nonatomic, strong) COSCoord *ll; 138 | @property (nonatomic, strong) COSCoord *lr; 139 | 140 | @property (nonatomic, strong) COSCoord *bb; 141 | @property (nonatomic, strong) COSCoord *bt; 142 | 143 | @property (nonatomic, strong) COSCoord *rr; 144 | @property (nonatomic, strong) COSCoord *rl; 145 | 146 | @property (nonatomic, strong) COSCoord *ct; 147 | @property (nonatomic, strong) COSCoord *cl; 148 | @property (nonatomic, strong) COSCoord *cb; 149 | @property (nonatomic, strong) COSCoord *cr; 150 | 151 | @property (nonatomic, assign) CGRect frame; 152 | 153 | - (instancetype)initWithView:(UIView *)view; 154 | 155 | - (void)updateLayoutDriver; 156 | 157 | - (NSSet *)dependencies; 158 | 159 | - (void)startLayout; 160 | 161 | @end 162 | 163 | 164 | @interface COSLayoutRulesSolver : NSObject 165 | 166 | @property (nonatomic, weak) UIView *view; 167 | 168 | - (CGRect)solveTt:(NSArray *)rules; 169 | - (CGRect)solveTtCt:(NSArray *)rules; 170 | - (CGRect)solveTtBt:(NSArray *)rules; 171 | 172 | - (CGRect)solveLl:(NSArray *)rules; 173 | - (CGRect)solveLlCl:(NSArray *)rules; 174 | - (CGRect)solveLlRl:(NSArray *)rules; 175 | 176 | - (CGRect)solveBt:(NSArray *)rules; 177 | - (CGRect)solveBtCt:(NSArray *)rules; 178 | - (CGRect)solveBtTt:(NSArray *)rules; 179 | 180 | - (CGRect)solveRl:(NSArray *)rules; 181 | - (CGRect)solveRlCl:(NSArray *)rules; 182 | - (CGRect)solveRlLl:(NSArray *)rules; 183 | 184 | - (CGRect)solveCt:(NSArray *)rules; 185 | - (CGRect)solveCtTt:(NSArray *)rules; 186 | - (CGRect)solveCtBt:(NSArray *)rules; 187 | 188 | - (CGRect)solveCl:(NSArray *)rules; 189 | - (CGRect)solveClLl:(NSArray *)rules; 190 | - (CGRect)solveClRl:(NSArray *)rules; 191 | 192 | @end 193 | 194 | 195 | @interface COSLayoutSolver : NSObject 196 | 197 | + (instancetype)layoutSolverOfView:(UIView *)view; 198 | 199 | @property (nonatomic, weak) UIView *view; 200 | 201 | - (instancetype)initWithView:(UIView *)view; 202 | 203 | - (void)solve; 204 | 205 | @end 206 | 207 | 208 | @interface COSLayoutDriver : NSObject 209 | 210 | @property (nonatomic, weak) UIView *view; 211 | 212 | @property (nonatomic, strong) COSLayoutSolver *solver; 213 | 214 | - (instancetype)initWithView:(UIView *)view; 215 | 216 | - (void)beginSolves; 217 | - (void)endSolves; 218 | 219 | @end 220 | 221 | 222 | @implementation COSLayoutRule 223 | 224 | + (instancetype)layoutRuleWithView:(UIView *)view 225 | name:(NSString *)name 226 | coord:(COSCoord *)coord 227 | dir:(COSLayoutDir)dir 228 | { 229 | COSLayoutRule *rule = [[COSLayoutRule alloc] init]; 230 | 231 | rule.view = view; 232 | rule.name = name; 233 | rule.coord = coord; 234 | rule.dir = dir; 235 | 236 | return rule; 237 | } 238 | 239 | - (CGFloat)floatValue { 240 | return self.coord.block(self); 241 | } 242 | 243 | - (BOOL)valid { 244 | return [self.coord valid]; 245 | } 246 | 247 | @end 248 | 249 | 250 | @implementation COSLayoutRuleHub 251 | 252 | @synthesize vRules = _vRules; 253 | @synthesize hRules = _hRules; 254 | 255 | - (NSMutableArray *)vRules { 256 | return _vRules ?: (_vRules = [[NSMutableArray alloc] init]); 257 | } 258 | 259 | - (NSMutableArray *)hRules { 260 | return _hRules ?: (_hRules = [[NSMutableArray alloc] init]); 261 | } 262 | 263 | - (void)addRule:(COSLayoutRule *)rule toRules:(NSMutableArray *)rules { 264 | [rules filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.name != %@", rule.name]]; 265 | 266 | if ([rules count] > 1) [rules removeObjectAtIndex:0]; 267 | 268 | if ([rule valid]) [rules addObject:rule]; 269 | } 270 | 271 | - (void)vAddRule:(COSLayoutRule *)rule { 272 | [self addRule:rule toRules:self.vRules]; 273 | } 274 | 275 | - (void)hAddRule:(COSLayoutRule *)rule { 276 | [self addRule:rule toRules:self.hRules]; 277 | } 278 | 279 | @end 280 | 281 | 282 | #define COS_FRAME_WIDTH (frame.size.width) 283 | #define COS_FRAME_HEIGHT (frame.size.height) 284 | 285 | #define COS_SUPERVIEW_WIDTH (view.superview.bounds.size.width) 286 | #define COS_SUPERVIEW_HEIGHT (view.superview.bounds.size.height) 287 | 288 | #define COSLAYOUT_FRAME(view) \ 289 | ([objc_getAssociatedObject(view, COSLayoutKey) frame]) 290 | 291 | #define COSLAYOUT_SOLVE_SINGLE_H(var, left) \ 292 | do { \ 293 | COSLayoutRule *rule = rules[0]; \ 294 | CGFloat var = [[rule coord] block](rule); \ 295 | UIView *view = _view; \ 296 | CGRect frame = COSLAYOUT_FRAME(view); \ 297 | frame.origin.x = (left); \ 298 | return frame; \ 299 | } while (0) 300 | 301 | #define COSLAYOUT_SOLVE_SINGLE_V(var, top) \ 302 | do { \ 303 | COSLayoutRule *rule = rules[0]; \ 304 | CGFloat var = [[rule coord] block](rule); \ 305 | UIView *view = _view; \ 306 | CGRect frame = COSLAYOUT_FRAME(view); \ 307 | frame.origin.y = (top); \ 308 | return frame; \ 309 | } while (0) 310 | 311 | #define COSLAYOUT_SOLVE_DOUBLE_H(var1, var2, width_, left) \ 312 | do { \ 313 | COSLayoutRule *rule0 = rules[0]; \ 314 | COSLayoutRule *rule1 = rules[1]; \ 315 | CGFloat var1 = [[rule0 coord] block](rule0); \ 316 | CGFloat var2 = [[rule1 coord] block](rule1); \ 317 | UIView *view = _view; \ 318 | CGRect frame = COSLAYOUT_FRAME(view); \ 319 | frame.size.width = [self calcWidth:(width_)]; \ 320 | frame.origin.x = (left); \ 321 | return frame; \ 322 | } while (0) 323 | 324 | #define COSLAYOUT_SOLVE_DOUBLE_V(var1, var2, height_, top) \ 325 | do { \ 326 | COSLayoutRule *rule0 = rules[0]; \ 327 | COSLayoutRule *rule1 = rules[1]; \ 328 | CGFloat var1 = [[rule0 coord] block](rule0); \ 329 | CGFloat var2 = [[rule1 coord] block](rule1); \ 330 | UIView *view = _view; \ 331 | CGRect frame = COSLAYOUT_FRAME(view); \ 332 | frame.size.height = [self calcHeight:(height_)]; \ 333 | frame.origin.y = (top); \ 334 | return frame; \ 335 | } while (0) 336 | 337 | #define COS_MM_RAW_VALUE(layout, var) \ 338 | ({ \ 339 | COSLayoutRule *rule = layout.ruleMap[@(#var)]; \ 340 | \ 341 | [rule valid] ? [rule floatValue] : NAN; \ 342 | }) 343 | 344 | #define COS_VALID_DIM(value) (!isnan(value) && (value) >= 0) 345 | 346 | 347 | @implementation COSLayoutRulesSolver 348 | 349 | - (CGFloat)calcWidth:(CGFloat)width { 350 | COSLayout *layout = objc_getAssociatedObject(_view, COSLayoutKey); 351 | 352 | CGFloat minw = COS_MM_RAW_VALUE(layout, minw); 353 | 354 | if (COS_VALID_DIM(minw) && width < minw) { 355 | width = minw; 356 | } 357 | 358 | CGFloat maxw = COS_MM_RAW_VALUE(layout, maxw); 359 | 360 | if (COS_VALID_DIM(maxw) && width > maxw) { 361 | width = maxw; 362 | } 363 | 364 | return MAX(width, 0); 365 | } 366 | 367 | - (CGFloat)calcHeight:(CGFloat)height { 368 | COSLayout *layout = objc_getAssociatedObject(_view, COSLayoutKey); 369 | 370 | CGFloat minh = COS_MM_RAW_VALUE(layout, minh); 371 | 372 | if (COS_VALID_DIM(minh) && height < minh) { 373 | height = minh; 374 | } 375 | 376 | CGFloat maxh = COS_MM_RAW_VALUE(layout, maxh); 377 | 378 | if (COS_VALID_DIM(maxh) && height > maxh) { 379 | height = maxh; 380 | } 381 | 382 | return MAX(height, 0); 383 | } 384 | 385 | - (CGRect)solveTt:(NSArray *)rules { 386 | COSLAYOUT_SOLVE_SINGLE_V(top, top); 387 | } 388 | 389 | - (CGRect)solveTtCt:(NSArray *)rules { 390 | COSLAYOUT_SOLVE_DOUBLE_V(top, axisY, (axisY - top) * 2, axisY - COS_FRAME_HEIGHT / 2); 391 | } 392 | 393 | - (CGRect)solveTtBt:(NSArray *)rules { 394 | COSLAYOUT_SOLVE_DOUBLE_V(top, bottom, bottom - top, bottom - COS_FRAME_HEIGHT); 395 | } 396 | 397 | - (CGRect)solveLl:(NSArray *)rules { 398 | COSLAYOUT_SOLVE_SINGLE_H(left, left); 399 | } 400 | 401 | - (CGRect)solveLlCl:(NSArray *)rules { 402 | COSLAYOUT_SOLVE_DOUBLE_H(left, axisX, (axisX - left) * 2, axisX - COS_FRAME_WIDTH / 2); 403 | } 404 | 405 | - (CGRect)solveLlRl:(NSArray *)rules { 406 | COSLAYOUT_SOLVE_DOUBLE_H(left, right, right - left, right - COS_FRAME_WIDTH); 407 | } 408 | 409 | - (CGRect)solveBt:(NSArray *)rules { 410 | COSLAYOUT_SOLVE_SINGLE_V(bottom, bottom - COS_FRAME_HEIGHT); 411 | } 412 | 413 | - (CGRect)solveBtCt:(NSArray *)rules { 414 | COSLAYOUT_SOLVE_DOUBLE_V(bottom, axisY, (bottom - axisY) * 2, axisY - COS_FRAME_HEIGHT / 2); 415 | } 416 | 417 | - (CGRect)solveBtTt:(NSArray *)rules { 418 | COSLAYOUT_SOLVE_DOUBLE_V(bottom, top, bottom - top, top); 419 | } 420 | 421 | - (CGRect)solveRl:(NSArray *)rules { 422 | COSLAYOUT_SOLVE_SINGLE_H(right, right - COS_FRAME_WIDTH); 423 | } 424 | 425 | - (CGRect)solveRlCl:(NSArray *)rules { 426 | COSLAYOUT_SOLVE_DOUBLE_H(right, axisX, (right - axisX) * 2, axisX - COS_FRAME_WIDTH / 2); 427 | } 428 | 429 | - (CGRect)solveRlLl:(NSArray *)rules { 430 | COSLAYOUT_SOLVE_DOUBLE_H(right, left, right - left, left); 431 | } 432 | 433 | - (CGRect)solveCt:(NSArray *)rules { 434 | COSLAYOUT_SOLVE_SINGLE_V(axisY, axisY - COS_FRAME_HEIGHT / 2); 435 | } 436 | 437 | - (CGRect)solveCtTt:(NSArray *)rules { 438 | COSLAYOUT_SOLVE_DOUBLE_V(axisY, top, (axisY - top) * 2, top); 439 | } 440 | 441 | - (CGRect)solveCtBt:(NSArray *)rules { 442 | COSLAYOUT_SOLVE_DOUBLE_V(axisY, bottom, (bottom - axisY) * 2, bottom - COS_FRAME_HEIGHT); 443 | } 444 | 445 | - (CGRect)solveCl:(NSArray *)rules { 446 | COSLAYOUT_SOLVE_SINGLE_H(axisX, axisX - COS_FRAME_WIDTH / 2); 447 | } 448 | 449 | - (CGRect)solveClLl:(NSArray *)rules { 450 | COSLAYOUT_SOLVE_DOUBLE_H(axisX, left, (axisX - left) * 2, left); 451 | } 452 | 453 | - (CGRect)solveClRl:(NSArray *)rules { 454 | COSLAYOUT_SOLVE_DOUBLE_H(axisX, right, (right - axisX) * 2, right - COS_FRAME_WIDTH); 455 | } 456 | 457 | @end 458 | 459 | 460 | enum COSLayoutVisitStat { 461 | COSLayoutVisitStatUnvisited, 462 | COSLayoutVisitStatVisiting, 463 | COSLayoutVisitStatVisited 464 | }; 465 | 466 | typedef enum COSLayoutVisitStat COSLayoutVisitStat; 467 | 468 | static const void *COSVisitKey = &COSVisitKey; 469 | 470 | NS_INLINE 471 | void COSMakeViewUnvisited(UIView *view) { 472 | objc_setAssociatedObject(view, COSVisitKey, nil, OBJC_ASSOCIATION_RETAIN); 473 | } 474 | 475 | NS_INLINE 476 | void COSMakeViewVisiting(UIView *view) { 477 | objc_setAssociatedObject(view, COSVisitKey, @(COSLayoutVisitStatVisiting), OBJC_ASSOCIATION_RETAIN); 478 | } 479 | 480 | NS_INLINE 481 | void COSMakeViewVisited(UIView *view) { 482 | objc_setAssociatedObject(view, COSVisitKey, @(COSLayoutVisitStatVisited), OBJC_ASSOCIATION_RETAIN); 483 | } 484 | 485 | 486 | @interface COSLayoutIterator : NSObject 487 | 488 | @property (nonatomic, strong) NSSet *layouts; 489 | 490 | - (NSMutableArray *)viewTopo; 491 | 492 | - (void)iterate; 493 | 494 | @end 495 | 496 | 497 | @implementation COSLayoutIterator { 498 | NSMutableSet *_viewSet; 499 | NSMutableArray *_viewTopo; 500 | } 501 | 502 | - (NSMutableSet *)viewSet { 503 | return (_viewSet ?: (_viewSet = [[NSMutableSet alloc] init])); 504 | } 505 | 506 | - (NSMutableArray *)viewTopo { 507 | return (_viewTopo ?: (_viewTopo = [[NSMutableArray alloc] init])); 508 | } 509 | 510 | - (void)iterate { 511 | [self makeViewSet]; 512 | [self cleanVisitFlag]; 513 | [self makeViewTopo]; 514 | [self cleanVisitFlag]; 515 | } 516 | 517 | - (void)makeViewSet { 518 | for (COSLayout *layout in _layouts) { 519 | [self makeViewSetVisit:layout.view]; 520 | } 521 | } 522 | 523 | - (void)makeViewSetVisit:(UIView *)view { 524 | COSMakeViewVisiting(view); 525 | 526 | COSLayout *layout = objc_getAssociatedObject(view, COSLayoutKey); 527 | 528 | for (UIView *adjView in [layout dependencies]) { 529 | NSNumber *stat = objc_getAssociatedObject(adjView, COSVisitKey); 530 | COSLayoutVisitStat istat = stat ? [stat intValue] : COSLayoutVisitStatUnvisited; 531 | 532 | if (istat == COSLayoutVisitStatUnvisited) { 533 | [self makeViewSetVisit:adjView]; 534 | } else if (istat == COSLayoutVisitStatVisiting) { 535 | [self cleanVisitFlag]; 536 | [self cycleError]; 537 | } 538 | } 539 | 540 | [[self viewSet] addObject:view]; 541 | 542 | COSMakeViewVisited(view); 543 | } 544 | 545 | - (void)makeViewTopo { 546 | for (UIView *view in [self viewSet]) { 547 | NSNumber *stat = objc_getAssociatedObject(view, COSVisitKey); 548 | COSLayoutVisitStat istat = stat ? [stat intValue] : COSLayoutVisitStatUnvisited; 549 | if (istat == COSLayoutVisitStatUnvisited) { 550 | [self makeViewTopoVisit:view]; 551 | } 552 | } 553 | } 554 | 555 | - (void)makeViewTopoVisit:(UIView *)view { 556 | COSMakeViewVisiting(view); 557 | 558 | COSLayout *layout = objc_getAssociatedObject(view, COSLayoutKey); 559 | 560 | for (UIView *adjView in [layout dependencies]) { 561 | NSNumber *stat = objc_getAssociatedObject(adjView, COSVisitKey); 562 | COSLayoutVisitStat istat = stat ? [stat intValue] : COSLayoutVisitStatUnvisited; 563 | 564 | if (istat == COSLayoutVisitStatUnvisited) { 565 | [self makeViewTopoVisit:adjView]; 566 | } else if (istat == COSLayoutVisitStatVisiting) { 567 | [self cleanVisitFlag]; 568 | [self cycleError]; 569 | } 570 | } 571 | 572 | [[self viewTopo] addObject:view]; 573 | 574 | COSMakeViewVisited(view); 575 | } 576 | 577 | - (void)cleanVisitFlag { 578 | for (UIView *view in [self viewSet]) { 579 | COSMakeViewUnvisited(view); 580 | } 581 | 582 | for (UIView *view in [self viewTopo]) { 583 | COSMakeViewUnvisited(view); 584 | } 585 | } 586 | 587 | - (void)cycleError { 588 | [NSException raise:COSLayoutCycleExceptionName format:@"%@", COSLayoutCycleExceptionDesc]; 589 | } 590 | 591 | @end 592 | 593 | 594 | @implementation COSLayoutSolver 595 | 596 | + (instancetype)layoutSolverOfView:(UIView *)view { 597 | static const void *layoutSolverKey = &layoutSolverKey; 598 | 599 | COSLayoutSolver *solver = objc_getAssociatedObject(view, layoutSolverKey); 600 | 601 | if (!solver) { 602 | solver = [[COSLayoutSolver alloc] initWithView:view]; 603 | 604 | objc_setAssociatedObject(view, layoutSolverKey, solver, OBJC_ASSOCIATION_RETAIN); 605 | } 606 | 607 | return solver; 608 | } 609 | 610 | - (instancetype)initWithView:(UIView *)view { 611 | self = [super init]; 612 | 613 | if (self) { 614 | _view = view; 615 | } 616 | 617 | return self; 618 | } 619 | 620 | - (void)solve { 621 | NSArray *subviews = [self.view subviews]; 622 | 623 | NSMutableSet *layouts = [[NSMutableSet alloc] init]; 624 | 625 | for (UIView *subview in subviews) { 626 | COSLayout *layout = objc_getAssociatedObject(subview, COSLayoutKey); 627 | 628 | if (layout) { 629 | [layouts addObject:layout]; 630 | } 631 | } 632 | 633 | COSLayoutIterator *iterator = [[COSLayoutIterator alloc] init]; 634 | 635 | iterator.layouts = layouts; 636 | 637 | [iterator iterate]; 638 | 639 | for (UIView *view in [iterator viewTopo]) { 640 | if (view == _view) continue; 641 | 642 | COSLayout *layout = objc_getAssociatedObject(view, COSLayoutKey); 643 | 644 | [layout startLayout]; 645 | } 646 | } 647 | 648 | @end 649 | 650 | 651 | @implementation COSLayoutDriver 652 | 653 | { 654 | NSInteger stackId; 655 | } 656 | 657 | - (instancetype)initWithView:(UIView *)view { 658 | self = [super init]; 659 | 660 | if (self) { 661 | _view = view; 662 | } 663 | 664 | return self; 665 | } 666 | 667 | - (COSLayoutSolver *)solver { 668 | return _solver ?: (_solver = [COSLayoutSolver layoutSolverOfView:self.view]); 669 | } 670 | 671 | - (void)beginSolves { 672 | stackId += 1; 673 | } 674 | 675 | - (void)endSolves { 676 | if ((--stackId) == 0) { 677 | [self.solver solve]; 678 | } 679 | } 680 | 681 | @end 682 | 683 | 684 | #define COSCOORD_MAKE(dependencies_, expr) \ 685 | ({ \ 686 | __weak UIView *__view = _view; \ 687 | \ 688 | COSCoord *coord = [[COSCoord alloc] init]; \ 689 | \ 690 | coord.dependencies = (dependencies_); \ 691 | coord.block = ^CGFloat(COSLayoutRule *rule) { \ 692 | UIView *view = __view; \ 693 | \ 694 | return (expr); \ 695 | }; \ 696 | \ 697 | coord; \ 698 | }) 699 | 700 | #define COSCOORD_OR_NIL(c_) ({ COSCoord *c = (c_); [c valid] ? c : nil; }) 701 | 702 | #define COSLAYOUT_ADD_RULE(var, dir_) \ 703 | do { \ 704 | _##var = COSCOORD_OR_NIL(var); \ 705 | \ 706 | COSLayoutRule *rule = \ 707 | [COSLayoutRule layoutRuleWithView:_view \ 708 | name:@(#var) \ 709 | coord:_##var \ 710 | dir:COSLayoutDir##dir_]; \ 711 | \ 712 | [self.ruleHub dir_##AddRule:rule]; \ 713 | } while (0) 714 | 715 | #define COSLAYOUT_ADD_TRANS_RULE(var, dst, exp) \ 716 | do { \ 717 | COSCoord *c = _##var = COSCOORD_OR_NIL(var); \ 718 | self.dst = c ? COSCOORD_MAKE(c.dependencies, (exp)) : nil; \ 719 | } while (0) 720 | 721 | #define COSLAYOUT_ADD_BOUND_RULE(var, dir_) \ 722 | do { \ 723 | _##var = COSCOORD_OR_NIL(var); \ 724 | NSString *name = @(#var); \ 725 | \ 726 | COSLayoutRule *rule = \ 727 | [COSLayoutRule layoutRuleWithView:_view \ 728 | name:name \ 729 | coord:_##var \ 730 | dir:COSLayoutDir##dir_]; \ 731 | \ 732 | self.ruleMap[name] = rule; \ 733 | } while (0) 734 | 735 | NS_INLINE 736 | void cos_initialize_layout_if_needed(UIView *view) { 737 | Class class = [view class]; 738 | 739 | if ([swizzledLayoutClasses containsObject:class]) return; 740 | 741 | SEL name = @selector(didMoveToSuperview); 742 | 743 | IMP origImp = class_getMethodImplementation(class, name); 744 | IMP overImp = imp_implementationWithBlock(^(UIView *view) { 745 | ((void(*)(id, SEL))(origImp))(view, name); 746 | 747 | COSLayout *layout = objc_getAssociatedObject(view, COSLayoutKey); 748 | 749 | if (layout) [layout updateLayoutDriver]; 750 | }); 751 | 752 | class_replaceMethod(class, name, overImp, "v@:"); 753 | 754 | [swizzledLayoutClasses addObject:class]; 755 | } 756 | 757 | NS_INLINE 758 | void cos_initialize_driver_if_needed(UIView *view) { 759 | Class class = [view class]; 760 | 761 | if ([swizzledDriverClasses containsObject:class]) return; 762 | 763 | SEL name = @selector(layoutSubviews); 764 | 765 | IMP origImp = class_getMethodImplementation(class, name); 766 | IMP overImp = imp_implementationWithBlock(^(UIView *view) { 767 | COSLayoutDriver *driver = objc_getAssociatedObject(view, COSLayoutDriverKey); 768 | 769 | if (driver) { 770 | [driver beginSolves]; 771 | ((void(*)(id, SEL))(origImp))(view, name); 772 | [driver endSolves]; 773 | } else { 774 | ((void(*)(id, SEL))(origImp))(view, name); 775 | } 776 | }); 777 | 778 | class_replaceMethod(class, name, overImp, "v@:"); 779 | 780 | [swizzledDriverClasses addObject:class]; 781 | } 782 | 783 | 784 | @protocol COSLayoutArguments 785 | 786 | - (CGFloat)floatValue; 787 | - (COSFloatBlock)floatBlockValue; 788 | - (id)objectValue; 789 | 790 | @end 791 | 792 | 793 | @interface COSLayoutVaList : NSObject 794 | 795 | - (instancetype)initWithVaList:(va_list)valist; 796 | 797 | @end 798 | 799 | 800 | @implementation COSLayoutVaList { 801 | va_list _valist; 802 | } 803 | 804 | - (instancetype)initWithVaList:(va_list)valist { 805 | self = [super init]; 806 | 807 | if (self) { 808 | va_copy(_valist, valist); 809 | } 810 | 811 | return self; 812 | } 813 | 814 | - (CGFloat)floatValue { 815 | return va_arg(_valist, double); 816 | } 817 | 818 | - (COSFloatBlock)floatBlockValue { 819 | return va_arg(_valist, COSFloatBlock); 820 | } 821 | 822 | - (id)objectValue { 823 | return va_arg(_valist, id); 824 | } 825 | 826 | - (void)dealloc { 827 | va_end(_valist); 828 | } 829 | 830 | @end 831 | 832 | 833 | @interface COSLayoutArrayArguments : NSObject 834 | 835 | - (instancetype)initWithArray:(NSArray *)array; 836 | 837 | @end 838 | 839 | 840 | @implementation COSLayoutArrayArguments { 841 | NSMutableArray *_array; 842 | } 843 | 844 | - (instancetype)initWithArray:(NSArray *)array { 845 | self = [super init]; 846 | 847 | if (self) { 848 | _array = [array mutableCopy]; 849 | } 850 | 851 | return self; 852 | } 853 | 854 | - (id)shiftArgument { 855 | id argument = [_array firstObject]; 856 | 857 | [_array removeObjectAtIndex:0]; 858 | 859 | return argument; 860 | } 861 | 862 | - (CGFloat)floatValue { 863 | return (CGFloat)[[self shiftArgument] doubleValue]; 864 | } 865 | 866 | - (COSFloatBlock)floatBlockValue { 867 | return [self shiftArgument]; 868 | } 869 | 870 | - (id)objectValue { 871 | return [self shiftArgument]; 872 | } 873 | 874 | @end 875 | 876 | 877 | #define COS_STRING(coord) \ 878 | [NSString stringWithCString:(coord) encoding:NSASCIIStringEncoding] 879 | 880 | 881 | @implementation COSLayout 882 | 883 | + (void)initialize { 884 | static dispatch_once_t onceToken; 885 | 886 | dispatch_once(&onceToken, ^{ 887 | swizzledDriverClasses = [[NSMutableSet alloc] init]; 888 | swizzledLayoutClasses = [[NSMutableSet alloc] init]; 889 | }); 890 | } 891 | 892 | + (instancetype)layoutOfView:(UIView *)view { 893 | if (![view isKindOfClass:[UIView class]]) return nil; 894 | 895 | COSLayout *layout = objc_getAssociatedObject(view, COSLayoutKey); 896 | 897 | if (!layout) { 898 | layout = [[COSLayout alloc] initWithView:view]; 899 | 900 | objc_setAssociatedObject(view, COSLayoutKey, layout, OBJC_ASSOCIATION_RETAIN); 901 | 902 | cos_initialize_layout_if_needed(view); 903 | 904 | [layout updateLayoutDriver]; 905 | } 906 | 907 | return layout; 908 | } 909 | 910 | - (instancetype)initWithView:(UIView *)view { 911 | self = [super init]; 912 | 913 | if (self) { 914 | _view = view; 915 | } 916 | 917 | return self; 918 | } 919 | 920 | - (void)addRule:(NSString *)format, ... { 921 | va_list argv; 922 | va_start(argv, format); 923 | 924 | [self addRule:format args:argv]; 925 | 926 | va_end(argv); 927 | } 928 | 929 | - (void)addRule:(NSString *)format args:(va_list)args { 930 | COSLayoutVaList *valist = [[COSLayoutVaList alloc] initWithVaList:args]; 931 | 932 | [self addRule:format _args:valist]; 933 | } 934 | 935 | - (void)addRule:(NSString *)format arguments:(NSArray *)arguments { 936 | COSLayoutArrayArguments *array = [[COSLayoutArrayArguments alloc] initWithArray:arguments]; 937 | 938 | [self addRule:format _args:array]; 939 | } 940 | 941 | - (void)addRule:(NSString *)format _args:(id)args { 942 | NSArray *subRules = [format componentsSeparatedByString:@","]; 943 | 944 | for (NSString *subRule in subRules) { 945 | COSLAYOUT_AST *ast = NULL; 946 | 947 | char *expr = (char *)[subRule cStringUsingEncoding:NSASCIIStringEncoding]; 948 | 949 | int result = coslayout_parse_rule(expr, &ast); 950 | 951 | switch (result) { 952 | case 0: { 953 | NSMutableSet *keeper = [NSMutableSet set]; 954 | 955 | [self parseAst:ast parent:NULL args:args keeper:keeper]; 956 | 957 | coslayout_destroy_ast(ast); 958 | } 959 | break; 960 | 961 | case 1: { 962 | [NSException raise:COSLayoutSyntaxExceptionName format:@"%@", COSLayoutSyntaxExceptionDesc]; 963 | } 964 | break; 965 | 966 | default: 967 | return; 968 | } 969 | } 970 | 971 | [self layoutSiblingViews]; 972 | } 973 | 974 | - (void)layoutSiblingViews { 975 | UIView *superview = self.view.superview; 976 | 977 | if (superview) { 978 | [[objc_getAssociatedObject(superview, COSLayoutDriverKey) solver] solve]; 979 | } 980 | } 981 | 982 | #define COSCOORD_FOR_NAME(name_) ({ \ 983 | [self valueForKey:name_] ?: ({ \ 984 | COSCoord *coord = [COSCoord coordWithFloat:0]; \ 985 | [keeper addObject:coord]; \ 986 | coord; \ 987 | }); \ 988 | }) 989 | 990 | - (void)parseAst:(COSLAYOUT_AST *)ast parent:(COSLAYOUT_AST *)parent args:(id)args keeper:(NSMutableSet *)keeper { 991 | if (ast == NULL) return; 992 | 993 | [self parseAst:ast->l parent:ast args:args keeper:keeper]; 994 | [self parseAst:ast->r parent:ast args:args keeper:keeper]; 995 | 996 | switch (ast->node_type) { 997 | case COSLAYOUT_TOKEN_ATTR: { 998 | if (parent) { 999 | if (parent->node_type == '=' && 1000 | parent->l == ast) break; 1001 | 1002 | COSCoord *coord = COSCOORD_FOR_NAME(COS_STRING(ast->value.coord)); 1003 | 1004 | ast->data = (__bridge void *)(coord); 1005 | } else { 1006 | [self setValue:[COSCoord coordWithFloat:0] forKey:COS_STRING(ast->value.coord)]; 1007 | } 1008 | } 1009 | break; 1010 | 1011 | case COSLAYOUT_TOKEN_NUMBER: { 1012 | COSCoord *coord = [COSCoord coordWithFloat:ast->value.number]; 1013 | 1014 | ast->data = (__bridge void *)(coord); 1015 | 1016 | [keeper addObject:coord]; 1017 | } 1018 | break; 1019 | 1020 | case COSLAYOUT_TOKEN_PERCENTAGE: 1021 | case COSLAYOUT_TOKEN_PERCENTAGE_H: 1022 | case COSLAYOUT_TOKEN_PERCENTAGE_V: { 1023 | COSLayoutDir dir = 0; 1024 | 1025 | switch (ast->node_type) { 1026 | case COSLAYOUT_TOKEN_PERCENTAGE_H: dir = COSLayoutDirh; break; 1027 | case COSLAYOUT_TOKEN_PERCENTAGE_V: dir = COSLayoutDirv; break; 1028 | } 1029 | 1030 | COSCoord *coord = [COSCoord coordWithPercentage:ast->value.percentage dir:dir]; 1031 | 1032 | ast->data = (__bridge void *)(coord); 1033 | 1034 | [keeper addObject:coord]; 1035 | } 1036 | break; 1037 | 1038 | case COSLAYOUT_TOKEN_COORD: { 1039 | COSCoord *coord = nil; 1040 | char *spec = ast->value.coord; 1041 | 1042 | switch (spec[0]) { 1043 | case '^': { 1044 | COSFloatBlock block = [args floatBlockValue]; 1045 | 1046 | coord = [COSCoord coordWithBlock:^CGFloat(COSLayoutRule *rule) { 1047 | return block(rule.view); 1048 | }]; 1049 | } 1050 | break; 1051 | 1052 | case '@': { 1053 | id value = [args objectValue]; 1054 | 1055 | coord = [COSCoord coordWithBlock:^CGFloat(COSLayoutRule *rule) { 1056 | return [value cos_CGFloatValue]; 1057 | }]; 1058 | } 1059 | break; 1060 | 1061 | default: { 1062 | switch (spec[0]) { 1063 | case 'f': 1064 | coord = [COSCoord coordWithFloat:[args floatValue]]; 1065 | break; 1066 | 1067 | default: { 1068 | COSCoords *coords = [COSCoords coordsOfView:[args objectValue]]; 1069 | coord = [coords valueForKey:COS_STRING(spec)]; 1070 | } 1071 | break; 1072 | } 1073 | } 1074 | break; 1075 | } 1076 | 1077 | ast->data = (__bridge void *)(coord); 1078 | 1079 | [keeper addObject:coord]; 1080 | } 1081 | break; 1082 | 1083 | case COSLAYOUT_TOKEN_COORD_PERCENTAGE: 1084 | case COSLAYOUT_TOKEN_COORD_PERCENTAGE_H: 1085 | case COSLAYOUT_TOKEN_COORD_PERCENTAGE_V: { 1086 | COSCoord *coord = nil; 1087 | char *spec = ast->value.coord; 1088 | 1089 | COSLayoutDir dir = 0; 1090 | 1091 | switch (ast->node_type) { 1092 | case COSLAYOUT_TOKEN_COORD_PERCENTAGE_H: dir = COSLayoutDirh; break; 1093 | case COSLAYOUT_TOKEN_COORD_PERCENTAGE_V: dir = COSLayoutDirv; break; 1094 | } 1095 | 1096 | switch (spec[0]) { 1097 | case '^':{ 1098 | COSFloatBlock block = [args floatBlockValue]; 1099 | 1100 | coord = [COSCoord coordWithBlock:^CGFloat(COSLayoutRule *rule) { 1101 | CGFloat percentage = block(rule.view); 1102 | COSCoord *coord = [COSCoord coordWithPercentage:percentage dir:dir]; 1103 | 1104 | return coord.block(rule); 1105 | }]; 1106 | } 1107 | break; 1108 | 1109 | case '@': { 1110 | id object = [args objectValue]; 1111 | 1112 | coord = [COSCoord coordWithBlock:^CGFloat(COSLayoutRule *rule) { 1113 | CGFloat percentage = [object cos_CGFloatValue]; 1114 | COSCoord *coord = [COSCoord coordWithPercentage:percentage dir:dir]; 1115 | 1116 | return coord.block(rule); 1117 | }]; 1118 | } 1119 | break; 1120 | 1121 | default: 1122 | coord = [COSCoord coordWithPercentage:[args floatValue] dir:dir]; 1123 | break; 1124 | } 1125 | 1126 | ast->data = (__bridge void *)(coord); 1127 | 1128 | [keeper addObject:coord]; 1129 | } 1130 | break; 1131 | 1132 | case COSLAYOUT_TOKEN_NIL: { 1133 | ast->data = (__bridge void *)([COSCoord nilCoord]); 1134 | } 1135 | break; 1136 | 1137 | case '+': { 1138 | COSCoord *coord1 = (__bridge COSCoord *)(ast->l->data); 1139 | COSCoord *coord2 = (__bridge COSCoord *)(ast->r->data); 1140 | 1141 | COSCoord *coord = [coord1 add:coord2]; 1142 | 1143 | ast->data = (__bridge void *)(coord); 1144 | 1145 | [keeper addObject:coord]; 1146 | } 1147 | break; 1148 | 1149 | case '-': { 1150 | COSCoord *coord1 = (__bridge COSCoord *)(ast->l->data); 1151 | COSCoord *coord2 = (__bridge COSCoord *)(ast->r->data); 1152 | 1153 | COSCoord *coord = [coord1 sub:coord2]; 1154 | 1155 | ast->data = (__bridge void *)(coord); 1156 | 1157 | [keeper addObject:coord]; 1158 | } 1159 | break; 1160 | 1161 | case '*': { 1162 | COSCoord *coord1 = (__bridge COSCoord *)(ast->l->data); 1163 | COSCoord *coord2 = (__bridge COSCoord *)(ast->r->data); 1164 | 1165 | COSCoord *coord = [coord1 mul:coord2]; 1166 | 1167 | ast->data = (__bridge void *)(coord); 1168 | 1169 | [keeper addObject:coord]; 1170 | } 1171 | break; 1172 | 1173 | case '/': { 1174 | COSCoord *coord1 = (__bridge COSCoord *)(ast->l->data); 1175 | COSCoord *coord2 = (__bridge COSCoord *)(ast->r->data); 1176 | 1177 | COSCoord *coord = [coord1 div:coord2]; 1178 | 1179 | ast->data = (__bridge void *)(coord); 1180 | 1181 | [keeper addObject:coord]; 1182 | } 1183 | break; 1184 | 1185 | case '=': { 1186 | COSCoord *coord = (__bridge COSCoord *)(ast->r->data); 1187 | 1188 | [self setValue:coord forKey:COS_STRING(ast->l->value.coord)]; 1189 | 1190 | ast->data = (__bridge void *)(coord); 1191 | } 1192 | break; 1193 | 1194 | case COSLAYOUT_TOKEN_ADD_ASSIGN: 1195 | case COSLAYOUT_TOKEN_SUB_ASSIGN: 1196 | case COSLAYOUT_TOKEN_MUL_ASSIGN: 1197 | case COSLAYOUT_TOKEN_DIV_ASSIGN: { 1198 | NSString *name = COS_STRING(ast->l->value.coord); 1199 | 1200 | COSCoord *lval = COSCOORD_FOR_NAME(name); 1201 | COSCoord *rval = (__bridge COSCoord *)(ast->r->data); 1202 | 1203 | SEL sel = NULL; 1204 | 1205 | switch (ast->node_type) { 1206 | case COSLAYOUT_TOKEN_ADD_ASSIGN: sel = @selector(add:); break; 1207 | case COSLAYOUT_TOKEN_SUB_ASSIGN: sel = @selector(sub:); break; 1208 | case COSLAYOUT_TOKEN_MUL_ASSIGN: sel = @selector(mul:); break; 1209 | case COSLAYOUT_TOKEN_DIV_ASSIGN: sel = @selector(div:); break; 1210 | } 1211 | 1212 | IMP imp = [lval methodForSelector:sel]; 1213 | 1214 | COSCoord *coord = ((id(*)(id, SEL, id))(imp))(lval, sel, rval); 1215 | 1216 | [self setValue:coord forKey:name]; 1217 | 1218 | ast->data = (__bridge void *)(coord); 1219 | } 1220 | break; 1221 | 1222 | default: 1223 | break; 1224 | } 1225 | } 1226 | 1227 | - (void)setValue:(id)value forKey:(NSString *)key { 1228 | @try { 1229 | [super setValue:value forKey:key]; 1230 | } @catch (NSException *exception) { 1231 | fprintf(stderr, "COSLayout: Invalid constraint \"%s\", ignored.\n", [key UTF8String]); 1232 | } 1233 | } 1234 | 1235 | - (void)updateLayoutDriver { 1236 | UIView *superview = _view.superview; 1237 | 1238 | if (superview && !objc_getAssociatedObject(superview, COSLayoutDriverKey)) { 1239 | COSLayoutDriver *driver = [[COSLayoutDriver alloc] initWithView:superview]; 1240 | 1241 | objc_setAssociatedObject(superview, COSLayoutDriverKey, driver, OBJC_ASSOCIATION_RETAIN); 1242 | cos_initialize_driver_if_needed(superview); 1243 | } 1244 | } 1245 | 1246 | - (NSSet *)dependencies { 1247 | NSMutableSet *viewSet = [[NSMutableSet alloc] init]; 1248 | NSMutableSet *ruleSet = [[NSMutableSet alloc] init]; 1249 | 1250 | if (self.hRule) { 1251 | [ruleSet addObject:self.hRule]; 1252 | } 1253 | 1254 | if (self.wRule) { 1255 | [ruleSet addObject:self.wRule]; 1256 | } 1257 | 1258 | [ruleSet addObjectsFromArray:self.ruleHub.vRules]; 1259 | [ruleSet addObjectsFromArray:self.ruleHub.hRules]; 1260 | [ruleSet addObjectsFromArray:self.ruleMap.allValues]; 1261 | 1262 | for (COSLayoutRule *rule in ruleSet) { 1263 | [viewSet unionSet:rule.coord.dependencies]; 1264 | } 1265 | 1266 | if (_view.superview) { 1267 | [viewSet addObject:_view.superview]; 1268 | } 1269 | 1270 | return viewSet; 1271 | } 1272 | 1273 | - (void)solveRules:(NSArray *)rules { 1274 | COSLayoutRulesSolver *solver = [[COSLayoutRulesSolver alloc] init]; 1275 | 1276 | solver.view = _view; 1277 | 1278 | NSMutableString *selStr = [NSMutableString stringWithString:@"solve"]; 1279 | 1280 | for (COSLayoutRule *rule in rules) { 1281 | [selStr appendString:[rule.name capitalizedString]]; 1282 | } 1283 | 1284 | [selStr appendString:@":"]; 1285 | 1286 | SEL sel = NSSelectorFromString(selStr); 1287 | CGRect (*imp)(id, SEL, NSArray *) = (void *)[solver methodForSelector:sel]; 1288 | 1289 | _frame = imp(solver, sel, rules); 1290 | 1291 | [self checkBounds]; 1292 | } 1293 | 1294 | - (void)checkBounds { 1295 | CGSize size = _frame.size; 1296 | 1297 | CGFloat minw = COS_MM_RAW_VALUE(self, minw); 1298 | 1299 | if (COS_VALID_DIM(minw) && size.width < minw) { 1300 | size.width = minw; 1301 | } 1302 | 1303 | CGFloat maxw = COS_MM_RAW_VALUE(self, maxw); 1304 | 1305 | if (COS_VALID_DIM(maxw) && size.width > maxw) { 1306 | size.width = maxw; 1307 | } 1308 | 1309 | CGFloat minh = COS_MM_RAW_VALUE(self, minh); 1310 | 1311 | if (COS_VALID_DIM(minh) && size.height < minh) { 1312 | size.height = minh; 1313 | } 1314 | 1315 | CGFloat maxh = COS_MM_RAW_VALUE(self, maxh); 1316 | 1317 | if (COS_VALID_DIM(maxh) && size.height > maxh) { 1318 | size.height = maxh; 1319 | } 1320 | 1321 | _frame.size = size; 1322 | } 1323 | 1324 | - (void)startLayout { 1325 | _frame = _view.frame; 1326 | 1327 | [self checkBounds]; 1328 | 1329 | NSUInteger hRuleCount = [_ruleHub.hRules count]; 1330 | NSUInteger vRuleCount = [_ruleHub.vRules count]; 1331 | 1332 | if (self.wRule && hRuleCount < 2) { 1333 | _frame.size.width = [self.wRule floatValue]; 1334 | } 1335 | 1336 | if (self.hRule && vRuleCount < 2) { 1337 | _frame.size.height = [self.hRule floatValue]; 1338 | } 1339 | 1340 | if (hRuleCount > 0) { 1341 | [self solveRules:_ruleHub.hRules]; 1342 | } 1343 | 1344 | if (vRuleCount > 0) { 1345 | [self solveRules:_ruleHub.vRules]; 1346 | } 1347 | 1348 | [self checkBounds]; 1349 | 1350 | if (!CGRectEqualToRect(_frame, _view.frame)) { 1351 | _view.frame = _frame; 1352 | } 1353 | } 1354 | 1355 | - (void)setW:(COSCoord *)w { 1356 | _w = w; 1357 | self.wRule = [COSLayoutRule layoutRuleWithView:_view name:nil coord:w dir:COSLayoutDirh]; 1358 | } 1359 | 1360 | - (void)setH:(COSCoord *)h { 1361 | _h = h; 1362 | self.hRule = [COSLayoutRule layoutRuleWithView:_view name:nil coord:h dir:COSLayoutDirv]; 1363 | } 1364 | 1365 | - (void)setMinw:(COSCoord *)minw { 1366 | COSLAYOUT_ADD_BOUND_RULE(minw, h); 1367 | } 1368 | 1369 | - (void)setMaxw:(COSCoord *)maxw { 1370 | COSLAYOUT_ADD_BOUND_RULE(maxw, h); 1371 | } 1372 | 1373 | - (void)setMinh:(COSCoord *)minh { 1374 | COSLAYOUT_ADD_BOUND_RULE(minh, v); 1375 | } 1376 | 1377 | - (void)setMaxh:(COSCoord *)maxh { 1378 | COSLAYOUT_ADD_BOUND_RULE(maxh, v); 1379 | } 1380 | 1381 | - (void)setTt:(COSCoord *)tt { 1382 | COSLAYOUT_ADD_RULE(tt, v); 1383 | } 1384 | 1385 | - (void)setTb:(COSCoord *)tb { 1386 | COSLAYOUT_ADD_TRANS_RULE(tb, tt, COS_SUPERVIEW_HEIGHT - tb.block(rule)); 1387 | } 1388 | 1389 | - (void)setLl:(COSCoord *)ll { 1390 | COSLAYOUT_ADD_RULE(ll, h); 1391 | } 1392 | 1393 | - (void)setLr:(COSCoord *)lr { 1394 | COSLAYOUT_ADD_TRANS_RULE(lr, ll, COS_SUPERVIEW_WIDTH - lr.block(rule)); 1395 | } 1396 | 1397 | - (void)setBb:(COSCoord *)bb { 1398 | COSLAYOUT_ADD_TRANS_RULE(bb, bt, COS_SUPERVIEW_HEIGHT - bb.block(rule)); 1399 | } 1400 | 1401 | - (void)setBt:(COSCoord *)bt { 1402 | COSLAYOUT_ADD_RULE(bt, v); 1403 | } 1404 | 1405 | - (void)setRr:(COSCoord *)rr { 1406 | COSLAYOUT_ADD_TRANS_RULE(rr, rl, COS_SUPERVIEW_WIDTH - rr.block(rule)); 1407 | } 1408 | 1409 | - (void)setRl:(COSCoord *)rl { 1410 | COSLAYOUT_ADD_RULE(rl, h); 1411 | } 1412 | 1413 | - (void)setCt:(COSCoord *)ct { 1414 | COSLAYOUT_ADD_RULE(ct, v); 1415 | } 1416 | 1417 | - (void)setCl:(COSCoord *)cl { 1418 | COSLAYOUT_ADD_RULE(cl, h); 1419 | } 1420 | 1421 | - (void)setCb:(COSCoord *)cb { 1422 | COSLAYOUT_ADD_TRANS_RULE(cb, ct, COS_SUPERVIEW_HEIGHT - cb.block(rule)); 1423 | } 1424 | 1425 | - (void)setCr:(COSCoord *)cr { 1426 | COSLAYOUT_ADD_TRANS_RULE(cr, cl, COS_SUPERVIEW_WIDTH - cr.block(rule)); 1427 | } 1428 | 1429 | - (COSLayoutRuleHub *)ruleHub { 1430 | return (_ruleHub ?: (_ruleHub = [[COSLayoutRuleHub alloc] init])); 1431 | } 1432 | 1433 | - (NSMutableDictionary *)ruleMap { 1434 | return (_ruleMap ?: (_ruleMap = [[NSMutableDictionary alloc] init])); 1435 | } 1436 | 1437 | @end 1438 | 1439 | 1440 | #define COSCOORD_CALC(expr) \ 1441 | do { \ 1442 | if ([self valid] && [other valid]) { \ 1443 | COSCoord *coord = [[COSCoord alloc] init]; \ 1444 | NSMutableSet *dependencies = self.dependencies; \ 1445 | \ 1446 | [dependencies unionSet:other.dependencies]; \ 1447 | \ 1448 | coord.dependencies = dependencies; \ 1449 | coord.block = ^CGFloat(COSLayoutRule *rule) { \ 1450 | return (expr); \ 1451 | }; \ 1452 | \ 1453 | return coord; \ 1454 | } else if ([self valid] && ![other valid]) { \ 1455 | return self; \ 1456 | } else if (![self valid] && [other valid]) { \ 1457 | return other; \ 1458 | } else { \ 1459 | return self; \ 1460 | } \ 1461 | } while (0) 1462 | 1463 | 1464 | @implementation COSCoord 1465 | 1466 | + (instancetype)nilCoord { 1467 | static COSCoord *nilCoord = nil; 1468 | static dispatch_once_t onceToken; 1469 | 1470 | dispatch_once(&onceToken, ^{ 1471 | nilCoord = [[COSCoord alloc] init]; 1472 | }); 1473 | 1474 | return nilCoord; 1475 | } 1476 | 1477 | + (instancetype)coordWithFloat:(CGFloat)value { 1478 | COSCoord *coord = [[COSCoord alloc] init]; 1479 | 1480 | coord.block = ^CGFloat(COSLayoutRule *rule) { 1481 | return value; 1482 | }; 1483 | 1484 | return coord; 1485 | } 1486 | 1487 | + (instancetype)coordWithPercentage:(CGFloat)percentage { 1488 | return [self coordWithPercentage:percentage dir:0]; 1489 | } 1490 | 1491 | + (instancetype)coordWithPercentage:(CGFloat)percentage dir:(COSLayoutDir)dir_ { 1492 | COSCoord *coord = [[COSCoord alloc] init]; 1493 | 1494 | percentage /= 100.0; 1495 | 1496 | coord.block = ^CGFloat(COSLayoutRule *rule) { 1497 | UIView *view = rule.view; 1498 | 1499 | COSLayoutDir dir = dir_ ? dir_ : rule.dir; 1500 | CGFloat size = (dir == COSLayoutDirv ? COS_SUPERVIEW_HEIGHT : COS_SUPERVIEW_WIDTH); 1501 | 1502 | return size * percentage; 1503 | }; 1504 | 1505 | return coord; 1506 | } 1507 | 1508 | + (instancetype)coordWithBlock:(COSCoordBlock)block { 1509 | COSCoord *coord = [[COSCoord alloc] init]; 1510 | 1511 | coord.block = block; 1512 | 1513 | return coord; 1514 | } 1515 | 1516 | - (instancetype)add:(COSCoord *)other { 1517 | COSCOORD_CALC(self.block(rule) + other.block(rule)); 1518 | } 1519 | 1520 | - (instancetype)sub:(COSCoord *)other { 1521 | COSCOORD_CALC(self.block(rule) - other.block(rule)); 1522 | } 1523 | 1524 | - (instancetype)mul:(COSCoord *)other { 1525 | COSCOORD_CALC(self.block(rule) * other.block(rule)); 1526 | } 1527 | 1528 | - (instancetype)div:(COSCoord *)other { 1529 | COSCOORD_CALC(self.block(rule) / other.block(rule)); 1530 | } 1531 | 1532 | - (NSMutableSet *)dependencies { 1533 | return _dependencies ?: (_dependencies = [[NSMutableSet alloc] init]); 1534 | } 1535 | 1536 | - (BOOL)valid { 1537 | return self != [COSCoord nilCoord] && self.block; 1538 | } 1539 | 1540 | @end 1541 | 1542 | 1543 | @interface COSCoords () 1544 | 1545 | @property (nonatomic, weak) UIView *view; 1546 | 1547 | @property (nonatomic, strong) COSCoord *w; 1548 | @property (nonatomic, strong) COSCoord *h; 1549 | 1550 | @property (nonatomic, strong) COSCoord *tt; 1551 | @property (nonatomic, strong) COSCoord *tb; 1552 | 1553 | @property (nonatomic, strong) COSCoord *ll; 1554 | @property (nonatomic, strong) COSCoord *lr; 1555 | 1556 | @property (nonatomic, strong) COSCoord *bb; 1557 | @property (nonatomic, strong) COSCoord *bt; 1558 | 1559 | @property (nonatomic, strong) COSCoord *rr; 1560 | @property (nonatomic, strong) COSCoord *rl; 1561 | 1562 | @property (nonatomic, strong) COSCoord *ct; 1563 | @property (nonatomic, strong) COSCoord *cl; 1564 | @property (nonatomic, strong) COSCoord *cb; 1565 | @property (nonatomic, strong) COSCoord *cr; 1566 | 1567 | - (instancetype)initWithView:(UIView *)view; 1568 | 1569 | @end 1570 | 1571 | 1572 | #define COS_VIEW_TOP ([view convertRect:view.bounds toView:rule.view.superview].origin.y) 1573 | #define COS_VIEW_LEFT ([view convertRect:view.bounds toView:rule.view.superview].origin.x) 1574 | #define COS_VIEW_WIDTH (view.bounds.size.width) 1575 | #define COS_VIEW_HEIGHT (view.bounds.size.height) 1576 | 1577 | #define LAZY_LOAD_COORD(ivar, expr) \ 1578 | (ivar ?: (ivar = COSCOORD_MAKE([NSMutableSet setWithObject:_view], expr))) 1579 | 1580 | 1581 | @implementation COSCoords 1582 | 1583 | + (instancetype)coordsOfView:(UIView *)view { 1584 | static const void *coordsKey = &coordsKey; 1585 | 1586 | if (![view isKindOfClass:[UIView class]]) { 1587 | return nil; 1588 | } 1589 | 1590 | COSCoords *coords = objc_getAssociatedObject(view, coordsKey); 1591 | 1592 | if (!coords) { 1593 | coords = [[COSCoords alloc] initWithView:view]; 1594 | 1595 | objc_setAssociatedObject(view, coordsKey, coords, OBJC_ASSOCIATION_RETAIN); 1596 | } 1597 | 1598 | return coords; 1599 | } 1600 | 1601 | - (instancetype)initWithView:(UIView *)view { 1602 | self = [super init]; 1603 | 1604 | if (self) { 1605 | _view = view; 1606 | } 1607 | 1608 | return self; 1609 | } 1610 | 1611 | - (COSCoord *)tt { 1612 | return LAZY_LOAD_COORD(_tt, COS_VIEW_TOP); 1613 | } 1614 | 1615 | - (COSCoord *)tb { 1616 | return LAZY_LOAD_COORD(_tb, COS_SUPERVIEW_HEIGHT - COS_VIEW_TOP); 1617 | } 1618 | 1619 | - (COSCoord *)ll { 1620 | return LAZY_LOAD_COORD(_ll, COS_VIEW_LEFT); 1621 | } 1622 | 1623 | - (COSCoord *)lr { 1624 | return LAZY_LOAD_COORD(_lr, COS_SUPERVIEW_WIDTH - COS_VIEW_LEFT); 1625 | } 1626 | 1627 | - (COSCoord *)bb { 1628 | return LAZY_LOAD_COORD(_bb, COS_SUPERVIEW_HEIGHT - COS_VIEW_TOP - COS_VIEW_HEIGHT); 1629 | } 1630 | 1631 | - (COSCoord *)bt { 1632 | return LAZY_LOAD_COORD(_bt, COS_VIEW_TOP + COS_VIEW_HEIGHT); 1633 | } 1634 | 1635 | - (COSCoord *)rr { 1636 | return LAZY_LOAD_COORD(_rr, COS_SUPERVIEW_WIDTH - COS_VIEW_LEFT - COS_VIEW_WIDTH); 1637 | } 1638 | 1639 | - (COSCoord *)rl { 1640 | return LAZY_LOAD_COORD(_rl, COS_VIEW_LEFT + COS_VIEW_WIDTH); 1641 | } 1642 | 1643 | - (COSCoord *)ct { 1644 | return LAZY_LOAD_COORD(_ct, COS_VIEW_TOP + COS_VIEW_HEIGHT / 2); 1645 | } 1646 | 1647 | - (COSCoord *)cl { 1648 | return LAZY_LOAD_COORD(_cl, COS_VIEW_LEFT + COS_VIEW_WIDTH / 2); 1649 | } 1650 | 1651 | - (COSCoord *)cb { 1652 | return LAZY_LOAD_COORD(_cb, COS_SUPERVIEW_HEIGHT - COS_VIEW_TOP - COS_VIEW_HEIGHT / 2); 1653 | } 1654 | 1655 | - (COSCoord *)cr { 1656 | return LAZY_LOAD_COORD(_cr, COS_SUPERVIEW_WIDTH - COS_VIEW_LEFT - COS_VIEW_WIDTH / 2); 1657 | } 1658 | 1659 | - (COSCoord *)w { 1660 | return LAZY_LOAD_COORD(_w, COS_VIEW_WIDTH); 1661 | } 1662 | 1663 | - (COSCoord *)h { 1664 | return LAZY_LOAD_COORD(_h, COS_VIEW_HEIGHT); 1665 | } 1666 | 1667 | @end 1668 | 1669 | 1670 | @implementation UIView (COSLayout) 1671 | 1672 | - (COSLayout *)coslayout { 1673 | return [COSLayout layoutOfView:self]; 1674 | } 1675 | 1676 | @end 1677 | -------------------------------------------------------------------------------- /COSLayout/COSLayoutLex.c: -------------------------------------------------------------------------------- 1 | #line 2 "COSLayoutLex.c" 2 | 3 | #line 4 "COSLayoutLex.c" 4 | 5 | #define YY_INT_ALIGNED short int 6 | 7 | /* A lexical scanner generated by flex */ 8 | 9 | #define FLEX_SCANNER 10 | #define YY_FLEX_MAJOR_VERSION 2 11 | #define YY_FLEX_MINOR_VERSION 5 12 | #define YY_FLEX_SUBMINOR_VERSION 35 13 | #if YY_FLEX_SUBMINOR_VERSION > 0 14 | #define FLEX_BETA 15 | #endif 16 | 17 | /* First, we deal with platform-specific or compiler-specific issues. */ 18 | 19 | /* begin standard C headers. */ 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | /* end standard C headers. */ 26 | 27 | /* flex integer type definitions */ 28 | 29 | #ifndef FLEXINT_H 30 | #define FLEXINT_H 31 | 32 | /* C99 systems have . Non-C99 systems may or may not. */ 33 | 34 | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 35 | 36 | /* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, 37 | * if you want the limit (max/min) macros for int types. 38 | */ 39 | #ifndef __STDC_LIMIT_MACROS 40 | #define __STDC_LIMIT_MACROS 1 41 | #endif 42 | 43 | #include 44 | typedef int8_t flex_int8_t; 45 | typedef uint8_t flex_uint8_t; 46 | typedef int16_t flex_int16_t; 47 | typedef uint16_t flex_uint16_t; 48 | typedef int32_t flex_int32_t; 49 | typedef uint32_t flex_uint32_t; 50 | typedef uint64_t flex_uint64_t; 51 | #else 52 | typedef signed char flex_int8_t; 53 | typedef short int flex_int16_t; 54 | typedef int flex_int32_t; 55 | typedef unsigned char flex_uint8_t; 56 | typedef unsigned short int flex_uint16_t; 57 | typedef unsigned int flex_uint32_t; 58 | #endif /* ! C99 */ 59 | 60 | /* Limits of integral types. */ 61 | #ifndef INT8_MIN 62 | #define INT8_MIN (-128) 63 | #endif 64 | #ifndef INT16_MIN 65 | #define INT16_MIN (-32767-1) 66 | #endif 67 | #ifndef INT32_MIN 68 | #define INT32_MIN (-2147483647-1) 69 | #endif 70 | #ifndef INT8_MAX 71 | #define INT8_MAX (127) 72 | #endif 73 | #ifndef INT16_MAX 74 | #define INT16_MAX (32767) 75 | #endif 76 | #ifndef INT32_MAX 77 | #define INT32_MAX (2147483647) 78 | #endif 79 | #ifndef UINT8_MAX 80 | #define UINT8_MAX (255U) 81 | #endif 82 | #ifndef UINT16_MAX 83 | #define UINT16_MAX (65535U) 84 | #endif 85 | #ifndef UINT32_MAX 86 | #define UINT32_MAX (4294967295U) 87 | #endif 88 | 89 | #endif /* ! FLEXINT_H */ 90 | 91 | #ifdef __cplusplus 92 | 93 | /* The "const" storage-class-modifier is valid. */ 94 | #define YY_USE_CONST 95 | 96 | #else /* ! __cplusplus */ 97 | 98 | /* C99 requires __STDC__ to be defined as 1. */ 99 | #if defined (__STDC__) 100 | 101 | #define YY_USE_CONST 102 | 103 | #endif /* defined (__STDC__) */ 104 | #endif /* ! __cplusplus */ 105 | 106 | #ifdef YY_USE_CONST 107 | #define yyconst const 108 | #else 109 | #define yyconst 110 | #endif 111 | 112 | /* Returned upon end-of-file. */ 113 | #define YY_NULL 0 114 | 115 | /* Promotes a possibly negative, possibly signed char to an unsigned 116 | * integer for use as an array index. If the signed char is negative, 117 | * we want to instead treat it as an 8-bit unsigned char, hence the 118 | * double cast. 119 | */ 120 | #define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) 121 | 122 | /* An opaque pointer. */ 123 | #ifndef YY_TYPEDEF_YY_SCANNER_T 124 | #define YY_TYPEDEF_YY_SCANNER_T 125 | typedef void* yyscan_t; 126 | #endif 127 | 128 | /* For convenience, these vars (plus the bison vars far below) 129 | are macros in the reentrant scanner. */ 130 | #define yyin yyg->yyin_r 131 | #define yyout yyg->yyout_r 132 | #define yyextra yyg->yyextra_r 133 | #define yyleng yyg->yyleng_r 134 | #define yytext yyg->yytext_r 135 | #define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno) 136 | #define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column) 137 | #define yy_flex_debug yyg->yy_flex_debug_r 138 | 139 | /* Enter a start condition. This macro really ought to take a parameter, 140 | * but we do it the disgusting crufty way forced on us by the ()-less 141 | * definition of BEGIN. 142 | */ 143 | #define BEGIN yyg->yy_start = 1 + 2 * 144 | 145 | /* Translate the current start state into a value that can be later handed 146 | * to BEGIN to return to the state. The YYSTATE alias is for lex 147 | * compatibility. 148 | */ 149 | #define YY_START ((yyg->yy_start - 1) / 2) 150 | #define YYSTATE YY_START 151 | 152 | /* Action number for EOF rule of a given start state. */ 153 | #define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) 154 | 155 | /* Special action meaning "start processing a new file". */ 156 | #define YY_NEW_FILE coslayoutrestart(yyin ,yyscanner ) 157 | 158 | #define YY_END_OF_BUFFER_CHAR 0 159 | 160 | /* Size of default input buffer. */ 161 | #ifndef YY_BUF_SIZE 162 | #define YY_BUF_SIZE 16384 163 | #endif 164 | 165 | /* The state buf must be large enough to hold one state per character in the main buffer. 166 | */ 167 | #define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) 168 | 169 | #ifndef YY_TYPEDEF_YY_BUFFER_STATE 170 | #define YY_TYPEDEF_YY_BUFFER_STATE 171 | typedef struct yy_buffer_state *YY_BUFFER_STATE; 172 | #endif 173 | 174 | #ifndef YY_TYPEDEF_YY_SIZE_T 175 | #define YY_TYPEDEF_YY_SIZE_T 176 | typedef size_t yy_size_t; 177 | #endif 178 | 179 | #define EOB_ACT_CONTINUE_SCAN 0 180 | #define EOB_ACT_END_OF_FILE 1 181 | #define EOB_ACT_LAST_MATCH 2 182 | 183 | #define YY_LESS_LINENO(n) 184 | 185 | /* Return all but the first "n" matched characters back to the input stream. */ 186 | #define yyless(n) \ 187 | do \ 188 | { \ 189 | /* Undo effects of setting up yytext. */ \ 190 | int yyless_macro_arg = (n); \ 191 | YY_LESS_LINENO(yyless_macro_arg);\ 192 | *yy_cp = yyg->yy_hold_char; \ 193 | YY_RESTORE_YY_MORE_OFFSET \ 194 | yyg->yy_c_buf_p = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ 195 | YY_DO_BEFORE_ACTION; /* set up yytext again */ \ 196 | } \ 197 | while ( 0 ) 198 | 199 | #define unput(c) yyunput( c, yyg->yytext_ptr , yyscanner ) 200 | 201 | #ifndef YY_STRUCT_YY_BUFFER_STATE 202 | #define YY_STRUCT_YY_BUFFER_STATE 203 | struct yy_buffer_state 204 | { 205 | FILE *yy_input_file; 206 | 207 | char *yy_ch_buf; /* input buffer */ 208 | char *yy_buf_pos; /* current position in input buffer */ 209 | 210 | /* Size of input buffer in bytes, not including room for EOB 211 | * characters. 212 | */ 213 | yy_size_t yy_buf_size; 214 | 215 | /* Number of characters read into yy_ch_buf, not including EOB 216 | * characters. 217 | */ 218 | yy_size_t yy_n_chars; 219 | 220 | /* Whether we "own" the buffer - i.e., we know we created it, 221 | * and can realloc() it to grow it, and should free() it to 222 | * delete it. 223 | */ 224 | int yy_is_our_buffer; 225 | 226 | /* Whether this is an "interactive" input source; if so, and 227 | * if we're using stdio for input, then we want to use getc() 228 | * instead of fread(), to make sure we stop fetching input after 229 | * each newline. 230 | */ 231 | int yy_is_interactive; 232 | 233 | /* Whether we're considered to be at the beginning of a line. 234 | * If so, '^' rules will be active on the next match, otherwise 235 | * not. 236 | */ 237 | int yy_at_bol; 238 | 239 | int yy_bs_lineno; /**< The line count. */ 240 | int yy_bs_column; /**< The column count. */ 241 | 242 | /* Whether to try to fill the input buffer when we reach the 243 | * end of it. 244 | */ 245 | int yy_fill_buffer; 246 | 247 | int yy_buffer_status; 248 | 249 | #define YY_BUFFER_NEW 0 250 | #define YY_BUFFER_NORMAL 1 251 | /* When an EOF's been seen but there's still some text to process 252 | * then we mark the buffer as YY_EOF_PENDING, to indicate that we 253 | * shouldn't try reading from the input source any more. We might 254 | * still have a bunch of tokens to match, though, because of 255 | * possible backing-up. 256 | * 257 | * When we actually see the EOF, we change the status to "new" 258 | * (via coslayoutrestart()), so that the user can continue scanning by 259 | * just pointing yyin at a new input file. 260 | */ 261 | #define YY_BUFFER_EOF_PENDING 2 262 | 263 | }; 264 | #endif /* !YY_STRUCT_YY_BUFFER_STATE */ 265 | 266 | /* We provide macros for accessing buffer states in case in the 267 | * future we want to put the buffer states in a more general 268 | * "scanner state". 269 | * 270 | * Returns the top of the stack, or NULL. 271 | */ 272 | #define YY_CURRENT_BUFFER ( yyg->yy_buffer_stack \ 273 | ? yyg->yy_buffer_stack[yyg->yy_buffer_stack_top] \ 274 | : NULL) 275 | 276 | /* Same as previous macro, but useful when we know that the buffer stack is not 277 | * NULL or when we need an lvalue. For internal use only. 278 | */ 279 | #define YY_CURRENT_BUFFER_LVALUE yyg->yy_buffer_stack[yyg->yy_buffer_stack_top] 280 | 281 | void coslayoutrestart (FILE *input_file ,yyscan_t yyscanner ); 282 | void coslayout_switch_to_buffer (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner ); 283 | YY_BUFFER_STATE coslayout_create_buffer (FILE *file,int size ,yyscan_t yyscanner ); 284 | void coslayout_delete_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner ); 285 | void coslayout_flush_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner ); 286 | void coslayoutpush_buffer_state (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner ); 287 | void coslayoutpop_buffer_state (yyscan_t yyscanner ); 288 | 289 | static void coslayoutensure_buffer_stack (yyscan_t yyscanner ); 290 | static void coslayout_load_buffer_state (yyscan_t yyscanner ); 291 | static void coslayout_init_buffer (YY_BUFFER_STATE b,FILE *file ,yyscan_t yyscanner ); 292 | 293 | #define YY_FLUSH_BUFFER coslayout_flush_buffer(YY_CURRENT_BUFFER ,yyscanner) 294 | 295 | YY_BUFFER_STATE coslayout_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner ); 296 | YY_BUFFER_STATE coslayout_scan_string (yyconst char *yy_str ,yyscan_t yyscanner ); 297 | YY_BUFFER_STATE coslayout_scan_bytes (yyconst char *bytes,yy_size_t len ,yyscan_t yyscanner ); 298 | 299 | void *coslayoutalloc (yy_size_t ,yyscan_t yyscanner ); 300 | void *coslayoutrealloc (void *,yy_size_t ,yyscan_t yyscanner ); 301 | void coslayoutfree (void * ,yyscan_t yyscanner ); 302 | 303 | #define yy_new_buffer coslayout_create_buffer 304 | 305 | #define yy_set_interactive(is_interactive) \ 306 | { \ 307 | if ( ! YY_CURRENT_BUFFER ){ \ 308 | coslayoutensure_buffer_stack (yyscanner); \ 309 | YY_CURRENT_BUFFER_LVALUE = \ 310 | coslayout_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); \ 311 | } \ 312 | YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ 313 | } 314 | 315 | #define yy_set_bol(at_bol) \ 316 | { \ 317 | if ( ! YY_CURRENT_BUFFER ){\ 318 | coslayoutensure_buffer_stack (yyscanner); \ 319 | YY_CURRENT_BUFFER_LVALUE = \ 320 | coslayout_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); \ 321 | } \ 322 | YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ 323 | } 324 | 325 | #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) 326 | 327 | /* Begin user sect3 */ 328 | 329 | #define coslayoutwrap(n) 1 330 | #define YY_SKIP_YYWRAP 331 | 332 | typedef unsigned char YY_CHAR; 333 | 334 | typedef int yy_state_type; 335 | 336 | #define yytext_ptr yytext_r 337 | 338 | static yy_state_type yy_get_previous_state (yyscan_t yyscanner ); 339 | static yy_state_type yy_try_NUL_trans (yy_state_type current_state ,yyscan_t yyscanner); 340 | static int yy_get_next_buffer (yyscan_t yyscanner ); 341 | static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner ); 342 | 343 | /* Done after the current pattern has been matched and before the 344 | * corresponding action - sets up yytext. 345 | */ 346 | #define YY_DO_BEFORE_ACTION \ 347 | yyg->yytext_ptr = yy_bp; \ 348 | yyleng = (yy_size_t) (yy_cp - yy_bp); \ 349 | yyg->yy_hold_char = *yy_cp; \ 350 | *yy_cp = '\0'; \ 351 | yyg->yy_c_buf_p = yy_cp; 352 | 353 | #define YY_NUM_RULES 20 354 | #define YY_END_OF_BUFFER 21 355 | /* This struct is not used in this scanner, 356 | but its presence is necessary. */ 357 | struct yy_trans_info 358 | { 359 | flex_int32_t yy_verify; 360 | flex_int32_t yy_nxt; 361 | }; 362 | static yyconst flex_int16_t yy_accept[53] = 363 | { 0, 364 | 0, 0, 21, 19, 1, 1, 19, 11, 12, 9, 365 | 7, 8, 19, 10, 15, 6, 19, 14, 14, 0, 366 | 0, 0, 0, 18, 0, 17, 0, 0, 4, 0, 367 | 15, 2, 3, 15, 5, 16, 15, 0, 14, 14, 368 | 15, 0, 0, 0, 0, 13, 0, 0, 0, 0, 369 | 0, 0 370 | } ; 371 | 372 | static yyconst flex_int32_t yy_ec[256] = 373 | { 0, 374 | 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 375 | 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 376 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 377 | 1, 2, 1, 1, 1, 1, 4, 1, 1, 5, 378 | 6, 7, 8, 1, 9, 10, 11, 12, 12, 12, 379 | 12, 12, 12, 12, 12, 12, 12, 13, 1, 1, 380 | 14, 1, 1, 15, 1, 1, 1, 1, 1, 1, 381 | 1, 16, 1, 1, 1, 1, 1, 1, 1, 1, 382 | 1, 1, 1, 1, 1, 16, 1, 1, 1, 1, 383 | 1, 1, 1, 17, 18, 1, 18, 19, 20, 18, 384 | 385 | 18, 21, 18, 22, 23, 18, 18, 24, 18, 25, 386 | 18, 26, 18, 27, 18, 28, 18, 18, 29, 18, 387 | 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 388 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 389 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 390 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 391 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 392 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 393 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 394 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 395 | 396 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 397 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 398 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 399 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 400 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 401 | 1, 1, 1, 1, 1 402 | } ; 403 | 404 | static yyconst flex_int32_t yy_meta[30] = 405 | { 0, 406 | 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 407 | 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 408 | 2, 2, 2, 2, 2, 2, 2, 2, 2 409 | } ; 410 | 411 | static yyconst flex_int16_t yy_base[54] = 412 | { 0, 413 | 0, 0, 105, 106, 106, 106, 15, 106, 106, 90, 414 | 35, 36, 91, 88, 47, 106, 88, 0, 77, 12, 415 | 32, 12, 36, 106, 38, 106, 42, 33, 106, 83, 416 | 63, 106, 106, 64, 106, 106, 66, 75, 0, 70, 417 | 68, 71, 44, 81, 86, 0, 66, 51, 70, 77, 418 | 87, 106, 69 419 | } ; 420 | 421 | static yyconst flex_int16_t yy_def[54] = 422 | { 0, 423 | 52, 1, 52, 52, 52, 52, 52, 52, 52, 52, 424 | 52, 52, 52, 52, 52, 52, 52, 53, 53, 52, 425 | 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 426 | 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 427 | 52, 52, 52, 52, 52, 53, 52, 52, 52, 52, 428 | 52, 0, 52 429 | } ; 430 | 431 | static yyconst flex_int16_t yy_nxt[136] = 432 | { 0, 433 | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 434 | 14, 15, 4, 16, 4, 17, 4, 18, 18, 18, 435 | 18, 18, 18, 18, 19, 18, 18, 18, 18, 20, 436 | 24, 21, 24, 22, 23, 24, 24, 26, 25, 24, 437 | 26, 27, 28, 24, 30, 30, 31, 31, 32, 33, 438 | 36, 24, 24, 44, 24, 45, 37, 26, 31, 24, 439 | 24, 24, 24, 24, 24, 24, 36, 36, 24, 36, 440 | 39, 36, 37, 36, 31, 34, 26, 41, 42, 41, 441 | 36, 49, 43, 43, 44, 47, 45, 48, 51, 36, 442 | 36, 26, 49, 46, 34, 50, 26, 45, 51, 40, 443 | 444 | 38, 35, 34, 29, 52, 3, 52, 52, 52, 52, 445 | 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 446 | 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 447 | 52, 52, 52, 52, 52 448 | } ; 449 | 450 | static yyconst flex_int16_t yy_chk[136] = 451 | { 0, 452 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 453 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 454 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 455 | 22, 7, 20, 7, 7, 7, 7, 20, 7, 22, 456 | 7, 7, 7, 7, 11, 12, 11, 12, 11, 12, 457 | 15, 28, 21, 43, 23, 43, 15, 21, 15, 23, 458 | 28, 25, 23, 23, 25, 27, 31, 34, 27, 37, 459 | 53, 41, 31, 49, 31, 34, 48, 37, 38, 41, 460 | 50, 49, 38, 38, 38, 42, 38, 42, 50, 45, 461 | 51, 47, 44, 40, 30, 45, 42, 45, 51, 19, 462 | 463 | 17, 14, 13, 10, 3, 52, 52, 52, 52, 52, 464 | 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 465 | 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 466 | 52, 52, 52, 52, 52 467 | } ; 468 | 469 | /* The intent behind this definition is that it'll catch 470 | * any uses of REJECT which flex missed. 471 | */ 472 | #define REJECT reject_used_but_not_detected 473 | #define yymore() yymore_used_but_not_detected 474 | #define YY_MORE_ADJ 0 475 | #define YY_RESTORE_YY_MORE_OFFSET 476 | #line 1 "COSLayoutLex.l" 477 | #line 2 "COSLayoutLex.l" 478 | #include 479 | #include "COSLayoutParser.h" 480 | #define YY_NO_INPUT 1 481 | #line 482 "COSLayoutLex.c" 482 | 483 | #define INITIAL 0 484 | 485 | #ifndef YY_NO_UNISTD_H 486 | /* Special case for "unistd.h", since it is non-ANSI. We include it way 487 | * down here because we want the user's section 1 to have been scanned first. 488 | * The user has a chance to override it with an option. 489 | */ 490 | #include 491 | #endif 492 | 493 | #ifndef YY_EXTRA_TYPE 494 | #define YY_EXTRA_TYPE void * 495 | #endif 496 | 497 | /* Holds the entire state of the reentrant scanner. */ 498 | struct yyguts_t 499 | { 500 | 501 | /* User-defined. Not touched by flex. */ 502 | YY_EXTRA_TYPE yyextra_r; 503 | 504 | /* The rest are the same as the globals declared in the non-reentrant scanner. */ 505 | FILE *yyin_r, *yyout_r; 506 | size_t yy_buffer_stack_top; /**< index of top of stack. */ 507 | size_t yy_buffer_stack_max; /**< capacity of stack. */ 508 | YY_BUFFER_STATE * yy_buffer_stack; /**< Stack as an array. */ 509 | char yy_hold_char; 510 | yy_size_t yy_n_chars; 511 | yy_size_t yyleng_r; 512 | char *yy_c_buf_p; 513 | int yy_init; 514 | int yy_start; 515 | int yy_did_buffer_switch_on_eof; 516 | int yy_start_stack_ptr; 517 | int yy_start_stack_depth; 518 | int *yy_start_stack; 519 | yy_state_type yy_last_accepting_state; 520 | char* yy_last_accepting_cpos; 521 | 522 | int yylineno_r; 523 | int yy_flex_debug_r; 524 | 525 | char *yytext_r; 526 | int yy_more_flag; 527 | int yy_more_len; 528 | 529 | YYSTYPE * yylval_r; 530 | 531 | }; /* end struct yyguts_t */ 532 | 533 | static int yy_init_globals (yyscan_t yyscanner ); 534 | 535 | /* This must go here because YYSTYPE and YYLTYPE are included 536 | * from bison output in section 1.*/ 537 | # define yylval yyg->yylval_r 538 | 539 | int coslayoutlex_init (yyscan_t* scanner); 540 | 541 | int coslayoutlex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner); 542 | 543 | /* Accessor methods to globals. 544 | These are made visible to non-reentrant scanners for convenience. */ 545 | 546 | int coslayoutlex_destroy (yyscan_t yyscanner ); 547 | 548 | int coslayoutget_debug (yyscan_t yyscanner ); 549 | 550 | void coslayoutset_debug (int debug_flag ,yyscan_t yyscanner ); 551 | 552 | YY_EXTRA_TYPE coslayoutget_extra (yyscan_t yyscanner ); 553 | 554 | void coslayoutset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner ); 555 | 556 | FILE *coslayoutget_in (yyscan_t yyscanner ); 557 | 558 | void coslayoutset_in (FILE * in_str ,yyscan_t yyscanner ); 559 | 560 | FILE *coslayoutget_out (yyscan_t yyscanner ); 561 | 562 | void coslayoutset_out (FILE * out_str ,yyscan_t yyscanner ); 563 | 564 | yy_size_t coslayoutget_leng (yyscan_t yyscanner ); 565 | 566 | char *coslayoutget_text (yyscan_t yyscanner ); 567 | 568 | int coslayoutget_lineno (yyscan_t yyscanner ); 569 | 570 | void coslayoutset_lineno (int line_number ,yyscan_t yyscanner ); 571 | 572 | YYSTYPE * coslayoutget_lval (yyscan_t yyscanner ); 573 | 574 | void coslayoutset_lval (YYSTYPE * yylval_param ,yyscan_t yyscanner ); 575 | 576 | /* Macros after this point can all be overridden by user definitions in 577 | * section 1. 578 | */ 579 | 580 | #ifndef YY_SKIP_YYWRAP 581 | #ifdef __cplusplus 582 | extern "C" int coslayoutwrap (yyscan_t yyscanner ); 583 | #else 584 | extern int coslayoutwrap (yyscan_t yyscanner ); 585 | #endif 586 | #endif 587 | 588 | #ifndef yytext_ptr 589 | static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner); 590 | #endif 591 | 592 | #ifdef YY_NEED_STRLEN 593 | static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner); 594 | #endif 595 | 596 | #ifndef YY_NO_INPUT 597 | 598 | #ifdef __cplusplus 599 | static int yyinput (yyscan_t yyscanner ); 600 | #else 601 | static int input (yyscan_t yyscanner ); 602 | #endif 603 | 604 | #endif 605 | 606 | /* Amount of stuff to slurp up with each read. */ 607 | #ifndef YY_READ_BUF_SIZE 608 | #define YY_READ_BUF_SIZE 8192 609 | #endif 610 | 611 | /* Copy whatever the last rule matched to the standard output. */ 612 | #ifndef ECHO 613 | /* This used to be an fputs(), but since the string might contain NUL's, 614 | * we now use fwrite(). 615 | */ 616 | #define ECHO fwrite( yytext, yyleng, 1, yyout ) 617 | #endif 618 | 619 | /* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, 620 | * is returned in "result". 621 | */ 622 | #ifndef YY_INPUT 623 | #define YY_INPUT(buf,result,max_size) \ 624 | if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ 625 | { \ 626 | int c = '*'; \ 627 | yy_size_t n; \ 628 | for ( n = 0; n < max_size && \ 629 | (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ 630 | buf[n] = (char) c; \ 631 | if ( c == '\n' ) \ 632 | buf[n++] = (char) c; \ 633 | if ( c == EOF && ferror( yyin ) ) \ 634 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 635 | result = n; \ 636 | } \ 637 | else \ 638 | { \ 639 | errno=0; \ 640 | while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ 641 | { \ 642 | if( errno != EINTR) \ 643 | { \ 644 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 645 | break; \ 646 | } \ 647 | errno=0; \ 648 | clearerr(yyin); \ 649 | } \ 650 | }\ 651 | \ 652 | 653 | #endif 654 | 655 | /* No semi-colon after return; correct usage is to write "yyterminate();" - 656 | * we don't want an extra ';' after the "return" because that will cause 657 | * some compilers to complain about unreachable statements. 658 | */ 659 | #ifndef yyterminate 660 | #define yyterminate() return YY_NULL 661 | #endif 662 | 663 | /* Number of entries by which start-condition stack grows. */ 664 | #ifndef YY_START_STACK_INCR 665 | #define YY_START_STACK_INCR 25 666 | #endif 667 | 668 | /* Report a fatal error. */ 669 | #ifndef YY_FATAL_ERROR 670 | #define YY_FATAL_ERROR(msg) yy_fatal_error( msg , yyscanner) 671 | #endif 672 | 673 | /* end tables serialization structures and prototypes */ 674 | 675 | /* Default declaration of generated scanner - a define so the user can 676 | * easily add parameters. 677 | */ 678 | #ifndef YY_DECL 679 | #define YY_DECL_IS_OURS 1 680 | 681 | extern int coslayoutlex \ 682 | (YYSTYPE * yylval_param ,yyscan_t yyscanner); 683 | 684 | #define YY_DECL int coslayoutlex \ 685 | (YYSTYPE * yylval_param , yyscan_t yyscanner) 686 | #endif /* !YY_DECL */ 687 | 688 | /* Code executed at the beginning of each rule, after yytext and yyleng 689 | * have been set up. 690 | */ 691 | #ifndef YY_USER_ACTION 692 | #define YY_USER_ACTION 693 | #endif 694 | 695 | /* Code executed at the end of each rule. */ 696 | #ifndef YY_BREAK 697 | #define YY_BREAK break; 698 | #endif 699 | 700 | #define YY_RULE_SETUP \ 701 | YY_USER_ACTION 702 | 703 | /** The main scanner function which does all the work. 704 | */ 705 | YY_DECL 706 | { 707 | register yy_state_type yy_current_state; 708 | register char *yy_cp, *yy_bp; 709 | register int yy_act; 710 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 711 | 712 | #line 25 "COSLayoutLex.l" 713 | 714 | 715 | #line 716 "COSLayoutLex.c" 716 | 717 | yylval = yylval_param; 718 | 719 | if ( !yyg->yy_init ) 720 | { 721 | yyg->yy_init = 1; 722 | 723 | #ifdef YY_USER_INIT 724 | YY_USER_INIT; 725 | #endif 726 | 727 | if ( ! yyg->yy_start ) 728 | yyg->yy_start = 1; /* first start state */ 729 | 730 | if ( ! yyin ) 731 | yyin = stdin; 732 | 733 | if ( ! yyout ) 734 | yyout = stdout; 735 | 736 | if ( ! YY_CURRENT_BUFFER ) { 737 | coslayoutensure_buffer_stack (yyscanner); 738 | YY_CURRENT_BUFFER_LVALUE = 739 | coslayout_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); 740 | } 741 | 742 | coslayout_load_buffer_state(yyscanner ); 743 | } 744 | 745 | while ( 1 ) /* loops until end-of-file is reached */ 746 | { 747 | yy_cp = yyg->yy_c_buf_p; 748 | 749 | /* Support of yytext. */ 750 | *yy_cp = yyg->yy_hold_char; 751 | 752 | /* yy_bp points to the position in yy_ch_buf of the start of 753 | * the current run. 754 | */ 755 | yy_bp = yy_cp; 756 | 757 | yy_current_state = yyg->yy_start; 758 | yy_match: 759 | do 760 | { 761 | register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; 762 | if ( yy_accept[yy_current_state] ) 763 | { 764 | yyg->yy_last_accepting_state = yy_current_state; 765 | yyg->yy_last_accepting_cpos = yy_cp; 766 | } 767 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 768 | { 769 | yy_current_state = (int) yy_def[yy_current_state]; 770 | if ( yy_current_state >= 53 ) 771 | yy_c = yy_meta[(unsigned int) yy_c]; 772 | } 773 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 774 | ++yy_cp; 775 | } 776 | while ( yy_base[yy_current_state] != 106 ); 777 | 778 | yy_find_action: 779 | yy_act = yy_accept[yy_current_state]; 780 | if ( yy_act == 0 ) 781 | { /* have to back up */ 782 | yy_cp = yyg->yy_last_accepting_cpos; 783 | yy_current_state = yyg->yy_last_accepting_state; 784 | yy_act = yy_accept[yy_current_state]; 785 | } 786 | 787 | YY_DO_BEFORE_ACTION; 788 | 789 | do_action: /* This label is used only to access EOF actions. */ 790 | 791 | switch ( yy_act ) 792 | { /* beginning of action switch */ 793 | case 0: /* must back up */ 794 | /* undo the effects of YY_DO_BEFORE_ACTION */ 795 | *yy_cp = yyg->yy_hold_char; 796 | yy_cp = yyg->yy_last_accepting_cpos; 797 | yy_current_state = yyg->yy_last_accepting_state; 798 | goto yy_find_action; 799 | 800 | case 1: 801 | /* rule 1 can match eol */ 802 | YY_RULE_SETUP 803 | #line 27 "COSLayoutLex.l" 804 | 805 | YY_BREAK 806 | case 2: 807 | YY_RULE_SETUP 808 | #line 29 "COSLayoutLex.l" 809 | { return COSLAYOUT_TOKEN_ADD_ASSIGN; } 810 | YY_BREAK 811 | case 3: 812 | YY_RULE_SETUP 813 | #line 30 "COSLayoutLex.l" 814 | { return COSLAYOUT_TOKEN_SUB_ASSIGN; } 815 | YY_BREAK 816 | case 4: 817 | YY_RULE_SETUP 818 | #line 31 "COSLayoutLex.l" 819 | { return COSLAYOUT_TOKEN_MUL_ASSIGN; } 820 | YY_BREAK 821 | case 5: 822 | YY_RULE_SETUP 823 | #line 32 "COSLayoutLex.l" 824 | { return COSLAYOUT_TOKEN_DIV_ASSIGN; } 825 | YY_BREAK 826 | case 6: 827 | YY_RULE_SETUP 828 | #line 33 "COSLayoutLex.l" 829 | { return '='; } 830 | YY_BREAK 831 | case 7: 832 | YY_RULE_SETUP 833 | #line 34 "COSLayoutLex.l" 834 | { return '+'; } 835 | YY_BREAK 836 | case 8: 837 | YY_RULE_SETUP 838 | #line 35 "COSLayoutLex.l" 839 | { return '-'; } 840 | YY_BREAK 841 | case 9: 842 | YY_RULE_SETUP 843 | #line 36 "COSLayoutLex.l" 844 | { return '*'; } 845 | YY_BREAK 846 | case 10: 847 | YY_RULE_SETUP 848 | #line 37 "COSLayoutLex.l" 849 | { return '/'; } 850 | YY_BREAK 851 | case 11: 852 | YY_RULE_SETUP 853 | #line 38 "COSLayoutLex.l" 854 | { return '('; } 855 | YY_BREAK 856 | case 12: 857 | YY_RULE_SETUP 858 | #line 39 "COSLayoutLex.l" 859 | { return ')'; } 860 | YY_BREAK 861 | case 13: 862 | YY_RULE_SETUP 863 | #line 41 "COSLayoutLex.l" 864 | { 865 | *yylval = coslayout_create_ast(COSLAYOUT_TOKEN_NIL, NULL, NULL); 866 | 867 | return COSLAYOUT_TOKEN_NIL; 868 | } 869 | YY_BREAK 870 | case 14: 871 | YY_RULE_SETUP 872 | #line 47 "COSLayoutLex.l" 873 | { 874 | COSLAYOUT_AST *ast = *yylval = coslayout_create_ast(COSLAYOUT_TOKEN_ATTR, NULL, NULL); 875 | 876 | ast->value.coord = (char *)malloc((strlen(yytext) + 1) * sizeof(char)); 877 | strcpy(ast->value.coord, yytext); 878 | 879 | return COSLAYOUT_TOKEN_ATTR; 880 | } 881 | YY_BREAK 882 | case 15: 883 | YY_RULE_SETUP 884 | #line 56 "COSLayoutLex.l" 885 | { 886 | COSLAYOUT_AST *ast = *yylval = coslayout_create_ast(COSLAYOUT_TOKEN_NUMBER, NULL, NULL); 887 | 888 | ast->value.number = atof(yytext); 889 | 890 | return COSLAYOUT_TOKEN_NUMBER; 891 | } 892 | YY_BREAK 893 | case 16: 894 | YY_RULE_SETUP 895 | #line 64 "COSLayoutLex.l" 896 | { 897 | float value = 0.0; 898 | int type = COSLAYOUT_TOKEN_PERCENTAGE; 899 | 900 | switch(yytext[0]) { 901 | case 'H': 902 | value = atof(yytext + 2); 903 | type = COSLAYOUT_TOKEN_PERCENTAGE_H; 904 | break; 905 | 906 | case 'V': 907 | value = atof(yytext + 2); 908 | type = COSLAYOUT_TOKEN_PERCENTAGE_V; 909 | break; 910 | 911 | default: 912 | value = atof(yytext); 913 | break; 914 | } 915 | 916 | COSLAYOUT_AST *ast = *yylval = coslayout_create_ast(type, NULL, NULL); 917 | 918 | ast->value.percentage = value; 919 | 920 | return type; 921 | } 922 | YY_BREAK 923 | case 17: 924 | YY_RULE_SETUP 925 | #line 91 "COSLayoutLex.l" 926 | { 927 | char *text = yytext; 928 | int type = COSLAYOUT_TOKEN_COORD_PERCENTAGE; 929 | 930 | switch(yytext[0]) { 931 | case 'H': 932 | text = yytext + 2; 933 | type = COSLAYOUT_TOKEN_COORD_PERCENTAGE_H; 934 | break; 935 | 936 | case 'V': 937 | text = yytext + 2; 938 | type = COSLAYOUT_TOKEN_COORD_PERCENTAGE_H; 939 | break; 940 | 941 | default: 942 | break; 943 | } 944 | 945 | char *spec = text + 1; 946 | 947 | COSLAYOUT_AST *ast = *yylval = coslayout_create_ast(type, NULL, NULL); 948 | 949 | ast->value.coord = (char *)malloc((strlen(spec) + 1) * sizeof(char)); 950 | strcpy(ast->value.coord, spec); 951 | 952 | return type; 953 | } 954 | YY_BREAK 955 | case 18: 956 | YY_RULE_SETUP 957 | #line 120 "COSLayoutLex.l" 958 | { 959 | char *spec = yytext + 1; 960 | 961 | COSLAYOUT_AST *ast = *yylval = coslayout_create_ast(COSLAYOUT_TOKEN_COORD, NULL, NULL); 962 | 963 | ast->value.coord = (char *)malloc((strlen(spec) + 1) * sizeof(char)); 964 | strcpy(ast->value.coord, spec); 965 | 966 | return COSLAYOUT_TOKEN_COORD; 967 | } 968 | YY_BREAK 969 | case 19: 970 | YY_RULE_SETUP 971 | #line 131 "COSLayoutLex.l" 972 | { 973 | fprintf(stderr, "COSLayout: Unrecognized text \"%s\", ignored.\n", yytext); 974 | } 975 | YY_BREAK 976 | case 20: 977 | YY_RULE_SETUP 978 | #line 135 "COSLayoutLex.l" 979 | ECHO; 980 | YY_BREAK 981 | #line 982 "COSLayoutLex.c" 982 | case YY_STATE_EOF(INITIAL): 983 | yyterminate(); 984 | 985 | case YY_END_OF_BUFFER: 986 | { 987 | /* Amount of text matched not including the EOB char. */ 988 | int yy_amount_of_matched_text = (int) (yy_cp - yyg->yytext_ptr) - 1; 989 | 990 | /* Undo the effects of YY_DO_BEFORE_ACTION. */ 991 | *yy_cp = yyg->yy_hold_char; 992 | YY_RESTORE_YY_MORE_OFFSET 993 | 994 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) 995 | { 996 | /* We're scanning a new file or input source. It's 997 | * possible that this happened because the user 998 | * just pointed yyin at a new source and called 999 | * coslayoutlex(). If so, then we have to assure 1000 | * consistency between YY_CURRENT_BUFFER and our 1001 | * globals. Here is the right place to do so, because 1002 | * this is the first action (other than possibly a 1003 | * back-up) that will match for the new input source. 1004 | */ 1005 | yyg->yy_n_chars = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 1006 | YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; 1007 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; 1008 | } 1009 | 1010 | /* Note that here we test for yy_c_buf_p "<=" to the position 1011 | * of the first EOB in the buffer, since yy_c_buf_p will 1012 | * already have been incremented past the NUL character 1013 | * (since all states make transitions on EOB to the 1014 | * end-of-buffer state). Contrast this with the test 1015 | * in input(). 1016 | */ 1017 | if ( yyg->yy_c_buf_p <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] ) 1018 | { /* This was really a NUL. */ 1019 | yy_state_type yy_next_state; 1020 | 1021 | yyg->yy_c_buf_p = yyg->yytext_ptr + yy_amount_of_matched_text; 1022 | 1023 | yy_current_state = yy_get_previous_state( yyscanner ); 1024 | 1025 | /* Okay, we're now positioned to make the NUL 1026 | * transition. We couldn't have 1027 | * yy_get_previous_state() go ahead and do it 1028 | * for us because it doesn't know how to deal 1029 | * with the possibility of jamming (and we don't 1030 | * want to build jamming into it because then it 1031 | * will run more slowly). 1032 | */ 1033 | 1034 | yy_next_state = yy_try_NUL_trans( yy_current_state , yyscanner); 1035 | 1036 | yy_bp = yyg->yytext_ptr + YY_MORE_ADJ; 1037 | 1038 | if ( yy_next_state ) 1039 | { 1040 | /* Consume the NUL. */ 1041 | yy_cp = ++yyg->yy_c_buf_p; 1042 | yy_current_state = yy_next_state; 1043 | goto yy_match; 1044 | } 1045 | 1046 | else 1047 | { 1048 | yy_cp = yyg->yy_c_buf_p; 1049 | goto yy_find_action; 1050 | } 1051 | } 1052 | 1053 | else switch ( yy_get_next_buffer( yyscanner ) ) 1054 | { 1055 | case EOB_ACT_END_OF_FILE: 1056 | { 1057 | yyg->yy_did_buffer_switch_on_eof = 0; 1058 | 1059 | if ( coslayoutwrap(yyscanner ) ) 1060 | { 1061 | /* Note: because we've taken care in 1062 | * yy_get_next_buffer() to have set up 1063 | * yytext, we can now set up 1064 | * yy_c_buf_p so that if some total 1065 | * hoser (like flex itself) wants to 1066 | * call the scanner after we return the 1067 | * YY_NULL, it'll still work - another 1068 | * YY_NULL will get returned. 1069 | */ 1070 | yyg->yy_c_buf_p = yyg->yytext_ptr + YY_MORE_ADJ; 1071 | 1072 | yy_act = YY_STATE_EOF(YY_START); 1073 | goto do_action; 1074 | } 1075 | 1076 | else 1077 | { 1078 | if ( ! yyg->yy_did_buffer_switch_on_eof ) 1079 | YY_NEW_FILE; 1080 | } 1081 | break; 1082 | } 1083 | 1084 | case EOB_ACT_CONTINUE_SCAN: 1085 | yyg->yy_c_buf_p = 1086 | yyg->yytext_ptr + yy_amount_of_matched_text; 1087 | 1088 | yy_current_state = yy_get_previous_state( yyscanner ); 1089 | 1090 | yy_cp = yyg->yy_c_buf_p; 1091 | yy_bp = yyg->yytext_ptr + YY_MORE_ADJ; 1092 | goto yy_match; 1093 | 1094 | case EOB_ACT_LAST_MATCH: 1095 | yyg->yy_c_buf_p = 1096 | &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars]; 1097 | 1098 | yy_current_state = yy_get_previous_state( yyscanner ); 1099 | 1100 | yy_cp = yyg->yy_c_buf_p; 1101 | yy_bp = yyg->yytext_ptr + YY_MORE_ADJ; 1102 | goto yy_find_action; 1103 | } 1104 | break; 1105 | } 1106 | 1107 | default: 1108 | YY_FATAL_ERROR( 1109 | "fatal flex scanner internal error--no action found" ); 1110 | } /* end of action switch */ 1111 | } /* end of scanning one token */ 1112 | } /* end of coslayoutlex */ 1113 | 1114 | /* yy_get_next_buffer - try to read in a new buffer 1115 | * 1116 | * Returns a code representing an action: 1117 | * EOB_ACT_LAST_MATCH - 1118 | * EOB_ACT_CONTINUE_SCAN - continue scanning from current position 1119 | * EOB_ACT_END_OF_FILE - end of file 1120 | */ 1121 | static int yy_get_next_buffer (yyscan_t yyscanner) 1122 | { 1123 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1124 | register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; 1125 | register char *source = yyg->yytext_ptr; 1126 | register int number_to_move, i; 1127 | int ret_val; 1128 | 1129 | if ( yyg->yy_c_buf_p > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] ) 1130 | YY_FATAL_ERROR( 1131 | "fatal flex scanner internal error--end of buffer missed" ); 1132 | 1133 | if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) 1134 | { /* Don't try to fill the buffer, so this is an EOF. */ 1135 | if ( yyg->yy_c_buf_p - yyg->yytext_ptr - YY_MORE_ADJ == 1 ) 1136 | { 1137 | /* We matched a single character, the EOB, so 1138 | * treat this as a final EOF. 1139 | */ 1140 | return EOB_ACT_END_OF_FILE; 1141 | } 1142 | 1143 | else 1144 | { 1145 | /* We matched some text prior to the EOB, first 1146 | * process it. 1147 | */ 1148 | return EOB_ACT_LAST_MATCH; 1149 | } 1150 | } 1151 | 1152 | /* Try to read more data. */ 1153 | 1154 | /* First move last chars to start of buffer. */ 1155 | number_to_move = (int) (yyg->yy_c_buf_p - yyg->yytext_ptr) - 1; 1156 | 1157 | for ( i = 0; i < number_to_move; ++i ) 1158 | *(dest++) = *(source++); 1159 | 1160 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) 1161 | /* don't do the read, it's not guaranteed to return an EOF, 1162 | * just force an EOF 1163 | */ 1164 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars = 0; 1165 | 1166 | else 1167 | { 1168 | yy_size_t num_to_read = 1169 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; 1170 | 1171 | while ( num_to_read <= 0 ) 1172 | { /* Not enough room in the buffer - grow it. */ 1173 | 1174 | /* just a shorter name for the current buffer */ 1175 | YY_BUFFER_STATE b = YY_CURRENT_BUFFER; 1176 | 1177 | int yy_c_buf_p_offset = 1178 | (int) (yyg->yy_c_buf_p - b->yy_ch_buf); 1179 | 1180 | if ( b->yy_is_our_buffer ) 1181 | { 1182 | yy_size_t new_size = b->yy_buf_size * 2; 1183 | 1184 | if ( new_size <= 0 ) 1185 | b->yy_buf_size += b->yy_buf_size / 8; 1186 | else 1187 | b->yy_buf_size *= 2; 1188 | 1189 | b->yy_ch_buf = (char *) 1190 | /* Include room in for 2 EOB chars. */ 1191 | coslayoutrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ,yyscanner ); 1192 | } 1193 | else 1194 | /* Can't grow it, we don't own it. */ 1195 | b->yy_ch_buf = 0; 1196 | 1197 | if ( ! b->yy_ch_buf ) 1198 | YY_FATAL_ERROR( 1199 | "fatal error - scanner input buffer overflow" ); 1200 | 1201 | yyg->yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset]; 1202 | 1203 | num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - 1204 | number_to_move - 1; 1205 | 1206 | } 1207 | 1208 | if ( num_to_read > YY_READ_BUF_SIZE ) 1209 | num_to_read = YY_READ_BUF_SIZE; 1210 | 1211 | /* Read in more data. */ 1212 | YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), 1213 | yyg->yy_n_chars, num_to_read ); 1214 | 1215 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars; 1216 | } 1217 | 1218 | if ( yyg->yy_n_chars == 0 ) 1219 | { 1220 | if ( number_to_move == YY_MORE_ADJ ) 1221 | { 1222 | ret_val = EOB_ACT_END_OF_FILE; 1223 | coslayoutrestart(yyin ,yyscanner); 1224 | } 1225 | 1226 | else 1227 | { 1228 | ret_val = EOB_ACT_LAST_MATCH; 1229 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = 1230 | YY_BUFFER_EOF_PENDING; 1231 | } 1232 | } 1233 | 1234 | else 1235 | ret_val = EOB_ACT_CONTINUE_SCAN; 1236 | 1237 | if ((yy_size_t) (yyg->yy_n_chars + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { 1238 | /* Extend the array by 50%, plus the number we really need. */ 1239 | yy_size_t new_size = yyg->yy_n_chars + number_to_move + (yyg->yy_n_chars >> 1); 1240 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) coslayoutrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ,yyscanner ); 1241 | if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) 1242 | YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); 1243 | } 1244 | 1245 | yyg->yy_n_chars += number_to_move; 1246 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] = YY_END_OF_BUFFER_CHAR; 1247 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR; 1248 | 1249 | yyg->yytext_ptr = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; 1250 | 1251 | return ret_val; 1252 | } 1253 | 1254 | /* yy_get_previous_state - get the state just before the EOB char was reached */ 1255 | 1256 | static yy_state_type yy_get_previous_state (yyscan_t yyscanner) 1257 | { 1258 | register yy_state_type yy_current_state; 1259 | register char *yy_cp; 1260 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1261 | 1262 | yy_current_state = yyg->yy_start; 1263 | 1264 | for ( yy_cp = yyg->yytext_ptr + YY_MORE_ADJ; yy_cp < yyg->yy_c_buf_p; ++yy_cp ) 1265 | { 1266 | register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); 1267 | if ( yy_accept[yy_current_state] ) 1268 | { 1269 | yyg->yy_last_accepting_state = yy_current_state; 1270 | yyg->yy_last_accepting_cpos = yy_cp; 1271 | } 1272 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1273 | { 1274 | yy_current_state = (int) yy_def[yy_current_state]; 1275 | if ( yy_current_state >= 53 ) 1276 | yy_c = yy_meta[(unsigned int) yy_c]; 1277 | } 1278 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1279 | } 1280 | 1281 | return yy_current_state; 1282 | } 1283 | 1284 | /* yy_try_NUL_trans - try to make a transition on the NUL character 1285 | * 1286 | * synopsis 1287 | * next_state = yy_try_NUL_trans( current_state ); 1288 | */ 1289 | static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state , yyscan_t yyscanner) 1290 | { 1291 | register int yy_is_jam; 1292 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; /* This var may be unused depending upon options. */ 1293 | register char *yy_cp = yyg->yy_c_buf_p; 1294 | 1295 | register YY_CHAR yy_c = 1; 1296 | if ( yy_accept[yy_current_state] ) 1297 | { 1298 | yyg->yy_last_accepting_state = yy_current_state; 1299 | yyg->yy_last_accepting_cpos = yy_cp; 1300 | } 1301 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1302 | { 1303 | yy_current_state = (int) yy_def[yy_current_state]; 1304 | if ( yy_current_state >= 53 ) 1305 | yy_c = yy_meta[(unsigned int) yy_c]; 1306 | } 1307 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1308 | yy_is_jam = (yy_current_state == 52); 1309 | 1310 | return yy_is_jam ? 0 : yy_current_state; 1311 | } 1312 | 1313 | #ifndef YY_NO_INPUT 1314 | #ifdef __cplusplus 1315 | static int yyinput (yyscan_t yyscanner) 1316 | #else 1317 | static int input (yyscan_t yyscanner) 1318 | #endif 1319 | 1320 | { 1321 | int c; 1322 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1323 | 1324 | *yyg->yy_c_buf_p = yyg->yy_hold_char; 1325 | 1326 | if ( *yyg->yy_c_buf_p == YY_END_OF_BUFFER_CHAR ) 1327 | { 1328 | /* yy_c_buf_p now points to the character we want to return. 1329 | * If this occurs *before* the EOB characters, then it's a 1330 | * valid NUL; if not, then we've hit the end of the buffer. 1331 | */ 1332 | if ( yyg->yy_c_buf_p < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] ) 1333 | /* This was really a NUL. */ 1334 | *yyg->yy_c_buf_p = '\0'; 1335 | 1336 | else 1337 | { /* need more input */ 1338 | yy_size_t offset = yyg->yy_c_buf_p - yyg->yytext_ptr; 1339 | ++yyg->yy_c_buf_p; 1340 | 1341 | switch ( yy_get_next_buffer( yyscanner ) ) 1342 | { 1343 | case EOB_ACT_LAST_MATCH: 1344 | /* This happens because yy_g_n_b() 1345 | * sees that we've accumulated a 1346 | * token and flags that we need to 1347 | * try matching the token before 1348 | * proceeding. But for input(), 1349 | * there's no matching to consider. 1350 | * So convert the EOB_ACT_LAST_MATCH 1351 | * to EOB_ACT_END_OF_FILE. 1352 | */ 1353 | 1354 | /* Reset buffer status. */ 1355 | coslayoutrestart(yyin ,yyscanner); 1356 | 1357 | /*FALLTHROUGH*/ 1358 | 1359 | case EOB_ACT_END_OF_FILE: 1360 | { 1361 | if ( coslayoutwrap(yyscanner ) ) 1362 | return 0; 1363 | 1364 | if ( ! yyg->yy_did_buffer_switch_on_eof ) 1365 | YY_NEW_FILE; 1366 | #ifdef __cplusplus 1367 | return yyinput(yyscanner); 1368 | #else 1369 | return input(yyscanner); 1370 | #endif 1371 | } 1372 | 1373 | case EOB_ACT_CONTINUE_SCAN: 1374 | yyg->yy_c_buf_p = yyg->yytext_ptr + offset; 1375 | break; 1376 | } 1377 | } 1378 | } 1379 | 1380 | c = *(unsigned char *) yyg->yy_c_buf_p; /* cast for 8-bit char's */ 1381 | *yyg->yy_c_buf_p = '\0'; /* preserve yytext */ 1382 | yyg->yy_hold_char = *++yyg->yy_c_buf_p; 1383 | 1384 | return c; 1385 | } 1386 | #endif /* ifndef YY_NO_INPUT */ 1387 | 1388 | /** Immediately switch to a different input stream. 1389 | * @param input_file A readable stream. 1390 | * @param yyscanner The scanner object. 1391 | * @note This function does not reset the start condition to @c INITIAL . 1392 | */ 1393 | void coslayoutrestart (FILE * input_file , yyscan_t yyscanner) 1394 | { 1395 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1396 | 1397 | if ( ! YY_CURRENT_BUFFER ){ 1398 | coslayoutensure_buffer_stack (yyscanner); 1399 | YY_CURRENT_BUFFER_LVALUE = 1400 | coslayout_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); 1401 | } 1402 | 1403 | coslayout_init_buffer(YY_CURRENT_BUFFER,input_file ,yyscanner); 1404 | coslayout_load_buffer_state(yyscanner ); 1405 | } 1406 | 1407 | /** Switch to a different input buffer. 1408 | * @param new_buffer The new input buffer. 1409 | * @param yyscanner The scanner object. 1410 | */ 1411 | void coslayout_switch_to_buffer (YY_BUFFER_STATE new_buffer , yyscan_t yyscanner) 1412 | { 1413 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1414 | 1415 | /* TODO. We should be able to replace this entire function body 1416 | * with 1417 | * coslayoutpop_buffer_state(); 1418 | * coslayoutpush_buffer_state(new_buffer); 1419 | */ 1420 | coslayoutensure_buffer_stack (yyscanner); 1421 | if ( YY_CURRENT_BUFFER == new_buffer ) 1422 | return; 1423 | 1424 | if ( YY_CURRENT_BUFFER ) 1425 | { 1426 | /* Flush out information for old buffer. */ 1427 | *yyg->yy_c_buf_p = yyg->yy_hold_char; 1428 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = yyg->yy_c_buf_p; 1429 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars; 1430 | } 1431 | 1432 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1433 | coslayout_load_buffer_state(yyscanner ); 1434 | 1435 | /* We don't actually know whether we did this switch during 1436 | * EOF (coslayoutwrap()) processing, but the only time this flag 1437 | * is looked at is after coslayoutwrap() is called, so it's safe 1438 | * to go ahead and always set it. 1439 | */ 1440 | yyg->yy_did_buffer_switch_on_eof = 1; 1441 | } 1442 | 1443 | static void coslayout_load_buffer_state (yyscan_t yyscanner) 1444 | { 1445 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1446 | yyg->yy_n_chars = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 1447 | yyg->yytext_ptr = yyg->yy_c_buf_p = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; 1448 | yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; 1449 | yyg->yy_hold_char = *yyg->yy_c_buf_p; 1450 | } 1451 | 1452 | /** Allocate and initialize an input buffer state. 1453 | * @param file A readable stream. 1454 | * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. 1455 | * @param yyscanner The scanner object. 1456 | * @return the allocated buffer state. 1457 | */ 1458 | YY_BUFFER_STATE coslayout_create_buffer (FILE * file, int size , yyscan_t yyscanner) 1459 | { 1460 | YY_BUFFER_STATE b; 1461 | 1462 | b = (YY_BUFFER_STATE) coslayoutalloc(sizeof( struct yy_buffer_state ) ,yyscanner ); 1463 | if ( ! b ) 1464 | YY_FATAL_ERROR( "out of dynamic memory in coslayout_create_buffer()" ); 1465 | 1466 | b->yy_buf_size = size; 1467 | 1468 | /* yy_ch_buf has to be 2 characters longer than the size given because 1469 | * we need to put in 2 end-of-buffer characters. 1470 | */ 1471 | b->yy_ch_buf = (char *) coslayoutalloc(b->yy_buf_size + 2 ,yyscanner ); 1472 | if ( ! b->yy_ch_buf ) 1473 | YY_FATAL_ERROR( "out of dynamic memory in coslayout_create_buffer()" ); 1474 | 1475 | b->yy_is_our_buffer = 1; 1476 | 1477 | coslayout_init_buffer(b,file ,yyscanner); 1478 | 1479 | return b; 1480 | } 1481 | 1482 | /** Destroy the buffer. 1483 | * @param b a buffer created with coslayout_create_buffer() 1484 | * @param yyscanner The scanner object. 1485 | */ 1486 | void coslayout_delete_buffer (YY_BUFFER_STATE b , yyscan_t yyscanner) 1487 | { 1488 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1489 | 1490 | if ( ! b ) 1491 | return; 1492 | 1493 | if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ 1494 | YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; 1495 | 1496 | if ( b->yy_is_our_buffer ) 1497 | coslayoutfree((void *) b->yy_ch_buf ,yyscanner ); 1498 | 1499 | coslayoutfree((void *) b ,yyscanner ); 1500 | } 1501 | 1502 | #ifndef __cplusplus 1503 | extern int isatty (int ); 1504 | #endif /* __cplusplus */ 1505 | 1506 | /* Initializes or reinitializes a buffer. 1507 | * This function is sometimes called more than once on the same buffer, 1508 | * such as during a coslayoutrestart() or at EOF. 1509 | */ 1510 | static void coslayout_init_buffer (YY_BUFFER_STATE b, FILE * file , yyscan_t yyscanner) 1511 | 1512 | { 1513 | int oerrno = errno; 1514 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1515 | 1516 | coslayout_flush_buffer(b ,yyscanner); 1517 | 1518 | b->yy_input_file = file; 1519 | b->yy_fill_buffer = 1; 1520 | 1521 | /* If b is the current buffer, then coslayout_init_buffer was _probably_ 1522 | * called from coslayoutrestart() or through yy_get_next_buffer. 1523 | * In that case, we don't want to reset the lineno or column. 1524 | */ 1525 | if (b != YY_CURRENT_BUFFER){ 1526 | b->yy_bs_lineno = 1; 1527 | b->yy_bs_column = 0; 1528 | } 1529 | 1530 | b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; 1531 | 1532 | errno = oerrno; 1533 | } 1534 | 1535 | /** Discard all buffered characters. On the next scan, YY_INPUT will be called. 1536 | * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. 1537 | * @param yyscanner The scanner object. 1538 | */ 1539 | void coslayout_flush_buffer (YY_BUFFER_STATE b , yyscan_t yyscanner) 1540 | { 1541 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1542 | if ( ! b ) 1543 | return; 1544 | 1545 | b->yy_n_chars = 0; 1546 | 1547 | /* We always need two end-of-buffer characters. The first causes 1548 | * a transition to the end-of-buffer state. The second causes 1549 | * a jam in that state. 1550 | */ 1551 | b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; 1552 | b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; 1553 | 1554 | b->yy_buf_pos = &b->yy_ch_buf[0]; 1555 | 1556 | b->yy_at_bol = 1; 1557 | b->yy_buffer_status = YY_BUFFER_NEW; 1558 | 1559 | if ( b == YY_CURRENT_BUFFER ) 1560 | coslayout_load_buffer_state(yyscanner ); 1561 | } 1562 | 1563 | /** Pushes the new state onto the stack. The new state becomes 1564 | * the current state. This function will allocate the stack 1565 | * if necessary. 1566 | * @param new_buffer The new state. 1567 | * @param yyscanner The scanner object. 1568 | */ 1569 | void coslayoutpush_buffer_state (YY_BUFFER_STATE new_buffer , yyscan_t yyscanner) 1570 | { 1571 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1572 | if (new_buffer == NULL) 1573 | return; 1574 | 1575 | coslayoutensure_buffer_stack(yyscanner); 1576 | 1577 | /* This block is copied from coslayout_switch_to_buffer. */ 1578 | if ( YY_CURRENT_BUFFER ) 1579 | { 1580 | /* Flush out information for old buffer. */ 1581 | *yyg->yy_c_buf_p = yyg->yy_hold_char; 1582 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = yyg->yy_c_buf_p; 1583 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars; 1584 | } 1585 | 1586 | /* Only push if top exists. Otherwise, replace top. */ 1587 | if (YY_CURRENT_BUFFER) 1588 | yyg->yy_buffer_stack_top++; 1589 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1590 | 1591 | /* copied from coslayout_switch_to_buffer. */ 1592 | coslayout_load_buffer_state(yyscanner ); 1593 | yyg->yy_did_buffer_switch_on_eof = 1; 1594 | } 1595 | 1596 | /** Removes and deletes the top of the stack, if present. 1597 | * The next element becomes the new top. 1598 | * @param yyscanner The scanner object. 1599 | */ 1600 | void coslayoutpop_buffer_state (yyscan_t yyscanner) 1601 | { 1602 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1603 | if (!YY_CURRENT_BUFFER) 1604 | return; 1605 | 1606 | coslayout_delete_buffer(YY_CURRENT_BUFFER ,yyscanner); 1607 | YY_CURRENT_BUFFER_LVALUE = NULL; 1608 | if (yyg->yy_buffer_stack_top > 0) 1609 | --yyg->yy_buffer_stack_top; 1610 | 1611 | if (YY_CURRENT_BUFFER) { 1612 | coslayout_load_buffer_state(yyscanner ); 1613 | yyg->yy_did_buffer_switch_on_eof = 1; 1614 | } 1615 | } 1616 | 1617 | /* Allocates the stack if it does not exist. 1618 | * Guarantees space for at least one push. 1619 | */ 1620 | static void coslayoutensure_buffer_stack (yyscan_t yyscanner) 1621 | { 1622 | yy_size_t num_to_alloc; 1623 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1624 | 1625 | if (!yyg->yy_buffer_stack) { 1626 | 1627 | /* First allocation is just for 2 elements, since we don't know if this 1628 | * scanner will even need a stack. We use 2 instead of 1 to avoid an 1629 | * immediate realloc on the next call. 1630 | */ 1631 | num_to_alloc = 1; 1632 | yyg->yy_buffer_stack = (struct yy_buffer_state**)coslayoutalloc 1633 | (num_to_alloc * sizeof(struct yy_buffer_state*) 1634 | , yyscanner); 1635 | if ( ! yyg->yy_buffer_stack ) 1636 | YY_FATAL_ERROR( "out of dynamic memory in coslayoutensure_buffer_stack()" ); 1637 | 1638 | memset(yyg->yy_buffer_stack, 0, num_to_alloc * sizeof(struct yy_buffer_state*)); 1639 | 1640 | yyg->yy_buffer_stack_max = num_to_alloc; 1641 | yyg->yy_buffer_stack_top = 0; 1642 | return; 1643 | } 1644 | 1645 | if (yyg->yy_buffer_stack_top >= (yyg->yy_buffer_stack_max) - 1){ 1646 | 1647 | /* Increase the buffer to prepare for a possible push. */ 1648 | int grow_size = 8 /* arbitrary grow size */; 1649 | 1650 | num_to_alloc = yyg->yy_buffer_stack_max + grow_size; 1651 | yyg->yy_buffer_stack = (struct yy_buffer_state**)coslayoutrealloc 1652 | (yyg->yy_buffer_stack, 1653 | num_to_alloc * sizeof(struct yy_buffer_state*) 1654 | , yyscanner); 1655 | if ( ! yyg->yy_buffer_stack ) 1656 | YY_FATAL_ERROR( "out of dynamic memory in coslayoutensure_buffer_stack()" ); 1657 | 1658 | /* zero only the new slots.*/ 1659 | memset(yyg->yy_buffer_stack + yyg->yy_buffer_stack_max, 0, grow_size * sizeof(struct yy_buffer_state*)); 1660 | yyg->yy_buffer_stack_max = num_to_alloc; 1661 | } 1662 | } 1663 | 1664 | /** Setup the input buffer state to scan directly from a user-specified character buffer. 1665 | * @param base the character buffer 1666 | * @param size the size in bytes of the character buffer 1667 | * @param yyscanner The scanner object. 1668 | * @return the newly allocated buffer state object. 1669 | */ 1670 | YY_BUFFER_STATE coslayout_scan_buffer (char * base, yy_size_t size , yyscan_t yyscanner) 1671 | { 1672 | YY_BUFFER_STATE b; 1673 | 1674 | if ( size < 2 || 1675 | base[size-2] != YY_END_OF_BUFFER_CHAR || 1676 | base[size-1] != YY_END_OF_BUFFER_CHAR ) 1677 | /* They forgot to leave room for the EOB's. */ 1678 | return 0; 1679 | 1680 | b = (YY_BUFFER_STATE) coslayoutalloc(sizeof( struct yy_buffer_state ) ,yyscanner ); 1681 | if ( ! b ) 1682 | YY_FATAL_ERROR( "out of dynamic memory in coslayout_scan_buffer()" ); 1683 | 1684 | b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ 1685 | b->yy_buf_pos = b->yy_ch_buf = base; 1686 | b->yy_is_our_buffer = 0; 1687 | b->yy_input_file = 0; 1688 | b->yy_n_chars = b->yy_buf_size; 1689 | b->yy_is_interactive = 0; 1690 | b->yy_at_bol = 1; 1691 | b->yy_fill_buffer = 0; 1692 | b->yy_buffer_status = YY_BUFFER_NEW; 1693 | 1694 | coslayout_switch_to_buffer(b ,yyscanner ); 1695 | 1696 | return b; 1697 | } 1698 | 1699 | /** Setup the input buffer state to scan a string. The next call to coslayoutlex() will 1700 | * scan from a @e copy of @a str. 1701 | * @param yystr a NUL-terminated string to scan 1702 | * @param yyscanner The scanner object. 1703 | * @return the newly allocated buffer state object. 1704 | * @note If you want to scan bytes that may contain NUL values, then use 1705 | * coslayout_scan_bytes() instead. 1706 | */ 1707 | YY_BUFFER_STATE coslayout_scan_string (yyconst char * yystr , yyscan_t yyscanner) 1708 | { 1709 | 1710 | return coslayout_scan_bytes(yystr,strlen(yystr) ,yyscanner); 1711 | } 1712 | 1713 | /** Setup the input buffer state to scan the given bytes. The next call to coslayoutlex() will 1714 | * scan from a @e copy of @a bytes. 1715 | * @param bytes the byte buffer to scan 1716 | * @param len the number of bytes in the buffer pointed to by @a bytes. 1717 | * @param yyscanner The scanner object. 1718 | * @return the newly allocated buffer state object. 1719 | */ 1720 | YY_BUFFER_STATE coslayout_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len , yyscan_t yyscanner) 1721 | { 1722 | YY_BUFFER_STATE b; 1723 | char *buf; 1724 | yy_size_t n, i; 1725 | 1726 | /* Get memory for full buffer, including space for trailing EOB's. */ 1727 | n = _yybytes_len + 2; 1728 | buf = (char *) coslayoutalloc(n ,yyscanner ); 1729 | if ( ! buf ) 1730 | YY_FATAL_ERROR( "out of dynamic memory in coslayout_scan_bytes()" ); 1731 | 1732 | for ( i = 0; i < _yybytes_len; ++i ) 1733 | buf[i] = yybytes[i]; 1734 | 1735 | buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; 1736 | 1737 | b = coslayout_scan_buffer(buf,n ,yyscanner); 1738 | if ( ! b ) 1739 | YY_FATAL_ERROR( "bad buffer in coslayout_scan_bytes()" ); 1740 | 1741 | /* It's okay to grow etc. this buffer, and we should throw it 1742 | * away when we're done. 1743 | */ 1744 | b->yy_is_our_buffer = 1; 1745 | 1746 | return b; 1747 | } 1748 | 1749 | #ifndef YY_EXIT_FAILURE 1750 | #define YY_EXIT_FAILURE 2 1751 | #endif 1752 | 1753 | static void yy_fatal_error (yyconst char* msg , yyscan_t yyscanner) 1754 | { 1755 | (void) fprintf( stderr, "%s\n", msg ); 1756 | exit( YY_EXIT_FAILURE ); 1757 | } 1758 | 1759 | /* Redefine yyless() so it works in section 3 code. */ 1760 | 1761 | #undef yyless 1762 | #define yyless(n) \ 1763 | do \ 1764 | { \ 1765 | /* Undo effects of setting up yytext. */ \ 1766 | int yyless_macro_arg = (n); \ 1767 | YY_LESS_LINENO(yyless_macro_arg);\ 1768 | yytext[yyleng] = yyg->yy_hold_char; \ 1769 | yyg->yy_c_buf_p = yytext + yyless_macro_arg; \ 1770 | yyg->yy_hold_char = *yyg->yy_c_buf_p; \ 1771 | *yyg->yy_c_buf_p = '\0'; \ 1772 | yyleng = yyless_macro_arg; \ 1773 | } \ 1774 | while ( 0 ) 1775 | 1776 | /* Accessor methods (get/set functions) to struct members. */ 1777 | 1778 | /** Get the user-defined data for this scanner. 1779 | * @param yyscanner The scanner object. 1780 | */ 1781 | YY_EXTRA_TYPE coslayoutget_extra (yyscan_t yyscanner) 1782 | { 1783 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1784 | return yyextra; 1785 | } 1786 | 1787 | /** Get the current line number. 1788 | * @param yyscanner The scanner object. 1789 | */ 1790 | int coslayoutget_lineno (yyscan_t yyscanner) 1791 | { 1792 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1793 | 1794 | if (! YY_CURRENT_BUFFER) 1795 | return 0; 1796 | 1797 | return yylineno; 1798 | } 1799 | 1800 | /** Get the current column number. 1801 | * @param yyscanner The scanner object. 1802 | */ 1803 | int coslayoutget_column (yyscan_t yyscanner) 1804 | { 1805 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1806 | 1807 | if (! YY_CURRENT_BUFFER) 1808 | return 0; 1809 | 1810 | return yycolumn; 1811 | } 1812 | 1813 | /** Get the input stream. 1814 | * @param yyscanner The scanner object. 1815 | */ 1816 | FILE *coslayoutget_in (yyscan_t yyscanner) 1817 | { 1818 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1819 | return yyin; 1820 | } 1821 | 1822 | /** Get the output stream. 1823 | * @param yyscanner The scanner object. 1824 | */ 1825 | FILE *coslayoutget_out (yyscan_t yyscanner) 1826 | { 1827 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1828 | return yyout; 1829 | } 1830 | 1831 | /** Get the length of the current token. 1832 | * @param yyscanner The scanner object. 1833 | */ 1834 | yy_size_t coslayoutget_leng (yyscan_t yyscanner) 1835 | { 1836 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1837 | return yyleng; 1838 | } 1839 | 1840 | /** Get the current token. 1841 | * @param yyscanner The scanner object. 1842 | */ 1843 | 1844 | char *coslayoutget_text (yyscan_t yyscanner) 1845 | { 1846 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1847 | return yytext; 1848 | } 1849 | 1850 | /** Set the user-defined data. This data is never touched by the scanner. 1851 | * @param user_defined The data to be associated with this scanner. 1852 | * @param yyscanner The scanner object. 1853 | */ 1854 | void coslayoutset_extra (YY_EXTRA_TYPE user_defined , yyscan_t yyscanner) 1855 | { 1856 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1857 | yyextra = user_defined ; 1858 | } 1859 | 1860 | /** Set the current line number. 1861 | * @param line_number 1862 | * @param yyscanner The scanner object. 1863 | */ 1864 | void coslayoutset_lineno (int line_number , yyscan_t yyscanner) 1865 | { 1866 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1867 | 1868 | /* lineno is only valid if an input buffer exists. */ 1869 | if (! YY_CURRENT_BUFFER ) 1870 | yy_fatal_error( "coslayoutset_lineno called with no buffer" , yyscanner); 1871 | 1872 | yylineno = line_number; 1873 | } 1874 | 1875 | /** Set the current column. 1876 | * @param line_number 1877 | * @param yyscanner The scanner object. 1878 | */ 1879 | void coslayoutset_column (int column_no , yyscan_t yyscanner) 1880 | { 1881 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1882 | 1883 | /* column is only valid if an input buffer exists. */ 1884 | if (! YY_CURRENT_BUFFER ) 1885 | yy_fatal_error( "coslayoutset_column called with no buffer" , yyscanner); 1886 | 1887 | yycolumn = column_no; 1888 | } 1889 | 1890 | /** Set the input stream. This does not discard the current 1891 | * input buffer. 1892 | * @param in_str A readable stream. 1893 | * @param yyscanner The scanner object. 1894 | * @see coslayout_switch_to_buffer 1895 | */ 1896 | void coslayoutset_in (FILE * in_str , yyscan_t yyscanner) 1897 | { 1898 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1899 | yyin = in_str ; 1900 | } 1901 | 1902 | void coslayoutset_out (FILE * out_str , yyscan_t yyscanner) 1903 | { 1904 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1905 | yyout = out_str ; 1906 | } 1907 | 1908 | int coslayoutget_debug (yyscan_t yyscanner) 1909 | { 1910 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1911 | return yy_flex_debug; 1912 | } 1913 | 1914 | void coslayoutset_debug (int bdebug , yyscan_t yyscanner) 1915 | { 1916 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1917 | yy_flex_debug = bdebug ; 1918 | } 1919 | 1920 | /* Accessor methods for yylval and yylloc */ 1921 | 1922 | YYSTYPE * coslayoutget_lval (yyscan_t yyscanner) 1923 | { 1924 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1925 | return yylval; 1926 | } 1927 | 1928 | void coslayoutset_lval (YYSTYPE * yylval_param , yyscan_t yyscanner) 1929 | { 1930 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 1931 | yylval = yylval_param; 1932 | } 1933 | 1934 | /* User-visible API */ 1935 | 1936 | /* coslayoutlex_init is special because it creates the scanner itself, so it is 1937 | * the ONLY reentrant function that doesn't take the scanner as the last argument. 1938 | * That's why we explicitly handle the declaration, instead of using our macros. 1939 | */ 1940 | 1941 | int coslayoutlex_init(yyscan_t* ptr_yy_globals) 1942 | 1943 | { 1944 | if (ptr_yy_globals == NULL){ 1945 | errno = EINVAL; 1946 | return 1; 1947 | } 1948 | 1949 | *ptr_yy_globals = (yyscan_t) coslayoutalloc ( sizeof( struct yyguts_t ), NULL ); 1950 | 1951 | if (*ptr_yy_globals == NULL){ 1952 | errno = ENOMEM; 1953 | return 1; 1954 | } 1955 | 1956 | /* By setting to 0xAA, we expose bugs in yy_init_globals. Leave at 0x00 for releases. */ 1957 | memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t)); 1958 | 1959 | return yy_init_globals ( *ptr_yy_globals ); 1960 | } 1961 | 1962 | /* coslayoutlex_init_extra has the same functionality as coslayoutlex_init, but follows the 1963 | * convention of taking the scanner as the last argument. Note however, that 1964 | * this is a *pointer* to a scanner, as it will be allocated by this call (and 1965 | * is the reason, too, why this function also must handle its own declaration). 1966 | * The user defined value in the first argument will be available to coslayoutalloc in 1967 | * the yyextra field. 1968 | */ 1969 | 1970 | int coslayoutlex_init_extra(YY_EXTRA_TYPE yy_user_defined,yyscan_t* ptr_yy_globals ) 1971 | 1972 | { 1973 | struct yyguts_t dummy_yyguts; 1974 | 1975 | coslayoutset_extra (yy_user_defined, &dummy_yyguts); 1976 | 1977 | if (ptr_yy_globals == NULL){ 1978 | errno = EINVAL; 1979 | return 1; 1980 | } 1981 | 1982 | *ptr_yy_globals = (yyscan_t) coslayoutalloc ( sizeof( struct yyguts_t ), &dummy_yyguts ); 1983 | 1984 | if (*ptr_yy_globals == NULL){ 1985 | errno = ENOMEM; 1986 | return 1; 1987 | } 1988 | 1989 | /* By setting to 0xAA, we expose bugs in 1990 | yy_init_globals. Leave at 0x00 for releases. */ 1991 | memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t)); 1992 | 1993 | coslayoutset_extra (yy_user_defined, *ptr_yy_globals); 1994 | 1995 | return yy_init_globals ( *ptr_yy_globals ); 1996 | } 1997 | 1998 | static int yy_init_globals (yyscan_t yyscanner) 1999 | { 2000 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 2001 | /* Initialization is the same as for the non-reentrant scanner. 2002 | * This function is called from coslayoutlex_destroy(), so don't allocate here. 2003 | */ 2004 | 2005 | yyg->yy_buffer_stack = 0; 2006 | yyg->yy_buffer_stack_top = 0; 2007 | yyg->yy_buffer_stack_max = 0; 2008 | yyg->yy_c_buf_p = (char *) 0; 2009 | yyg->yy_init = 0; 2010 | yyg->yy_start = 0; 2011 | 2012 | yyg->yy_start_stack_ptr = 0; 2013 | yyg->yy_start_stack_depth = 0; 2014 | yyg->yy_start_stack = NULL; 2015 | 2016 | /* Defined in main.c */ 2017 | #ifdef YY_STDINIT 2018 | yyin = stdin; 2019 | yyout = stdout; 2020 | #else 2021 | yyin = (FILE *) 0; 2022 | yyout = (FILE *) 0; 2023 | #endif 2024 | 2025 | /* For future reference: Set errno on error, since we are called by 2026 | * coslayoutlex_init() 2027 | */ 2028 | return 0; 2029 | } 2030 | 2031 | /* coslayoutlex_destroy is for both reentrant and non-reentrant scanners. */ 2032 | int coslayoutlex_destroy (yyscan_t yyscanner) 2033 | { 2034 | struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; 2035 | 2036 | /* Pop the buffer stack, destroying each element. */ 2037 | while(YY_CURRENT_BUFFER){ 2038 | coslayout_delete_buffer(YY_CURRENT_BUFFER ,yyscanner ); 2039 | YY_CURRENT_BUFFER_LVALUE = NULL; 2040 | coslayoutpop_buffer_state(yyscanner); 2041 | } 2042 | 2043 | /* Destroy the stack itself. */ 2044 | coslayoutfree(yyg->yy_buffer_stack ,yyscanner); 2045 | yyg->yy_buffer_stack = NULL; 2046 | 2047 | /* Destroy the start condition stack. */ 2048 | coslayoutfree(yyg->yy_start_stack ,yyscanner ); 2049 | yyg->yy_start_stack = NULL; 2050 | 2051 | /* Reset the globals. This is important in a non-reentrant scanner so the next time 2052 | * coslayoutlex() is called, initialization will occur. */ 2053 | yy_init_globals( yyscanner); 2054 | 2055 | /* Destroy the main struct (reentrant only). */ 2056 | coslayoutfree ( yyscanner , yyscanner ); 2057 | yyscanner = NULL; 2058 | return 0; 2059 | } 2060 | 2061 | /* 2062 | * Internal utility routines. 2063 | */ 2064 | 2065 | #ifndef yytext_ptr 2066 | static void yy_flex_strncpy (char* s1, yyconst char * s2, int n , yyscan_t yyscanner) 2067 | { 2068 | register int i; 2069 | for ( i = 0; i < n; ++i ) 2070 | s1[i] = s2[i]; 2071 | } 2072 | #endif 2073 | 2074 | #ifdef YY_NEED_STRLEN 2075 | static int yy_flex_strlen (yyconst char * s , yyscan_t yyscanner) 2076 | { 2077 | register int n; 2078 | for ( n = 0; s[n]; ++n ) 2079 | ; 2080 | 2081 | return n; 2082 | } 2083 | #endif 2084 | 2085 | void *coslayoutalloc (yy_size_t size , yyscan_t yyscanner) 2086 | { 2087 | return (void *) malloc( size ); 2088 | } 2089 | 2090 | void *coslayoutrealloc (void * ptr, yy_size_t size , yyscan_t yyscanner) 2091 | { 2092 | /* The cast to (char *) in the following accommodates both 2093 | * implementations that use char* generic pointers, and those 2094 | * that use void* generic pointers. It works with the latter 2095 | * because both ANSI C and C++ allow castless assignment from 2096 | * any pointer type to void*, and deal with argument conversions 2097 | * as though doing an assignment. 2098 | */ 2099 | return (void *) realloc( (char *) ptr, size ); 2100 | } 2101 | 2102 | void coslayoutfree (void * ptr , yyscan_t yyscanner) 2103 | { 2104 | free( (char *) ptr ); /* see coslayoutrealloc() for (char *) cast */ 2105 | } 2106 | 2107 | #define YYTABLES_NAME "yytables" 2108 | 2109 | #line 135 "COSLayoutLex.l" 2110 | 2111 | 2112 | 2113 | -------------------------------------------------------------------------------- /COSLayout/COSLayoutLex.h: -------------------------------------------------------------------------------- 1 | #ifndef coslayoutHEADER_H 2 | #define coslayoutHEADER_H 1 3 | #define coslayoutIN_HEADER 1 4 | 5 | #line 6 "COSLayoutLex.h" 6 | 7 | #line 8 "COSLayoutLex.h" 8 | 9 | #define YY_INT_ALIGNED short int 10 | 11 | /* A lexical scanner generated by flex */ 12 | 13 | #define FLEX_SCANNER 14 | #define YY_FLEX_MAJOR_VERSION 2 15 | #define YY_FLEX_MINOR_VERSION 5 16 | #define YY_FLEX_SUBMINOR_VERSION 35 17 | #if YY_FLEX_SUBMINOR_VERSION > 0 18 | #define FLEX_BETA 19 | #endif 20 | 21 | /* First, we deal with platform-specific or compiler-specific issues. */ 22 | 23 | /* begin standard C headers. */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | /* end standard C headers. */ 30 | 31 | /* flex integer type definitions */ 32 | 33 | #ifndef FLEXINT_H 34 | #define FLEXINT_H 35 | 36 | /* C99 systems have . Non-C99 systems may or may not. */ 37 | 38 | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 39 | 40 | /* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, 41 | * if you want the limit (max/min) macros for int types. 42 | */ 43 | #ifndef __STDC_LIMIT_MACROS 44 | #define __STDC_LIMIT_MACROS 1 45 | #endif 46 | 47 | #include 48 | typedef int8_t flex_int8_t; 49 | typedef uint8_t flex_uint8_t; 50 | typedef int16_t flex_int16_t; 51 | typedef uint16_t flex_uint16_t; 52 | typedef int32_t flex_int32_t; 53 | typedef uint32_t flex_uint32_t; 54 | typedef uint64_t flex_uint64_t; 55 | #else 56 | typedef signed char flex_int8_t; 57 | typedef short int flex_int16_t; 58 | typedef int flex_int32_t; 59 | typedef unsigned char flex_uint8_t; 60 | typedef unsigned short int flex_uint16_t; 61 | typedef unsigned int flex_uint32_t; 62 | #endif /* ! C99 */ 63 | 64 | /* Limits of integral types. */ 65 | #ifndef INT8_MIN 66 | #define INT8_MIN (-128) 67 | #endif 68 | #ifndef INT16_MIN 69 | #define INT16_MIN (-32767-1) 70 | #endif 71 | #ifndef INT32_MIN 72 | #define INT32_MIN (-2147483647-1) 73 | #endif 74 | #ifndef INT8_MAX 75 | #define INT8_MAX (127) 76 | #endif 77 | #ifndef INT16_MAX 78 | #define INT16_MAX (32767) 79 | #endif 80 | #ifndef INT32_MAX 81 | #define INT32_MAX (2147483647) 82 | #endif 83 | #ifndef UINT8_MAX 84 | #define UINT8_MAX (255U) 85 | #endif 86 | #ifndef UINT16_MAX 87 | #define UINT16_MAX (65535U) 88 | #endif 89 | #ifndef UINT32_MAX 90 | #define UINT32_MAX (4294967295U) 91 | #endif 92 | 93 | #endif /* ! FLEXINT_H */ 94 | 95 | #ifdef __cplusplus 96 | 97 | /* The "const" storage-class-modifier is valid. */ 98 | #define YY_USE_CONST 99 | 100 | #else /* ! __cplusplus */ 101 | 102 | /* C99 requires __STDC__ to be defined as 1. */ 103 | #if defined (__STDC__) 104 | 105 | #define YY_USE_CONST 106 | 107 | #endif /* defined (__STDC__) */ 108 | #endif /* ! __cplusplus */ 109 | 110 | #ifdef YY_USE_CONST 111 | #define yyconst const 112 | #else 113 | #define yyconst 114 | #endif 115 | 116 | /* An opaque pointer. */ 117 | #ifndef YY_TYPEDEF_YY_SCANNER_T 118 | #define YY_TYPEDEF_YY_SCANNER_T 119 | typedef void* yyscan_t; 120 | #endif 121 | 122 | /* For convenience, these vars (plus the bison vars far below) 123 | are macros in the reentrant scanner. */ 124 | #define yyin yyg->yyin_r 125 | #define yyout yyg->yyout_r 126 | #define yyextra yyg->yyextra_r 127 | #define yyleng yyg->yyleng_r 128 | #define yytext yyg->yytext_r 129 | #define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno) 130 | #define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column) 131 | #define yy_flex_debug yyg->yy_flex_debug_r 132 | 133 | /* Size of default input buffer. */ 134 | #ifndef YY_BUF_SIZE 135 | #define YY_BUF_SIZE 16384 136 | #endif 137 | 138 | #ifndef YY_TYPEDEF_YY_BUFFER_STATE 139 | #define YY_TYPEDEF_YY_BUFFER_STATE 140 | typedef struct yy_buffer_state *YY_BUFFER_STATE; 141 | #endif 142 | 143 | #ifndef YY_TYPEDEF_YY_SIZE_T 144 | #define YY_TYPEDEF_YY_SIZE_T 145 | typedef size_t yy_size_t; 146 | #endif 147 | 148 | #ifndef YY_STRUCT_YY_BUFFER_STATE 149 | #define YY_STRUCT_YY_BUFFER_STATE 150 | struct yy_buffer_state 151 | { 152 | FILE *yy_input_file; 153 | 154 | char *yy_ch_buf; /* input buffer */ 155 | char *yy_buf_pos; /* current position in input buffer */ 156 | 157 | /* Size of input buffer in bytes, not including room for EOB 158 | * characters. 159 | */ 160 | yy_size_t yy_buf_size; 161 | 162 | /* Number of characters read into yy_ch_buf, not including EOB 163 | * characters. 164 | */ 165 | yy_size_t yy_n_chars; 166 | 167 | /* Whether we "own" the buffer - i.e., we know we created it, 168 | * and can realloc() it to grow it, and should free() it to 169 | * delete it. 170 | */ 171 | int yy_is_our_buffer; 172 | 173 | /* Whether this is an "interactive" input source; if so, and 174 | * if we're using stdio for input, then we want to use getc() 175 | * instead of fread(), to make sure we stop fetching input after 176 | * each newline. 177 | */ 178 | int yy_is_interactive; 179 | 180 | /* Whether we're considered to be at the beginning of a line. 181 | * If so, '^' rules will be active on the next match, otherwise 182 | * not. 183 | */ 184 | int yy_at_bol; 185 | 186 | int yy_bs_lineno; /**< The line count. */ 187 | int yy_bs_column; /**< The column count. */ 188 | 189 | /* Whether to try to fill the input buffer when we reach the 190 | * end of it. 191 | */ 192 | int yy_fill_buffer; 193 | 194 | int yy_buffer_status; 195 | 196 | }; 197 | #endif /* !YY_STRUCT_YY_BUFFER_STATE */ 198 | 199 | void coslayoutrestart (FILE *input_file ,yyscan_t yyscanner ); 200 | void coslayout_switch_to_buffer (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner ); 201 | YY_BUFFER_STATE coslayout_create_buffer (FILE *file,int size ,yyscan_t yyscanner ); 202 | void coslayout_delete_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner ); 203 | void coslayout_flush_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner ); 204 | void coslayoutpush_buffer_state (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner ); 205 | void coslayoutpop_buffer_state (yyscan_t yyscanner ); 206 | 207 | YY_BUFFER_STATE coslayout_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner ); 208 | YY_BUFFER_STATE coslayout_scan_string (yyconst char *yy_str ,yyscan_t yyscanner ); 209 | YY_BUFFER_STATE coslayout_scan_bytes (yyconst char *bytes,yy_size_t len ,yyscan_t yyscanner ); 210 | 211 | void *coslayoutalloc (yy_size_t ,yyscan_t yyscanner ); 212 | void *coslayoutrealloc (void *,yy_size_t ,yyscan_t yyscanner ); 213 | void coslayoutfree (void * ,yyscan_t yyscanner ); 214 | 215 | /* Begin user sect3 */ 216 | 217 | #define coslayoutwrap(n) 1 218 | #define YY_SKIP_YYWRAP 219 | 220 | #define yytext_ptr yytext_r 221 | 222 | #ifdef YY_HEADER_EXPORT_START_CONDITIONS 223 | #define INITIAL 0 224 | 225 | #endif 226 | 227 | #ifndef YY_NO_UNISTD_H 228 | /* Special case for "unistd.h", since it is non-ANSI. We include it way 229 | * down here because we want the user's section 1 to have been scanned first. 230 | * The user has a chance to override it with an option. 231 | */ 232 | #include 233 | #endif 234 | 235 | #ifndef YY_EXTRA_TYPE 236 | #define YY_EXTRA_TYPE void * 237 | #endif 238 | 239 | int coslayoutlex_init (yyscan_t* scanner); 240 | 241 | int coslayoutlex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner); 242 | 243 | /* Accessor methods to globals. 244 | These are made visible to non-reentrant scanners for convenience. */ 245 | 246 | int coslayoutlex_destroy (yyscan_t yyscanner ); 247 | 248 | int coslayoutget_debug (yyscan_t yyscanner ); 249 | 250 | void coslayoutset_debug (int debug_flag ,yyscan_t yyscanner ); 251 | 252 | YY_EXTRA_TYPE coslayoutget_extra (yyscan_t yyscanner ); 253 | 254 | void coslayoutset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner ); 255 | 256 | FILE *coslayoutget_in (yyscan_t yyscanner ); 257 | 258 | void coslayoutset_in (FILE * in_str ,yyscan_t yyscanner ); 259 | 260 | FILE *coslayoutget_out (yyscan_t yyscanner ); 261 | 262 | void coslayoutset_out (FILE * out_str ,yyscan_t yyscanner ); 263 | 264 | yy_size_t coslayoutget_leng (yyscan_t yyscanner ); 265 | 266 | char *coslayoutget_text (yyscan_t yyscanner ); 267 | 268 | int coslayoutget_lineno (yyscan_t yyscanner ); 269 | 270 | void coslayoutset_lineno (int line_number ,yyscan_t yyscanner ); 271 | 272 | YYSTYPE * coslayoutget_lval (yyscan_t yyscanner ); 273 | 274 | void coslayoutset_lval (YYSTYPE * yylval_param ,yyscan_t yyscanner ); 275 | 276 | /* Macros after this point can all be overridden by user definitions in 277 | * section 1. 278 | */ 279 | 280 | #ifndef YY_SKIP_YYWRAP 281 | #ifdef __cplusplus 282 | extern "C" int coslayoutwrap (yyscan_t yyscanner ); 283 | #else 284 | extern int coslayoutwrap (yyscan_t yyscanner ); 285 | #endif 286 | #endif 287 | 288 | #ifndef yytext_ptr 289 | static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner); 290 | #endif 291 | 292 | #ifdef YY_NEED_STRLEN 293 | static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner); 294 | #endif 295 | 296 | #ifndef YY_NO_INPUT 297 | 298 | #endif 299 | 300 | /* Amount of stuff to slurp up with each read. */ 301 | #ifndef YY_READ_BUF_SIZE 302 | #define YY_READ_BUF_SIZE 8192 303 | #endif 304 | 305 | /* Number of entries by which start-condition stack grows. */ 306 | #ifndef YY_START_STACK_INCR 307 | #define YY_START_STACK_INCR 25 308 | #endif 309 | 310 | /* Default declaration of generated scanner - a define so the user can 311 | * easily add parameters. 312 | */ 313 | #ifndef YY_DECL 314 | #define YY_DECL_IS_OURS 1 315 | 316 | extern int coslayoutlex \ 317 | (YYSTYPE * yylval_param ,yyscan_t yyscanner); 318 | 319 | #define YY_DECL int coslayoutlex \ 320 | (YYSTYPE * yylval_param , yyscan_t yyscanner) 321 | #endif /* !YY_DECL */ 322 | 323 | /* yy_get_previous_state - get the state just before the EOB char was reached */ 324 | 325 | #undef YY_NEW_FILE 326 | #undef YY_FLUSH_BUFFER 327 | #undef yy_set_bol 328 | #undef yy_new_buffer 329 | #undef yy_set_interactive 330 | #undef YY_DO_BEFORE_ACTION 331 | 332 | #ifdef YY_DECL_IS_OURS 333 | #undef YY_DECL_IS_OURS 334 | #undef YY_DECL 335 | #endif 336 | 337 | #line 135 "COSLayoutLex.l" 338 | 339 | 340 | #line 341 "COSLayoutLex.h" 341 | #undef coslayoutIN_HEADER 342 | #endif /* coslayoutHEADER_H */ 343 | -------------------------------------------------------------------------------- /COSLayout/COSLayoutParser.c: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.0.2. */ 2 | 3 | /* Bison implementation for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | /* C LALR(1) parser skeleton written by Richard Stallman, by 34 | simplifying the original so-called "semantic" parser. */ 35 | 36 | /* All symbols defined below should begin with yy or YY, to avoid 37 | infringing on user name space. This should be done even for local 38 | variables, as they might otherwise be expanded by user macros. 39 | There are some unavoidable exceptions within include files to 40 | define necessary library symbols; they are noted "INFRINGES ON 41 | USER NAME SPACE" below. */ 42 | 43 | /* Identify Bison output. */ 44 | #define YYBISON 1 45 | 46 | /* Bison version. */ 47 | #define YYBISON_VERSION "3.0.2" 48 | 49 | /* Skeleton name. */ 50 | #define YYSKELETON_NAME "yacc.c" 51 | 52 | /* Pure parsers. */ 53 | #define YYPURE 2 54 | 55 | /* Push parsers. */ 56 | #define YYPUSH 0 57 | 58 | /* Pull parsers. */ 59 | #define YYPULL 1 60 | 61 | /* Substitute the type names. */ 62 | #define YYSTYPE COSLAYOUTSTYPE 63 | /* Substitute the variable and function names. */ 64 | #define yyparse coslayoutparse 65 | #define yylex coslayoutlex 66 | #define yyerror coslayouterror 67 | #define yydebug coslayoutdebug 68 | #define yynerrs coslayoutnerrs 69 | 70 | 71 | /* Copy the first part of user declarations. */ 72 | #line 1 "COSLayoutParser.y" /* yacc.c:339 */ 73 | 74 | #include 75 | #include "COSLayoutParser.h" 76 | #include "COSLayoutLex.h" 77 | 78 | void coslayouterror(void *scanner, COSLAYOUT_AST **astpp, char *msg); 79 | int coslayoutlex(YYSTYPE *lvalp, void *scanner, COSLAYOUT_AST **astpp); 80 | 81 | #line 82 "COSLayoutParser.c" /* yacc.c:339 */ 82 | 83 | # ifndef YY_NULLPTR 84 | # if defined __cplusplus && 201103L <= __cplusplus 85 | # define YY_NULLPTR nullptr 86 | # else 87 | # define YY_NULLPTR 0 88 | # endif 89 | # endif 90 | 91 | /* Enabling verbose error messages. */ 92 | #ifdef YYERROR_VERBOSE 93 | # undef YYERROR_VERBOSE 94 | # define YYERROR_VERBOSE 1 95 | #else 96 | # define YYERROR_VERBOSE 0 97 | #endif 98 | 99 | /* In a future release of Bison, this section will be replaced 100 | by #include "COSLayoutParser.h". */ 101 | #ifndef YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED 102 | # define YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED 103 | /* Debug traces. */ 104 | #ifndef COSLAYOUTDEBUG 105 | # if defined YYDEBUG 106 | #if YYDEBUG 107 | # define COSLAYOUTDEBUG 1 108 | # else 109 | # define COSLAYOUTDEBUG 0 110 | # endif 111 | # else /* ! defined YYDEBUG */ 112 | # define COSLAYOUTDEBUG 0 113 | # endif /* ! defined YYDEBUG */ 114 | #endif /* ! defined COSLAYOUTDEBUG */ 115 | #if COSLAYOUTDEBUG 116 | extern int coslayoutdebug; 117 | #endif 118 | /* "%code requires" blocks. */ 119 | #line 18 "COSLayoutParser.y" /* yacc.c:355 */ 120 | 121 | #define YYSTYPE COSLAYOUTSTYPE 122 | 123 | #define YY_DECL int coslayoutlex \ 124 | (YYSTYPE *yylval_param, yyscan_t yyscanner, COSLAYOUT_AST **astpp) 125 | 126 | struct COSLAYOUT_AST { 127 | int node_type; 128 | struct COSLAYOUT_AST *l; 129 | struct COSLAYOUT_AST *r; 130 | union { 131 | float number; 132 | float percentage; 133 | char *coord; 134 | } value; 135 | void *data; 136 | }; 137 | 138 | typedef struct COSLAYOUT_AST COSLAYOUT_AST; 139 | 140 | COSLAYOUT_AST *coslayout_create_ast(int type, COSLAYOUT_AST *l, COSLAYOUT_AST *r); 141 | 142 | int coslayout_parse_rule(char *rule, COSLAYOUT_AST **astpp); 143 | void coslayout_destroy_ast(COSLAYOUT_AST *astp); 144 | 145 | #line 146 "COSLayoutParser.c" /* yacc.c:355 */ 146 | 147 | /* Token type. */ 148 | #ifndef COSLAYOUTTOKENTYPE 149 | # define COSLAYOUTTOKENTYPE 150 | enum coslayouttokentype 151 | { 152 | COSLAYOUT_TOKEN_ATTR = 258, 153 | COSLAYOUT_TOKEN_NUMBER = 259, 154 | COSLAYOUT_TOKEN_PERCENTAGE = 260, 155 | COSLAYOUT_TOKEN_PERCENTAGE_H = 261, 156 | COSLAYOUT_TOKEN_PERCENTAGE_V = 262, 157 | COSLAYOUT_TOKEN_COORD = 263, 158 | COSLAYOUT_TOKEN_COORD_PERCENTAGE = 264, 159 | COSLAYOUT_TOKEN_COORD_PERCENTAGE_H = 265, 160 | COSLAYOUT_TOKEN_COORD_PERCENTAGE_V = 266, 161 | COSLAYOUT_TOKEN_NIL = 267, 162 | COSLAYOUT_TOKEN_ADD_ASSIGN = 268, 163 | COSLAYOUT_TOKEN_SUB_ASSIGN = 269, 164 | COSLAYOUT_TOKEN_MUL_ASSIGN = 270, 165 | COSLAYOUT_TOKEN_DIV_ASSIGN = 271 166 | }; 167 | #endif 168 | 169 | /* Value type. */ 170 | #if ! defined COSLAYOUTSTYPE && ! defined COSLAYOUTSTYPE_IS_DECLARED 171 | typedef COSLAYOUT_AST * COSLAYOUTSTYPE; 172 | # define COSLAYOUTSTYPE_IS_TRIVIAL 1 173 | # define COSLAYOUTSTYPE_IS_DECLARED 1 174 | #endif 175 | 176 | 177 | 178 | int coslayoutparse (void *scanner, COSLAYOUT_AST **astpp); 179 | 180 | #endif /* !YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED */ 181 | 182 | /* Copy the second part of user declarations. */ 183 | 184 | #line 185 "COSLayoutParser.c" /* yacc.c:358 */ 185 | 186 | #ifdef short 187 | # undef short 188 | #endif 189 | 190 | #ifdef YYTYPE_UINT8 191 | typedef YYTYPE_UINT8 yytype_uint8; 192 | #else 193 | typedef unsigned char yytype_uint8; 194 | #endif 195 | 196 | #ifdef YYTYPE_INT8 197 | typedef YYTYPE_INT8 yytype_int8; 198 | #else 199 | typedef signed char yytype_int8; 200 | #endif 201 | 202 | #ifdef YYTYPE_UINT16 203 | typedef YYTYPE_UINT16 yytype_uint16; 204 | #else 205 | typedef unsigned short int yytype_uint16; 206 | #endif 207 | 208 | #ifdef YYTYPE_INT16 209 | typedef YYTYPE_INT16 yytype_int16; 210 | #else 211 | typedef short int yytype_int16; 212 | #endif 213 | 214 | #ifndef YYSIZE_T 215 | # ifdef __SIZE_TYPE__ 216 | # define YYSIZE_T __SIZE_TYPE__ 217 | # elif defined size_t 218 | # define YYSIZE_T size_t 219 | # elif ! defined YYSIZE_T 220 | # include /* INFRINGES ON USER NAME SPACE */ 221 | # define YYSIZE_T size_t 222 | # else 223 | # define YYSIZE_T unsigned int 224 | # endif 225 | #endif 226 | 227 | #define YYSIZE_MAXIMUM ((YYSIZE_T) -1) 228 | 229 | #ifndef YY_ 230 | # if defined YYENABLE_NLS && YYENABLE_NLS 231 | # if ENABLE_NLS 232 | # include /* INFRINGES ON USER NAME SPACE */ 233 | # define YY_(Msgid) dgettext ("bison-runtime", Msgid) 234 | # endif 235 | # endif 236 | # ifndef YY_ 237 | # define YY_(Msgid) Msgid 238 | # endif 239 | #endif 240 | 241 | #ifndef YY_ATTRIBUTE 242 | # if (defined __GNUC__ \ 243 | && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ 244 | || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C 245 | # define YY_ATTRIBUTE(Spec) __attribute__(Spec) 246 | # else 247 | # define YY_ATTRIBUTE(Spec) /* empty */ 248 | # endif 249 | #endif 250 | 251 | #ifndef YY_ATTRIBUTE_PURE 252 | # define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) 253 | #endif 254 | 255 | #ifndef YY_ATTRIBUTE_UNUSED 256 | # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) 257 | #endif 258 | 259 | #if !defined _Noreturn \ 260 | && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) 261 | # if defined _MSC_VER && 1200 <= _MSC_VER 262 | # define _Noreturn __declspec (noreturn) 263 | # else 264 | # define _Noreturn YY_ATTRIBUTE ((__noreturn__)) 265 | # endif 266 | #endif 267 | 268 | /* Suppress unused-variable warnings by "using" E. */ 269 | #if ! defined lint || defined __GNUC__ 270 | # define YYUSE(E) ((void) (E)) 271 | #else 272 | # define YYUSE(E) /* empty */ 273 | #endif 274 | 275 | #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ 276 | /* Suppress an incorrect diagnostic about yylval being uninitialized. */ 277 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ 278 | _Pragma ("GCC diagnostic push") \ 279 | _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ 280 | _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") 281 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END \ 282 | _Pragma ("GCC diagnostic pop") 283 | #else 284 | # define YY_INITIAL_VALUE(Value) Value 285 | #endif 286 | #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 287 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 288 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END 289 | #endif 290 | #ifndef YY_INITIAL_VALUE 291 | # define YY_INITIAL_VALUE(Value) /* Nothing. */ 292 | #endif 293 | 294 | 295 | #if ! defined yyoverflow || YYERROR_VERBOSE 296 | 297 | /* The parser invokes alloca or malloc; define the necessary symbols. */ 298 | 299 | # ifdef YYSTACK_USE_ALLOCA 300 | # if YYSTACK_USE_ALLOCA 301 | # ifdef __GNUC__ 302 | # define YYSTACK_ALLOC __builtin_alloca 303 | # elif defined __BUILTIN_VA_ARG_INCR 304 | # include /* INFRINGES ON USER NAME SPACE */ 305 | # elif defined _AIX 306 | # define YYSTACK_ALLOC __alloca 307 | # elif defined _MSC_VER 308 | # include /* INFRINGES ON USER NAME SPACE */ 309 | # define alloca _alloca 310 | # else 311 | # define YYSTACK_ALLOC alloca 312 | # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS 313 | # include /* INFRINGES ON USER NAME SPACE */ 314 | /* Use EXIT_SUCCESS as a witness for stdlib.h. */ 315 | # ifndef EXIT_SUCCESS 316 | # define EXIT_SUCCESS 0 317 | # endif 318 | # endif 319 | # endif 320 | # endif 321 | # endif 322 | 323 | # ifdef YYSTACK_ALLOC 324 | /* Pacify GCC's 'empty if-body' warning. */ 325 | # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) 326 | # ifndef YYSTACK_ALLOC_MAXIMUM 327 | /* The OS might guarantee only one guard page at the bottom of the stack, 328 | and a page size can be as small as 4096 bytes. So we cannot safely 329 | invoke alloca (N) if N exceeds 4096. Use a slightly smaller number 330 | to allow for a few compiler-allocated temporary stack slots. */ 331 | # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ 332 | # endif 333 | # else 334 | # define YYSTACK_ALLOC YYMALLOC 335 | # define YYSTACK_FREE YYFREE 336 | # ifndef YYSTACK_ALLOC_MAXIMUM 337 | # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM 338 | # endif 339 | # if (defined __cplusplus && ! defined EXIT_SUCCESS \ 340 | && ! ((defined YYMALLOC || defined malloc) \ 341 | && (defined YYFREE || defined free))) 342 | # include /* INFRINGES ON USER NAME SPACE */ 343 | # ifndef EXIT_SUCCESS 344 | # define EXIT_SUCCESS 0 345 | # endif 346 | # endif 347 | # ifndef YYMALLOC 348 | # define YYMALLOC malloc 349 | # if ! defined malloc && ! defined EXIT_SUCCESS 350 | void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ 351 | # endif 352 | # endif 353 | # ifndef YYFREE 354 | # define YYFREE free 355 | # if ! defined free && ! defined EXIT_SUCCESS 356 | void free (void *); /* INFRINGES ON USER NAME SPACE */ 357 | # endif 358 | # endif 359 | # endif 360 | #endif /* ! defined yyoverflow || YYERROR_VERBOSE */ 361 | 362 | 363 | #if (! defined yyoverflow \ 364 | && (! defined __cplusplus \ 365 | || (defined COSLAYOUTSTYPE_IS_TRIVIAL && COSLAYOUTSTYPE_IS_TRIVIAL))) 366 | 367 | /* A type that is properly aligned for any stack member. */ 368 | union yyalloc 369 | { 370 | yytype_int16 yyss_alloc; 371 | YYSTYPE yyvs_alloc; 372 | }; 373 | 374 | /* The size of the maximum gap between one aligned stack and the next. */ 375 | # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) 376 | 377 | /* The size of an array large to enough to hold all stacks, each with 378 | N elements. */ 379 | # define YYSTACK_BYTES(N) \ 380 | ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \ 381 | + YYSTACK_GAP_MAXIMUM) 382 | 383 | # define YYCOPY_NEEDED 1 384 | 385 | /* Relocate STACK from its old location to the new one. The 386 | local variables YYSIZE and YYSTACKSIZE give the old and new number of 387 | elements in the stack, and YYPTR gives the new location of the 388 | stack. Advance YYPTR to a properly aligned location for the next 389 | stack. */ 390 | # define YYSTACK_RELOCATE(Stack_alloc, Stack) \ 391 | do \ 392 | { \ 393 | YYSIZE_T yynewbytes; \ 394 | YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ 395 | Stack = &yyptr->Stack_alloc; \ 396 | yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ 397 | yyptr += yynewbytes / sizeof (*yyptr); \ 398 | } \ 399 | while (0) 400 | 401 | #endif 402 | 403 | #if defined YYCOPY_NEEDED && YYCOPY_NEEDED 404 | /* Copy COUNT objects from SRC to DST. The source and destination do 405 | not overlap. */ 406 | # ifndef YYCOPY 407 | # if defined __GNUC__ && 1 < __GNUC__ 408 | # define YYCOPY(Dst, Src, Count) \ 409 | __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src))) 410 | # else 411 | # define YYCOPY(Dst, Src, Count) \ 412 | do \ 413 | { \ 414 | YYSIZE_T yyi; \ 415 | for (yyi = 0; yyi < (Count); yyi++) \ 416 | (Dst)[yyi] = (Src)[yyi]; \ 417 | } \ 418 | while (0) 419 | # endif 420 | # endif 421 | #endif /* !YYCOPY_NEEDED */ 422 | 423 | /* YYFINAL -- State number of the termination state. */ 424 | #define YYFINAL 24 425 | /* YYLAST -- Last index in YYTABLE. */ 426 | #define YYLAST 88 427 | 428 | /* YYNTOKENS -- Number of terminals. */ 429 | #define YYNTOKENS 24 430 | /* YYNNTS -- Number of nonterminals. */ 431 | #define YYNNTS 6 432 | /* YYNRULES -- Number of rules. */ 433 | #define YYNRULES 27 434 | /* YYNSTATES -- Number of states. */ 435 | #define YYNSTATES 36 436 | 437 | /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned 438 | by yylex, with out-of-bounds checking. */ 439 | #define YYUNDEFTOK 2 440 | #define YYMAXUTOK 271 441 | 442 | #define YYTRANSLATE(YYX) \ 443 | ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) 444 | 445 | /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM 446 | as returned by yylex, without out-of-bounds checking. */ 447 | static const yytype_uint8 yytranslate[] = 448 | { 449 | 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 450 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 451 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 452 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 453 | 22, 23, 19, 17, 2, 18, 2, 20, 2, 2, 454 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 455 | 2, 21, 2, 2, 2, 2, 2, 2, 2, 2, 456 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 457 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 458 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 459 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 460 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 461 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 462 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 463 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 464 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 465 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 466 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 467 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 468 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 469 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 470 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 471 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 472 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 473 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 474 | 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, 475 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 476 | 15, 16 477 | }; 478 | 479 | #if COSLAYOUTDEBUG 480 | /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ 481 | static const yytype_uint8 yyrline[] = 482 | { 483 | 0, 70, 70, 71, 72, 73, 77, 78, 79, 80, 484 | 81, 85, 86, 87, 91, 92, 93, 97, 98, 99, 485 | 100, 101, 102, 103, 104, 105, 106, 107 486 | }; 487 | #endif 488 | 489 | #if COSLAYOUTDEBUG || YYERROR_VERBOSE || 0 490 | /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. 491 | First, the terminals, then, starting at YYNTOKENS, nonterminals. */ 492 | static const char *const yytname[] = 493 | { 494 | "$end", "error", "$undefined", "COSLAYOUT_TOKEN_ATTR", 495 | "COSLAYOUT_TOKEN_NUMBER", "COSLAYOUT_TOKEN_PERCENTAGE", 496 | "COSLAYOUT_TOKEN_PERCENTAGE_H", "COSLAYOUT_TOKEN_PERCENTAGE_V", 497 | "COSLAYOUT_TOKEN_COORD", "COSLAYOUT_TOKEN_COORD_PERCENTAGE", 498 | "COSLAYOUT_TOKEN_COORD_PERCENTAGE_H", 499 | "COSLAYOUT_TOKEN_COORD_PERCENTAGE_V", "COSLAYOUT_TOKEN_NIL", 500 | "COSLAYOUT_TOKEN_ADD_ASSIGN", "COSLAYOUT_TOKEN_SUB_ASSIGN", 501 | "COSLAYOUT_TOKEN_MUL_ASSIGN", "COSLAYOUT_TOKEN_DIV_ASSIGN", "'+'", "'-'", 502 | "'*'", "'/'", "'='", "'('", "')'", "$accept", "expr", "assign", "rval", 503 | "item", "atom", YY_NULLPTR 504 | }; 505 | #endif 506 | 507 | # ifdef YYPRINT 508 | /* YYTOKNUM[NUM] -- (External) token number corresponding to the 509 | (internal) symbol number NUM (which must be that of a token). */ 510 | static const yytype_uint16 yytoknum[] = 511 | { 512 | 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, 513 | 265, 266, 267, 268, 269, 270, 271, 43, 45, 42, 514 | 47, 61, 40, 41 515 | }; 516 | # endif 517 | 518 | #define YYPACT_NINF -9 519 | 520 | #define yypact_value_is_default(Yystate) \ 521 | (!!((Yystate) == (-9))) 522 | 523 | #define YYTABLE_NINF -3 524 | 525 | #define yytable_value_is_error(Yytable_value) \ 526 | 0 527 | 528 | /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing 529 | STATE-NUM. */ 530 | static const yytype_int8 yypact[] = 531 | { 532 | 24, -9, 67, -9, -9, -9, -9, -9, -9, -9, 533 | -9, -9, 44, 2, -4, -2, -9, -9, -9, -9, 534 | -9, -9, 0, -8, -9, 65, 65, 65, 65, -9, 535 | -9, -9, -2, -2, -9, -9 536 | }; 537 | 538 | /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. 539 | Performed when YYTABLE does not specify something else to do. Zero 540 | means the default is an error. */ 541 | static const yytype_uint8 yydefact[] = 542 | { 543 | 0, 3, 17, 18, 19, 20, 21, 22, 23, 24, 544 | 25, 26, 0, 0, 5, 13, 16, 7, 8, 9, 545 | 10, 6, 0, 0, 1, 0, 0, 0, 0, 4, 546 | 27, 17, 11, 12, 14, 15 547 | }; 548 | 549 | /* YYPGOTO[NTERM-NUM]. */ 550 | static const yytype_int8 yypgoto[] = 551 | { 552 | -9, 4, -9, -9, -6, 10 553 | }; 554 | 555 | /* YYDEFGOTO[NTERM-NUM]. */ 556 | static const yytype_int8 yydefgoto[] = 557 | { 558 | -1, 13, 22, 14, 15, 16 559 | }; 560 | 561 | /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If 562 | positive, shift that token. If negative, reduce the rule whose 563 | number is the opposite. If YYTABLE_NINF, syntax error. */ 564 | static const yytype_int8 yytable[] = 565 | { 566 | -2, 1, 24, 2, 3, 4, 5, 6, 7, 8, 567 | 9, 10, 11, 25, 26, 30, 23, 27, 28, 32, 568 | 33, 0, 12, -2, -2, 1, 29, 2, 3, 4, 569 | 5, 6, 7, 8, 9, 10, 11, 34, 35, 0, 570 | 0, 0, 0, 0, 0, 1, 12, 2, 3, 4, 571 | 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 572 | 0, 0, 0, 0, 0, 0, 12, -2, 31, 3, 573 | 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 574 | 17, 18, 19, 20, 0, 0, 0, 12, 21 575 | }; 576 | 577 | static const yytype_int8 yycheck[] = 578 | { 579 | 0, 1, 0, 3, 4, 5, 6, 7, 8, 9, 580 | 10, 11, 12, 17, 18, 23, 12, 19, 20, 25, 581 | 26, -1, 22, 23, 0, 1, 22, 3, 4, 5, 582 | 6, 7, 8, 9, 10, 11, 12, 27, 28, -1, 583 | -1, -1, -1, -1, -1, 1, 22, 3, 4, 5, 584 | 6, 7, 8, 9, 10, 11, 12, -1, -1, -1, 585 | -1, -1, -1, -1, -1, -1, 22, 23, 3, 4, 586 | 5, 6, 7, 8, 9, 10, 11, 12, -1, -1, 587 | 13, 14, 15, 16, -1, -1, -1, 22, 21 588 | }; 589 | 590 | /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing 591 | symbol of state STATE-NUM. */ 592 | static const yytype_uint8 yystos[] = 593 | { 594 | 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 595 | 11, 12, 22, 25, 27, 28, 29, 13, 14, 15, 596 | 16, 21, 26, 25, 0, 17, 18, 19, 20, 25, 597 | 23, 3, 28, 28, 29, 29 598 | }; 599 | 600 | /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ 601 | static const yytype_uint8 yyr1[] = 602 | { 603 | 0, 24, 25, 25, 25, 25, 26, 26, 26, 26, 604 | 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 605 | 29, 29, 29, 29, 29, 29, 29, 29 606 | }; 607 | 608 | /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ 609 | static const yytype_uint8 yyr2[] = 610 | { 611 | 0, 2, 0, 1, 3, 1, 1, 1, 1, 1, 612 | 1, 3, 3, 1, 3, 3, 1, 1, 1, 1, 613 | 1, 1, 1, 1, 1, 1, 1, 3 614 | }; 615 | 616 | 617 | #define yyerrok (yyerrstatus = 0) 618 | #define yyclearin (yychar = YYEMPTY) 619 | #define YYEMPTY (-2) 620 | #define YYEOF 0 621 | 622 | #define YYACCEPT goto yyacceptlab 623 | #define YYABORT goto yyabortlab 624 | #define YYERROR goto yyerrorlab 625 | 626 | 627 | #define YYRECOVERING() (!!yyerrstatus) 628 | 629 | #define YYBACKUP(Token, Value) \ 630 | do \ 631 | if (yychar == YYEMPTY) \ 632 | { \ 633 | yychar = (Token); \ 634 | yylval = (Value); \ 635 | YYPOPSTACK (yylen); \ 636 | yystate = *yyssp; \ 637 | goto yybackup; \ 638 | } \ 639 | else \ 640 | { \ 641 | yyerror (scanner, astpp, YY_("syntax error: cannot back up")); \ 642 | YYERROR; \ 643 | } \ 644 | while (0) 645 | 646 | /* Error token number */ 647 | #define YYTERROR 1 648 | #define YYERRCODE 256 649 | 650 | 651 | 652 | /* Enable debugging if requested. */ 653 | #if COSLAYOUTDEBUG 654 | 655 | # ifndef YYFPRINTF 656 | # include /* INFRINGES ON USER NAME SPACE */ 657 | # define YYFPRINTF fprintf 658 | # endif 659 | 660 | # define YYDPRINTF(Args) \ 661 | do { \ 662 | if (yydebug) \ 663 | YYFPRINTF Args; \ 664 | } while (0) 665 | 666 | /* This macro is provided for backward compatibility. */ 667 | #ifndef YY_LOCATION_PRINT 668 | # define YY_LOCATION_PRINT(File, Loc) ((void) 0) 669 | #endif 670 | 671 | 672 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ 673 | do { \ 674 | if (yydebug) \ 675 | { \ 676 | YYFPRINTF (stderr, "%s ", Title); \ 677 | yy_symbol_print (stderr, \ 678 | Type, Value, scanner, astpp); \ 679 | YYFPRINTF (stderr, "\n"); \ 680 | } \ 681 | } while (0) 682 | 683 | 684 | /*----------------------------------------. 685 | | Print this symbol's value on YYOUTPUT. | 686 | `----------------------------------------*/ 687 | 688 | static void 689 | yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *scanner, COSLAYOUT_AST **astpp) 690 | { 691 | FILE *yyo = yyoutput; 692 | YYUSE (yyo); 693 | YYUSE (scanner); 694 | YYUSE (astpp); 695 | if (!yyvaluep) 696 | return; 697 | # ifdef YYPRINT 698 | if (yytype < YYNTOKENS) 699 | YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); 700 | # endif 701 | YYUSE (yytype); 702 | } 703 | 704 | 705 | /*--------------------------------. 706 | | Print this symbol on YYOUTPUT. | 707 | `--------------------------------*/ 708 | 709 | static void 710 | yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *scanner, COSLAYOUT_AST **astpp) 711 | { 712 | YYFPRINTF (yyoutput, "%s %s (", 713 | yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); 714 | 715 | yy_symbol_value_print (yyoutput, yytype, yyvaluep, scanner, astpp); 716 | YYFPRINTF (yyoutput, ")"); 717 | } 718 | 719 | /*------------------------------------------------------------------. 720 | | yy_stack_print -- Print the state stack from its BOTTOM up to its | 721 | | TOP (included). | 722 | `------------------------------------------------------------------*/ 723 | 724 | static void 725 | yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop) 726 | { 727 | YYFPRINTF (stderr, "Stack now"); 728 | for (; yybottom <= yytop; yybottom++) 729 | { 730 | int yybot = *yybottom; 731 | YYFPRINTF (stderr, " %d", yybot); 732 | } 733 | YYFPRINTF (stderr, "\n"); 734 | } 735 | 736 | # define YY_STACK_PRINT(Bottom, Top) \ 737 | do { \ 738 | if (yydebug) \ 739 | yy_stack_print ((Bottom), (Top)); \ 740 | } while (0) 741 | 742 | 743 | /*------------------------------------------------. 744 | | Report that the YYRULE is going to be reduced. | 745 | `------------------------------------------------*/ 746 | 747 | static void 748 | yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule, void *scanner, COSLAYOUT_AST **astpp) 749 | { 750 | unsigned long int yylno = yyrline[yyrule]; 751 | int yynrhs = yyr2[yyrule]; 752 | int yyi; 753 | YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n", 754 | yyrule - 1, yylno); 755 | /* The symbols being reduced. */ 756 | for (yyi = 0; yyi < yynrhs; yyi++) 757 | { 758 | YYFPRINTF (stderr, " $%d = ", yyi + 1); 759 | yy_symbol_print (stderr, 760 | yystos[yyssp[yyi + 1 - yynrhs]], 761 | &(yyvsp[(yyi + 1) - (yynrhs)]) 762 | , scanner, astpp); 763 | YYFPRINTF (stderr, "\n"); 764 | } 765 | } 766 | 767 | # define YY_REDUCE_PRINT(Rule) \ 768 | do { \ 769 | if (yydebug) \ 770 | yy_reduce_print (yyssp, yyvsp, Rule, scanner, astpp); \ 771 | } while (0) 772 | 773 | /* Nonzero means print parse trace. It is left uninitialized so that 774 | multiple parsers can coexist. */ 775 | int yydebug; 776 | #else /* !COSLAYOUTDEBUG */ 777 | # define YYDPRINTF(Args) 778 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) 779 | # define YY_STACK_PRINT(Bottom, Top) 780 | # define YY_REDUCE_PRINT(Rule) 781 | #endif /* !COSLAYOUTDEBUG */ 782 | 783 | 784 | /* YYINITDEPTH -- initial size of the parser's stacks. */ 785 | #ifndef YYINITDEPTH 786 | # define YYINITDEPTH 200 787 | #endif 788 | 789 | /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only 790 | if the built-in stack extension method is used). 791 | 792 | Do not make this value too large; the results are undefined if 793 | YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) 794 | evaluated with infinite-precision integer arithmetic. */ 795 | 796 | #ifndef YYMAXDEPTH 797 | # define YYMAXDEPTH 10000 798 | #endif 799 | 800 | 801 | #if YYERROR_VERBOSE 802 | 803 | # ifndef yystrlen 804 | # if defined __GLIBC__ && defined _STRING_H 805 | # define yystrlen strlen 806 | # else 807 | /* Return the length of YYSTR. */ 808 | static YYSIZE_T 809 | yystrlen (const char *yystr) 810 | { 811 | YYSIZE_T yylen; 812 | for (yylen = 0; yystr[yylen]; yylen++) 813 | continue; 814 | return yylen; 815 | } 816 | # endif 817 | # endif 818 | 819 | # ifndef yystpcpy 820 | # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE 821 | # define yystpcpy stpcpy 822 | # else 823 | /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in 824 | YYDEST. */ 825 | static char * 826 | yystpcpy (char *yydest, const char *yysrc) 827 | { 828 | char *yyd = yydest; 829 | const char *yys = yysrc; 830 | 831 | while ((*yyd++ = *yys++) != '\0') 832 | continue; 833 | 834 | return yyd - 1; 835 | } 836 | # endif 837 | # endif 838 | 839 | # ifndef yytnamerr 840 | /* Copy to YYRES the contents of YYSTR after stripping away unnecessary 841 | quotes and backslashes, so that it's suitable for yyerror. The 842 | heuristic is that double-quoting is unnecessary unless the string 843 | contains an apostrophe, a comma, or backslash (other than 844 | backslash-backslash). YYSTR is taken from yytname. If YYRES is 845 | null, do not copy; instead, return the length of what the result 846 | would have been. */ 847 | static YYSIZE_T 848 | yytnamerr (char *yyres, const char *yystr) 849 | { 850 | if (*yystr == '"') 851 | { 852 | YYSIZE_T yyn = 0; 853 | char const *yyp = yystr; 854 | 855 | for (;;) 856 | switch (*++yyp) 857 | { 858 | case '\'': 859 | case ',': 860 | goto do_not_strip_quotes; 861 | 862 | case '\\': 863 | if (*++yyp != '\\') 864 | goto do_not_strip_quotes; 865 | /* Fall through. */ 866 | default: 867 | if (yyres) 868 | yyres[yyn] = *yyp; 869 | yyn++; 870 | break; 871 | 872 | case '"': 873 | if (yyres) 874 | yyres[yyn] = '\0'; 875 | return yyn; 876 | } 877 | do_not_strip_quotes: ; 878 | } 879 | 880 | if (! yyres) 881 | return yystrlen (yystr); 882 | 883 | return yystpcpy (yyres, yystr) - yyres; 884 | } 885 | # endif 886 | 887 | /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message 888 | about the unexpected token YYTOKEN for the state stack whose top is 889 | YYSSP. 890 | 891 | Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is 892 | not large enough to hold the message. In that case, also set 893 | *YYMSG_ALLOC to the required number of bytes. Return 2 if the 894 | required number of bytes is too large to store. */ 895 | static int 896 | yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, 897 | yytype_int16 *yyssp, int yytoken) 898 | { 899 | YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); 900 | YYSIZE_T yysize = yysize0; 901 | enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; 902 | /* Internationalized format string. */ 903 | const char *yyformat = YY_NULLPTR; 904 | /* Arguments of yyformat. */ 905 | char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; 906 | /* Number of reported tokens (one for the "unexpected", one per 907 | "expected"). */ 908 | int yycount = 0; 909 | 910 | /* There are many possibilities here to consider: 911 | - If this state is a consistent state with a default action, then 912 | the only way this function was invoked is if the default action 913 | is an error action. In that case, don't check for expected 914 | tokens because there are none. 915 | - The only way there can be no lookahead present (in yychar) is if 916 | this state is a consistent state with a default action. Thus, 917 | detecting the absence of a lookahead is sufficient to determine 918 | that there is no unexpected or expected token to report. In that 919 | case, just report a simple "syntax error". 920 | - Don't assume there isn't a lookahead just because this state is a 921 | consistent state with a default action. There might have been a 922 | previous inconsistent state, consistent state with a non-default 923 | action, or user semantic action that manipulated yychar. 924 | - Of course, the expected token list depends on states to have 925 | correct lookahead information, and it depends on the parser not 926 | to perform extra reductions after fetching a lookahead from the 927 | scanner and before detecting a syntax error. Thus, state merging 928 | (from LALR or IELR) and default reductions corrupt the expected 929 | token list. However, the list is correct for canonical LR with 930 | one exception: it will still contain any token that will not be 931 | accepted due to an error action in a later state. 932 | */ 933 | if (yytoken != YYEMPTY) 934 | { 935 | int yyn = yypact[*yyssp]; 936 | yyarg[yycount++] = yytname[yytoken]; 937 | if (!yypact_value_is_default (yyn)) 938 | { 939 | /* Start YYX at -YYN if negative to avoid negative indexes in 940 | YYCHECK. In other words, skip the first -YYN actions for 941 | this state because they are default actions. */ 942 | int yyxbegin = yyn < 0 ? -yyn : 0; 943 | /* Stay within bounds of both yycheck and yytname. */ 944 | int yychecklim = YYLAST - yyn + 1; 945 | int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; 946 | int yyx; 947 | 948 | for (yyx = yyxbegin; yyx < yyxend; ++yyx) 949 | if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR 950 | && !yytable_value_is_error (yytable[yyx + yyn])) 951 | { 952 | if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) 953 | { 954 | yycount = 1; 955 | yysize = yysize0; 956 | break; 957 | } 958 | yyarg[yycount++] = yytname[yyx]; 959 | { 960 | YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); 961 | if (! (yysize <= yysize1 962 | && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) 963 | return 2; 964 | yysize = yysize1; 965 | } 966 | } 967 | } 968 | } 969 | 970 | switch (yycount) 971 | { 972 | # define YYCASE_(N, S) \ 973 | case N: \ 974 | yyformat = S; \ 975 | break 976 | YYCASE_(0, YY_("syntax error")); 977 | YYCASE_(1, YY_("syntax error, unexpected %s")); 978 | YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); 979 | YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); 980 | YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); 981 | YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); 982 | # undef YYCASE_ 983 | } 984 | 985 | { 986 | YYSIZE_T yysize1 = yysize + yystrlen (yyformat); 987 | if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) 988 | return 2; 989 | yysize = yysize1; 990 | } 991 | 992 | if (*yymsg_alloc < yysize) 993 | { 994 | *yymsg_alloc = 2 * yysize; 995 | if (! (yysize <= *yymsg_alloc 996 | && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) 997 | *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; 998 | return 1; 999 | } 1000 | 1001 | /* Avoid sprintf, as that infringes on the user's name space. 1002 | Don't have undefined behavior even if the translation 1003 | produced a string with the wrong number of "%s"s. */ 1004 | { 1005 | char *yyp = *yymsg; 1006 | int yyi = 0; 1007 | while ((*yyp = *yyformat) != '\0') 1008 | if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) 1009 | { 1010 | yyp += yytnamerr (yyp, yyarg[yyi++]); 1011 | yyformat += 2; 1012 | } 1013 | else 1014 | { 1015 | yyp++; 1016 | yyformat++; 1017 | } 1018 | } 1019 | return 0; 1020 | } 1021 | #endif /* YYERROR_VERBOSE */ 1022 | 1023 | /*-----------------------------------------------. 1024 | | Release the memory associated to this symbol. | 1025 | `-----------------------------------------------*/ 1026 | 1027 | static void 1028 | yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, void *scanner, COSLAYOUT_AST **astpp) 1029 | { 1030 | YYUSE (yyvaluep); 1031 | YYUSE (scanner); 1032 | YYUSE (astpp); 1033 | if (!yymsg) 1034 | yymsg = "Deleting"; 1035 | YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); 1036 | 1037 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1038 | YYUSE (yytype); 1039 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1040 | } 1041 | 1042 | 1043 | 1044 | 1045 | /*----------. 1046 | | yyparse. | 1047 | `----------*/ 1048 | 1049 | int 1050 | yyparse (void *scanner, COSLAYOUT_AST **astpp) 1051 | { 1052 | /* The lookahead symbol. */ 1053 | int yychar; 1054 | 1055 | 1056 | /* The semantic value of the lookahead symbol. */ 1057 | /* Default value used for initialization, for pacifying older GCCs 1058 | or non-GCC compilers. */ 1059 | YY_INITIAL_VALUE (static YYSTYPE yyval_default;) 1060 | YYSTYPE yylval YY_INITIAL_VALUE (= yyval_default); 1061 | 1062 | /* Number of syntax errors so far. */ 1063 | int yynerrs; 1064 | 1065 | int yystate; 1066 | /* Number of tokens to shift before error messages enabled. */ 1067 | int yyerrstatus; 1068 | 1069 | /* The stacks and their tools: 1070 | 'yyss': related to states. 1071 | 'yyvs': related to semantic values. 1072 | 1073 | Refer to the stacks through separate pointers, to allow yyoverflow 1074 | to reallocate them elsewhere. */ 1075 | 1076 | /* The state stack. */ 1077 | yytype_int16 yyssa[YYINITDEPTH]; 1078 | yytype_int16 *yyss; 1079 | yytype_int16 *yyssp; 1080 | 1081 | /* The semantic value stack. */ 1082 | YYSTYPE yyvsa[YYINITDEPTH]; 1083 | YYSTYPE *yyvs; 1084 | YYSTYPE *yyvsp; 1085 | 1086 | YYSIZE_T yystacksize; 1087 | 1088 | int yyn; 1089 | int yyresult; 1090 | /* Lookahead token as an internal (translated) token number. */ 1091 | int yytoken = 0; 1092 | /* The variables used to return semantic value and location from the 1093 | action routines. */ 1094 | YYSTYPE yyval; 1095 | 1096 | #if YYERROR_VERBOSE 1097 | /* Buffer for error messages, and its allocated size. */ 1098 | char yymsgbuf[128]; 1099 | char *yymsg = yymsgbuf; 1100 | YYSIZE_T yymsg_alloc = sizeof yymsgbuf; 1101 | #endif 1102 | 1103 | #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) 1104 | 1105 | /* The number of symbols on the RHS of the reduced rule. 1106 | Keep to zero when no symbol should be popped. */ 1107 | int yylen = 0; 1108 | 1109 | yyssp = yyss = yyssa; 1110 | yyvsp = yyvs = yyvsa; 1111 | yystacksize = YYINITDEPTH; 1112 | 1113 | YYDPRINTF ((stderr, "Starting parse\n")); 1114 | 1115 | yystate = 0; 1116 | yyerrstatus = 0; 1117 | yynerrs = 0; 1118 | yychar = YYEMPTY; /* Cause a token to be read. */ 1119 | goto yysetstate; 1120 | 1121 | /*------------------------------------------------------------. 1122 | | yynewstate -- Push a new state, which is found in yystate. | 1123 | `------------------------------------------------------------*/ 1124 | yynewstate: 1125 | /* In all cases, when you get here, the value and location stacks 1126 | have just been pushed. So pushing a state here evens the stacks. */ 1127 | yyssp++; 1128 | 1129 | yysetstate: 1130 | *yyssp = yystate; 1131 | 1132 | if (yyss + yystacksize - 1 <= yyssp) 1133 | { 1134 | /* Get the current used size of the three stacks, in elements. */ 1135 | YYSIZE_T yysize = yyssp - yyss + 1; 1136 | 1137 | #ifdef yyoverflow 1138 | { 1139 | /* Give user a chance to reallocate the stack. Use copies of 1140 | these so that the &'s don't force the real ones into 1141 | memory. */ 1142 | YYSTYPE *yyvs1 = yyvs; 1143 | yytype_int16 *yyss1 = yyss; 1144 | 1145 | /* Each stack pointer address is followed by the size of the 1146 | data in use in that stack, in bytes. This used to be a 1147 | conditional around just the two extra args, but that might 1148 | be undefined if yyoverflow is a macro. */ 1149 | yyoverflow (YY_("memory exhausted"), 1150 | &yyss1, yysize * sizeof (*yyssp), 1151 | &yyvs1, yysize * sizeof (*yyvsp), 1152 | &yystacksize); 1153 | 1154 | yyss = yyss1; 1155 | yyvs = yyvs1; 1156 | } 1157 | #else /* no yyoverflow */ 1158 | # ifndef YYSTACK_RELOCATE 1159 | goto yyexhaustedlab; 1160 | # else 1161 | /* Extend the stack our own way. */ 1162 | if (YYMAXDEPTH <= yystacksize) 1163 | goto yyexhaustedlab; 1164 | yystacksize *= 2; 1165 | if (YYMAXDEPTH < yystacksize) 1166 | yystacksize = YYMAXDEPTH; 1167 | 1168 | { 1169 | yytype_int16 *yyss1 = yyss; 1170 | union yyalloc *yyptr = 1171 | (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); 1172 | if (! yyptr) 1173 | goto yyexhaustedlab; 1174 | YYSTACK_RELOCATE (yyss_alloc, yyss); 1175 | YYSTACK_RELOCATE (yyvs_alloc, yyvs); 1176 | # undef YYSTACK_RELOCATE 1177 | if (yyss1 != yyssa) 1178 | YYSTACK_FREE (yyss1); 1179 | } 1180 | # endif 1181 | #endif /* no yyoverflow */ 1182 | 1183 | yyssp = yyss + yysize - 1; 1184 | yyvsp = yyvs + yysize - 1; 1185 | 1186 | YYDPRINTF ((stderr, "Stack size increased to %lu\n", 1187 | (unsigned long int) yystacksize)); 1188 | 1189 | if (yyss + yystacksize - 1 <= yyssp) 1190 | YYABORT; 1191 | } 1192 | 1193 | YYDPRINTF ((stderr, "Entering state %d\n", yystate)); 1194 | 1195 | if (yystate == YYFINAL) 1196 | YYACCEPT; 1197 | 1198 | goto yybackup; 1199 | 1200 | /*-----------. 1201 | | yybackup. | 1202 | `-----------*/ 1203 | yybackup: 1204 | 1205 | /* Do appropriate processing given the current state. Read a 1206 | lookahead token if we need one and don't already have one. */ 1207 | 1208 | /* First try to decide what to do without reference to lookahead token. */ 1209 | yyn = yypact[yystate]; 1210 | if (yypact_value_is_default (yyn)) 1211 | goto yydefault; 1212 | 1213 | /* Not known => get a lookahead token if don't already have one. */ 1214 | 1215 | /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ 1216 | if (yychar == YYEMPTY) 1217 | { 1218 | YYDPRINTF ((stderr, "Reading a token: ")); 1219 | yychar = yylex (&yylval, scanner, astpp); 1220 | } 1221 | 1222 | if (yychar <= YYEOF) 1223 | { 1224 | yychar = yytoken = YYEOF; 1225 | YYDPRINTF ((stderr, "Now at end of input.\n")); 1226 | } 1227 | else 1228 | { 1229 | yytoken = YYTRANSLATE (yychar); 1230 | YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); 1231 | } 1232 | 1233 | /* If the proper action on seeing token YYTOKEN is to reduce or to 1234 | detect an error, take that action. */ 1235 | yyn += yytoken; 1236 | if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) 1237 | goto yydefault; 1238 | yyn = yytable[yyn]; 1239 | if (yyn <= 0) 1240 | { 1241 | if (yytable_value_is_error (yyn)) 1242 | goto yyerrlab; 1243 | yyn = -yyn; 1244 | goto yyreduce; 1245 | } 1246 | 1247 | /* Count tokens shifted since error; after three, turn off error 1248 | status. */ 1249 | if (yyerrstatus) 1250 | yyerrstatus--; 1251 | 1252 | /* Shift the lookahead token. */ 1253 | YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); 1254 | 1255 | /* Discard the shifted token. */ 1256 | yychar = YYEMPTY; 1257 | 1258 | yystate = yyn; 1259 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1260 | *++yyvsp = yylval; 1261 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1262 | 1263 | goto yynewstate; 1264 | 1265 | 1266 | /*-----------------------------------------------------------. 1267 | | yydefault -- do the default action for the current state. | 1268 | `-----------------------------------------------------------*/ 1269 | yydefault: 1270 | yyn = yydefact[yystate]; 1271 | if (yyn == 0) 1272 | goto yyerrlab; 1273 | goto yyreduce; 1274 | 1275 | 1276 | /*-----------------------------. 1277 | | yyreduce -- Do a reduction. | 1278 | `-----------------------------*/ 1279 | yyreduce: 1280 | /* yyn is the number of a rule to reduce with. */ 1281 | yylen = yyr2[yyn]; 1282 | 1283 | /* If YYLEN is nonzero, implement the default value of the action: 1284 | '$$ = $1'. 1285 | 1286 | Otherwise, the following line sets YYVAL to garbage. 1287 | This behavior is undocumented and Bison 1288 | users should not rely upon it. Assigning to YYVAL 1289 | unconditionally makes the parser a bit smaller, and it avoids a 1290 | GCC warning that YYVAL may be used uninitialized. */ 1291 | yyval = yyvsp[1-yylen]; 1292 | 1293 | 1294 | YY_REDUCE_PRINT (yyn); 1295 | switch (yyn) 1296 | { 1297 | case 3: 1298 | #line 71 "COSLayoutParser.y" /* yacc.c:1661 */ 1299 | { coslayout_destroy_ast(*astpp); *astpp = NULL; YYABORT; } 1300 | #line 1301 "COSLayoutParser.c" /* yacc.c:1661 */ 1301 | break; 1302 | 1303 | case 4: 1304 | #line 72 "COSLayoutParser.y" /* yacc.c:1661 */ 1305 | { *astpp = (yyval) = coslayout_create_ast((yyvsp[-1])->node_type, (yyvsp[-2]), (yyvsp[0])); } 1306 | #line 1307 "COSLayoutParser.c" /* yacc.c:1661 */ 1307 | break; 1308 | 1309 | case 5: 1310 | #line 73 "COSLayoutParser.y" /* yacc.c:1661 */ 1311 | { *astpp = (yyval) = (yyvsp[0]); } 1312 | #line 1313 "COSLayoutParser.c" /* yacc.c:1661 */ 1313 | break; 1314 | 1315 | case 6: 1316 | #line 77 "COSLayoutParser.y" /* yacc.c:1661 */ 1317 | { *astpp = (yyval) = coslayout_create_ast('=', NULL, NULL); } 1318 | #line 1319 "COSLayoutParser.c" /* yacc.c:1661 */ 1319 | break; 1320 | 1321 | case 7: 1322 | #line 78 "COSLayoutParser.y" /* yacc.c:1661 */ 1323 | { *astpp = (yyval) = coslayout_create_ast(COSLAYOUT_TOKEN_ADD_ASSIGN, NULL, NULL); } 1324 | #line 1325 "COSLayoutParser.c" /* yacc.c:1661 */ 1325 | break; 1326 | 1327 | case 8: 1328 | #line 79 "COSLayoutParser.y" /* yacc.c:1661 */ 1329 | { *astpp = (yyval) = coslayout_create_ast(COSLAYOUT_TOKEN_SUB_ASSIGN, NULL, NULL); } 1330 | #line 1331 "COSLayoutParser.c" /* yacc.c:1661 */ 1331 | break; 1332 | 1333 | case 9: 1334 | #line 80 "COSLayoutParser.y" /* yacc.c:1661 */ 1335 | { *astpp = (yyval) = coslayout_create_ast(COSLAYOUT_TOKEN_MUL_ASSIGN, NULL, NULL); } 1336 | #line 1337 "COSLayoutParser.c" /* yacc.c:1661 */ 1337 | break; 1338 | 1339 | case 10: 1340 | #line 81 "COSLayoutParser.y" /* yacc.c:1661 */ 1341 | { *astpp = (yyval) = coslayout_create_ast(COSLAYOUT_TOKEN_DIV_ASSIGN, NULL, NULL); } 1342 | #line 1343 "COSLayoutParser.c" /* yacc.c:1661 */ 1343 | break; 1344 | 1345 | case 11: 1346 | #line 85 "COSLayoutParser.y" /* yacc.c:1661 */ 1347 | { *astpp = (yyval) = coslayout_create_ast('+', (yyvsp[-2]), (yyvsp[0])); } 1348 | #line 1349 "COSLayoutParser.c" /* yacc.c:1661 */ 1349 | break; 1350 | 1351 | case 12: 1352 | #line 86 "COSLayoutParser.y" /* yacc.c:1661 */ 1353 | { *astpp = (yyval) = coslayout_create_ast('-', (yyvsp[-2]), (yyvsp[0])); } 1354 | #line 1355 "COSLayoutParser.c" /* yacc.c:1661 */ 1355 | break; 1356 | 1357 | case 13: 1358 | #line 87 "COSLayoutParser.y" /* yacc.c:1661 */ 1359 | { *astpp = (yyval) = (yyvsp[0]); } 1360 | #line 1361 "COSLayoutParser.c" /* yacc.c:1661 */ 1361 | break; 1362 | 1363 | case 14: 1364 | #line 91 "COSLayoutParser.y" /* yacc.c:1661 */ 1365 | { *astpp = (yyval) = coslayout_create_ast('*', (yyvsp[-2]), (yyvsp[0])); } 1366 | #line 1367 "COSLayoutParser.c" /* yacc.c:1661 */ 1367 | break; 1368 | 1369 | case 15: 1370 | #line 92 "COSLayoutParser.y" /* yacc.c:1661 */ 1371 | { *astpp = (yyval) = coslayout_create_ast('/', (yyvsp[-2]), (yyvsp[0])); } 1372 | #line 1373 "COSLayoutParser.c" /* yacc.c:1661 */ 1373 | break; 1374 | 1375 | case 16: 1376 | #line 93 "COSLayoutParser.y" /* yacc.c:1661 */ 1377 | { *astpp = (yyval) = (yyvsp[0]); } 1378 | #line 1379 "COSLayoutParser.c" /* yacc.c:1661 */ 1379 | break; 1380 | 1381 | case 17: 1382 | #line 97 "COSLayoutParser.y" /* yacc.c:1661 */ 1383 | { *astpp = (yyval) = (yyvsp[0]); } 1384 | #line 1385 "COSLayoutParser.c" /* yacc.c:1661 */ 1385 | break; 1386 | 1387 | case 18: 1388 | #line 98 "COSLayoutParser.y" /* yacc.c:1661 */ 1389 | { *astpp = (yyval) = (yyvsp[0]); } 1390 | #line 1391 "COSLayoutParser.c" /* yacc.c:1661 */ 1391 | break; 1392 | 1393 | case 19: 1394 | #line 99 "COSLayoutParser.y" /* yacc.c:1661 */ 1395 | { *astpp = (yyval) = (yyvsp[0]); } 1396 | #line 1397 "COSLayoutParser.c" /* yacc.c:1661 */ 1397 | break; 1398 | 1399 | case 20: 1400 | #line 100 "COSLayoutParser.y" /* yacc.c:1661 */ 1401 | { *astpp = (yyval) = (yyvsp[0]); } 1402 | #line 1403 "COSLayoutParser.c" /* yacc.c:1661 */ 1403 | break; 1404 | 1405 | case 21: 1406 | #line 101 "COSLayoutParser.y" /* yacc.c:1661 */ 1407 | { *astpp = (yyval) = (yyvsp[0]); } 1408 | #line 1409 "COSLayoutParser.c" /* yacc.c:1661 */ 1409 | break; 1410 | 1411 | case 22: 1412 | #line 102 "COSLayoutParser.y" /* yacc.c:1661 */ 1413 | { *astpp = (yyval) = (yyvsp[0]); } 1414 | #line 1415 "COSLayoutParser.c" /* yacc.c:1661 */ 1415 | break; 1416 | 1417 | case 23: 1418 | #line 103 "COSLayoutParser.y" /* yacc.c:1661 */ 1419 | { *astpp = (yyval) = (yyvsp[0]); } 1420 | #line 1421 "COSLayoutParser.c" /* yacc.c:1661 */ 1421 | break; 1422 | 1423 | case 24: 1424 | #line 104 "COSLayoutParser.y" /* yacc.c:1661 */ 1425 | { *astpp = (yyval) = (yyvsp[0]); } 1426 | #line 1427 "COSLayoutParser.c" /* yacc.c:1661 */ 1427 | break; 1428 | 1429 | case 25: 1430 | #line 105 "COSLayoutParser.y" /* yacc.c:1661 */ 1431 | { *astpp = (yyval) = (yyvsp[0]); } 1432 | #line 1433 "COSLayoutParser.c" /* yacc.c:1661 */ 1433 | break; 1434 | 1435 | case 26: 1436 | #line 106 "COSLayoutParser.y" /* yacc.c:1661 */ 1437 | { *astpp = (yyval) = (yyvsp[0]); } 1438 | #line 1439 "COSLayoutParser.c" /* yacc.c:1661 */ 1439 | break; 1440 | 1441 | case 27: 1442 | #line 107 "COSLayoutParser.y" /* yacc.c:1661 */ 1443 | { *astpp = (yyval) = (yyvsp[-1]); } 1444 | #line 1445 "COSLayoutParser.c" /* yacc.c:1661 */ 1445 | break; 1446 | 1447 | 1448 | #line 1449 "COSLayoutParser.c" /* yacc.c:1661 */ 1449 | default: break; 1450 | } 1451 | /* User semantic actions sometimes alter yychar, and that requires 1452 | that yytoken be updated with the new translation. We take the 1453 | approach of translating immediately before every use of yytoken. 1454 | One alternative is translating here after every semantic action, 1455 | but that translation would be missed if the semantic action invokes 1456 | YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or 1457 | if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an 1458 | incorrect destructor might then be invoked immediately. In the 1459 | case of YYERROR or YYBACKUP, subsequent parser actions might lead 1460 | to an incorrect destructor call or verbose syntax error message 1461 | before the lookahead is translated. */ 1462 | YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); 1463 | 1464 | YYPOPSTACK (yylen); 1465 | yylen = 0; 1466 | YY_STACK_PRINT (yyss, yyssp); 1467 | 1468 | *++yyvsp = yyval; 1469 | 1470 | /* Now 'shift' the result of the reduction. Determine what state 1471 | that goes to, based on the state we popped back to and the rule 1472 | number reduced by. */ 1473 | 1474 | yyn = yyr1[yyn]; 1475 | 1476 | yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; 1477 | if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) 1478 | yystate = yytable[yystate]; 1479 | else 1480 | yystate = yydefgoto[yyn - YYNTOKENS]; 1481 | 1482 | goto yynewstate; 1483 | 1484 | 1485 | /*--------------------------------------. 1486 | | yyerrlab -- here on detecting error. | 1487 | `--------------------------------------*/ 1488 | yyerrlab: 1489 | /* Make sure we have latest lookahead translation. See comments at 1490 | user semantic actions for why this is necessary. */ 1491 | yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); 1492 | 1493 | /* If not already recovering from an error, report this error. */ 1494 | if (!yyerrstatus) 1495 | { 1496 | ++yynerrs; 1497 | #if ! YYERROR_VERBOSE 1498 | yyerror (scanner, astpp, YY_("syntax error")); 1499 | #else 1500 | # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ 1501 | yyssp, yytoken) 1502 | { 1503 | char const *yymsgp = YY_("syntax error"); 1504 | int yysyntax_error_status; 1505 | yysyntax_error_status = YYSYNTAX_ERROR; 1506 | if (yysyntax_error_status == 0) 1507 | yymsgp = yymsg; 1508 | else if (yysyntax_error_status == 1) 1509 | { 1510 | if (yymsg != yymsgbuf) 1511 | YYSTACK_FREE (yymsg); 1512 | yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc); 1513 | if (!yymsg) 1514 | { 1515 | yymsg = yymsgbuf; 1516 | yymsg_alloc = sizeof yymsgbuf; 1517 | yysyntax_error_status = 2; 1518 | } 1519 | else 1520 | { 1521 | yysyntax_error_status = YYSYNTAX_ERROR; 1522 | yymsgp = yymsg; 1523 | } 1524 | } 1525 | yyerror (scanner, astpp, yymsgp); 1526 | if (yysyntax_error_status == 2) 1527 | goto yyexhaustedlab; 1528 | } 1529 | # undef YYSYNTAX_ERROR 1530 | #endif 1531 | } 1532 | 1533 | 1534 | 1535 | if (yyerrstatus == 3) 1536 | { 1537 | /* If just tried and failed to reuse lookahead token after an 1538 | error, discard it. */ 1539 | 1540 | if (yychar <= YYEOF) 1541 | { 1542 | /* Return failure if at end of input. */ 1543 | if (yychar == YYEOF) 1544 | YYABORT; 1545 | } 1546 | else 1547 | { 1548 | yydestruct ("Error: discarding", 1549 | yytoken, &yylval, scanner, astpp); 1550 | yychar = YYEMPTY; 1551 | } 1552 | } 1553 | 1554 | /* Else will try to reuse lookahead token after shifting the error 1555 | token. */ 1556 | goto yyerrlab1; 1557 | 1558 | 1559 | /*---------------------------------------------------. 1560 | | yyerrorlab -- error raised explicitly by YYERROR. | 1561 | `---------------------------------------------------*/ 1562 | yyerrorlab: 1563 | 1564 | /* Do not reclaim the symbols of the rule whose action triggered 1565 | this YYERROR. */ 1566 | YYPOPSTACK (yylen); 1567 | yylen = 0; 1568 | YY_STACK_PRINT (yyss, yyssp); 1569 | yystate = *yyssp; 1570 | goto yyerrlab1; 1571 | 1572 | 1573 | /*-------------------------------------------------------------. 1574 | | yyerrlab1 -- common code for both syntax error and YYERROR. | 1575 | `-------------------------------------------------------------*/ 1576 | yyerrlab1: 1577 | yyerrstatus = 3; /* Each real token shifted decrements this. */ 1578 | 1579 | for (;;) 1580 | { 1581 | yyn = yypact[yystate]; 1582 | if (!yypact_value_is_default (yyn)) 1583 | { 1584 | yyn += YYTERROR; 1585 | if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) 1586 | { 1587 | yyn = yytable[yyn]; 1588 | if (0 < yyn) 1589 | break; 1590 | } 1591 | } 1592 | 1593 | /* Pop the current state because it cannot handle the error token. */ 1594 | if (yyssp == yyss) 1595 | YYABORT; 1596 | 1597 | 1598 | yydestruct ("Error: popping", 1599 | yystos[yystate], yyvsp, scanner, astpp); 1600 | YYPOPSTACK (1); 1601 | yystate = *yyssp; 1602 | YY_STACK_PRINT (yyss, yyssp); 1603 | } 1604 | 1605 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1606 | *++yyvsp = yylval; 1607 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1608 | 1609 | 1610 | /* Shift the error token. */ 1611 | YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp); 1612 | 1613 | yystate = yyn; 1614 | goto yynewstate; 1615 | 1616 | 1617 | /*-------------------------------------. 1618 | | yyacceptlab -- YYACCEPT comes here. | 1619 | `-------------------------------------*/ 1620 | yyacceptlab: 1621 | yyresult = 0; 1622 | goto yyreturn; 1623 | 1624 | /*-----------------------------------. 1625 | | yyabortlab -- YYABORT comes here. | 1626 | `-----------------------------------*/ 1627 | yyabortlab: 1628 | yyresult = 1; 1629 | goto yyreturn; 1630 | 1631 | #if !defined yyoverflow || YYERROR_VERBOSE 1632 | /*-------------------------------------------------. 1633 | | yyexhaustedlab -- memory exhaustion comes here. | 1634 | `-------------------------------------------------*/ 1635 | yyexhaustedlab: 1636 | yyerror (scanner, astpp, YY_("memory exhausted")); 1637 | yyresult = 2; 1638 | /* Fall through. */ 1639 | #endif 1640 | 1641 | yyreturn: 1642 | if (yychar != YYEMPTY) 1643 | { 1644 | /* Make sure we have latest lookahead translation. See comments at 1645 | user semantic actions for why this is necessary. */ 1646 | yytoken = YYTRANSLATE (yychar); 1647 | yydestruct ("Cleanup: discarding lookahead", 1648 | yytoken, &yylval, scanner, astpp); 1649 | } 1650 | /* Do not reclaim the symbols of the rule whose action triggered 1651 | this YYABORT or YYACCEPT. */ 1652 | YYPOPSTACK (yylen); 1653 | YY_STACK_PRINT (yyss, yyssp); 1654 | while (yyssp != yyss) 1655 | { 1656 | yydestruct ("Cleanup: popping", 1657 | yystos[*yyssp], yyvsp, scanner, astpp); 1658 | YYPOPSTACK (1); 1659 | } 1660 | #ifndef yyoverflow 1661 | if (yyss != yyssa) 1662 | YYSTACK_FREE (yyss); 1663 | #endif 1664 | #if YYERROR_VERBOSE 1665 | if (yymsg != yymsgbuf) 1666 | YYSTACK_FREE (yymsg); 1667 | #endif 1668 | return yyresult; 1669 | } 1670 | #line 110 "COSLayoutParser.y" /* yacc.c:1906 */ 1671 | 1672 | 1673 | void coslayouterror(void *scanner, COSLAYOUT_AST **astpp, char *msg) { 1674 | fprintf(stderr, "COSLayout: %s\n", msg); 1675 | } 1676 | 1677 | int coslayoutparse (void *scanner, COSLAYOUT_AST **astpp); 1678 | int coslayoutlex_init (yyscan_t* scanner); 1679 | int coslayoutlex_destroy (yyscan_t yyscanner); 1680 | 1681 | COSLAYOUT_AST *coslayout_create_ast(int type, COSLAYOUT_AST *l, COSLAYOUT_AST *r) { 1682 | COSLAYOUT_AST *astp = (COSLAYOUT_AST *)malloc(sizeof(COSLAYOUT_AST)); 1683 | 1684 | astp->node_type = type; 1685 | astp->l = l; 1686 | astp->r = r; 1687 | astp->value.coord = NULL; 1688 | astp->data = NULL; 1689 | 1690 | return astp; 1691 | } 1692 | 1693 | int coslayout_parse_rule(char *rule, COSLAYOUT_AST **astpp) { 1694 | yyscan_t scanner; 1695 | coslayoutlex_init(&scanner); 1696 | YY_BUFFER_STATE state = coslayout_scan_string(rule, scanner); 1697 | 1698 | int result = coslayoutparse(scanner, astpp); 1699 | 1700 | coslayout_delete_buffer(state, scanner); 1701 | coslayoutlex_destroy(scanner); 1702 | 1703 | if (result) { 1704 | coslayout_destroy_ast(*astpp); 1705 | *astpp = NULL; 1706 | } 1707 | 1708 | return result; 1709 | } 1710 | 1711 | void coslayout_destroy_ast(COSLAYOUT_AST *astp) { 1712 | if (astp != NULL) { 1713 | coslayout_destroy_ast(astp->l); 1714 | coslayout_destroy_ast(astp->r); 1715 | 1716 | int type = astp->node_type; 1717 | char *coord = astp->value.coord; 1718 | 1719 | if ((type == COSLAYOUT_TOKEN_ATTR || type == COSLAYOUT_TOKEN_COORD) && coord != NULL) 1720 | free(coord); 1721 | 1722 | free(astp); 1723 | } 1724 | } 1725 | -------------------------------------------------------------------------------- /COSLayout/COSLayoutParser.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.0.2. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED 34 | # define YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED 35 | /* Debug traces. */ 36 | #ifndef COSLAYOUTDEBUG 37 | # if defined YYDEBUG 38 | #if YYDEBUG 39 | # define COSLAYOUTDEBUG 1 40 | # else 41 | # define COSLAYOUTDEBUG 0 42 | # endif 43 | # else /* ! defined YYDEBUG */ 44 | # define COSLAYOUTDEBUG 0 45 | # endif /* ! defined YYDEBUG */ 46 | #endif /* ! defined COSLAYOUTDEBUG */ 47 | #if COSLAYOUTDEBUG 48 | extern int coslayoutdebug; 49 | #endif 50 | /* "%code requires" blocks. */ 51 | #line 18 "COSLayoutParser.y" /* yacc.c:1915 */ 52 | 53 | #define YYSTYPE COSLAYOUTSTYPE 54 | 55 | #define YY_DECL int coslayoutlex \ 56 | (YYSTYPE *yylval_param, yyscan_t yyscanner, COSLAYOUT_AST **astpp) 57 | 58 | struct COSLAYOUT_AST { 59 | int node_type; 60 | struct COSLAYOUT_AST *l; 61 | struct COSLAYOUT_AST *r; 62 | union { 63 | float number; 64 | float percentage; 65 | char *coord; 66 | } value; 67 | void *data; 68 | }; 69 | 70 | typedef struct COSLAYOUT_AST COSLAYOUT_AST; 71 | 72 | COSLAYOUT_AST *coslayout_create_ast(int type, COSLAYOUT_AST *l, COSLAYOUT_AST *r); 73 | 74 | int coslayout_parse_rule(char *rule, COSLAYOUT_AST **astpp); 75 | void coslayout_destroy_ast(COSLAYOUT_AST *astp); 76 | 77 | #line 78 "COSLayoutParser.h" /* yacc.c:1915 */ 78 | 79 | /* Token type. */ 80 | #ifndef COSLAYOUTTOKENTYPE 81 | # define COSLAYOUTTOKENTYPE 82 | enum coslayouttokentype 83 | { 84 | COSLAYOUT_TOKEN_ATTR = 258, 85 | COSLAYOUT_TOKEN_NUMBER = 259, 86 | COSLAYOUT_TOKEN_PERCENTAGE = 260, 87 | COSLAYOUT_TOKEN_PERCENTAGE_H = 261, 88 | COSLAYOUT_TOKEN_PERCENTAGE_V = 262, 89 | COSLAYOUT_TOKEN_COORD = 263, 90 | COSLAYOUT_TOKEN_COORD_PERCENTAGE = 264, 91 | COSLAYOUT_TOKEN_COORD_PERCENTAGE_H = 265, 92 | COSLAYOUT_TOKEN_COORD_PERCENTAGE_V = 266, 93 | COSLAYOUT_TOKEN_NIL = 267, 94 | COSLAYOUT_TOKEN_ADD_ASSIGN = 268, 95 | COSLAYOUT_TOKEN_SUB_ASSIGN = 269, 96 | COSLAYOUT_TOKEN_MUL_ASSIGN = 270, 97 | COSLAYOUT_TOKEN_DIV_ASSIGN = 271 98 | }; 99 | #endif 100 | 101 | /* Value type. */ 102 | #if ! defined COSLAYOUTSTYPE && ! defined COSLAYOUTSTYPE_IS_DECLARED 103 | typedef COSLAYOUT_AST * COSLAYOUTSTYPE; 104 | # define COSLAYOUTSTYPE_IS_TRIVIAL 1 105 | # define COSLAYOUTSTYPE_IS_DECLARED 1 106 | #endif 107 | 108 | 109 | 110 | int coslayoutparse (void *scanner, COSLAYOUT_AST **astpp); 111 | 112 | #endif /* !YY_COSLAYOUT_COSLAYOUTPARSER_H_INCLUDED */ 113 | -------------------------------------------------------------------------------- /COSObserver/COSObserver.h: -------------------------------------------------------------------------------- 1 | // COSObserver.h 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import 27 | 28 | typedef void(^COSObserverBlock)(id object, id target, NSDictionary *change); 29 | 30 | 31 | @interface COSObserver : NSObject 32 | 33 | + (instancetype)observerForObject:(NSObject *)object; 34 | 35 | @property (atomic, weak, readonly) NSObject *object; 36 | 37 | - (void)addTarget:(NSObject *)target 38 | forKeyPath:(NSString *)keyPath 39 | options:(NSKeyValueObservingOptions)options 40 | block:(COSObserverBlock)block; 41 | 42 | - (void)removeTarget:(NSObject *)target forKeyPath:(NSString *)keyPath; 43 | - (void)removeTarget:(NSObject *)target; 44 | 45 | @end 46 | 47 | 48 | NS_INLINE 49 | COSObserver *COSObserverMake(NSObject *object) { 50 | return [COSObserver observerForObject:object]; 51 | } 52 | -------------------------------------------------------------------------------- /COSObserver/COSObserver.m: -------------------------------------------------------------------------------- 1 | // COSObserver.m 2 | // 3 | // Copyright (c) 2014 Tianyong Tang 4 | // 5 | // Permission is hereby granted, free of charge, to any person 6 | // obtaining a copy of this software and associated documentation 7 | // files (the "Software"), to deal in the Software without 8 | // restriction, including without limitation the rights to use, 9 | // copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the 11 | // Software is furnished to do so, subject to the following 12 | // conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be 15 | // included in all copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 18 | // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 20 | // AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | // OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | #import "COSObserver.h" 27 | #import 28 | 29 | 30 | @interface COSObserver () 31 | 32 | @property (atomic, weak) NSObject *object; 33 | 34 | - (instancetype)initWithObject:(NSObject *)object; 35 | 36 | @end 37 | 38 | 39 | @interface COSObservation : NSObject 40 | 41 | @property (atomic, unsafe_unretained) id object; 42 | @property (atomic, unsafe_unretained) id target; 43 | @property (atomic, copy) NSString *keyPath; 44 | @property (atomic, assign) NSKeyValueObservingOptions options; 45 | @property (atomic, copy) COSObserverBlock block; 46 | 47 | - (void)deregister; 48 | 49 | @end 50 | 51 | 52 | static SEL deallocSel = NULL; 53 | static NSMutableSet *swizzledClasses = nil; 54 | static void *cos_context = &cos_context; 55 | 56 | NS_INLINE 57 | NSMutableSet *cos_observation_pool(NSObject *object) { 58 | static const void *poolKey = &poolKey; 59 | 60 | NSMutableSet *observationPool = objc_getAssociatedObject(object, poolKey); 61 | 62 | if (!observationPool) { 63 | observationPool = [[NSMutableSet alloc] init]; 64 | 65 | objc_setAssociatedObject(object, poolKey, observationPool, OBJC_ASSOCIATION_RETAIN); 66 | } 67 | 68 | return observationPool; 69 | } 70 | 71 | NS_INLINE 72 | void cos_hook_object_if_needed(NSObject *object) { 73 | @synchronized (swizzledClasses) { 74 | Class class = [object class]; 75 | 76 | if ([swizzledClasses containsObject:class]) return; 77 | 78 | IMP oldDeallocImp = class_getMethodImplementation(class, deallocSel); 79 | IMP newDeallocImp = imp_implementationWithBlock(^(void *object) { 80 | for (COSObservation *observation in [cos_observation_pool((__bridge id)object) copy]) { 81 | [observation deregister]; 82 | } 83 | 84 | ((void(*)(void *, SEL))oldDeallocImp)(object, deallocSel); 85 | }); 86 | 87 | class_replaceMethod(class, deallocSel, newDeallocImp, "v@:"); 88 | 89 | [swizzledClasses addObject:class]; 90 | } 91 | } 92 | 93 | 94 | @implementation COSObserver 95 | 96 | + (void)initialize { 97 | deallocSel = sel_registerName("dealloc"); 98 | swizzledClasses = [[NSMutableSet alloc] init]; 99 | } 100 | 101 | + (instancetype)observerForObject:(NSObject *)object { 102 | COSObserver *observer = nil; 103 | 104 | if (object) { 105 | static const void *observerKey = &observerKey; 106 | 107 | observer = objc_getAssociatedObject(object, observerKey); 108 | 109 | if (!observer) { 110 | observer = [[COSObserver alloc] initWithObject:object]; 111 | 112 | objc_setAssociatedObject(object, observerKey, observer, OBJC_ASSOCIATION_RETAIN); 113 | } 114 | } 115 | 116 | return observer; 117 | } 118 | 119 | - (instancetype)initWithObject:(NSObject *)object { 120 | self = [super init]; 121 | 122 | if (self) _object = object; 123 | 124 | return self; 125 | } 126 | 127 | - (void)addTarget:(NSObject *)target 128 | forKeyPath:(NSString *)keyPath 129 | options:(NSKeyValueObservingOptions)options 130 | block:(COSObserverBlock)block { 131 | if (target) { 132 | NSObject *object = self.object; 133 | 134 | cos_hook_object_if_needed(object); 135 | cos_hook_object_if_needed(target); 136 | 137 | COSObservation *observation = [[COSObservation alloc] init]; 138 | 139 | observation.object = object; 140 | observation.target = target; 141 | observation.keyPath = keyPath; 142 | observation.options = options; 143 | observation.block = block; 144 | 145 | [cos_observation_pool(object) addObject:observation]; 146 | [cos_observation_pool(target) addObject:observation]; 147 | 148 | if ([target isKindOfClass:[NSArray class]]) { 149 | NSArray *array = (NSArray *)target; 150 | 151 | [array addObserver:observation 152 | toObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [array count])] 153 | forKeyPath:keyPath 154 | options:options 155 | context:cos_context]; 156 | } else { 157 | [target addObserver:observation 158 | forKeyPath:keyPath 159 | options:options 160 | context:cos_context]; 161 | } 162 | } 163 | } 164 | 165 | - (void)removeTarget:(NSObject *)target forKeyPath:(NSString *)keyPath { 166 | if (target && keyPath) { 167 | NSMutableSet *find = [[NSMutableSet alloc] init]; 168 | 169 | for (COSObservation *observation in cos_observation_pool(self.object)) { 170 | if (observation.target == target && [observation.keyPath isEqualToString:keyPath]) { 171 | [find addObject:observation]; 172 | } 173 | } 174 | 175 | for (COSObservation *observation in find) { 176 | [observation deregister]; 177 | } 178 | } else if (target) { 179 | [self removeTarget:target]; 180 | } else { 181 | [self removeTarget:nil]; 182 | } 183 | } 184 | 185 | - (void)removeTarget:(NSObject *)target { 186 | NSMutableSet *find = nil; 187 | NSMutableSet *observationPool = cos_observation_pool(self.object); 188 | 189 | if (target) { 190 | find = [[NSMutableSet alloc] init]; 191 | 192 | for (COSObservation *observation in observationPool) { 193 | if (observation.target == target) { 194 | [find addObject:observation]; 195 | } 196 | } 197 | } else { 198 | find = [observationPool copy]; 199 | } 200 | 201 | for (COSObservation *observation in find) { 202 | [observation deregister]; 203 | } 204 | } 205 | 206 | @end 207 | 208 | 209 | @implementation COSObservation 210 | 211 | - (void)observeValueForKeyPath:(NSString *)keyPath 212 | ofObject:(id)object 213 | change:(NSDictionary *)change 214 | context:(void *)context { 215 | if (context == cos_context) { 216 | if (self.block) { 217 | self.block(self.object, self.target, change); 218 | } 219 | } else { 220 | [super observeValueForKeyPath:keyPath 221 | ofObject:object 222 | change:change 223 | context:context]; 224 | } 225 | } 226 | 227 | - (void)deregister { 228 | if ([_target isKindOfClass:[NSArray class]]) { 229 | NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [_target count])]; 230 | 231 | [_target removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:_keyPath context:cos_context]; 232 | } else { 233 | [_target removeObserver:self forKeyPath:_keyPath context:cos_context]; 234 | } 235 | 236 | [cos_observation_pool(_target) removeObject:self], _target = nil; 237 | [cos_observation_pool(_object) removeObject:self]; 238 | } 239 | 240 | @end 241 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Tianyong Tang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | CocoaSugar is an Objective-C library that can make developing apps easier. It includes a collection of runtime and Cocoa Touch improvements to solve some practical problem. 5 | 6 | Documentation 7 | ============= 8 | 9 | ## `COSLayout` 10 | 11 | `COSLayout` is yet another layout library. It's neither a wrapper nor a replacement for Auto Layout. It dose not handle circular references of constraints and constraint priority. Besides that, `COSLayout` can solve all layout cases. What's more, `COSLayout` provides some additional benefits: smaller memory footprint, better performance and more intuitive expression. 12 | 13 | `COSLayout` is an abstraction of layout of view. With `COSLayout`, you can specify view's layout relative to it's superview, sibling views and non-sibling views. Following example specifies a 10-points constraint from view's bottom to superview's bottom: 14 | 15 | ```objc 16 | UIView *view = [[UIView alloc] init]; 17 | 18 | COSLayout *layout = [COSLayout layoutOfView:view]; 19 | 20 | [layout addRule:@"bb = 10"]; 21 | ``` 22 | 23 | In the example above, a rule has been added into layout by method `addRule:`. A rule is expressed in Sample Layout Language or "SLL", which can specify constraints intuitively. The syntax of SLL is very simple, just comma-separated assignment expressions. Each assignment expression specifies a constraint, l-value is constraint name, r-value is constraint value. 24 | 25 | `COSLayout` supports 18 constraints: 26 | 27 | Constraint | Direction | Description 28 | -----------|------------|------------ 29 | `w` | Horizontal | View's width 30 | `h` | Vertical | View's height 31 | `tt` | Vertical | Space from view's top to superview's top 32 | `tb` | Vertical | Space from view's top to superview's bottom 33 | `ll` | Horizontal | Space from view's left to superview's left 34 | `lr` | Horizontal | Space from view's left to superview's right 35 | `bb` | Vertical | Space from view's bottom to superview's bottom 36 | `bt` | Vertical | Space from view's bottom to superview's top 37 | `rr` | Horizontal | Space from view's right to superview's right 38 | `rl` | Horizontal | Space from view's right to superview's left 39 | `ct` | Vertical | Space from view's center to superview's top 40 | `cl` | Horizontal | Space from view's center to superview's left 41 | `cb` | Vertical | Space from view's center to superview's bottom 42 | `cr` | Horizontal | Space from view's center to superview's right 43 | `minw` | Horizontal | Minimal width of view 44 | `maxw` | Horizontal | Maximal width of view 45 | `minh` | Vertical | Minimal height of view 46 | `maxh` | Vertical | Maximal height of view 47 | 48 | `COSLayout` supports 5 constraint value types: 49 | 50 | Constraint Value Type | Example | Description 51 | ----------------------|---------------------|------------ 52 | CGFloat | `5` `-10` `20.0` | Fixed length on screen 53 | Percentage | `5%` `-10%` `20.0%` | Percentage of superview's width or height 54 | Format specifier | `%tt` `%w` `%f` | Constraint value given by additional argument 55 | Constraint | `tt` `maxw` | Constraint value of current layout 56 | Nil | `nil` | Used to reset a constraint 57 | 58 | Note that the percentage has different means for different constraint directions. If current constraint direction is horizontal, the percentage represents the percentage of superview's width, otherwise, the percentage of superview's height. You can also specify direction of percentage by `H:` (horizontal) or `V:` (vertical) prefix. For example, `H:50%` means 50% width of superview, `V:30%` means 30% height of superview. 59 | 60 | Format specifier represents a constraint value given by additional argument. For example, `%tt` is the space from other view's top to superview's top. Here, the other view is given by additional argument, and the superview is the superview of layout's view. It means that `COSLayout` can specify constraints between non-sibling views. 61 | 62 | `COSLayout` support 20 format specifiers: 63 | 64 | Format | Type | Description 65 | -------|--------------------------|------------ 66 | `%tt` | `UIView *` | Space from view's top to superview's top 67 | `%tb` | `UIView *` | Space from view's top to superview's bottom 68 | `%ll` | `UIView *` | Space from view's left to superview's left 69 | `%lr` | `UIView *` | Space from view's left to superview's right 70 | `%bb` | `UIView *` | Space from view's bottom to superview's bottom 71 | `%bt` | `UIView *` | Space from view's bottom to superview's top 72 | `%rr` | `UIView *` | Space from view's right to superview's right 73 | `%rl` | `UIView *` | Space from view's right to superview's left 74 | `%ct` | `UIView *` | Space from view's center to superview's top 75 | `%cl` | `UIView *` | Space from view's center to superview's left 76 | `%cb` | `UIView *` | Space from view's center to superview's bottom 77 | `%cr` | `UIView *` | Space from view's center to superview's right 78 | `%w` | `UIView *` | Width of view 79 | `%h` | `UIView *` | Height of view 80 | `%f` | `CGFloat` | Fixed length on screen 81 | `%p` | `CGFloat` | Percentage of superview's width or superview's height 82 | `%^f` | `CGFloat(^)(UIView *)` | Space provided by a block 83 | `%^p` | `CGFloat(^)(UIView *)` | Percentage provided by a block 84 | `%@f` | `id` | Space provided by an object 85 | `%@p` | `id` | Percentage provided by an object 86 | 87 | Like percentage constraint value, you can also use `H:` and `V:` prefix to specify direction of percentage. For example, `H:%p`, `H:%^p`, `H:%@p` means percentage is horizontal, `V:%p`, `V:%^p`, `V:%@p` means percentage is vertical. 88 | 89 | It is worth mentioning that, format specifier also create a dependency between two views: the layout view and the other view given by additional argument. In `COSLayout`, the dependencies is presented by DAG. So `COSLayout` do not support the circular dependencies. When superview needs layout, all layouts of subviews will solve it's constraints according to the dependencies. 90 | 91 | ### Constraint value expression 92 | 93 | You can apply arithmetic operator between constraint values. Like other languages, SLL supports 5 basic arithmetic operators: 94 | 95 | Operator name | Priority | Associativity | Code examples 96 | --------------|----------|---------------|-------------- 97 | `=` | 1 | right | `tt = 20` `ct = 50%` 98 | `+=` | 1 | right | `tt += 20` `ct += 50%` 99 | `-=` | 1 | right | `tt -= 20` `ct -= 50%` 100 | `*=` | 1 | right | `tt *= 20` `ct *= 50%` 101 | `/=` | 1 | right | `tt /= 20` `ct /= 50%` 102 | `+` | 2 | left | `10 + 20` `50% + 10` `%w + 5` 103 | `-` | 2 | left | `20 - 10` `50% - 10` `%h - 5` 104 | `*` | 3 | left | `50 * 2` `80% * 0.5` `%h * 2` 105 | `/` | 3 | left | `100 / 2` `100% / 2` `%h / 2` 106 | 107 | You can also use `()` to group sub-expression to change the evaluation order of expression. 108 | 109 | ### Examples 110 | 111 | In the following example, `COSLayout` aligns view's top-right corner to superview's top-right corner with 5-points space. 112 | 113 | ```objc 114 | UIView *view = [[UIView alloc] init]; 115 | 116 | COSLayout *layout = [COSLayout layoutOfView:view]; 117 | 118 | [layout addRule:@"tt = rr = 5"]; 119 | ``` 120 | 121 | In the following example, `COSLayout` aligns view's left/bottom/right to superview's left/bottom/right with 10-points space, and make the view's top aligned to superview's center. 122 | 123 | ```objc 124 | UIView *view = [[UIView alloc] init]; 125 | 126 | COSLayout *layout = [COSLayout layoutOfView:view]; 127 | 128 | [layout addRule:@"ll = bb = rr = 10, tt = 50%"]; 129 | ``` 130 | 131 | ## `COSObserver` 132 | 133 | `COSObserver` is an improvement of KVO. Using `COSObserver`, you can use block for KVO notification. It eliminates some inconvenience of KVO. After making an observation by `COSObserver`, there's no need to remove observer manually, `COSObserver` can remove observer for target automatically when either observer or target is dealloced. 134 | 135 | In the following example, `observer` is the observer for `label1`. Then, `label2` is added to `observer` as target. When the text of `label2` changed, `observer` observes this change immediately and call the block, which makes text of `label1` the same as text of `label2`. 136 | 137 | ```objc 138 | UILabel *label1 = [[UILabel alloc] init]; 139 | UILabel *label2 = [[UILabel alloc] init]; 140 | 141 | COSObserver *observer = [COSObserver observerForObject:label1]; 142 | 143 | [observer 144 | addTarget:label2 145 | forKeyPath:@"text" 146 | options:NSKeyValueObservingOptionNew 147 | block:^(id object, id target, NSDictionary *change) { 148 | [target setText:change[NSKeyValueChangeNewKey]]; 149 | }]; 150 | ``` 151 | --------------------------------------------------------------------------------