Module: Calyx::Format

Defined in:
lib/calyx/format.rb

Overview

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

Defined Under Namespace

Classes: JSONGrammar, Trace, YAMLGrammar

Class Method Summary collapse

Class Method Details

.load(filename) ⇒ Calyx::Grammar

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`).

Parameters:

  • filename (String)

Returns:



73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Calyx::Format::JSONGrammar

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

Parameters:

  • filename (String)

Returns:



97
98
99
# File 'lib/calyx/format.rb', line 97

def self.load_json(filename)
  self.build_grammar(JSONGrammar.new(filename))
end

.load_yml(filename) ⇒ Calyx::Format::YAMLGrammar

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

Parameters:

  • filename (String)

Returns:



89
90
91
# File 'lib/calyx/format.rb', line 89

def self.load_yml(filename)
  self.build_grammar(YAMLGrammar.new(filename))
end