Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 221 lines (149 sloc) 5.52 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
//
// JSTScanner.m
// jstalk
//
// Created by August Mueller on 1/16/09.
// Copyright 2009 Flying Meat Inc. All rights reserved.
//

#import "JSTScanner.h"


@implementation JSTScanner
@synthesize jsString=_jsString;
@synthesize frames=_frames;

+ (JSTScanner*) scannerWithString:(NSString*)s {
    
    JSTScanner *sc = [[[JSTScanner alloc] init] autorelease];
    
    sc.jsString = s;
    
    return sc;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if (_uString) {
        free(_uString);
        _uString = nil;
    }
    
    [_jsString release];
    [super dealloc];
}

- (NSString*) nextToken {
    
    _tokenIsBreakChar = NO;
    _currentRange.length = 0;
    
    while (_currentRange.location + _currentRange.length < _stringLength) {
        
        //printf("%c", _uString[_currentRange.location + _currentRange.length]);
        
        if (isBreakChar(_uString[_currentRange.location + _currentRange.length])) {
            
            if (_currentRange.length == 0) {
                _tokenIsBreakChar = YES;
                NSString *ret = [NSString stringWithCharacters:&_uString[_currentRange.location] length:1];
                _breakChar = _uString[_currentRange.location];
                _currentRange.location++;
                return ret;
            }
            else {
                NSString *ret = [NSString stringWithCharacters:&_uString[_currentRange.location] length:_currentRange.length];
                _currentRange.location += _currentRange.length;
                return ret;
            }
        }
        
        _currentRange.length++;
        
        if (_currentRange.location + _currentRange.length == _stringLength) {
            // crap, we're at the end.
            NSString *ret = [NSString stringWithCharacters:&_uString[_currentRange.location] length:_currentRange.length];
            _currentRange.location += _currentRange.length;
            return ret;
        }
    }
    
    return nil;
}


- (BOOL) hasNextToken {
    return (_currentRange.location < _stringLength);
}

- (unichar) breakChar {
    if (_tokenIsBreakChar) {
        return _breakChar;
    }
    
    return 0;
}

- (unichar) peekChar {
    return _uString[_currentRange.location+1];
}

- (NSString*) restOfLine {
    NSMutableString *ret = [NSMutableString string];
    
    while ([self hasNextToken]) {
        
        NSString *tok = [self nextToken];
        
        if ([tok isEqualToString:@"\r"] || [tok isEqualToString:@"\n"]) {
            return ret;
        }
        
        [ret appendString:tok];
    }
    
    return ret;
}


- (NSDictionary*) readJSTalkFrame {
    
    NSString *zeLine = [[self restOfLine] substringFromIndex:2];
    
    int endLoc = [zeLine rangeOfString:@"\""].location;
    
    NSString *appName = [zeLine substringToIndex:endLoc];
    
    NSMutableString *frame = [NSMutableString string];
    
    int bracketCount = 1;
    
    while ([self hasNextToken]) {
        
        NSString *tok = [self nextToken];
        
        
        if ([tok isEqualToString:@"}"]) {
            bracketCount--;
            
            if (bracketCount <= 0) {
                break;
            }
        }
        else if ([tok isEqualToString:@"{"]) {
            bracketCount++;
        }
        
        [frame appendString:tok];
    }
    
    NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:appName, @"appName", frame, @"script", nil];
    
    return d;
}

- (BOOL) breakCharIsSpace {
    return isspace([self breakChar]);
}

- (int) location {
    return _currentRange.location;
}

- (void) setLocation:(int)newLocation {
    _currentRange.location = newLocation;
}

- (void) scan {
    
    if (!_jsString) {
        return;
    }
    
    if (_uString) {
        free(_uString);
        _uString = nil;
    }
    
    _stringLength = [_jsString length];
    _uString = malloc((sizeof(unichar) * _stringLength) + 2);
    [_jsString getCharacters:_uString];
    
    _uString[_stringLength] = ' ';
    
    
    
    self.frames = [NSMutableArray array];
    
    NSMutableString *currentLocalFrame = [NSMutableString string];
    
    while ([self hasNextToken]) {
        
        NSString *tok = [self nextToken];
        
        if (!_inComment && !_inQuote && [tok isEqualToString:@"/"] && (_breakChar == '/')) {
            //debug(@"comment? %c %d", _breakChar, [self peekChar]);
            //debug(@"restOfLine: %@", [self restOfLine]);
        }
        
        if ([tok isEqualToString:@"JSTalk"]) {
            
            // end our current frame.
            
            NSDictionary *localFrame = [NSDictionary dictionaryWithObjectsAndKeys:@".local", @"appName", currentLocalFrame, @"script", nil];
            [_frames addObject:localFrame];
            
            currentLocalFrame = [NSMutableString string];
            
            int currentLocation = [self location];
            
            NSString *rest = [self restOfLine];
            
            if ([rest hasPrefix:@"(\""]) {
                [self setLocation:currentLocation];
                NSDictionary *frameInfo = [self readJSTalkFrame];
                [_frames addObject:frameInfo];
            }
        }
        else {
            [currentLocalFrame appendString:tok];
        }
    }
    
    NSDictionary *localFrame = [NSDictionary dictionaryWithObjectsAndKeys:@".local", @"appName", currentLocalFrame, @"script", nil];
    [_frames addObject:localFrame];
    
}


@end
Something went wrong with that request. Please try again.