Class: Clojure::Parser

Inherits:
StringScanner
  • Object
show all
Includes:
StringParser
Defined in:
lib/cljdotrb.rb

Constant Summary collapse

INTEGER =
/([-+]?0)|([-+]?[1-9]\d*)(N)?/
HEX =
/([-+]?)0[xX]([0-9A-Fa-f]+)(N)?/
OCTAL =
/([-+]?)0([0-7]+)(N)?/
RADIX =
/([-+]?)([1-9][0-9]?)[rR]([0-9A-Za-z]+)/
RATIO =
/([-+]?[0-9]+)\/([0-9]+)/
FLOAT =
/([-+]?[0-9]+(\.[0-9]*)?([eE][-+]?[0-9]+)?)(M)?/
SYMBOLIC =
/([:]?)(([\D].*)(\/))?([\D][^\/]*)/
TRUE =
/true/
FALSE =
/false/
NIL =
/nil/
MAPO =
/\{/
MAPC =
/\}/
VECO =
/\[/
VECC =
/\]/
LISTO =
/\(/
LISTC =
/\)/
SET_START =
/#/
IGNORE =
/(?:[ \t\r\n,]+)/mx

Constants included from StringParser

StringParser::EMPTY_8BIT_STRING, StringParser::STRING, StringParser::UNESCAPE_MAP

Instance Method Summary collapse

Methods included from StringParser

#parse_string

Constructor Details

#initialize(src, opts = {}) ⇒ Parser

Returns a new instance of Parser.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cljdotrb.rb', line 69

def initialize src, opts = {}
  opts ||= {}
  super src

  @int_ctor    = opts[:int_ctor]    || lambda { |repr, radix| repr.to_i(radix) }
  @bint_ctor   = opts[:bint_ctor]   || @int_ctor
  @ratio_ctor  = opts[:ratio_ctor]  || lambda { |n,d| Rational(n.to_i, d.to_i) }
  @float_ctor  = opts[:float_ctor]  || lambda { |repr| repr.to_f }
  @bigdec_ctor = opts[:bigdec_ctor] || @float_ctor
  @kw_ctor     = opts[:kw_ctor]     || lambda { |prefix, name| "#{prefix ? prefix + '/' : prefix}#{name}".to_sym }
  @sym_ctor    = opts[:sym_ctor]    || lambda { |prefix, name| Clojure::Types::Sym.new(@kw_ctor.call(prefix, name)) }
end

Instance Method Details

#parse_floatObject



137
138
139
140
# File 'lib/cljdotrb.rb', line 137

def parse_float
  ctor = self[4] ? @bigdec_ctor : @float_ctor
  ctor.call self[1]
end

#parse_hexObject



123
124
125
126
# File 'lib/cljdotrb.rb', line 123

def parse_hex
  ctor = self[3] ? @bint_ctor : @int_ctor
  ctor.call "#{self[1]}" + self[2], 16
end

#parse_intObject



142
143
144
145
146
147
148
149
150
# File 'lib/cljdotrb.rb', line 142

def parse_int
  ctor = self[3] ? @bint_ctor : @int_ctor

  if self[1]
    ctor.call self[1], 10
  else
    ctor.call self[2], 10
  end
end

#parse_octalObject



128
129
130
131
# File 'lib/cljdotrb.rb', line 128

def parse_octal
  ctor = self[3] ? @bint_ctor : @int_ctor
  ctor.call "#{self[1]}" + self[2], 8
end

#parse_radixObject



133
134
135
# File 'lib/cljdotrb.rb', line 133

def parse_radix
  @int_ctor.call "#{self[1]}" + self[3], self[2].to_i
end

#parse_ratioObject



152
153
154
# File 'lib/cljdotrb.rb', line 152

def parse_ratio
  @ratio_ctor.call self[1], self[2]
end

#parse_scalarObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cljdotrb.rb', line 82

def parse_scalar
  case
  when scan(RATIO)
    parse_ratio
  when scan(HEX)
    parse_hex
  when scan(OCTAL)
    parse_octal
  when scan(RADIX)
    parse_radix
  when scan(FLOAT)
    parse_float
  when scan(INTEGER)
    parse_int
  when scan(TRUE)
    true
  when scan(FALSE)
    false
  when scan(NIL)
    nil
  when (str = parse_string) != :no_string
    str
  when scan(SYMBOLIC)
    parse_symbolic
  else
    throw "unparseable"
  end
end

#parse_symbolicObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cljdotrb.rb', line 111

def parse_symbolic
  is_kw  = !self[1].empty?
  prefix = self[4] ? self[3] : nil
  name   = self[5]

  if is_kw
    @kw_ctor.call prefix, name
  else
    @sym_ctor.call prefix, name
  end
end