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
- .parse_document(lines) ⇒ Object
- .parse_object(indent, lines, object) ⇒ Object
- .parse_value(indent, lines, text) ⇒ Object
Class Method Details
.parse_document(lines) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/zig/parser.rb', line 50 def self.parse_document(lines) raise ZIG::SyntaxError, "cannot parse empty document" if lines.empty? lines.next while lines.current[0] == '#' start = lines.current lines.next return parse_value(0, lines, start) end |
.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 30 31 |
# 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 ZIG::SyntaxError, "[line #{lines.num}] illegal indent" if spaces.size > indent lines.next if object.is_a?(String) object << "\n" unless object.empty? object << value elsif object.is_a?(Array) next if value[0] == '#' object << parse_value(indent, lines, value) else next if value[0] == '#' key, value = value.scan(KEY_VALUE).flatten raise ZIG::SyntaxError, "[line #{lines.num - 1}] missing key" if key.nil? object[key.strip.to_sym] = parse_value(indent, lines, value) end end end |
.parse_value(indent, lines, text) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/zig/parser.rb', line 33 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 ZIG::SyntaxError, "[line #{lines.num}] illegal value: #{text}" end end |