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