Class: Sexpr::Parser::Citrus

Inherits:
Object
  • Object
show all
Includes:
Sexpr::Parser
Defined in:
lib/sexpr/parser/citrus.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sexpr::Parser

factor, find_parser_class, register, registered_parser_classes

Constructor Details

#initialize(parser, options = {}) ⇒ Citrus

Returns a new instance of Citrus.



26
27
28
29
# File 'lib/sexpr/parser/citrus.rb', line 26

def initialize(parser, options = {})
  @parser  = parser
  @options = default_options.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

class << self



24
25
26
# File 'lib/sexpr/parser/citrus.rb', line 24

def options
  @options
end

Class Method Details

.looks_a_citrus_file?(arg) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/sexpr/parser/citrus.rb', line 13

def looks_a_citrus_file?(arg)
  arg = arg.to_path if arg.respond_to?(:to_path)
  arg.is_a?(String) && File.exists?(arg) && (File.extname(arg) == ".citrus")
end

.looks_a_citrus_grammar?(arg) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/sexpr/parser/citrus.rb', line 18

def looks_a_citrus_grammar?(arg)
  defined?(::Citrus::Grammar) && arg.is_a?(Module) && arg.include?(::Citrus::Grammar)
end

.recognizes?(arg) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/sexpr/parser/citrus.rb', line 8

def recognizes?(arg)
  looks_a_citrus_grammar?(arg) or
  looks_a_citrus_file?(arg)
end

Instance Method Details

#default_optionsObject



31
32
33
# File 'lib/sexpr/parser/citrus.rb', line 31

def default_options
  {:from_match_to_sexpr => lambda{|match| match.value}}
end

#parse(input, options = {}) ⇒ Object



49
50
51
52
# File 'lib/sexpr/parser/citrus.rb', line 49

def parse(input, options = {})
  input = input_text(input)
  parser.parse(input, options)
end

#parserObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sexpr/parser/citrus.rb', line 35

def parser
  @citrus_parser ||= begin
    if self.class.looks_a_citrus_grammar?(@parser)
      @parser
    elsif self.class.looks_a_citrus_file?(@parser)
      @parser = @parser.to_path if @parser.respond_to?(:to_path)
      @parser = @parser[0...-(".citrus".length)]
      ::Citrus.load(@parser).last
    else
      raise UnrecognizedParserError, "Not a citrus parser #{@parser}"
    end
  end
end

#sexpr(input, parse_options = {}) ⇒ Object



54
55
56
# File 'lib/sexpr/parser/citrus.rb', line 54

def sexpr(input, parse_options = {})
  from_match_to_sexpr parse(input, parse_options)
end