sexpr.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ## $Id$
  2. ##
  3. ## Flexlay - A Generic 2D Game Editor
  4. ## Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  5. ##
  6. ## This program is free software: you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation, either version 3 of the License, or
  9. ## (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ## GNU General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. require "stringio"
  19. def assoc_ref(lst, str)
  20. if lst == []
  21. return false
  22. elsif lst[0][0] == str
  23. return lst[0][1..-1]
  24. else
  25. return assoc_ref(lst[1..-1], str)
  26. end
  27. end
  28. def sexpr_filter(name, tree)
  29. ret = []
  30. for i in tree
  31. if i[0] == name
  32. ret.push(i[1..-1])
  33. end
  34. end
  35. return ret
  36. end
  37. def get_value_from_tree(spec, tree, default)
  38. if spec == []
  39. return tree
  40. elsif spec == ['_']
  41. # is it a translatable string?
  42. if(tree[0].instance_of?(Array) and tree[0][0] == :"_")
  43. return tree[0][1]
  44. else
  45. return tree[0]
  46. end
  47. elsif tree == []
  48. return default
  49. else
  50. el = assoc_ref(tree, spec[0].to_sym)
  51. if el
  52. return get_value_from_tree(spec[1..-1], el, default)
  53. else
  54. return default
  55. end
  56. end
  57. end
  58. def write_sexpr(f, sexpr, indent = 0)
  59. if sexpr.is_a?(Array) then
  60. f.print "("
  61. sexpr.each_with_index{|e, i|
  62. write_sexpr(f, e)
  63. if i != sexpr.length() - 1 then
  64. f.print " "
  65. end
  66. }
  67. f.print ")"
  68. else
  69. if sexpr.is_a?(Symbol)
  70. f.print sexpr.to_s
  71. else
  72. f.print sexpr.inspect
  73. end
  74. end
  75. end
  76. class SExpression
  77. def initialize(sexpr = [])
  78. @sexpr = sexpr
  79. end
  80. def SExpression.new_from_file(filename)
  81. return SExpression.new(sexpr_read_from_file(filename))
  82. end
  83. def car()
  84. return SExpression.new(@sexpr[0])
  85. end
  86. def cdr()
  87. return SExpression.new((@sexpr[1..-1] or []))
  88. end
  89. def [](i)
  90. return SExpression.new(@sexpr[i])
  91. end
  92. # Interprets the SExpression as a AList in the form of ((name value) ...)
  93. def get(name, default)
  94. v = @sexpr.find() { |el| el[0] == name }
  95. if v then
  96. return SExpression.new(v[1..-1])
  97. else
  98. return SExpression.new(default)
  99. end
  100. end
  101. def value()
  102. return @sexpr
  103. end
  104. def get_value(spec, default = nil)
  105. return get_value_from_tree(spec, @sexpr, default)
  106. end
  107. def each_pair()
  108. @sexpr.each() { |el|
  109. yield(el[0], SExpression.new(el[1..-1]))
  110. }
  111. end
  112. def is_atom?()
  113. return @sexpr.is_a?(Array)
  114. end
  115. def is_nil?()
  116. return @sexpr == []
  117. end
  118. def to_s()
  119. str = StringIO.new()
  120. write(str)
  121. return str.string()
  122. end
  123. def to_a()
  124. return @sexpr
  125. end
  126. def write(f = $stdout, indent = 0)
  127. write_sexpr(f, @sexpr)
  128. end
  129. end
  130. # EOF #