This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / lyxlex.h
blobd955d273819bbbdab87e6072238791ed612a77d7
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
10 #ifdef __GNUG__
11 #pragma interface
12 #endif
14 #include <stdio.h>
15 #include "LString.h"
17 ///
18 struct keyword_item {
19 ///
20 char const* tag;
21 ///
22 short code;
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.
31 class LyXLex {
32 public:
33 ///
34 LyXLex (keyword_item*, int);
35 ///
36 ~LyXLex() { if (file && owns_file) fclose(file); };
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
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(string 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 void setLineNo(int l) { lineno = l; }
62 /// returns a lex code
63 int lex();
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);
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.
73 bool nextToken();
75 ///
76 int GetLineNo() { return lineno; }
77 ///
78 int GetInteger();
79 ///
80 bool GetBool();
81 ///
82 float GetFloat();
83 ///
84 string GetString() const;
86 /// get a long string, ended by the tag `endtag'
87 string getLongString(string const &endtoken);
89 ///
90 bool EatLine();
91 ///
92 int FindToken(char const* string[]);
93 ///
94 int CheckToken(char const* string[], int print_error);
96 ///
97 char const *text() const { return &buff[0]; }
99 /** Pushes a token list on a stack and replaces it with a new one.
101 void pushTable(keyword_item*, int);
103 /** Pops a token list into void and replaces it with the one now
104 on top of the stack.
106 void popTable();
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()
112 void printError(string const & message);
115 Prints the current token table on cerr.
117 void printTable();
118 protected:
120 enum {
122 LEX_MAX_BUFF = 2048
126 struct pushed_table {
128 pushed_table(){
129 next=0;
130 table_elem=0;
133 pushed_table *next;
135 keyword_item *table_elem;
137 int table_siz;
141 FILE *file;
143 bool owns_file;
144 ///
145 string name;
147 int lineno;
149 keyword_item *table;
151 int no_items;
153 char buff[LEX_MAX_BUFF];
155 pushed_table *pushed;
157 int search_kw(char const * const) const;
159 short status;
163 inline
164 bool LyXLex::IsOK()
166 return (file && !feof(file));
169 #endif