Class: SXP::Reader::Basic
Overview
A basic S-expression parser.
Constant Summary
collapse
- LPARENS =
[?(]
- RPARENS =
[?)]
- ATOM =
/^[^\s()]+/
- RATIONAL =
/^([+-]?\d+)\/(\d+)$/
- DECIMAL =
/^[+-]?(\d*)?\.\d*$/
- INTEGER =
/^[+-]?\d+$/
Instance Attribute Summary
Attributes inherited from SXP::Reader
#input, #options
Instance Method Summary
collapse
Methods inherited from SXP::Reader
#each, #initialize, read, #read, read_all, #read_all, read_file, #read_files, #read_integer, #read_list, #read_sharp, read_url
Constructor Details
This class inherits a constructor from SXP::Reader
Instance Method Details
#read_atom ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/sxp/reader/basic.rb', line 25
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.end_with?('.') ? "#{buffer}0" : buffer)
when INTEGER then Integer(buffer)
else buffer.to_sym
end
end
|
#read_character ⇒ String
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/sxp/reader/basic.rb', line 53
def read_character
case char = read_char
when ?b then ?\b
when ?f then ?\f
when ?n then ?\n
when ?r then ?\r
when ?t then ?\t
when ?u then read_chars(4).to_i(16).chr(Encoding::UTF_8)
when ?U then read_chars(8).to_i(16).chr(Encoding::UTF_8)
when ?" then char when ?\\ then char
else char
end
end
|
#read_literal ⇒ String
70
71
72
73
74
75
|
# File 'lib/sxp/reader/basic.rb', line 70
def read_literal
grammar = self.class.const_get(:ATOM)
buffer = ""
buffer << read_char while !eof? && peek_char.chr =~ grammar
buffer
end
|
#read_string ⇒ String
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/sxp/reader/basic.rb', line 37
def read_string
buffer = ""
skip_char until peek_char == ?" buffer <<
case char = read_char
when ?\\ then read_character
else char
end
end
skip_char buffer
end
|
#read_token ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/sxp/reader/basic.rb', line 15
def read_token
case peek_char
when ?(, ?) then [:list, read_char]
when ?" then [:atom, read_string] else super
end
end
|