Skip to content

Commit

Permalink
css2: Support tokenizing numbers w/ starting w/ sign and a decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Sep 29, 2024
1 parent 1ba2662 commit 04f1114
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions css2/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void Tokenizer::run() {
emit(CloseParenToken{});
continue;
case '+': {
// TODO(robinlinden): This only handles integers.
if (inputs_starts_number(*c)) {
auto number = consume_number(*c);
emit(NumberToken{number});
Expand All @@ -83,7 +82,6 @@ void Tokenizer::run() {
emit(CommaToken{});
continue;
case '-': {
// TODO(robinlinden): This only handles integers.
if (inputs_starts_number(*c)) {
auto number = consume_number(*c);
emit(NumberToken{number});
Expand Down Expand Up @@ -367,11 +365,21 @@ bool Tokenizer::inputs_starts_ident_sequence(char first_character) const {
bool Tokenizer::inputs_starts_number(char first_character) const {
assert(first_character == '-' || first_character == '+');

if (auto next_input = peek_input(0); next_input && util::is_digit(*next_input)) {
auto next_input = peek_input(0);
if (!next_input) {
return false;
}

if (util::is_digit(*next_input)) {
return true;
}

return false;
auto next_next_input = peek_input(1);
if (!next_next_input) {
return false;
}

return next_input == '.' && util::is_digit(*next_next_input);
}

bool Tokenizer::is_eof() const {
Expand Down
16 changes: 16 additions & 0 deletions css2/tokenizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ int main() {
expect_token(output, NumberToken{13.25});
});

s.add_test("number: negative, no digits before decimal point", [](etest::IActions &a) {
auto output = run_tokenizer(a, "-.25");
expect_token(output, NumberToken{-.25});
});

s.add_test("number: with +, no digits before decimal point", [](etest::IActions &a) {
auto output = run_tokenizer(a, "+.25");
expect_token(output, NumberToken{.25});
});

s.add_test("number: negative, abrupt end", [](etest::IActions &a) {
auto output = run_tokenizer(a, "-.");
expect_token(output, DelimToken{'-'});
expect_token(output, DelimToken{'.'});
});

s.add_test("number: no digits before decimal point", [](etest::IActions &a) {
auto output = run_tokenizer(a, ".25");
expect_token(output, NumberToken{.25});
Expand Down

0 comments on commit 04f1114

Please sign in to comment.