module Citrus

Citrus is a compact and powerful parsing library for Ruby that combines the elegance and expressiveness of the language with the simplicity and power of parsing expressions.

mjackson.github.io/citrus

Constants

CLOSE
DOT

A pattern to match any character, including newline.

Infinity
VERSION

The current version of Citrus as [major, minor, patch].

Public Class Methods

cache() click to toggle source

Returns a map of paths of files that have been loaded via load to the result of eval on the code in that file.

Note: These paths are not absolute unless you pass an absolute path to load. That means that if you change the working directory and try to require the same file with a different relative path, it will be loaded twice.

# File lib/citrus.rb, line 28
def self.cache
  @cache ||= {}
end
eval(code, options={}) click to toggle source

Evaluates the given Citrus parsing expression grammar code and returns an array of any grammar modules that are created. Accepts the same options as Citrus::GrammarMethods#parse.

Citrus.eval("grammar MyGrammar
  rule abc
    "abc"
  end
end
")
# => [MyGrammar]
# File lib/citrus.rb, line 45
def self.eval(code, options={})
  File.parse(code, options).value
end
load(file, options={}) click to toggle source

Loads the grammar(s) from the given file. Accepts the same options as eval, plus the following:

force

Normally this method will not reload a file that is already in the cache. However, if this option is true the file will be loaded, regardless of whether or not it is in the cache. Defaults to false.

Citrus.load('mygrammar')
# => [MyGrammar]
# File lib/citrus.rb, line 70
def self.load(file, options={})
  file += '.citrus' unless /\.citrus$/ === file
  force = options.delete(:force)

  if force || !cache[file]
    begin
      cache[file] = eval(::File.read(file), options)
    rescue SyntaxError => e
      e.message.replace("#{::File.expand_path(file)}: #{e.message}")
      raise e
    end
  end

  cache[file]
end
require(file, options={}) click to toggle source

Searches the $LOAD_PATH for a file with the .citrus suffix and attempts to load it via load. Returns the path to the file that was loaded on success, nil on failure. Accepts the same options as load.

path = Citrus.require('mygrammar')
# => "/path/to/mygrammar.citrus"
Citrus.cache[path]
# => [MyGrammar]
# File lib/citrus.rb, line 95
def self.require(file, options={})
  file += '.citrus' unless /\.citrus$/ === file
  found = nil

  paths = ['']
  paths += $LOAD_PATH unless Pathname.new(file).absolute?
  paths.each do |path|
    found = Dir[::File.join(path, file)].first
    break if found
  end

  if found
    Citrus.load(found, options)
  else
    raise LoadError, "Cannot find file #{file}"
  end

  found
end
rule(expr, options={}) click to toggle source

Evaluates the given expression and creates a new Rule object from it. Accepts the same options as eval.

Citrus.rule('"a" | "b"')
# => #<Citrus::Rule: ... >
# File lib/citrus.rb, line 55
def self.rule(expr, options={})
  eval(expr, options.merge(:root => :expression))
end
version() click to toggle source

Returns the current version of Citrus as a string.

# File lib/citrus/version.rb, line 6
def self.version
  VERSION.join('.')
end