1 #ifndef INC_CharScanner_hpp__
2 #define INC_CharScanner_hpp__
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/CharScanner.hpp#2 $
11 #include <antlr/config.hpp>
15 #ifdef HAS_NOT_CCTYPE_H
21 #if ( _MSC_VER == 1200 )
22 // VC6 seems to need this
23 // note that this is not a standard C++ include file.
27 #include <antlr/TokenStream.hpp>
28 #include <antlr/RecognitionException.hpp>
29 #include <antlr/SemanticException.hpp>
30 #include <antlr/MismatchedCharException.hpp>
31 #include <antlr/InputBuffer.hpp>
32 #include <antlr/BitSet.hpp>
33 #include <antlr/LexerSharedInputState.hpp>
35 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
39 class ANTLR_API CharScanner;
41 ANTLR_C_USING(tolower)
43 #ifdef ANTLR_REALLY_NO_STRCASECMP
44 // Apparently, neither strcasecmp nor stricmp is standard, and Codewarrior
45 // on the mac has neither...
46 inline int strcasecmp(const char *s1, const char *s2)
50 char c1 = tolower(*s1++),
52 if (c1 < c2) return -1;
53 if (c1 > c2) return 1;
54 if (c1 == 0) return 0;
59 ANTLR_C_USING(stricmp)
61 ANTLR_C_USING(strcasecmp)
65 /** Functor for the literals map
67 class ANTLR_API CharScannerLiteralsLess : public ANTLR_USE_NAMESPACE(std)binary_function<ANTLR_USE_NAMESPACE(std)string,ANTLR_USE_NAMESPACE(std)string,bool> {
69 const CharScanner* scanner;
71 #ifdef NO_TEMPLATE_PARTS
72 CharScannerLiteralsLess() {} // not really used, definition to appease MSVC
74 CharScannerLiteralsLess(const CharScanner* theScanner)
78 bool operator() (const ANTLR_USE_NAMESPACE(std)string& x,const ANTLR_USE_NAMESPACE(std)string& y) const;
79 // defaults are good enough..
80 // CharScannerLiteralsLess(const CharScannerLiteralsLess&);
81 // CharScannerLiteralsLess& operator=(const CharScannerLiteralsLess&);
84 /** Superclass of generated lexers
86 class ANTLR_API CharScanner : public TokenStream {
88 typedef RefToken (*factory_type)();
90 CharScanner(InputBuffer& cb, bool case_sensitive );
91 CharScanner(InputBuffer* cb, bool case_sensitive );
92 CharScanner(const LexerSharedInputState& state, bool case_sensitive );
94 virtual ~CharScanner()
98 virtual int LA(unsigned int i);
100 virtual void append(char c)
102 if (saveConsumedInput)
104 size_t l = text.length();
109 text.replace(l,0,&c,1);
113 virtual void append(const ANTLR_USE_NAMESPACE(std)string& s)
115 if( saveConsumedInput )
119 virtual void commit()
121 inputState->getInput().commit();
124 /** called by the generated lexer to do error recovery, override to
125 * customize the behaviour.
127 virtual void recover(const RecognitionException& ex, const BitSet& tokenSet)
130 consumeUntil(tokenSet);
133 virtual void consume()
135 if (inputState->guessing == 0)
144 // use input.LA(), not LA(), to get original case
145 // CharScanner.LA() would toLower it.
146 append(inputState->getInput().LA(1));
149 // RK: in a sense I don't like this automatic handling.
153 inputState->column++;
155 inputState->getInput().consume();
158 /** Consume chars until one matches the given char */
159 virtual void consumeUntil(int c)
164 if( la_1 == EOF_CHAR || la_1 == c )
170 /** Consume chars until one matches the given set */
171 virtual void consumeUntil(const BitSet& set)
176 if( la_1 == EOF_CHAR || set.member(la_1) )
182 /// Mark the current position and return a id for it
183 virtual unsigned int mark()
185 return inputState->getInput().mark();
187 /// Rewind the scanner to a previously marked position
188 virtual void rewind(unsigned int pos)
190 inputState->getInput().rewind(pos);
193 /// See if input contains character 'c' throw MismatchedCharException if not
194 virtual void match(int c)
198 throw MismatchedCharException(la_1, c, false, this);
202 /** See if input contains element from bitset b
203 * throw MismatchedCharException if not
205 virtual void match(const BitSet& b)
209 if ( !b.member(la_1) )
210 throw MismatchedCharException( la_1, b, false, this );
214 /** See if input contains string 's' throw MismatchedCharException if not
215 * @note the string cannot match EOF
217 virtual void match( const char* s )
221 // the & 0xFF is here to prevent sign extension lateron
222 int la_1 = LA(1), c = (*s++ & 0xFF);
225 throw MismatchedCharException(la_1, c, false, this);
230 /** See if input contains string 's' throw MismatchedCharException if not
231 * @note the string cannot match EOF
233 virtual void match(const ANTLR_USE_NAMESPACE(std)string& s)
235 size_t len = s.length();
237 for (size_t i = 0; i < len; i++)
239 // the & 0xFF is here to prevent sign extension lateron
240 int la_1 = LA(1), c = (s[i] & 0xFF);
243 throw MismatchedCharException(la_1, c, false, this);
248 /** See if input does not contain character 'c'
249 * throw MismatchedCharException if not
251 virtual void matchNot(int c)
256 throw MismatchedCharException(la_1, c, true, this);
260 /** See if input contains character in range c1-c2
261 * throw MismatchedCharException if not
263 virtual void matchRange(int c1, int c2)
267 if ( la_1 < c1 || la_1 > c2 )
268 throw MismatchedCharException(la_1, c1, c2, false, this);
273 virtual bool getCaseSensitive() const
275 return caseSensitive;
278 virtual void setCaseSensitive(bool t)
283 virtual bool getCaseSensitiveLiterals() const=0;
285 /// Get the line the scanner currently is in (starts at 1)
286 virtual int getLine() const
288 return inputState->line;
291 /// set the line number
292 virtual void setLine(int l)
294 inputState->line = l;
297 /// Get the column the scanner currently is in (starts at 1)
298 virtual int getColumn() const
300 return inputState->column;
302 /// set the column number
303 virtual void setColumn(int c)
305 inputState->column = c;
308 /// get the filename for the file currently used
309 virtual const ANTLR_USE_NAMESPACE(std)string& getFilename() const
311 return inputState->filename;
313 /// Set the filename the scanner is using (used in error messages)
314 virtual void setFilename(const ANTLR_USE_NAMESPACE(std)string& f)
316 inputState->filename = f;
319 virtual bool getCommitToPath() const
324 virtual void setCommitToPath(bool commit)
326 commitToPath = commit;
329 /** return a copy of the current text buffer */
330 virtual const ANTLR_USE_NAMESPACE(std)string& getText() const
335 virtual void setText(const ANTLR_USE_NAMESPACE(std)string& s)
340 virtual void resetText()
343 inputState->tokenStartColumn = inputState->column;
344 inputState->tokenStartLine = inputState->line;
347 virtual RefToken getTokenObject() const
352 /** Used to keep track of line breaks, needs to be called from
353 * within generated lexers when a \n \r is encountered.
355 virtual void newline()
358 inputState->column = 1;
361 /** Advance the current column number by an appropriate amount according
362 * to the tabsize. This method needs to be explicitly called from the
363 * lexer rules encountering tabs.
368 int nc = ( ((c-1)/tabsize) + 1) * tabsize + 1; // calculate tab stop
371 /// set the tabsize. Returns the old tabsize
372 int setTabsize( int size )
374 int oldsize = tabsize;
378 /// Return the tabsize used by the scanner
379 int getTabSize() const
384 /** Report exception errors caught in nextToken() */
385 virtual void reportError(const RecognitionException& e);
387 /** Parser error-reporting function can be overridden in subclass */
388 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
390 /** Parser warning-reporting function can be overridden in subclass */
391 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
393 virtual InputBuffer& getInputBuffer()
395 return inputState->getInput();
398 virtual LexerSharedInputState getInputState()
403 /** set the input state for the lexer.
404 * @note state is a reference counted object, hence no reference */
405 virtual void setInputState(LexerSharedInputState state)
410 /// Set the factory for created tokens
411 virtual void setTokenObjectFactory(factory_type factory)
413 tokenFactory = factory;
416 /** Test the token text against the literals table
417 * Override this method to perform a different literals test
419 virtual int testLiteralsTable(int ttype) const
421 ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,int,CharScannerLiteralsLess>::const_iterator i = literals.find(text);
422 if (i != literals.end())
427 /** Test the text passed in against the literals table
428 * Override this method to perform a different literals test
429 * This is used primarily when you want to test a portion of
432 virtual int testLiteralsTable(const ANTLR_USE_NAMESPACE(std)string& txt,int ttype) const
434 ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,int,CharScannerLiteralsLess>::const_iterator i = literals.find(txt);
435 if (i != literals.end())
440 /// Override this method to get more specific case handling
441 virtual int toLower(int c) const
443 // test on EOF_CHAR for buggy (?) STLPort tolower (or HPUX tolower?)
444 // also VC++ 6.0 does this. (see fix 422 (is reverted by this fix)
445 // this one is more structural. Maybe make this configurable.
446 return (c == EOF_CHAR ? EOF_CHAR : tolower(c));
449 /** This method is called by YourLexer::nextToken() when the lexer has
450 * hit EOF condition. EOF is NOT a character.
451 * This method is not called if EOF is reached during
452 * syntactic predicate evaluation or during evaluation
453 * of normal lexical rules, which presumably would be
454 * an IOException. This traps the "normal" EOF condition.
456 * uponEOF() is called after the complete evaluation of
457 * the previous token and only if your parser asks
458 * for another token beyond that last non-EOF token.
460 * You might want to throw token or char stream exceptions
461 * like: "Heh, premature eof" or a retry stream exception
462 * ("I found the end of this file, go back to referencing file").
464 virtual void uponEOF()
468 /// Methods used to change tracing behavior
469 virtual void traceIndent();
470 virtual void traceIn(const char* rname);
471 virtual void traceOut(const char* rname);
473 #ifndef NO_STATIC_CONSTS
474 static const int EOF_CHAR = EOF;
481 ANTLR_USE_NAMESPACE(std)string text; ///< Text of current token
482 /// flag indicating wether consume saves characters
483 bool saveConsumedInput;
484 factory_type tokenFactory; ///< Factory for tokens
485 bool caseSensitive; ///< Is this lexer case sensitive
486 ANTLR_USE_NAMESPACE(std)map<ANTLR_USE_NAMESPACE(std)string,int,CharScannerLiteralsLess> literals; // set by subclass
488 RefToken _returnToken; ///< used to return tokens w/o using return val
490 /// Input state, gives access to input stream, shared among different lexers
491 LexerSharedInputState inputState;
493 /** Used during filter mode to indicate that path is desired.
494 * A subsequent scan error will report an error as usual
495 * if acceptPath=true;
499 int tabsize; ///< tab size the scanner uses.
501 /// Create a new RefToken of type t
502 virtual RefToken makeToken(int t)
504 RefToken tok = tokenFactory();
506 tok->setColumn(inputState->tokenStartColumn);
507 tok->setLine(inputState->tokenStartLine);
511 /** Tracer class, used when -traceLexer is passed to antlr
518 Tracer(const Tracer& other); // undefined
519 Tracer& operator=(const Tracer& other); // undefined
521 Tracer( CharScanner* p,const char* t )
524 parser->traceIn(text);
528 parser->traceOut(text);
534 CharScanner( const CharScanner& other ); // undefined
535 CharScanner& operator=( const CharScanner& other ); // undefined
537 #ifndef NO_STATIC_CONSTS
538 static const int NO_CHAR = 0;
546 inline int CharScanner::LA(unsigned int i)
548 int c = inputState->getInput().LA(i);
553 return toLower(c); // VC 6 tolower bug caught in toLower.
556 inline bool CharScannerLiteralsLess::operator() (const ANTLR_USE_NAMESPACE(std)string& x,const ANTLR_USE_NAMESPACE(std)string& y) const
558 if (scanner->getCaseSensitiveLiterals())
559 return ANTLR_USE_NAMESPACE(std)less<ANTLR_USE_NAMESPACE(std)string>()(x,y);
563 return (stricmp(x.c_str(),y.c_str())<0);
565 return (strcasecmp(x.c_str(),y.c_str())<0);
570 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
574 #endif //INC_CharScanner_hpp__