]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
prepare for 1.1.6pre2
[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 #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 std::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 = frontStrip(strip(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_no_case(token, endtoken) != 0) {
171                         string tmpstr = GetString();
172                         if (firstline) {
173                                 unsigned int i = 0;
174                                 while(i < tmpstr.length()
175                                       && tmpstr[i] == ' ') {
176                                         ++i;
177                                         prefix += ' ';
178                                 }
179                                 firstline = false;
180                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
181                                                       << '\'' << endl;
182                         } 
183
184                         if (!prefix.empty() 
185                             && prefixIs(tmpstr, prefix)) {
186                                 tmpstr.erase(0, prefix.length() - 1);
187                         }
188                         str += frontStrip(tmpstr, "\t") + '\n';
189                 }
190                 else // token == endtoken
191                         break;
192         }
193         if (!IsOK())
194                 printError("Long string not ended by `" + endtoken + '\'');
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         return false;
207 }
208
209
210 bool LyXLex::EatLine()
211 {
212         return pimpl_->EatLine();
213 }
214
215
216 bool LyXLex::next(bool esc)
217 {
218         return pimpl_->next(esc);
219 }
220
221
222 bool LyXLex::nextToken()
223 {
224         return pimpl_->nextToken();
225 }
226
227
228 void LyXLex::pushToken(string const & pt)
229 {
230         pimpl_->pushToken(pt);
231 }
232
233
234 int LyXLex::FindToken(char const * str[])
235 {  
236    int i = -1;
237    
238    if (next()) {
239       if (compare(pimpl_->buff, "default")) {
240          for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
241          if (!str[i][0]) {
242             pimpl_->printError("Unknown argument `$$Token'");
243             i = -1;
244          }
245       }  
246    } else
247      pimpl_->printError("file ended while scanning string token");
248    return i;
249 }
250
251
252 int LyXLex::CheckToken(char const * str[], int print_error)
253 {  
254    int i = -1;
255    
256    if (compare(pimpl_->buff, "default")) {
257        for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
258        if (!str[i][0]) {
259            if (print_error)
260                pimpl_->printError("Unknown argument `$$Token'");
261            i = -1;
262        }
263    }
264    return i;
265 }