Class: ZIG::Parser
- Inherits:
-
Object
- Object
- ZIG::Parser
- Defined in:
- lib/zig/parser.rb
Constant Summary collapse
- INDENT =
2
- FULL_LINE =
/^( *)(.*)$/
- KEY_VALUE =
/^([^:]+):(.*)$/
- STRING =
/^'(.*)'$/
- INTEGER =
/^-?\d+$/
- FLOAT =
/^-?\d+\.\d+([eE][+-]?\d+)?$/
Class Method Summary collapse
Class Method Details
.parse_object(indent, lines, object) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/zig/parser.rb', line 11 def self.parse_object(indent, lines, object) loop do return object if lines.empty? spaces, value = lines.current.scan(FULL_LINE).flatten return object if spaces.size < indent raise "[line #{lines.num}] illegal indent" if spaces.size > indent lines.next if object.is_a?(Hash) key, value = value.scan(KEY_VALUE).flatten raise "[line #{lines.num}] missing key" if key.nil? object[key.strip.to_sym] = parse_value(indent, lines, value) elsif object.is_a?(Array) object << parse_value(indent, lines, value) else object << "\n" unless object.empty? object << value end end end |
.parse_value(indent, lines, text) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/zig/parser.rb', line 31 def self.parse_value(indent, lines, text) text = text.strip case text when 'nil' then nil when 'true' then true when 'false' then false when '{' then parse_object(indent + INDENT, lines, {}) when '[' then parse_object(indent + INDENT, lines, []) when '"' then parse_object(indent + INDENT, lines, "") when STRING then $1 when INTEGER then text.to_i when FLOAT then text.to_f else raise "[line #{lines.num}] illegal value: #{text}" end end |