allow for single character identifiers

FossilOrigin-Name: 6f663cda900d2517ff10ddcf74b1e294e3f07cb415bf7d00157b36fe7c780b72
private
alpha 2 years ago
parent 5033f48af8
commit b4e4edfe8c

@ -117,7 +117,7 @@ module Lox
scan_str(state) scan_str(state)
when number = state.scan(/\d+(\.\d+)?/) when number = state.scan(/\d+(\.\d+)?/)
state.add_token(:NUMBER, literal: number.to_f) 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) type = KEYWORDS.fetch(identifier, :IDENTIFIER)
state.add_token(type) state.add_token(type)
else state.scan(/./) # keep scanning else state.scan(/./) # keep scanning

@ -111,8 +111,9 @@ class TestScanner < Minitest::Test
assert_equal [ assert_equal [
Token.new(:OR, "or", nil, 1), Token.new(:OR, "or", nil, 1),
Token.new(:IDENTIFIER, "orchid", nil, 1), Token.new(:IDENTIFIER, "orchid", nil, 1),
Token.new(:IDENTIFIER, "o", nil, 1),
Token.new(:EOF, "", nil, 1), Token.new(:EOF, "", nil, 1),
], @scanner.scan("or orchid") ], @scanner.scan("or orchid o")
end end
def test_block_comments def test_block_comments

Loading…
Cancel
Save