boucher / tdparsekit

A non-deterministic recursive descent parser written in Objective-J (ported from Obj-C, created by Tod Ditchendorf)

This URL has Read+Write access

tdparsekit / TDTerminal.j
100644 74 lines (56 sloc) 1.261 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
@import "TDParser.j"
@import "TDToken.j"
@improt "TDAssembly.j"
 
@implementation TDTerminal : TDParser
{
    CPString string;
    BOOL discardFlag;
}
 
- (id)init
{
    return [self initWithString:nil];
}
 
- (id)initWithString:(CPString)s
{
    if (self = [super init]) {
        string = s;
    }
    
    return self;
}
 
- (CPSet)allMatchesFor:(CPSet)inAssemblies
{
    var outAssemblies = [CPSet set],
        values = [inAssemblies allValues];
    
    for (var i=0, count=[values count]; i<count; i++)
    {
        var a = values[i],
            b = [self matchOneAssembly:a];
    
        if (b)
            [outAssemblies addObject:b]
    }
    
    return outAssemblies;
}
 
- (TDAssembly)matchOneAssembly:(TDAssembly)inAssembly
{
    if (![inAssembly hasMore])
        return nil;
    
    var outAssembly = nil;
    
    if ([self qualifies:[inAssembly peek]]) {
        outAssembly = [inAssembly copy];
        
        var obj = [outAssembly next];
        if (!discardFlag)
            [outAssembly push:obj];
    }
    
    return outAssembly;
}
 
- (BOOL)qualifies:(id)obj
{
    //NSAssert1(0, @"-[TDTerminal %s] must be overriden", _cmd);
    return NO;
}
 
- (TDTerminal)discard
{
    discardFlag = YES;
    return self;
}
 
@end