Class: ParseGalenicForm

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb2xml/parslet_compositions.rb

Class Method Summary collapse

Class Method Details

.from_string(string) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/oddb2xml/parslet_compositions.rb', line 463

def ParseGalenicForm.from_string(string)
  return nil if string == nil
  stripped = string.gsub(/^"|["\n]+$/, '')
  return nil unless stripped
  puts "ParseGalenicForm.from_string #{string}" if VERBOSE_MESSAGES # /ng-tr/.match(Socket.gethostbyname(Socket.gethostname).first)

  parser = GalenicFormParser.new
  transf = GalenicFormTransformer.new
  begin
    if defined?(RSpec)
      ast = transf.apply(parser.parse_with_debug(string))
      puts "#{File.basename(__FILE__)}:#{__LINE__}: ==> " if VERBOSE_MESSAGES
      pp ast if VERBOSE_MESSAGES
    else
      ast = transf.apply(parser.parse(string))
    end
  rescue Parslet::ParseFailed => error
    @@errorHandler.nrParsingErrors += 1
    puts "#{File.basename(__FILE__)}:#{__LINE__}: failed parsing ==>  #{string}"
    return nil
  end
  return [] unless ast
  form = ast[:galenic_form] ? ast[:galenic_form].to_s.sub(/^\/\s+/, '') : nil
  name = ast[:prepation_name] ? ast[:prepation_name].to_s.strip : nil
  return [name,  form]
end