#!/usr/bin/env ruby
require 'parslet'
require 'pp'
class Stellaris < Parslet::Parser
rule(:space) { match('\s').repeat(1) }
rule(:space?) { match('\s').repeat }
rule(:leftbrace) { match('{') >> space? }
rule(:rightbrace) { match('}') >> space? }
rule(:number) { match('-').maybe >> match('[0-9]').repeat(1) >> (match('\.') >> match('[0-9]').repeat(1)).maybe }
rule(:string) { match('"') >> match('[^"]').repeat >> match('"') }
rule(:dictionary) { leftbrace >> keyvalue.repeat >> rightbrace }
rule(:array) { leftbrace >> value.repeat >> rightbrace }
rule(:identifier) { match('[a-zA-Z]') >> match('[a-zA-Z0-9_:]').repeat }
rule(:value) { (string | number | dictionary | array | identifier) >> space? }
rule(:equals) { match('=') }
rule(:key) { identifier | number | equals }
rule(:keyvalue) { key >> equals >> value >> space? }
rule(:save) { keyvalue.repeat }
root(:save)
end
s = <<EOF
version="Capek v1.8.3"
version_control_revision=31272
galactic_object={
530={
coordinate={
x=-304.038
y=-266.816
origin=4294967295
randomized=yes
}
type=star
name="NAME_Unicomplex_01"
planet=0
planet=1
planet=16777392
planet=16777393
planet=16777394
ambient_object={
0 1 50331731 50331732 16777301
}
star_class="sc_m"
hyperlane={
{
to=565
length=21.000
}
{
to=564
length=16.000
}
{
to=484
length=12.000
}
}
discovery={
70 35 39
}
flags={
borg_homeworld=62376000
unicomplex_2=62376000
unicomplex_1:0=62376000
}
initializer="borg_initializer"
fleet_presence={
0 1 522 654 1593 16778808 16777494 16777590 33556588 67109333
}
inner_radius=150.000
outer_radius=250.000
}
}
EOF
puts "Parsing..."
begin
tree = Stellaris.new.parse(s)
pp tree
rescue Parslet::ParseFailed => failure
puts failure.parse_failure_cause.ascii_tree
end
puts "Done"