]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
get builddir!=srcdir compiling working; allow successful make even without noweb...
[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 int LyXLex::lex()
108 {
109         return pimpl_->lex();
110 }
111
112
113 int LyXLex::GetInteger() const
114 {
115         if (isStrInt(pimpl_->GetString()))
116                 return strToInt(pimpl_->GetString());
117         else {
118                 pimpl_->printError("Bad integer `$$Token'");
119                 return -1;
120         }
121 }
122
123
124 float LyXLex::GetFloat() const
125 {
126         // replace comma with dot in case the file was written with
127         // the wrong locale (should be rare, but is easy enough to
128         // avoid). 
129         string str = subst(pimpl_->GetString(), ",", ".");
130         if (isStrDbl(str))
131                 return strToDbl(str);
132         else {
133                 pimpl_->printError("Bad float `$$Token'");
134                 return -1;
135         }
136 }
137
138
139 string const LyXLex::GetString() const
140 {
141         return pimpl_->GetString();
142 }
143
144
145 // I would prefer to give a tag number instead of an explicit token
146 // here, but it is not possible because Buffer::readLyXformat2 uses
147 // explicit tokens (JMarc) 
148 string const LyXLex::getLongString(string const & endtoken)
149 {
150         string str, prefix;
151         bool firstline = true;
152
153         while (IsOK()) {
154                 if (!EatLine())
155                         // blank line in the file being read
156                         continue;
157                 
158                 string const token = frontStrip(strip(GetString()), " \t");
159                 
160                 lyxerr[Debug::PARSER] << "LongString: `"
161                                       << GetString() << '\'' << endl;
162
163                 // We do a case independent comparison, like search_kw
164                 // does.
165                 if (compare_no_case(token, endtoken) != 0) {
166                         string tmpstr = GetString();
167                         if (firstline) {
168                                 unsigned int i = 0;
169                                 while(i < tmpstr.length()
170                                       && tmpstr[i] == ' ') {
171                                         ++i;
172                                         prefix += ' ';
173                                 }
174                                 firstline = false;
175                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
176                                                       << '\'' << endl;
177                         } 
178
179                         if (!prefix.empty() 
180                             && prefixIs(tmpstr, prefix)) {
181                                 tmpstr.erase(0, prefix.length() - 1);
182                         }
183                         str += frontStrip(tmpstr, "\t") + '\n';
184                 }
185                 else // token == endtoken
186                         break;
187         }
188         if (!IsOK())
189                 printError("Long string not ended by `" + endtoken + '\'');
190
191         return str;
192 }
193
194
195 bool LyXLex::GetBool() const
196 {
197         if (compare(pimpl_->buff, "true") == 0)
198                 return true;
199         else if (compare(pimpl_->buff, "false") != 0)
200                 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
201         return false;
202 }
203
204
205 bool LyXLex::EatLine()
206 {
207         return pimpl_->EatLine();
208 }
209
210
211 bool LyXLex::next(bool esc)
212 {
213         return pimpl_->next(esc);
214 }
215
216
217 bool LyXLex::nextToken()
218 {
219         return pimpl_->nextToken();
220 }
221
222
223 void LyXLex::pushToken(string const & pt)
224 {
225         pimpl_->pushToken(pt);
226 }
227
228
229 int LyXLex::FindToken(char const * str[])
230 {  
231    int i = -1;
232    
233    if (next()) {
234       if (compare(pimpl_->buff, "default")) {
235          for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
236          if (!str[i][0]) {
237             pimpl_->printError("Unknown argument `$$Token'");
238             i = -1;
239          }
240       }  
241    } else
242      pimpl_->printError("file ended while scanning string token");
243    return i;
244 }
245
246
247 int LyXLex::CheckToken(char const * str[], int print_error)
248 {  
249    int i = -1;
250    
251    if (compare(pimpl_->buff, "default")) {
252        for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
253        if (!str[i][0]) {
254            if (print_error)
255                pimpl_->printError("Unknown argument `$$Token'");
256            i = -1;
257        }
258    }
259    return i;
260 }