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