Method: Parspec.translate_string

Defined in:
lib/parspec.rb

.translate_string(str, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/parspec.rb', line 28

def self.translate_string(str, options = {})
  ParserSpecTransform.no_debug_parse = options[:no_debug_parse]
  tree = Parser.new.parse(str)
  tree = SharedTransform.new.apply(tree)
  translation = Parslet::Transform.new do
    spec = ->(type) {
      { header: {type: type, subject_class: simple(:subject_class) },
        rules: subtree(:rules) } }
    rule(spec['parser'])      { ParserSpecTransform.new.apply(tree) }
    rule(spec['transformer']) { TransformerSpecTransform.new.apply(tree) }
  end.apply(tree)
  raise Error, "unexpected translation: #{translation}" unless translation.is_a? String
  translation
rescue Parslet::ParseFailed => e
  deepest = deepest_cause e.cause
  line, column = deepest.source.line_and_column(deepest.pos)
  raise ParseError, "unexpected input at line #{line} column #{column}"
end