Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse integers #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably keep the parser functions always taking the source as an arg, they can pull the first character off themselves if they want to.

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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be done in a nicer way, instead of another switch statement. Any suggestions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit muddled because numbers and atoms are parsed by the same method. How about a parse_atom and parse_number? We already know if it's an atom or number in the function above so it knows what one to pick.

The latter could return an Emerald::Number

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is an atom, not a string. Atoms are just names.

The second is a number, which isn't an atom :)

source = "hey 65"
ast = Emerald::Parser.new(source).parse
expect(ast).to eq([Emerald::Atom.new("hey"),
Emerald::Atom.new("65")])
end
end
end