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
Instance methods
Iterates over each source character.
location is incremented between each character.
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
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.
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) }