Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Parsing for character literals
Browse files Browse the repository at this point in the history
Parse to single-character string.
Tests included
  • Loading branch information
JD Huntington committed Mar 20, 2008
1 parent 31e3f35 commit a1111ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 13 additions & 0 deletions lib/parser.rb
Expand Up @@ -88,6 +88,19 @@ def pop_token(input)
# Official Scheme valid identifiers:
# when /\A([A-Za-z!\$%&\*\.\/:<=>\?@\^_~][A-Za-z0-9!\$%&\*\+\-\.\/:<=>\?@\^_~]*)/ # symbol
# when /\A([^-0-9\. \n\)][^ \n\)]*)/
when /\A#\\(.)/ # Character literal
char = Regexp.last_match[1]
input[0 ... 2] = ''
if input[0 ... 5] == 'space'
input[0 ... 5] = ''
char = ' '
elsif input[0 ... 7] == 'newline'
input[0 ... 7] = ''
char = "\n"
else
input[0 ... 1] = ''
end
return char
when /\A([^ \n\)]+)/ # symbols
# puts "#{Regexp.last_match[1]} - #{@@lines}"
# cannot begin with a character that may begin a number
Expand Down
15 changes: 8 additions & 7 deletions test/test_parser.rb
Expand Up @@ -106,13 +106,14 @@ def test_explicitly_positive_floats
assert_parses_to "+0.10", 0.1
end

# def test_character_literals
# assert_parses_to "#\e", "e"
# assert_parses_to "#\A", "A"
# assert_parses_to "#\(", "("
# assert_parses_to "#\space", ' '
# assert_parses_to "#\newline", "\n"
# end
def test_character_literals
# must escape ruby string with backslask
assert_parses_to "#\\e", "e"
assert_parses_to "#\\A", "A"
assert_parses_to "#\\(", "("
assert_parses_to "#\\space", ' '
assert_parses_to "#\\newline", "\n"
end

def test_quote
assert_parses_to "'foo", [:quote.sym, :foo.sym]
Expand Down

0 comments on commit a1111ac

Please sign in to comment.