fork download
  1. #!/usr/bin/env ruby
  2.  
  3. require 'parslet'
  4. require 'pp'
  5.  
  6.  
  7. class Stellaris < Parslet::Parser
  8. rule(:space) { match('\s').repeat(1) }
  9. rule(:space?) { match('\s').repeat }
  10. rule(:leftbrace) { match('{') >> space? }
  11. rule(:rightbrace) { match('}') >> space? }
  12.  
  13. rule(:number) { match('-').maybe >> match('[0-9]').repeat(1) >> (match('\.') >> match('[0-9]').repeat(1)).maybe }
  14.  
  15. rule(:string) { match('"') >> match('[^"]').repeat >> match('"') }
  16. rule(:dictionary) { leftbrace >> keyvalue.repeat >> rightbrace }
  17. rule(:array) { leftbrace >> value.repeat >> rightbrace }
  18. rule(:identifier) { match('[a-zA-Z]') >> match('[a-zA-Z0-9_:]').repeat }
  19. rule(:value) { (string | number | dictionary | array | identifier) >> space? }
  20. rule(:equals) { match('=') }
  21.  
  22. rule(:key) { identifier | number | equals }
  23. rule(:keyvalue) { key >> equals >> value >> space? }
  24. rule(:save) { keyvalue.repeat }
  25.  
  26. root(:save)
  27. end
  28.  
  29. s = <<EOF
  30. version="Capek v1.8.3"
  31. version_control_revision=31272
  32. galactic_object={
  33. 530={
  34. coordinate={
  35. x=-304.038
  36. y=-266.816
  37. origin=4294967295
  38. randomized=yes
  39. }
  40. type=star
  41. name="NAME_Unicomplex_01"
  42. planet=0
  43. planet=1
  44. planet=16777392
  45. planet=16777393
  46. planet=16777394
  47. ambient_object={
  48. 0 1 50331731 50331732 16777301
  49. }
  50. star_class="sc_m"
  51. hyperlane={
  52. {
  53. to=565
  54. length=21.000
  55. }
  56. {
  57. to=564
  58. length=16.000
  59. }
  60. {
  61. to=484
  62. length=12.000
  63. }
  64. }
  65. discovery={
  66. 70 35 39
  67. }
  68. flags={
  69. borg_homeworld=62376000
  70. unicomplex_2=62376000
  71. unicomplex_1:0=62376000
  72. }
  73. initializer="borg_initializer"
  74. fleet_presence={
  75. 0 1 522 654 1593 16778808 16777494 16777590 33556588 67109333
  76. }
  77. inner_radius=150.000
  78. outer_radius=250.000
  79. }
  80. }
  81. EOF
  82.  
  83. puts "Parsing..."
  84. begin
  85. tree = Stellaris.new.parse(s)
  86. pp tree
  87. rescue Parslet::ParseFailed => failure
  88. puts failure.parse_failure_cause.ascii_tree
  89. end
  90. puts "Done"
  91.  
Runtime error #stdin #stdout #stderr 0s 28688KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.rb:3:in `require': cannot load such file -- parslet (LoadError)
	from prog.rb:3:in `<main>'