]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
- remove some unused code
[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 std::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 = frontStrip(strip(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_no_case(token, endtoken) != 0) {
172                         string tmpstr = getString();
173                         if (firstline) {
174                                 unsigned int i = 0;
175                                 while(i < tmpstr.length()
176                                       && tmpstr[i] == ' ') {
177                                         ++i;
178                                         prefix += ' ';
179                                 }
180                                 firstline = false;
181                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
182                                                       << '\'' << endl;
183                         } 
184
185                         if (!prefix.empty() 
186                             && prefixIs(tmpstr, prefix)) {
187                                 tmpstr.erase(0, prefix.length() - 1);
188                         }
189                         str += frontStrip(tmpstr, "\t") + '\n';
190                 }
191                 else // token == endtoken
192                         break;
193         }
194         if (!isOK())
195                 printError("Long string not ended by `" + endtoken + '\'');
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         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    int i = -1;
238    
239    if (next()) {
240       if (compare(pimpl_->buff, "default")) {
241          for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
242          if (!str[i][0]) {
243             pimpl_->printError("Unknown argument `$$Token'");
244             i = -1;
245          }
246       }  
247    } else
248      pimpl_->printError("file ended while scanning string token");
249    return i;
250 }
251
252
253 int LyXLex::checkToken(char const * str[], int print_error)
254 {  
255    int i = -1;
256    
257    if (compare(pimpl_->buff, "default")) {
258        for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
259        if (!str[i][0]) {
260            if (print_error)
261                pimpl_->printError("Unknown argument `$$Token'");
262            i = -1;
263        }
264    }
265    return i;
266 }