10 | #import "TDTokenizerState.h"
11 |
12 | @class TDSymbolRootNode;
13 |
14 | /*!
15 | @class TDSymbolState
16 | @brief The idea of a symbol is a character that stands on its own, such as an ampersand or a parenthesis.
17 | @details The idea of a symbol is a character that stands on its own, such as an ampersand or a parenthesis. For example, when tokenizing the expression (isReady)& (isWilling) , a typical tokenizer would return 7 tokens, including one for each parenthesis and one for the ampersand. Thus a series of symbols such as )&( becomes three tokens, while a series of letters such as isReady becomes a single word token.
18 | Multi-character symbols are an exception to the rule that a symbol is a standalone character. For example, a tokenizer may want less-than-or-equals to tokenize as a single token. This class provides a method for establishing which multi-character symbols an object of this class should treat as single symbols. This allows, for example, "cat <= dog" to tokenize as three tokens, rather than splitting the less-than and equals symbols into separate tokens.
19 | By default, this state recognizes the following multi- character symbols: !=, :-, <=, >=
20 | */
21 | @interface TDSymbolState : TDTokenizerState {
22 | TDSymbolRootNode *rootNode;
23 | NSMutableArray *addedSymbols;
24 | }
25 |
26 | /*!
27 | @brief Adds the given string as a multi-character symbol.
28 | @param s a multi-character symbol that should be recognized as a single symbol token by this state
29 | */
30 | - (void)add:(NSString *)s;
31 |
32 | /*!
33 | @brief Removes the given string as a multi-character symbol.
34 | @details If s was never added as a multi-character symbol, this has no effect.
35 | @param s a multi-character symbol that should no longer be recognized as a single symbol token by this state
36 | */
37 | - (void)remove:(NSString *)s;
38 | @end
39 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDSymbolState.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDSymbolState.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 1/20/06.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDSymbolState.h"
10 | #import "TDToken.h"
11 | #import "TDSymbolRootNode.h"
12 | #import "TDReader.h"
13 | #import "TDTokenizer.h"
14 |
15 | @interface TDSymbolState ()
16 | @property (nonatomic, retain) TDSymbolRootNode *rootNode;
17 | @property (nonatomic, retain) NSMutableArray *addedSymbols;
18 | @end
19 |
20 | @implementation TDSymbolState
21 |
22 | - (id)init {
23 | self = [super init];
24 | if (self) {
25 | self.rootNode = [[[TDSymbolRootNode alloc] init] autorelease];
26 | self.addedSymbols = [NSMutableArray array];
27 | }
28 | return self;
29 | }
30 |
31 |
32 | - (void)dealloc {
33 | self.rootNode = nil;
34 | self.addedSymbols = nil;
35 | [super dealloc];
36 | }
37 |
38 |
39 | - (TDToken *)nextTokenFromReader:(TDReader *)r startingWith:(NSInteger)cin tokenizer:(TDTokenizer *)t {
40 | NSParameterAssert(r);
41 | NSString *symbol = [rootNode nextSymbol:r startingWith:cin];
42 | NSInteger len = symbol.length;
43 |
44 | if (0 == len || (len > 1 && [addedSymbols containsObject:symbol])) {
45 | return [TDToken tokenWithTokenType:TDTokenTypeSymbol stringValue:symbol floatValue:0.0];
46 | } else {
47 | NSInteger i = 0;
48 | for ( ; i < len - 1; i++) {
49 | [r unread];
50 | }
51 | return [TDToken tokenWithTokenType:TDTokenTypeSymbol stringValue:[NSString stringWithFormat:@"%C", (unsigned short)cin] floatValue:0.0];
52 | }
53 | }
54 |
55 |
56 | - (void)add:(NSString *)s {
57 | NSParameterAssert(s);
58 | [rootNode add:s];
59 | [addedSymbols addObject:s];
60 | }
61 |
62 |
63 | - (void)remove:(NSString *)s {
64 | NSParameterAssert(s);
65 | [rootNode remove:s];
66 | [addedSymbols removeObject:s];
67 | }
68 |
69 | @synthesize rootNode;
70 | @synthesize addedSymbols;
71 | @end
72 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTerminal.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDTerminal.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 7/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDParser.h"
11 |
12 | @class TDToken;
13 |
14 | /*!
15 | @class TDTerminal
16 | @brief An Abstract Class. A TDTerminal is a parser that is not a composition of other parsers.
17 | */
18 | @interface TDTerminal : TDParser {
19 | NSString *string;
20 | BOOL discardFlag;
21 | }
22 |
23 | /*!
24 | @brief Designated Initializer for all concrete TDTerminal subclasses.
25 | @details Note this is an abtract class and this method must be called on a concrete subclass.
26 | @param s the string matched by this parser
27 | @result an initialized TDTerminal subclass object
28 | */
29 | - (id)initWithString:(NSString *)s;
30 |
31 | /*!
32 | @brief By default, terminals push themselves upon a assembly's stack, after a successful match. This method will turn off that behavior.
33 | @details This method returns this parser as a convenience for chainging-style usage.
34 | @result this parser, returned for chaining/convenience
35 | */
36 | - (TDTerminal *)discard;
37 |
38 | /*!
39 | @property string
40 | @brief the string matched by this parser.
41 | */
42 | @property (nonatomic, readonly, copy) NSString *string;
43 | @end
44 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTerminal.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDTerminal.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 7/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDTerminal.h"
10 | #import "TDAssembly.h"
11 | #import "TDToken.h"
12 |
13 | @interface TDTerminal ()
14 | - (TDAssembly *)matchOneAssembly:(TDAssembly *)inAssembly;
15 | - (BOOL)qualifies:(id)obj;
16 |
17 | @property (nonatomic, readwrite, copy) NSString *string;
18 | @end
19 |
20 | @implementation TDTerminal
21 |
22 | - (id)init {
23 | return [self initWithString:nil];
24 | }
25 |
26 |
27 | - (id)initWithString:(NSString *)s {
28 | self = [super init];
29 | if (self) {
30 | self.string = s;
31 | }
32 | return self;
33 | }
34 |
35 |
36 | - (void)dealloc {
37 | self.string = nil;
38 | [super dealloc];
39 | }
40 |
41 |
42 | - (NSSet *)allMatchesFor:(NSSet *)inAssemblies {
43 | NSParameterAssert(inAssemblies);
44 | NSMutableSet *outAssemblies = [NSMutableSet set];
45 |
46 | for (TDAssembly *a in inAssemblies) {
47 | TDAssembly *b = [self matchOneAssembly:a];
48 | if (b) {
49 | [outAssemblies addObject:b];
50 | }
51 | }
52 |
53 | return outAssemblies;
54 | }
55 |
56 |
57 | - (TDAssembly *)matchOneAssembly:(TDAssembly *)inAssembly {
58 | NSParameterAssert(inAssembly);
59 | if (![inAssembly hasMore]) {
60 | return nil;
61 | }
62 |
63 | TDAssembly *outAssembly = nil;
64 |
65 | if ([self qualifies:[inAssembly peek]]) {
66 | outAssembly = [[inAssembly copy] autorelease];
67 | id obj = [outAssembly next];
68 | if (!discardFlag) {
69 | [outAssembly push:obj];
70 | }
71 | }
72 |
73 | return outAssembly;
74 | }
75 |
76 |
77 | - (BOOL)qualifies:(id)obj {
78 | NSAssert1(0, @"-[TDTerminal %@] must be overriden", NSStringFromSelector(_cmd));
79 | return NO;
80 | }
81 |
82 |
83 | - (TDTerminal *)discard {
84 | discardFlag = YES;
85 | return self;
86 | }
87 |
88 | @synthesize string;
89 | @end
90 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTokenArraySource.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDTokenArraySource.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 12/11/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class TDTokenizer;
12 | @class TDToken;
13 |
14 | /*!
15 | @class TDTokenArraySource
16 | @brief A TokenArraySource is a handy utility that enumerates over a specified reader, returning NSArrays of TDTokens delimited by a specified delimiter.
17 | @details For example,
18 |
19 | @code
20 | NSString *s = @"I came; I saw; I left in peace;";
21 |
22 | TDTokenizer *t = [TDTokenizer tokenizerWithString:s];
23 | TDTokenArraySource *src = [[[TDTokenArraySource alloc] initWithTokenizer:t delimiter:@";"] autorelease];
24 |
25 | while ([src hasMore]) {
26 | NSLog(@"%@", [src nextTokenArray]);
27 | }
28 | @endcode
29 |
30 | prints out:
31 |
32 | @code
33 | I came
34 | I saw
35 | I left in peace
36 | @endcode
37 | */
38 | @interface TDTokenArraySource : NSObject {
39 | TDTokenizer *tokenizer;
40 | NSString *delimiter;
41 | TDToken *nextToken;
42 | }
43 |
44 | /*!
45 | @brief Constructs a TDTokenArraySource that will read an NSArrays of TDTokens using the specified tokenizer, delimited by the specified delimiter.
46 | @param tokenizer a tokenizer to read tokens from
47 | @param delimiter the character(s) that fences off where one array of tokens ends and the next begins
48 | */
49 | - (id)initWithTokenizer:(TDTokenizer *)t delimiter:(NSString *)s;
50 |
51 | /*!
52 | @brief true if the source has more arrays of tokens.
53 | @result true, if the source has more arrays of tokens that have not yet been popped with -nextTokenArray
54 | */
55 | - (BOOL)hasMore;
56 |
57 | /*!
58 | @brief Returns the next array of tokens from the source.
59 | @result the next array of tokens from the source
60 | */
61 | - (NSArray *)nextTokenArray;
62 | @end
63 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTokenArraySource.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDTokenArraySource.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 12/11/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDTokenArraySource.h"
10 | #import "TDToken.h"
11 | #import "TDTokenizer.h"
12 |
13 | @interface TDTokenArraySource ()
14 | @property (nonatomic, retain) TDTokenizer *tokenizer;
15 | @property (nonatomic, retain) NSString *delimiter;
16 | @property (nonatomic, retain) TDToken *nextToken;
17 | @end
18 |
19 | @implementation TDTokenArraySource
20 |
21 | - (id)init {
22 | return [self initWithTokenizer:nil delimiter:nil];
23 | }
24 |
25 |
26 | - (id)initWithTokenizer:(TDTokenizer *)t delimiter:(NSString *)s {
27 | NSParameterAssert(t);
28 | NSParameterAssert(s);
29 | self = [super init];
30 | if (self) {
31 | self.tokenizer = t;
32 | self.delimiter = s;
33 | }
34 | return self;
35 | }
36 |
37 |
38 | - (void)dealloc {
39 | self.tokenizer = nil;
40 | self.delimiter = nil;
41 | self.nextToken = nil;
42 | [super dealloc];
43 | }
44 |
45 |
46 | - (BOOL)hasMore {
47 | if (!nextToken) {
48 | self.nextToken = [tokenizer nextToken];
49 | }
50 |
51 | return ([TDToken EOFToken] != nextToken);
52 | }
53 |
54 |
55 | - (NSArray *)nextTokenArray {
56 | if (![self hasMore]) {
57 | return nil;
58 | }
59 |
60 | NSMutableArray *res = [NSMutableArray arrayWithObject:nextToken];
61 | self.nextToken = nil;
62 |
63 | TDToken *eof = [TDToken EOFToken];
64 | TDToken *tok = nil;
65 |
66 | while ((tok = [tokenizer nextToken]) != eof) {
67 | if ([tok.stringValue isEqualToString:delimiter]) {
68 | break; // discard delimiter tok
69 | }
70 | [res addObject:tok];
71 | }
72 |
73 | //return [[res copy] autorelease];
74 | return res; // optimization
75 | }
76 |
77 | @synthesize tokenizer;
78 | @synthesize delimiter;
79 | @synthesize nextToken;
80 | @end
81 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTokenAssembly.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDTokenAssembly.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 7/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDAssembly.h"
10 |
11 | @class TDTokenizer;
12 |
13 | /*!
14 | @class TDTokenAssembly
15 | @brief A TDTokenAssembly is a TDAssembly whose elements are TDTokens.
16 | @details TDTokens are, roughly, the chunks of text that a TDTokenizer returns.
17 | */
18 | @interface TDTokenAssembly : TDAssembly {
19 | TDTokenizer *tokenizer;
20 | NSArray *tokens;
21 | BOOL preservesWhitespaceTokens;
22 | }
23 |
24 | /*!
25 | @brief Convenience factory method for initializing an autoreleased assembly with the tokenizer t and its string
26 | @param t tokenizer whose string will be worked on
27 | @result an initialized autoreleased assembly
28 | */
29 | + (id)assemblyWithTokenizer:(TDTokenizer *)t;
30 |
31 | /*!
32 | @brief Initializes an assembly with the tokenizer t and its string
33 | @param t tokenizer whose string will be worked on
34 | @result an initialized assembly
35 | */
36 | - (id)initWithTokenzier:(TDTokenizer *)t;
37 |
38 | /*!
39 | @brief Convenience factory method for initializing an autoreleased assembly with the token array a and its string
40 | @param a token array whose string will be worked on
41 | @result an initialized autoreleased assembly
42 | */
43 | + (id)assemblyWithTokenArray:(NSArray *)a;
44 |
45 | /*!
46 | @brief Initializes an assembly with the token array a and its string
47 | @param a token array whose string will be worked on
48 | @result an initialized assembly
49 | */
50 | - (id)initWithTokenArray:(NSArray *)a;
51 |
52 | /*!
53 | @property preservesWhitespaceTokens
54 | @brief If true, whitespace tokens retreived from this assembly's tokenizier will be silently placed on this assembly's stack without being reported by -next or -peek. Default is false.
55 | */
56 | @property (nonatomic) BOOL preservesWhitespaceTokens;
57 | @end
58 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTokenizerState.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDParseKitState.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 1/20/06.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | #define TD_USE_MUTABLE_STRING_BUF 1
12 |
13 | @class TDToken;
14 | @class TDTokenizer;
15 | @class TDReader;
16 |
17 | /*!
18 | @class TDTokenizerState
19 | @brief A TDTokenizerState returns a token, given a reader, an initial character read from the reader, and a tokenizer that is conducting an overall tokenization of the reader.
20 | @details The tokenizer will typically have a character state table that decides which state to use, depending on an initial character. If a single character is insufficient, a state such as TDSlashState will read a second character, and may delegate to another state, such as TDSlashStarState. This prospect of delegation is the reason that the -nextToken method has a tokenizer argument.
21 | */
22 | @interface TDTokenizerState : NSObject {
23 | #if TD_USE_MUTABLE_STRING_BUF
24 | NSMutableString *stringbuf;
25 | #else
26 | unichar *__strong charbuf;
27 | NSUInteger length;
28 | NSUInteger index;
29 | #endif
30 | }
31 |
32 | /*!
33 | @brief Return a token that represents a logical piece of a reader.
34 | @param r the reader from which to read additional characters
35 | @param cin the character that a tokenizer used to determine to use this state
36 | @param t the tokenizer currently powering the tokenization
37 | @result a token that represents a logical piece of the reader
38 | */
39 | - (TDToken *)nextTokenFromReader:(TDReader *)r startingWith:(NSInteger)cin tokenizer:(TDTokenizer *)t;
40 | @end
41 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTrack.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDTrack.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDSequence.h"
11 |
12 | /*!
13 | @class TDTrack
14 | @brief A TDTrack is a sequence that throws a TDTrackException if the sequence begins but does not complete.
15 | @details If -[TDTrack allMatchesFor:] begins but does not complete, it throws a TDTrackException.
16 | */
17 | @interface TDTrack : TDSequence {
18 |
19 | }
20 |
21 | /*!
22 | @brief Convenience factory method for initializing an autoreleased TDTrack parser.
23 | @result an initialized autoreleased TDTrack parser.
24 | */
25 | + (id)track;
26 | @end
27 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTrack.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDTrack.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDTrack.h"
10 | #import "TDAssembly.h"
11 | #import "TDTrackException.h"
12 |
13 | @interface TDParser ()
14 | - (NSSet *)matchAndAssemble:(NSSet *)inAssemblies;
15 | - (TDAssembly *)best:(NSSet *)inAssemblies;
16 | @end
17 |
18 | @interface TDTrack ()
19 | - (void)throwTrackExceptionWithPreviousState:(NSSet *)inAssemblies parser:(TDParser *)p;
20 | @end
21 |
22 | @implementation TDTrack
23 |
24 | + (id)track {
25 | return [[[self alloc] init] autorelease];
26 | }
27 |
28 |
29 | - (NSSet *)allMatchesFor:(NSSet *)inAssemblies {
30 | NSParameterAssert(inAssemblies);
31 | BOOL inTrack = NO;
32 | NSSet *lastAssemblies = inAssemblies;
33 | NSSet *outAssemblies = inAssemblies;
34 |
35 | for (TDParser *p in subparsers) {
36 | outAssemblies = [p matchAndAssemble:outAssemblies];
37 | if (!outAssemblies.count) {
38 | if (inTrack) {
39 | [self throwTrackExceptionWithPreviousState:lastAssemblies parser:p];
40 | }
41 | break;
42 | }
43 | inTrack = YES;
44 | lastAssemblies = outAssemblies;
45 | }
46 |
47 | return outAssemblies;
48 | }
49 |
50 |
51 | - (void)throwTrackExceptionWithPreviousState:(NSSet *)inAssemblies parser:(TDParser *)p {
52 | TDAssembly *best = [self best:inAssemblies];
53 |
54 | NSString *after = [best consumedObjectsJoinedByString:@" "];
55 | if (!after.length) {
56 | after = @"-nothing-";
57 | }
58 |
59 | NSString *expected = [p description];
60 |
61 | id next = [best peek];
62 | NSString *found = next ? [next description] : @"-nothing-";
63 |
64 | NSString *reason = [NSString stringWithFormat:@"\n\nAfter : %@\nExpected : %@\nFound : %@\n\n", after, expected, found];
65 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
66 | after, @"after",
67 | expected, @"expected",
68 | found, @"found",
69 | nil];
70 | [[TDTrackException exceptionWithName:TDTrackExceptionName reason:reason userInfo:userInfo] raise];
71 | }
72 |
73 | @end
74 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTrackException.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDTrackException.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 10/14/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | extern NSString * const TDTrackExceptionName;
12 |
13 | /*!
14 | @class TDTrackException
15 | @brief Signals that a parser could not match text after a specific point.
16 | @details The userInfo for this exception contains the following keys:
17 | after (NSString *) - some indication of what text was interpretable before this exception occurred
18 | expected (NSString *) - some indication of what kind of thing was expected, such as a ')' token
19 | found (NSString *) - the text element the thrower actually found when it expected something else
20 | */
21 | @interface TDTrackException : NSException {
22 |
23 | }
24 |
25 | @end
26 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDTrackException.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDTrackException.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 10/14/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDTrackException.h"
10 |
11 | NSString * const TDTrackExceptionName = @"Track Exception";
12 |
13 | @implementation TDTrackException
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDUppercaseWord.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDUppercaseWord.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDWord.h"
11 |
12 | @interface TDUppercaseWord : TDWord {
13 |
14 | }
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDUppercaseWord.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDUppercaseWord.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDUppercaseWord.h"
10 | #import "TDToken.h"
11 |
12 | @implementation TDUppercaseWord
13 |
14 | - (BOOL)qualifies:(id)obj {
15 | TDToken *tok = (TDToken *)obj;
16 | if (!tok.isWord) {
17 | return NO;
18 | }
19 |
20 | NSString *s = tok.stringValue;
21 | return s.length && isupper([s characterAtIndex:0]);
22 | }
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWhitespaceState.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDWhitespaceState.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 1/20/06.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDTokenizerState.h"
11 |
12 | /*!
13 | @class TDWhitespaceState
14 | @brief A whitespace state ignores whitespace (such as blanks and tabs), and returns the tokenizer's next token.
15 | @details By default, all characters from 0 to 32 are whitespace.
16 | */
17 | @interface TDWhitespaceState : TDTokenizerState {
18 | NSMutableArray *whitespaceChars;
19 | BOOL reportsWhitespaceTokens;
20 | }
21 |
22 | /*!
23 | @brief Informs whether the given character is recognized as whitespace (and therefore ignored) by this state.
24 | @param cin the character to check
25 | @result true if the given chracter is recognized as whitespace
26 | */
27 | - (BOOL)isWhitespaceChar:(NSInteger)cin;
28 |
29 | /*!
30 | @brief Establish the given character range as whitespace to ignore.
31 | @param yn true if the given character range is whitespace
32 | @param start the "start" character. e.g. 'a' or 65.
33 | @param end the "end" character. 'z' or 90.
34 | */
35 | - (void)setWhitespaceChars:(BOOL)yn from:(NSInteger)start to:(NSInteger)end;
36 |
37 | /*!
38 | @property reportsWhitespaceTokens
39 | @brief determines whether a TDTokenizer associated with this state reports or silently consumes whitespace tokens. default is NO which causes silent consumption of whitespace chars
40 | */
41 | @property (nonatomic) BOOL reportsWhitespaceTokens;
42 | @end
43 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWord.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDWord.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 7/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDTerminal.h"
11 |
12 | /*!
13 | @class TDWord
14 | @brief A TDWord matches a word from a token assembly.
15 | */
16 | @interface TDWord : TDTerminal {
17 |
18 | }
19 |
20 | /*!
21 | @brief Convenience factory method for initializing an autoreleased TDWord object.
22 | @result an initialized autoreleased TDWord object
23 | */
24 | + (id)word;
25 | @end
26 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWord.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDWord.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 7/13/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDWord.h"
10 | #import "TDToken.h"
11 |
12 | @implementation TDWord
13 |
14 | + (id)word {
15 | return [[[self alloc] initWithString:nil] autorelease];
16 | }
17 |
18 |
19 | - (BOOL)qualifies:(id)obj {
20 | TDToken *tok = (TDToken *)obj;
21 | return tok.isWord;
22 | }
23 |
24 | @end
25 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWordOrReservedState.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDWordOrReservedState.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/14/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDWordState.h"
11 |
12 | /*!
13 | @class TDWordOrReservedState
14 | @brief Override TDWordState to return known reserved words as tokens of type TDTT_RESERVED.
15 | */
16 | @interface TDWordOrReservedState : TDWordState {
17 | NSMutableSet *reservedWords;
18 | }
19 |
20 | /*!
21 | @brief Adds the specified string as a known reserved word.
22 | @param s reserved word to add
23 | */
24 | - (void)addReservedWord:(NSString *)s;
25 | @end
26 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWordOrReservedState.m:
--------------------------------------------------------------------------------
1 | //
2 | // TDWordOrReservedState.m
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 8/14/08.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import "TDWordOrReservedState.h"
10 |
11 | @interface TDWordOrReservedState ()
12 | @property (nonatomic, retain) NSMutableSet *reservedWords;
13 | @end
14 |
15 | @implementation TDWordOrReservedState
16 |
17 | - (id)init {
18 | self = [super init];
19 | if (self) {
20 | self.reservedWords = [NSMutableSet set];
21 | }
22 | return self;
23 | }
24 |
25 |
26 | - (void)dealloc {
27 | self.reservedWords = nil;
28 | [super dealloc];
29 | }
30 |
31 |
32 | - (void)addReservedWord:(NSString *)s {
33 | [reservedWords addObject:s];
34 | }
35 |
36 |
37 | - (TDToken *)nextTokenFromReader:(TDReader *)r startingWith:(NSInteger)cin tokenizer:(TDTokenizer *)t {
38 | NSParameterAssert(r);
39 | return nil;
40 | }
41 |
42 | @synthesize reservedWords;
43 | @end
44 |
--------------------------------------------------------------------------------
/src/framework/todparsekit/TDWordState.h:
--------------------------------------------------------------------------------
1 | //
2 | // TDWordState.h
3 | // TDParseKit
4 | //
5 | // Created by Todd Ditchendorf on 1/20/06.
6 | // Copyright 2008 Todd Ditchendorf. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "TDTokenizerState.h"
11 |
12 | /*!
13 | @class TDWordState
14 | @brief A word state returns a word from a reader.
15 | @details Like other states, a tokenizer transfers the job of reading to this state, depending on an initial character. Thus, the tokenizer decides which characters may begin a word, and this state determines which characters may appear as a second or later character in a word. These are typically different sets of characters; in particular, it is typical for digits to appear as parts of a word, but not as the initial character of a word.
16 | By default, the following characters may appear in a word. The method setWordChars() allows customizing this.
17 | @code
18 | From To
19 | 'a' 'z'
20 | 'A' 'Z'
21 | '0' '9'
22 | @endcode
23 | as well as: minus sign -, underscore _, and apostrophe '.
24 | */
25 | @interface TDWordState : TDTokenizerState {
26 | NSMutableArray *wordChars;
27 | }
28 |
29 | /*!
30 | @brief Establish characters in the given range as valid characters for part of a word after the first character. Note that the tokenizer must determine which characters are valid as the beginning character of a word.
31 | @param yn true if characters in the given range are word characters
32 | @param start the "start" character. e.g. 'a' or 65.
33 | @param end the "end" character. 'z' or 90.
34 | */
35 | - (void)setWordChars:(BOOL)yn from:(NSInteger)start to:(NSInteger)end;
36 |
37 | /*!
38 | @brief Informs whether the given character is recognized as a word character by this state.
39 | @param cin the character to check
40 | @result true if the given chracter is recognized as a word character
41 | */
42 | - (BOOL)isWordChar:(NSInteger)c;
43 | @end
44 |
--------------------------------------------------------------------------------