Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

slexer.hpp

Go to the documentation of this file.
00001 // +-------------------------------------------------------------------------+
00002 // |               I__n__t__e__L__i__b           0.6.10 development          |
00003 // | Copyright (c) Andrey Vikt. Stolyarov <crocodil_AT_croco.net> 2000-2007. |
00004 // |                                                                         |
00005 // | This is free software. The library part is available under              |
00006 // |                               GNU LESSER GENERAL PUBLIC LICENSE v.2.1.  |
00007 // | GNU LGPL v2.1 is found in docs/gnu_gpl2.txt,  or at  http://www.gnu.org |
00008 // |     Please see also docs/readme.txt and visit http://www.intelib.org    |
00009 // |                                                                         |
00010 // | !!! THERE IS NO WARRANTY OF ANY KIND, NEITHER EXPRESSED NOR IMPLIED !!! |
00011 // +-------------------------------------------------------------------------+
00012 
00013 
00014 
00015 
00022 #ifndef INTELIB_SLEXER_HPP_SENTRY
00023 #define INTELIB_SLEXER_HPP_SENTRY
00024 
00025 #include "../sexpress/sexpress.hpp"
00026 #include "../sexpress/sstring.hpp"
00027 
00028 extern SLabel LexemaEof;
00029 
00030     /* this paranoia check should in fact never be positive, but... */
00031 #if defined(EOF) && EOF != -1
00032 #error This version assumes EOF is -1 and that is not true for your situation
00033     /* in case it is, edit slexer.cpp, remove the definition of EOF and
00034        add ``#include <stdio.h>'' instead... and, well, be sure to 
00035        report this :) */
00036 #endif
00037 
00038 
00040 
00047 class IntelibSLexAnalyser {
00048 
00049     struct SpecChar {
00050         char ch;
00051         enum {
00052             non_term, term, non_delim, read_rest, read_string, ignore_until
00053         } status : 8;
00054         SpecChar *sub;     
00055         SpecChar *next;    
00056         char closer[4];    
00057 
00058         SReference token;  
00059         SReference (*makestring)(const char *str);
00062 
00063         SpecChar() : ch(0), status(non_term), sub(0), next(0),
00064                      token(), makestring(0)
00065             { closer[0] = 0; }
00066     } *specchars, **special_state;
00067 
00068     SString delimiter_chars;
00069 
00070     enum State { 
00071         home,        // the home state
00072         string,      // reading a string constant until a closer
00073         stringq,     // within a string, reading escaped char
00074         token,       // reading a token (either a number or a symbol)
00075         token_force, // force reading one char, then go on as for a token
00076         special,     // special lexic element
00077         special2,    // check the next char, there can be longer delimiter
00078         special3,    // check the next char, make the non-delim token if
00079                      //    and only if the next char is a space or
00080                      //    delimiter
00081         comment      // ignore everything until the closer is found
00082     } state;
00083     const char *closer;    
00084     SReference (*string_finalizer)(const char *);
00086     int comment_closer_index; 
00087     int postponed_char;
00088     SString buf;
00089 
00090     int line, lexline;
00091     SReference lex;
00092     const char *error_message;
00093 public:
00095     IntelibSLexAnalyser();
00097     ~IntelibSLexAnalyser();
00098 
00100     bool AddDelimiter(const char *prefix, const SReference &token);
00102     bool AddNonDelimiter(const char *prefix, const SReference &token);
00104 
00105     bool AddTokenStarter(const char *prefix,
00106                          SReference (*fun)(const char *str) = 0);
00108     bool AddStringStarter(const char *prefix, int closer_char,
00109                           SReference (*fun)(const char *str) = 0);
00111     bool AddCommentStarter(const char *prefix, const char *closer = "\n");
00112 
00114     enum FeedResult { 
00115         res_empty,    
00116         res_ready,    
00117         res_continue, 
00118         res_eof,      
00119         res_error     
00120     };
00121 
00123 
00124     FeedResult FeedChar(int c);
00125 
00127     SReference Get() const;
00128 
00130     int GetLine() const { return line; }
00132     void SetLine(int l) { line = l; }
00134     const char *GetErrorMessage() const { return error_message; }
00135 
00137     void Drop();
00138 
00139 private:
00140     SpecChar *AddSpecial(const char *prefix, bool extendable);
00141     SpecChar *DoAddSpecial(SpecChar **p, const char *str, bool ex);
00142 
00143     FeedResult Home(int c);
00144     FeedResult String(int c);
00145     FeedResult Stringq(int c);
00146     FeedResult Token(int c);
00147     FeedResult Special(int c);
00148     FeedResult Special3(int c);
00149     FeedResult Comment(int c);
00150 
00151     SReference ProcessToken(const char *s);
00152 
00153     bool IsDelimiterChar(int c);
00154 };
00155 
00156 #endif

Generated on Tue Dec 18 00:39:45 2007 for InteLib by  doxygen 1.4.1