Skip to content

Commit

Permalink
Integer parser implementation
Browse files Browse the repository at this point in the history
Integer parser implementation to support: restarone/violet_rails#1424
  • Loading branch information
donrestarone authored Feb 23, 2023
2 parents 456fb85 + 637d15b commit ccf9415
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/comfortable_mexican_sofa/content/params_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Error < StandardError; end
HASH_CLOSE = %r{\}}
ARRAY_OPEN = %r{\[}
ARRAY_CLOSE = %r{\]}
INTEGER = %r{\b[0-9]+\b}i

# @param <String> string
def initialize(string = "")
Expand Down Expand Up @@ -133,6 +134,7 @@ def tokenize(args_string)
tokens <<
if (t = ss.scan(STRING_LITERAL)) then [:string, t[1...-1]]
elsif (t = ss.scan(HASH_KEY)) then [:hash_key, t[0...-1]]
elsif (t = ss.scan(INTEGER)) then [:string, t.to_i]
elsif (t = ss.scan(IDENTIFIER)) then [:string, t]
elsif (t = ss.scan(HASH_OPEN)) then [:hash_open, t]
elsif (t = ss.scan(HASH_CLOSE)) then [:hash_close, t]
Expand Down
12 changes: 12 additions & 0 deletions test/lib/content/params_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ def test_tokenizer_with_arrays
], tokens
end

def test_tokenizer_with_arrays_containing_numbers
tokens = PARSER.new.send(:tokenize, "arr: [1, 2, 3]")
assert_equal [
[:hash_key, "arr"],
[:array_open, "["],
[:string, "1".to_i],
[:string, "2".to_i],
[:string, "3".to_i],
[:array_close, "]"]
], tokens
end

def test_tokenizer_with_quoted_value
tokens = PARSER.new.send(:tokenize, "key: ''")
assert_equal [[:hash_key, "key"], [:string, ""]], tokens
Expand Down

0 comments on commit ccf9415

Please sign in to comment.