4 # Creates a parser using the specified object as input.
5 # The input object must support a method +getc+, which must
6 # return the next character of the input, or +nil+ to indicate
7 # the end of the input has been reached.
15 # Consumes the current lookahead character
22 @lookahead = @input.getc
23 @char = @char.succ unless @lookahead == nil
27 # Returns the current lookahead character,
28 # or +nil+ when the end of the input has been reached.
31 @lookahead = @input.getc
34 @lookahead == nil ? nil : @lookahead.chr
37 # Parses a line of input.
38 # Returns an array containing the parts of the input line.
39 # Eeach element of the array is a Symbol, a String, or an
51 # Newline; we're done!
56 while lookahead != nil && lookahead != "\n"
64 # Letter, underscore, or backslash; parse symbol
65 # Note: \w matches digites, too, so keep this case after \d
68 # Double quote; parse string
74 raise "Invalid character (#{@lookahead}) at #{@line}:#{@char}"
77 (!words.empty? || @lookahead) ? words : nil
80 # Parses an escape sequence.
81 # This method should be called while the lookahead is the escape
82 # character (backslash). It decodes the escape sequence and returns
83 # the result as a string.
89 raise "Unexpected end of input in escape sequence"
98 # \r is carriage return
102 # \xXX is byte with hex value XX
106 result = [code].pack('H2')
108 # \<newline> is line continuation character
110 # Skip indentation of next line
111 while lookahead =~ /\s/
116 # Default to just passing on next character
124 # This method should be called while the lookahead is the first
125 # character of the number.
129 while lookahead =~ /\d/
137 # This method should be called while the lookahead is the opening
148 result << parse_escape
158 # This method should be called while the lookahead is the first
159 # character of the symbol name.
170 # Colon parsed as last character of the symbol name