00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // token.h: token parsing routines header file 00022 /////////////////////////////////////////////////////////////////////////////// 00023 00024 #ifndef __TOKEN_H_ 00025 #define __TOKEN_H_ 00026 00027 #include <stdio.h> 00028 #include <iostream> 00029 00030 using namespace std; 00031 00032 // Constantes usada para indicar uma palavra chave invalida (diferente de um 00033 // identificador invalido, que e positivo) e o fim das opcoes. 00034 #define END_TOKEN -1 00035 #define ERROR_TOKEN -2 00036 00037 struct token_kw 00038 { 00039 const char *kw_word; // null for end of table 00040 int kw_value; 00041 }; 00042 00043 class token 00044 { 00045 private: 00046 static int ck_hex(int c); 00047 00048 int tk_flags; // flags 00049 static const int TK_DEBUG_TOKEN = 0x01; // display each token 00050 const char *tk_fname; 00051 FILE *tk_fp; 00052 ostream *tk_cerr; // error output stream or 0 00053 char *tk_strp; // ptr to string left 00054 char *tk_stre; // ptr past end of string left 00055 char *tk_tkp; // ptr to token 00056 int tk_tkl; // token length 00057 int tk_linecnt; // line count (for error msgs) 00058 char *tk_orgp; // ptr to orig string start 00059 char *tk_orge; // ptr past end of orig string 00060 static const int TK_WSTRL = 300; 00061 char *tk_wstr; // ptr to work string for error msgs 00062 char tk_wstrstr[ TK_WSTRL ]; 00063 static const int TK_LINEL = 300; 00064 char tk_line[ TK_LINEL ]; 00065 00066 private: 00067 token( token & ); // no copying (not implemented) 00068 00069 public: 00070 token(); 00071 ~token(); 00072 00073 void setcerr(ostream *x); 00074 void setstring(char *adr, int len); 00075 char *emsg(const char *s); 00076 void next(); 00077 int nextckend(); // eat current token, check are no more 00078 int ckend(); // check is end of statement - error otherwise 00079 00080 // compare current token with string 00081 int cmp(const char *s); 00082 // copy current token if length fits to char * 00083 int getstr(char *s, int maxlen); 00084 // make copy of current token as c char * (includes \0 at end) 00085 int getstrnew(char *s[]); 00086 // convert decimal number token to number 00087 int getnumber(int *num); 00088 00089 // convert double number token to number 00090 int getdouble(double *num); 00091 // -------------------------------------------------------------------- 00092 00093 // convert string of hex chars to hex (var length output) 00094 int gethex(char *o, int ol); 00095 int openfile(const char *fname); 00096 void closefile(); 00097 struct token_kw *findkw(struct token_kw *kp); 00098 int nextline(); 00099 int parseline(struct token_kw *kp); 00100 }; 00101 00102 #endif