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