• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//
2//  ParseTree.m
3//  ANTLR
4//
5//  Created by Alan Condit on 7/12/10.
6// [The "BSD licence"]
7// Copyright (c) 2010 Alan Condit
8// All rights reserved.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions
12// are met:
13// 1. Redistributions of source code must retain the above copyright
14//    notice, this list of conditions and the following disclaimer.
15// 2. Redistributions in binary form must reproduce the above copyright
16//    notice, this list of conditions and the following disclaimer in the
17//    documentation and/or other materials provided with the distribution.
18// 3. The name of the author may not be used to endorse or promote products
19//    derived from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#import "ParseTree.h"
33
34/** A record of the rules used to match a token sequence.  The tokens
35 *  end up as the leaves of this tree and rule nodes are the interior nodes.
36 *  This really adds no functionality, it is just an alias for CommonTree
37 *  that is more meaningful (specific) and holds a String to display for a node.
38 */
39@implementation ParseTree
40+ (ParseTree *)newParseTree:(id<Token>)label
41{
42    return [[ParseTree alloc] initWithLabel:label];
43}
44
45- (id)initWithLabel:(id<Token>)label
46{
47    self = [super init];
48    if ( self != nil) {
49        payload = [label retain];
50    }
51    return self;
52}
53
54- (id<BaseTree>)dupNode
55{
56    return nil;
57}
58
59- (NSInteger)type
60{
61    return 0;
62}
63
64- (NSString *)text
65{
66    return [self toString];
67}
68
69- (NSInteger)getTokenStartIndex
70{
71    return 0;
72}
73
74- (void)setTokenStartIndex:(NSInteger)anIndex
75{
76}
77
78- (NSInteger)getTokenStopIndex
79{
80    return 0;
81}
82
83- (void)setTokenStopIndex:(NSInteger)anIndex
84{
85}
86
87- (NSString *)description
88{
89    if ( [payload isKindOfClass:[CommonToken class]] ) {
90        id<Token> t = (id<Token>)payload;
91        if ( t.type == TokenTypeEOF ) {
92            return @"<EOF>";
93        }
94        return [t text];
95    }
96    return [payload description];
97}
98
99- (NSString *)toString
100{
101    return [self description];
102}
103
104/** Emit a token and all hidden nodes before.  EOF node holds all
105 *  hidden tokens after last real token.
106 */
107- (NSString *)toStringWithHiddenTokens
108{
109    NSMutableString *buf = [NSMutableString stringWithCapacity:25];
110    if ( hiddenTokens!=nil ) {
111        for (NSUInteger i = 0; i < [hiddenTokens count]; i++) {
112            id<Token>  hidden = (id<Token> ) [hiddenTokens objectAtIndex:i];
113            [buf appendString:[hidden text]];
114        }
115    }
116    NSString *nodeText = [self toString];
117    if ( ![nodeText isEqualTo:@"<EOF>"] )
118        [buf appendString:nodeText];
119    return buf;
120}
121
122/** Print out the leaves of this tree, which means printing original
123 *  input back out.
124 */
125- (NSString *)toInputString
126{
127    NSMutableString *buf = [NSMutableString stringWithCapacity:25];
128    [self _toStringLeaves:buf];
129    return buf;
130}
131
132- (void)_toStringLeaves:(NSMutableString *)buf
133{
134    if ( [payload isKindOfClass:[CommonToken class]] ) { // leaf node token?
135        [buf appendString:[self toStringWithHiddenTokens]];
136        return;
137    }
138    for (int i = 0; children!=nil && i < [children count]; i++) {
139        ParseTree *t = (ParseTree *) [children objectAtIndex:i];
140        [t _toStringLeaves:buf];
141    }
142}
143
144@synthesize payload;
145@synthesize hiddenTokens;
146//@synthesize children;
147//@synthesize anException;
148
149@end
150