Module: Piggly::Parser

Defined in:
lib/piggly/parser.rb,
lib/piggly/parser/nodes.rb,
lib/piggly/parser/traversal.rb

Overview

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

Defined Under Namespace

Modules: Nodes, Traversal Classes: Failure

Class Method Summary collapse

Class Method Details

.grammar_pathObject



32
# File 'lib/piggly/parser.rb', line 32

def grammar_path; "#{File.dirname(__FILE__)}/parser/grammar.tt" end

.nodes_pathObject



33
# File 'lib/piggly/parser.rb', line 33

def nodes_path;   "#{File.dirname(__FILE__)}/parser/nodes.rb"   end

.parse(string) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

.parserObject

Returns treetop parser (recompiled as needed)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_pathObject



31
# File 'lib/piggly/parser.rb', line 31

def parser_path;  "#{File.dirname(__FILE__)}/parser/parser.rb"  end