class

TopDown::CharReader

Inherits Reference < Object

Base class of a Parser.

It composed from a source and can read or peek characters taking in account a complete Location on the source.

Constructors

new(source : String)

Creates a new CharReader (or Parser), initialized to read the source.

Source

Instance methods

each_char

Iterates over each source character.

location is incremented between each character.

Source
location

Returns the current cursor location.

The location can be used later to raise an error at that point.

loc = self.location
parse('(')
exp = parse!(:expression)
parse!(')', error: "Unterminated parenthesis expression", at: loc)
exp
Source
location=(location : Location)

Move the cursor to the new location.

The location should be well formed, otherwise error display won't be right. It is recommended to always use a location got by self.location.

This methods is used to backtrack the parser. However, prefer using Parser.union, Parser.maybe, and Parser.repeat over manual backtracks.

Source
next_assci_char?
Source
next_char

Returns the next character to parse, and increments the cursor location.

Source
peek_char

Returns the next character to parse, without incrementing the cursor location.

This method is currently the only way to look ahead during the parsing. It allow for instance:

parse("if") do
  break Fail.new if peek_char.alphanumeric?
  Token.new(:if)
end
# as an equivalent to:
parse(/if\b/) { Token.new(:if) }
Source
previous_assci_char?
Source
source
Source
source=(source : String)

Modifies the source and reset the cursor location to zero.

Source