module Calyx::Format

Helper methods for loading and initializing grammars from static files on disk.

Public Class Methods

load(filename) click to toggle source

Reads a file and parses its format, based on the given extension.

Accepts a JSON or YAML file path, identified by its extension (`.json` or `.yml`).

@param [String] filename @return [Calyx::Grammar]

# File lib/calyx/format.rb, line 73
def self.load(filename)
  extension = File.extname(filename)

  if extension == ".yml"
    self.load_yml(filename)
  elsif extension == ".json"
    self.load_json(filename)
  else
    raise Errors::UnsupportedFormat.new(filename)
  end
end
load_json(filename) click to toggle source

Converts the given string of JSON data to a grammar instance.

@param [String] filename @return [Calyx::Format::JSONGrammar]

# File lib/calyx/format.rb, line 97
def self.load_json(filename)
  self.build_grammar(JSONGrammar.new(filename))
end
load_yml(filename) click to toggle source

Converts the given string of YAML data to a grammar instance.

@param [String] filename @return [Calyx::Format::YAMLGrammar]

# File lib/calyx/format.rb, line 89
def self.load_yml(filename)
  self.build_grammar(YAMLGrammar.new(filename))
end

Private Class Methods

build_grammar(grammar_format) click to toggle source
# File lib/calyx/format.rb, line 103
def self.build_grammar(grammar_format)
  Calyx::Grammar.new do
    grammar_format.each_rule do |label, productions, trace|
      productions = [productions] unless productions.is_a?(Enumerable)
      define_rule(label, trace, productions)
    end
  end
end