X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fparser%2Flexer.cpp;h=cc3be3d91768c1f930b89bd71b12ca8a0672e410;hp=ed1f894fea381bdc9c553d9a2811ff12b1697bcb;hb=44f71c740cc727dc30436e240b9a27021a6d7fdf;hpb=1222eac51cee964961d2aad889dc4ceccb144a36 diff --git a/ginac/parser/lexer.cpp b/ginac/parser/lexer.cpp index ed1f894f..cc3be3d9 100644 --- a/ginac/parser/lexer.cpp +++ b/ginac/parser/lexer.cpp @@ -1,11 +1,35 @@ -#include +/** @file lexer.cpp + * + * Implementation of GiNaC's lexer. */ + +/* + * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "lexer.h" +#include "compiler.h" + #include #include -#include "lexer.hpp" -#include "compiler.h" +#include +#include + +namespace GiNaC { -namespace GiNaC -{ /// Skip to the end of line static int skipline(std::istream* s); /// Skip to the next non-whitespace character @@ -19,12 +43,12 @@ int lexer::gettok() // Skip any whitespace. c = skipspace(input, c, line_num); - // identifier: [a-zA-Z][a-zA-Z0-9]* + // identifier: [a-zA-Z][a-zA-Z0-9_]* if (isalpha(c)) { str = c; do { c = input->get(); - if (isalnum(c)) + if ( isalnum(c) || c=='_' ) str += c; else break; @@ -35,13 +59,23 @@ int lexer::gettok() return token_type::identifier; } - // Number: [0-9.]+ + // Number: [0-9]+([.][0-9]*(eE[+-][0-9]+)*)* if (isdigit(c) || c == '.') { str = ""; do { str += c; c = input->get(); } while (isdigit(c) || c == '.'); + if (c == 'E' || c == 'e') { + str += 'E'; + c = input->get(); + if (isdigit(c)) + str += '+'; + do { + str += c; + c = input->get(); + } while (isdigit(c)); + } return token_type::number; } @@ -126,7 +160,21 @@ void lexer::switch_input(std::istream* in) input = in; line_num = 0; column = 0; + c = ' '; } -} // namespace GiNaC +/// Symbolic name of current token (for error reporting) +std::string lexer::tok2str(const int tok) const +{ + switch (tok) { + case lexer::token_type::identifier: + case lexer::token_type::number: + return std::string("\"") + str + "\""; + case lexer::token_type::eof: + return std::string("EOF"); + default: + return std::string("\"") + char(tok) + "\""; + } +} +} // namespace GiNaC