Class: SXP::Reader::Scheme
- Defined in:
- lib/sxp/reader/scheme.rb
Overview
A Scheme R4RS S-expressions parser.
Constant Summary collapse
- DECIMAL =
/^[+-]?(\d*)?\.\d*$/
- INTEGER_BASE_2 =
/^[+-]?[01]+$/
- INTEGER_BASE_8 =
/^[+-]?[0-7]+$/
- INTEGER_BASE_10 =
/^[+-]?\d+$/
- INTEGER_BASE_16 =
/^[+-]?[\da-z]+$/i
- RATIONAL =
/^([+-]?\d+)\/(\d+)$/
- CHARACTERS =
Escape characters, used in the form ‘#newline`. Case is treated insensitively
{ 'newline' => "\n", 'space' => " ", }
Constants inherited from Extended
Extended::ATOM, Extended::LPARENS, Extended::RPARENS
Constants inherited from Basic
Basic::ATOM, Basic::INTEGER, Basic::LPARENS, Basic::RPARENS
Instance Attribute Summary
Attributes inherited from SXP::Reader
Instance Method Summary collapse
-
#initialize(input, version: :r4rs, **options, &block) ⇒ Scheme
constructor
Initializes the reader.
- #read_atom ⇒ Object
-
#read_character ⇒ String
Read characters sequences like ‘#space`.
- #read_sharp ⇒ Object
- #read_token ⇒ Object
Methods inherited from Extended
Methods inherited from Basic
Methods inherited from SXP::Reader
#each, read, #read, read_all, #read_all, read_file, #read_files, #read_integer, #read_list, #read_literal, #read_string, read_url
Constructor Details
#initialize(input, version: :r4rs, **options, &block) ⇒ Scheme
Initializes the reader.
29 30 31 |
# File 'lib/sxp/reader/scheme.rb', line 29 def initialize(input, version: :r4rs, **, &block) super(input, version: version, **, &block) end |
Instance Method Details
#read_atom ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/sxp/reader/scheme.rb', line 44 def read_atom case buffer = read_literal when '.' then buffer.to_sym when RATIONAL then Rational($1.to_i, $2.to_i) when DECIMAL then Float(buffer) when INTEGER_BASE_10 then Integer(buffer) else buffer.to_sym end end |
#read_character ⇒ String
Read characters sequences like ‘#space`. Otherwise, reads a single character. Requires the ability to put eroneously read characters back in the input stream
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sxp/reader/scheme.rb', line 80 def read_character lit = read_literal return " " if lit.empty? && peek_char == " " CHARACTERS.fetch(lit.downcase) do # Return just the first character unread(lit[1..-1]) lit[0,1] end end |
#read_sharp ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sxp/reader/scheme.rb', line 56 def read_sharp skip_char # '#' case char = read_char when ?n, ?N then nil # not in Scheme per se when ?f, ?F then false when ?t, ?T then true when ?b, ?B then read_integer(2) when ?o, ?O then read_integer(8) when ?d, ?D then read_integer(10) when ?x, ?X then read_integer(16) when ?\\ then read_character when ?; then skip # comment character when ?! then skip_line; skip # shebang else raise Error, "invalid sharp-sign read syntax: ##{char.chr}" end end |
#read_token ⇒ Object
35 36 37 38 39 40 |
# File 'lib/sxp/reader/scheme.rb', line 35 def read_token case peek_char when ?# then [:atom, read_sharp] else super end end |