module Bruhl

Constants

BOM
EMPTY_OBJECT
VERSION

Public Class Methods

bom_present?(filename) click to toggle source
# File lib/bruhl.rb, line 56
def self.bom_present?(filename)
  bom_range = File.open(filename, 'rb').read(BOM.bytesize)
  bom_range == BOM.force_encoding(bom_range.encoding)
end
generate(root, indent = 0) click to toggle source
# File lib/bruhl.rb, line 5
def self.generate(root, indent = 0)
  case root
  when Bruhl::Object
    generate_object(root, indent)
  when Bruhl::Relation
    generate_relation(root, indent)
  when Bruhl::RawString
    root.to_s
  when Bruhl::QuotedString
    '"' + root.to_s + '"'
  when Integer, Float
    root.to_s
  when Enumerable
    root.map { |elem| Bruhl.generate(elem) }
  end
end
generate_object(root, indent = 0) click to toggle source
# File lib/bruhl.rb, line 22
def self.generate_object(root, indent = 0)
  str = ''
  str += "#{root.tag}: " if root.tag
  if root.contents.empty?
    str += '{}'
  elsif root.contents.none? { |elem| elem.is_a?(Bruhl::Object) || elem.is_a?(Bruhl::Relation) }
    str += '{ '
    str += root.contents.map { |elem| Bruhl.generate(elem) }.join(' ')
    str += ' }'
  else
    str += '{'
    str += "\n"
    root.contents.each do |elem|
      str += ((' ' * (indent + 2)) + Bruhl.generate(elem, indent + 2)) + "\n"
    end
    str += ' ' * indent
    str += '}'
  end
  str
end
generate_relation(root, indent = 0) click to toggle source
# File lib/bruhl.rb, line 43
def self.generate_relation(root, indent = 0)
  [
    root.left.to_s,
    root.operator.to_s,
    Bruhl.generate(root.right, indent)
  ].join(' ')
end
parse(str, opts = {}) click to toggle source
# File lib/bruhl.rb, line 51
def self.parse(str, opts = {})
  self.parser.parse(str, opts).value
end
parse_file(filename, opts = {}) click to toggle source
# File lib/bruhl.rb, line 61
def self.parse_file(filename, opts = {})
  file_body =
    if bom_present?(filename)
      File.open(filename, 'r:bom|utf-8')  { |f| f.read }
    else
      File.open(filename, 'r:utf-8') { |f| f.read }
    end
  self.parse(file_body, opts)
end
parser() click to toggle source
# File lib/bruhl.rb, line 71
def self.parser
  @@parser ||=
    begin
      Citrus.load(File.join(__dir__, 'bruhl', 'grammar'))
      @@parser = BruhlGrammar
    end
end