9 /// Skip to the end of line
10 static int skipline(std::istream* s);
11 /// Skip to the next non-whitespace character
12 static int skipspace(std::istream* s, int c, std::size_t& line);
13 /// Check if the identifier is predefined literal
14 static bool literal_p(const std::string& name);
16 /// gettok - Return the next token from standard input.
19 // Skip any whitespace.
20 c = skipspace(input, c, line_num);
22 // identifier: [a-zA-Z][a-zA-Z0-9]*
32 if (unlikely(literal_p(str)))
33 return token_type::literal;
35 return token_type::identifier;
39 if (isdigit(c) || c == '.') {
44 } while (isdigit(c) || c == '.');
45 return token_type::number;
48 // Comment until end of line.
56 // Check for end of file. Don't eat the EOF.
58 return token_type::eof;
60 // Otherwise, just return the character as its ascii value.
66 static int skipline(std::istream* s)
71 } while (c != EOF && c != '\n' && c != '\r');
75 static int skipspace(std::istream* s, int c, std::size_t& line)
85 static bool literal_p(const std::string& name)
89 else if (name == "Pi")
91 else if (name == "Euler")
93 else if (name == "Catalan")
99 lexer::lexer(std::istream* in, std::ostream* out, std::ostream* err)
124 void lexer::switch_input(std::istream* in)
132 /// Symbolic name of current token (for error reporting)
133 std::string lexer::tok2str(const int tok) const
136 case lexer::token_type::identifier:
137 case lexer::token_type::number:
138 return std::string("\"") + str + "\"";
139 case lexer::token_type::eof:
140 return std::string("EOF");
142 return std::string("\"") + char(tok) + "\"";