Skip to content

Commit

Permalink
Parse integers
Browse files Browse the repository at this point in the history
  • Loading branch information
emilywoods authored and KITSTABEmilyWoods committed Feb 19, 2017
1 parent aac7b5f commit 6af1375
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/emerald/parser.rb
Expand Up @@ -23,7 +23,9 @@ def parse_node(source)
when " "
parse_whitespace(source)
when /[a-zA-Z\-]/
parse_atom(source)
parse_atom(source,first_char)
when /\d+/
parse_atom(source, first_char)
end
end

Expand All @@ -34,8 +36,13 @@ def parse_whitespace(source)
[nil, rest_of_source]
end

def parse_atom(source)
pattern = /\A[a-z\-]+/
def parse_atom(source, non_whitespace_char)
case non_whitespace_char
when /\A[a-zA-Z\-]+/
pattern = /\A[a-zA-Z\-]+/
when /\d+/
pattern = /\d+/
end
atom_value = pattern.match(source).to_s
atom = Atom.new(atom_value)
rest_of_source = drop(source, atom_value.size)
Expand Down
11 changes: 9 additions & 2 deletions spec/emerald/parser_spec.rb
Expand Up @@ -16,10 +16,17 @@
end

it "parses two atoms" do
source = "hello world"
source = "hello World"
ast = Emerald::Parser.new(source).parse
expect(ast).to eq([Emerald::Atom.new("hello"),
Emerald::Atom.new("world")])
Emerald::Atom.new("World")])
end

it "parses two atoms: a string and an integer" do
source = "hey 65"
ast = Emerald::Parser.new(source).parse
expect(ast).to eq([Emerald::Atom.new("hey"),
Emerald::Atom.new("65")])
end
end
end

0 comments on commit 6af1375

Please sign in to comment.