1 /* This file is part of EdiTeX, an editor of mathematical
2 * expressions based on TeX syntax.
4 * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5 * 2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * For more information, please visit the project's home page
22 * http://helm.cs.unibo.it/editex/
23 * or send an email to <lpadovan@cs.unibo.it>
32 #include "LPushLexer.hh"
33 #include "APushParser.hh"
35 LPushLexer::LPushLexer(ALogger& l, APushParser& p) : APushLexer(l, p)
56 LPushLexer::transaction(char ch, State newState)
60 case '{': parser.push(TToken(TToken::BEGIN)); break;
61 case '}': parser.push(TToken(TToken::END)); break;
62 case '$': parser.push(TToken(TToken::SHIFT)); break;
63 case '&': parser.push(TToken(TToken::ALIGN)); break;
65 case '\r': parser.push(TToken(TToken::EOL, ch)); break;
66 case '^': parser.push(TToken(TToken::SUPERSCRIPT)); break;
67 case '_': parser.push(TToken(TToken::SUBSCRIPT)); break;
68 case '\t': parser.push(TToken(TToken::IGNORABLE_SPACE, ch)); break;
69 case ' ': parser.push(TToken(TToken::SPACE, ch)); break;
70 case '~': parser.push(TToken(TToken::ACTIVE, ch)); break;
71 case '%': parser.push(TToken(TToken::COMMENT)); break;
72 default: parser.push(TToken(TToken::OTHER, ch)); break;
78 LPushLexer::push(char ch)
83 if (ch == '\\') state = ESCAPE;
84 else if (ch == '#') state = PARAMETER;
96 else transaction(ch, ACCEPT);
101 buffer.push_back(ch);
104 else if (ch == -1) error();
105 else if (isdigit(ch))
107 // in this case, the previous '\' is ignored
108 buffer.push_back(ch);
113 parser.push(TToken(TToken::CONTROL, ch));
120 parser.push(TToken(TToken::CONTROL, buffer));
126 parser.push(TToken(TToken::CONTROL, buffer));
130 else if (isalpha(ch))
131 buffer.push_back(ch);
134 parser.push(TToken(TToken::CONTROL, buffer));
138 else if (isspace(ch))
140 // we don't call transaction, because a white space is useful to exit from the macro,
141 // without "side effect". It's the TeX syntax.
142 parser.push(TToken(TToken::CONTROL, buffer));
146 else if (isdigit(ch))
148 parser.push(TToken(TToken::CONTROL, buffer));
150 buffer.push_back(ch);
155 parser.push(TToken(TToken::CONTROL, buffer));
157 transaction(ch, ACCEPT);
161 if (ch == -1) error();
164 parser.push(TToken(TToken::PARAMETER, ch));
171 parser.push(TToken(TToken::LETTER, buffer));
175 else if (isalpha(ch) || isdigit(ch))
177 buffer.push_back(ch);
179 else if (ch == '\\') state = ESCAPED_CHARACTER;
182 parser.push(TToken(TToken::LETTER, buffer));
188 parser.push(TToken(TToken::LETTER, buffer));
190 transaction(ch, ACCEPT);
193 case ESCAPED_CHARACTER:
194 if ((ch == '-') || (ch == '_') || (ch == '/'))
196 buffer.push_back(ch);
199 else if (isalpha(ch))
201 parser.push(TToken(TToken::LETTER, buffer));
203 buffer.push_back(ch);
206 else if (ch == -1) error();
207 else if (isdigit(ch))
209 parser.push(TToken(TToken::LETTER, buffer));
211 buffer.push_back(ch);
216 parser.push(TToken(TToken::LETTER, buffer));
218 parser.push(TToken(TToken::CONTROL, ch));
223 if (isdigit(ch)) buffer.push_back(ch);
224 else if (isalpha(ch))
226 parser.push(TToken(TToken::DIGIT, buffer));
228 buffer.push_back(ch);
233 parser.push(TToken(TToken::DIGIT, buffer));
239 parser.push(TToken(TToken::DIGIT, buffer));
245 parser.push(TToken(TToken::DIGIT, buffer));
251 parser.push(TToken(TToken::DIGIT, buffer));
253 transaction(ch, ACCEPT);
266 LPushLexer::drop(bool alt)
268 std::string restore = "";
274 restore = parser.drop(alt);
275 long bs_pos = restore.find('\\');
276 if ((restore.length() > 0) && (bs_pos != std::string::npos))
278 // in this case we have to control the blackslash's position
281 //logger.debug(restore);
282 buffer = std::string(restore, 1, restore.length() - 1);
283 state = (buffer.length() > 0) ? MACRO : ESCAPE;
287 assert(bs_pos == restore.length() - 1);
288 buffer = std::string(restore, 0, bs_pos);
289 state = ESCAPED_CHARACTER;
292 else if (restore.length() > 0 && isdigit(restore[0]))
297 else if (restore.length() > 0 && isalpha(restore[0]))
304 /* if (restore.length() > 0 && restore[0] == '\\')
306 logger.debug(restore);
307 buffer = std::string(restore, 1, restore.length() - 1);
308 state = (buffer.length() > 0) ? MACRO : ESCAPE;
310 else if (restore.length() > 0 && isdigit(restore[0]))
315 else if (restore.length() > 0 && isalpha(restore[0]))
321 case ESCAPED_CHARACTER:
328 if (alt) buffer.erase();
329 else buffer.erase(buffer.length() - 1, 1);
330 if (buffer.length() == 0) state = ESCAPE;
333 switch (buffer[buffer.length() - 1])
337 buffer.erase(buffer.length() - 1, 1);
338 if (alt) state = ESCAPED_CHARACTER;
341 if (alt) buffer.erase();
342 else buffer.erase(buffer.length() - 1, 1);
343 if (buffer.length() == 0) state = ACCEPT;
348 if (alt) buffer.erase();
349 else buffer.erase(buffer.length() - 1, 1);
350 if (buffer.length() == 0) state = ACCEPT;
364 LPushLexer::displayCursor()
368 case ESCAPE: parser.setCursorHint("\\"); break;
369 case ESCAPED_CHARACTER: parser.setCursorHint(buffer + "\\"); break;
370 case MACRO: parser.setCursorHint("\\" + buffer); break;
371 case PARAMETER: parser.setCursorHint("#"); break;
372 case IDENTIFIER: parser.setCursorHint(buffer); break;
373 case NUMBER: parser.setCursorHint(buffer); break;
374 default: parser.setCursorHint(""); break;
379 LPushLexer::error() const