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