From b4e4edfe8c1f539bae159064b4de6b2c7e418d79 Mon Sep 17 00:00:00 2001 From: alpha Date: Mon, 18 Jul 2022 02:12:18 +0000 Subject: [PATCH] allow for single character identifiers FossilOrigin-Name: 6f663cda900d2517ff10ddcf74b1e294e3f07cb415bf7d00157b36fe7c780b72 --- ruby/lox.rb | 2 +- ruby/test_lox.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ruby/lox.rb b/ruby/lox.rb index 851d100..9ac7357 100755 --- a/ruby/lox.rb +++ b/ruby/lox.rb @@ -117,7 +117,7 @@ module Lox scan_str(state) when number = state.scan(/\d+(\.\d+)?/) state.add_token(:NUMBER, literal: number.to_f) - when identifier = state.scan(/[a-zA-Z_]\w+/) + when identifier = state.scan(/[a-zA-Z_]\w*/) type = KEYWORDS.fetch(identifier, :IDENTIFIER) state.add_token(type) else state.scan(/./) # keep scanning diff --git a/ruby/test_lox.rb b/ruby/test_lox.rb index a3ff067..6114659 100644 --- a/ruby/test_lox.rb +++ b/ruby/test_lox.rb @@ -111,8 +111,9 @@ class TestScanner < Minitest::Test assert_equal [ Token.new(:OR, "or", nil, 1), Token.new(:IDENTIFIER, "orchid", nil, 1), + Token.new(:IDENTIFIER, "o", nil, 1), Token.new(:EOF, "", nil, 1), - ], @scanner.scan("or orchid") + ], @scanner.scan("or orchid o") end def test_block_comments