home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / lyxlex.h < prev    next >
C/C++ Source or Header  |  1998-04-23  |  3KB  |  170 lines

  1. // -*- C++ -*-
  2. //  Generalized simple lexical analizer.
  3. //  It can be used for simple syntax parsers, like lyxrc,
  4. //  texclass and others to come.   [asierra30/03/96]
  5. //
  6. //   (C) 1996 Lyx Team.
  7. #ifndef _LYXLEX_H
  8. #define _LYXLEX_H
  9.  
  10. #ifdef __GNUG__
  11. #pragma interface
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include "LString.h"
  16.  
  17. ///
  18. struct keyword_item {
  19.     ///
  20.     char const *tag;
  21.     ///
  22.     short code;
  23. };
  24.  
  25. /*@Doc:
  26.   Generalized simple lexical analizer.
  27.   It can be used for simple syntax parsers, like lyxrc,
  28.   texclass and others to come.
  29.   See lyxrc.C for an example of usage.
  30.   */
  31. class LyXLex { 
  32. public:
  33.     ///
  34.     LyXLex (keyword_item*, int);
  35.     ///
  36.     ~LyXLex() { if (file && owns_file) fclose(file); };
  37.  
  38.     /// Lex basic codes
  39.     enum {
  40.         ///
  41.         LEX_UNDEF = -1,
  42.         ///
  43.         LEX_FEOF  = -2,
  44.         ///
  45.         LEX_DATA  = -3,
  46.         ///
  47.         LEX_TOKEN = -4
  48.     };
  49.  
  50.     /// file is open and end of file is not reached
  51.     bool IsOK();
  52.     /// return true if able to open file, else false
  53.     bool setFile(LString const & filename);
  54.     /// if file is already read from, line numbers will be wrong.
  55.     // should be removed
  56.     void setFile(FILE *f);
  57.     ///
  58.     // should be removed
  59.     FILE *getFile() { return file; }
  60.     /// Danger! Don't use it unless you know what you are doing.
  61.     int setLineNo(int l) { lineno = l; return lineno; }
  62.     /// returns a lex code
  63.     int lex();
  64.  
  65.     /** Just read athe next word. If esc is true remember that
  66.       some chars might be escaped: "\ atleast */
  67.     bool next(bool esc = false);
  68.  
  69.     /** Read next token. This one is almost the same as next,
  70.       but it will consider " as a regular character and always
  71.       split a word if it contains a backslash.
  72.       */
  73.     bool nextToken();
  74.     
  75.     /// 
  76.     int GetLineNo() { return lineno; }
  77.     ///
  78.     int GetInteger();
  79.     ///
  80.     bool GetBool();
  81.     ///
  82.     float GetFloat();
  83.     ///
  84.     LString GetString() const;
  85.     
  86.     /// get a long string, ended by the tag `endtag'
  87.     LString getLongString(LString const &endtoken);
  88.     
  89.     ///
  90.     bool EatLine();
  91.     ///
  92.     int FindToken(char const* string[]);
  93.     ///
  94.     int CheckToken(char const* string[], int print_error);
  95.  
  96.     ///
  97.     char const *text() const { return &buff[0]; }
  98.  
  99.     /** Pushes a token list on a stack and replaces it with a new one.
  100.      */
  101.     void pushTable(keyword_item*, int);
  102.  
  103.     /** Pops a token list into void and replaces it with the one now
  104.       on top of the stack.
  105.       */
  106.     void popTable();
  107.  
  108.     /** Prints an error message with the corresponding line number
  109.       and file name. If message contains the substring `$$Token',
  110.       it is replaced with the value of GetString()
  111.       */
  112.     void printError(LString const & message);
  113.  
  114.     /**
  115.       Prints the current token table on stderr.
  116.       */
  117.     void printTable();
  118. protected:
  119.     ///
  120.     enum {
  121.     ///
  122.         LEX_MAX_BUFF = 512
  123.     };
  124.  
  125.     ///
  126.     struct pushed_table {
  127.         ///
  128.         pushed_table(){
  129.             next=NULL;
  130.             table_elem=NULL;
  131.         }
  132.         ///
  133.         pushed_table *next;
  134.         ///
  135.         keyword_item *table_elem;
  136.         ///
  137.         int table_siz;
  138.     };
  139.  
  140.     ///
  141.     FILE *file;
  142.         ///
  143.         bool owns_file;
  144.     /// 
  145.     LString name;
  146.     ///
  147.     int lineno;
  148.     ///
  149.     keyword_item *table;
  150.     ///
  151.     int no_items;
  152.     ///
  153.     char buff[LEX_MAX_BUFF];
  154.     ///
  155.     pushed_table *pushed;
  156.     ///
  157.     int search_kw(char const *) const;
  158.     ///
  159.     short status;
  160. };
  161.  
  162.  
  163. inline
  164. bool LyXLex::IsOK()
  165. {
  166.     return (file && !feof(file));
  167. }
  168.  
  169. #endif
  170.