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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 The latter could return an |
||
atom = Atom.new(atom_value) | ||
rest_of_source = drop(source, atom_value.size) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.