Skip to content
Snippets Groups Projects
Commit 1287f393 authored by Ben Cumming's avatar Ben Cumming Committed by GitHub
Browse files

Merge pull request #24 from vkarak/bugfix/modcc-scientific-notation

Fixes support of scientific notation in modcc.
parents eb74bd20 517aa5b6
No related branches found
No related tags found
No related merge requests found
...@@ -257,6 +257,10 @@ std::string Lexer::number() { ...@@ -257,6 +257,10 @@ std::string Lexer::number() {
uses_scientific_notation++; uses_scientific_notation++;
str += c; str += c;
current_++; current_++;
// Consume the next char if +/-
if (*current_ == '+' || *current_ == '-') {
str += *current_++;
}
} }
else { else {
break; break;
......
#include <cmath> #include <cmath>
#include <iterator>
#include "test.hpp" #include "test.hpp"
#include "lexer.hpp" #include "lexer.hpp"
...@@ -230,40 +231,36 @@ TEST(Lexer, comments) { ...@@ -230,40 +231,36 @@ TEST(Lexer, comments) {
// test numbers // test numbers
TEST(Lexer, numbers) { TEST(Lexer, numbers) {
char string[] = "1 .3 23 87.99 12. -3"; std::istringstream floats_stream("1 23 .3 87.99 12. 1.e3 1.2e+2 23e-3 -3");
PRINT_LEX_STRING
Lexer lexer(string, string+sizeof(string)); std::vector<double> floats;
std::copy(std::istream_iterator<double>(floats_stream),
auto t1 = lexer.parse(); std::istream_iterator<double>(),
EXPECT_EQ(t1.type, tok::number); std::back_inserter(floats));
EXPECT_EQ(std::stod(t1.spelling), 1.0);
Lexer lexer(floats_stream.str());
auto t2 = lexer.parse(); auto t = lexer.parse();
EXPECT_EQ(t2.type, tok::number); auto iter = floats.cbegin();
EXPECT_EQ(std::stod(t2.spelling), 0.3); while (t.type != tok::eof && iter != floats.cend()) {
EXPECT_EQ(lexerStatus::happy, lexer.status());
auto t3 = lexer.parse(); if (*iter < 0) {
EXPECT_EQ(t3.type, tok::number); // the lexer does not decide where the - sign goes
EXPECT_EQ(std::stod(t3.spelling), 23.0); // the parser uses additional contextual information to
// decide if the minus is a binary or unary expression
auto t4 = lexer.parse(); EXPECT_EQ(tok::minus, t.type);
EXPECT_EQ(t4.type, tok::number); t = lexer.parse();
EXPECT_EQ(std::stod(t4.spelling), 87.99); EXPECT_EQ(tok::number, t.type);
EXPECT_EQ(-(*iter), std::stod(t.spelling));
auto t5 = lexer.parse(); }
EXPECT_EQ(t5.type, tok::number); else {
EXPECT_EQ(std::stod(t5.spelling), 12.0); EXPECT_EQ(t.type, tok::number);
EXPECT_EQ(*iter, std::stod(t.spelling));
// the lexer does not decide where the - sign goes }
// the parser uses additional contextual information to
// decide if the minus is a binary or unary expression ++iter;
auto t6 = lexer.parse(); t = lexer.parse();
EXPECT_EQ(t6.type, tok::minus); }
auto t7 = lexer.parse(); EXPECT_EQ(floats.cend(), iter);
EXPECT_EQ(t7.type, tok::number); EXPECT_EQ(tok::eof, t.type);
EXPECT_EQ(std::stod(t7.spelling), 3.0);
auto t8 = lexer.parse();
EXPECT_EQ(t8.type, tok::eof);
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment