module Piggly::Parser

Pl/pgSQL Parser, returns a tree of NodeClass values (see nodes.rb)

Public Class Methods

grammar_path() click to toggle source
# File lib/piggly/parser.rb, line 32
def grammar_path; "#{File.dirname(__FILE__)}/parser/grammar.tt" end
nodes_path() click to toggle source
# File lib/piggly/parser.rb, line 33
def nodes_path;   "#{File.dirname(__FILE__)}/parser/nodes.rb"   end
parse(string) click to toggle source

Returns lazy parse tree (only parsed when the value is needed)

# File lib/piggly/parser.rb, line 15
def parse(string)
  Util::Thunk.new do
    p = parser

    begin
      # Downcase input for case-insensitive parsing
      input = string.downcase
      tree = p.parse(input)
      tree or raise Failure, "#{p.failure_reason}"
    ensure
      # Restore the original string after parsing
      input.replace(string)
    end
  end
end
parser() click to toggle source

Returns treetop parser (recompiled as needed)

# File lib/piggly/parser.rb, line 36
def parser
  load_support

  # @todo: Compare with the version of treetop
  if Util::File.stale?(parser_path, grammar_path)
    # Regenerate the parser when the grammar is updated
    Treetop::Compiler::GrammarCompiler.new.compile(grammar_path, parser_path)
    load parser_path
  else
    require parser_path
  end

  ::PigglyParser.new
end
parser_path() click to toggle source
# File lib/piggly/parser.rb, line 31
def parser_path;  "#{File.dirname(__FILE__)}/parser/parser.rb"  end

Private Class Methods

load_support() click to toggle source
# File lib/piggly/parser.rb, line 53
def load_support
  require "treetop"
  require "piggly/parser/treetop_ruby19_patch"
  require "piggly/parser/nodes"
end