boucher / tdparsekit

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

tdparsekit / TDScientificNumberState.j
100644 65 lines (53 sloc) 1.229 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
@import "TDNumberState.j"
 
@implementation TDScientificNumberState : TDNumberState
{
    float exp;
    BOOL negativeExp;
}
 
- (void)parseRightSideFromReader:(TDReader)r
{
    [super parseRightSideFromReader:r];
 
    if ('e' == c || 'E' == c) {
        var e = c;
        c = [r read];
        
        var hasExp = isdigit(c),
            negativeExp = ('-' == c),
            positiveExp = ('+' == c);
 
        if (!hasExp && (negativeExp || positiveExp)) {
            c = [r read];
            hasExp = isdigit(c);
        }
        if (-1 != c) {
            [r unread];
        }
        if (hasExp) {
            [self append:e];
            if (negativeExp) {
                [self append:'-'];
            } else if (positiveExp) {
                [self append:'+'];
            }
            c = [r read];
            exp = [super absorbDigitsFromReader:r isFraction:NO];
        }
    }
}
 
- (void)reset:(int)cin
{
    [super reset:cin];
    exp = 0.0;
    negativeExp = NO;
}
 
- (float)value
{
    var result = floatValue;
    
    for (var i=0 ; i < exp; i++) {
        if (negativeExp) {
            result /= 10.0;
        } else {
            result *= 10.0;
        }
    }
    
    return result;
}
 
@end