Class: PgVerify::EbnfParser::ExpressionParser

Inherits:
Object
  • Object
show all
Includes:
EBNF::PEG::Parser
Defined in:
lib/pg-verify/ebnf_parser/expression_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: :Expression) ⇒ ExpressionParser

Returns a new instance of ExpressionParser.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 129

def initialize(type: :Expression)
    @type = type
    @options = {}
    # @options[:logger] = Logger.new(STDERR)
    # @options[:logger].level = :info
    # @options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
        
    # Intantiate the grammar
    ebnf_file = File.expand_path("expressions.ebnf", __dir__)
    # Perform PEG-specific transformation to the associated rules, which will be passed directly to the parser.
    @rules = EBNF.parse(File.open(ebnf_file)).make_peg.ast

    skeletton = EBNF.parse(File.open(ebnf_file)).make_peg.to_s.split("\n").reverse.map { |line|
        rule = line.split(/\s+/)[1]
        comment = "# Rule: '#{line.split(/\s+/).join(" ")}'"
        puts_line = 'puts "' + rule + ': \'#{input}\' -> \'#{output}\'"'
        body    = "\toutput = input\n\t#{puts_line}\n\toutput"
        method =  "production(:#{rule}) do |input|\n#{body}\nend"
        [comment, method].join("\n")
    }.join("\n\n")
    File.write(File.expand_path("expressions.rb", __dir__), skeletton)

    # TODO: Remove 
    File.write(File.expand_path("expressions.peg", __dir__), EBNF.parse(File.open(ebnf_file)).make_peg)
end

Instance Attribute Details

#astObject (readonly)

Abstract syntax tree from parse



8
9
10
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 8

def ast
  @ast
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 10

def options
  @options
end

#rulesObject

Returns the value of attribute rules.



9
10
11
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 9

def rules
  @rules
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 11

def type
  @type
end

Instance Method Details

#parse!(input) ⇒ Object



155
156
157
158
159
# File 'lib/pg-verify/ebnf_parser/expression_parser.rb', line 155

def parse!(input)
    raise "Empty expression!" if input.nil? || input.empty?
    ast_map = parse(input, @type, @rules, **@options)
    Ast.new(ast_map)
end