3 * Implementation of GiNaC's lexer. */
6 * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 /// Skip to the end of line
33 static int skipline(std::istream* s);
34 /// Skip to the next non-whitespace character
35 static int skipspace(std::istream* s, int c, std::size_t& line);
36 /// Check if the identifier is predefined literal
37 static bool literal_p(const std::string& name);
39 /// gettok - Return the next token from standard input.
42 // Skip any whitespace.
43 c = skipspace(input, c, line_num);
45 // identifier: [a-zA-Z][a-zA-Z0-9]*
55 if (unlikely(literal_p(str)))
56 return token_type::literal;
58 return token_type::identifier;
61 // Number: [0-9]+([.][0-9]*(eE[+-][0-9]+)*)*
62 if (isdigit(c) || c == '.') {
67 } while (isdigit(c) || c == '.');
68 if (c == 'E' || c == 'e') {
78 return token_type::number;
81 // Comment until end of line.
89 // Check for end of file. Don't eat the EOF.
91 return token_type::eof;
93 // Otherwise, just return the character as its ascii value.
99 static int skipline(std::istream* s)
104 } while (c != EOF && c != '\n' && c != '\r');
108 static int skipspace(std::istream* s, int c, std::size_t& line)
118 static bool literal_p(const std::string& name)
122 else if (name == "Pi")
124 else if (name == "Euler")
126 else if (name == "Catalan")
132 lexer::lexer(std::istream* in, std::ostream* out, std::ostream* err)
157 void lexer::switch_input(std::istream* in)
165 /// Symbolic name of current token (for error reporting)
166 std::string lexer::tok2str(const int tok) const
169 case lexer::token_type::identifier:
170 case lexer::token_type::number:
171 return std::string("\"") + str + "\"";
172 case lexer::token_type::eof:
173 return std::string("EOF");
175 return std::string("\"") + char(tok) + "\"";