]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
Dekel's patch -- I didn't fix the xforms-0.88 keysyms stuff so it still doesn't finis...
[lyx.git] / src / lyxlex.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1996-2000 The LyX Team.
7  *
8  *   Generalized simple lexical analizer.
9  *   It can be used for simple syntax parsers, like lyxrc,
10  *   texclass and others to come.   [asierra30/03/96]
11  *
12  * ====================================================== */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation "lyxlex.h"
18 #endif
19
20 #include "lyxlex.h"
21 #include "lyxlex_pimpl.h"
22 #include "support/filetools.h"
23
24 using std::ostream;
25 using std::istream;
26 using std::endl;
27
28
29 LyXLex::LyXLex(keyword_item * tab, int num)
30         : pimpl_(new Pimpl(tab, num))
31 {}
32
33
34 LyXLex::~LyXLex() 
35 {
36         delete pimpl_;
37 }
38
39
40 bool LyXLex::IsOK() const
41 {
42         return pimpl_->is.good();
43 }
44
45
46 void LyXLex::setLineNo(int l)
47 {
48         pimpl_->lineno = l;
49 }
50
51
52 int LyXLex::GetLineNo() const
53 {
54         return pimpl_->lineno;
55 }
56
57
58 char const * const LyXLex::text() const
59 {
60         return &pimpl_->buff[0];
61 }
62
63
64 std::istream & LyXLex::getStream()
65 {
66         return pimpl_->is;
67 }
68
69
70 void LyXLex::pushTable(keyword_item * tab, int num)
71 {
72         pimpl_->pushTable(tab, num);
73 }
74
75
76 void LyXLex::popTable()
77 {
78         pimpl_->popTable();
79 }
80
81
82 void LyXLex::printTable(ostream & os)
83 {
84         pimpl_->printTable(os);
85 }
86
87
88 void LyXLex::printError(string const & message) const
89 {
90         pimpl_->printError(message);
91 }
92
93
94 bool LyXLex::setFile(string const & filename)
95 {
96         return pimpl_->setFile(filename);
97 }
98
99
100 void LyXLex::setStream(istream & i)
101 {
102         pimpl_->setStream(i);
103 }
104
105
106 int LyXLex::lex()
107 {
108         return pimpl_->lex();
109 }
110
111
112 int LyXLex::GetInteger() const
113 {
114         if (pimpl_->buff[0] > ' ')   
115                 return atoi(pimpl_->buff);
116         else {
117                 pimpl_->printError("Bad integer `$$Token'");
118                 return -1;
119         }
120 }
121
122
123 float LyXLex::GetFloat() const
124 {
125    if (pimpl_->buff[0] > ' ')
126        return atof(pimpl_->buff);
127    else {
128         pimpl_->printError("Bad float `$$Token'");
129         return -1;
130    }
131 }
132
133
134 string const LyXLex::GetString() const
135 {
136         return pimpl_->GetString();
137 }
138
139
140 // I would prefer to give a tag number instead of an explicit token
141 // here, but it is not possible because Buffer::readLyXformat2 uses
142 // explicit tokens (JMarc) 
143 string const LyXLex::getLongString(string const & endtoken)
144 {
145         string str, prefix;
146         bool firstline = true;
147
148         while (IsOK()) {
149                 if (!EatLine())
150                         // blank line in the file being read
151                         continue;
152                 
153                 string const token = frontStrip(strip(GetString()), " \t");
154                 
155                 lyxerr[Debug::PARSER] << "LongString: `"
156                                       << GetString() << '\'' << endl;
157
158                 // We do a case independent comparison, like search_kw
159                 // does.
160                 if (compare_no_case(token, endtoken) != 0) {
161                         string tmpstr = GetString();
162                         if (firstline) {
163                                 unsigned int i = 0;
164                                 while(i < tmpstr.length()
165                                       && tmpstr[i] == ' ') {
166                                         ++i;
167                                         prefix += ' ';
168                                 }
169                                 firstline = false;
170                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
171                                                       << '\'' << endl;
172                         } 
173
174                         if (!prefix.empty() 
175                             && prefixIs(tmpstr, prefix.c_str())) {
176                                 tmpstr.erase(0, prefix.length() - 1);
177                         }
178                         str += frontStrip(tmpstr, "\t") + '\n';
179                 }
180                 else // token == endtoken
181                         break;
182         }
183         if (!IsOK())
184                 printError("Long string not ended by `" + endtoken + '\'');
185
186         return str;
187 }
188
189
190 bool LyXLex::GetBool() const
191 {
192         if (compare(pimpl_->buff, "true") == 0)
193                 return true;
194         else if (compare(pimpl_->buff, "false") != 0)
195                 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
196         return false;
197 }
198
199
200 bool LyXLex::EatLine()
201 {
202         return pimpl_->EatLine();
203 }
204
205
206 bool LyXLex::next(bool esc)
207 {
208         return pimpl_->next(esc);
209 }
210
211
212 bool LyXLex::nextToken()
213 {
214         return pimpl_->nextToken();
215 }
216
217
218 void LyXLex::pushToken(string const & pt)
219 {
220         pimpl_->pushToken(pt);
221 }
222
223
224 int LyXLex::FindToken(char const * str[])
225 {  
226    int i = -1;
227    
228    if (next()) {
229       if (compare(pimpl_->buff, "default")) {
230          for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
231          if (!str[i][0]) {
232             pimpl_->printError("Unknown argument `$$Token'");
233             i = -1;
234          }
235       }  
236    } else
237      pimpl_->printError("file ended while scanning string token");
238    return i;
239 }
240
241
242 int LyXLex::CheckToken(char const * str[], int print_error)
243 {  
244    int i = -1;
245    
246    if (compare(pimpl_->buff, "default")) {
247        for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
248        if (!str[i][0]) {
249            if (print_error)
250                pimpl_->printError("Unknown argument `$$Token'");
251            i = -1;
252        }
253    }
254    return i;
255 }