1 // [The "BSD licence"]
2 // Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. The name of the author may not be used to endorse or promote products
14 //    derived from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 
28 #import <Foundation/Foundation.h>
29 #import "TokenSource.h"
30 #import "BaseRecognizer.h"
31 #import "RecognizerSharedState.h"
32 #import "CharStream.h"
33 #import "Token.h"
34 #import "CommonToken.h"
35 #import "RecognitionException.h"
36 #import "MismatchedTokenException.h"
37 #import "MismatchedRangeException.h"
38 
39 @interface Lexer : BaseRecognizer <TokenSource> {
40 	id<CharStream> input;      ///< The character stream we pull tokens out of.
41 	NSUInteger ruleNestingLevel;
42 }
43 
44 @property (retain, getter=input, setter=setInput:) id<CharStream> input;
45 @property (getter=getRuleNestingLevel, setter=setRuleNestingLevel:) NSUInteger ruleNestingLevel;
46 
47 #pragma mark Initializer
48 - (id) initWithCharStream:(id<CharStream>) anInput;
49 - (id) initWithCharStream:(id<CharStream>)anInput State:(RecognizerSharedState *)state;
50 
51 - (id) copyWithZone:(NSZone *)zone;
52 
53 - (void) reset;
54 
55 // - (RecognizerSharedState *) state;
56 
57 #pragma mark Tokens
58 - (id<Token>)getToken;
59 - (void) setToken: (id<Token>) aToken;
60 - (id<Token>) nextToken;
61 - (void) mTokens;		// abstract, defined in generated sources
62 - (void) skip;
63 - (id<CharStream>) input;
64 - (void) setInput:(id<CharStream>)aCharStream;
65 
66 - (void) emit;
67 - (void) emit:(id<Token>)aToken;
68 
69 #pragma mark Matching
70 - (void) matchString:(NSString *)aString;
71 - (void) matchAny;
72 - (void) matchChar:(unichar) aChar;
73 - (void) matchRangeFromChar:(unichar)fromChar to:(unichar)toChar;
74 
75 #pragma mark Informational
76 - (NSUInteger) line;
77 - (NSUInteger) charPositionInLine;
78 - (NSInteger) index;
79 - (NSString *) text;
80 - (void) setText:(NSString *) theText;
81 
82 // error handling
83 - (void) reportError:(RecognitionException *)e;
84 - (NSString *)getErrorMessage:(RecognitionException *)e TokenNames:(AMutableArray *)tokenNames;
85 - (NSString *)getCharErrorDisplay:(NSInteger)c;
86 - (void) recover:(RecognitionException *)e;
87 - (void)traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex;
88 - (void)traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex;
89 
90 @end
91