#!/usr/bin/ruby

=begin
  rmsgfmt - ruby version of msgfmt

  Copyright (C) 2003 Masao Mutoh <mutoh@highway.ne.jp>
 
  You may redistribute it and/or modify it under the same
  license terms as Ruby.
=end

#
# DO NOT MODIFY!!!!
# This file is automatically generated by racc 1.4.3
# from racc grammer file "src/rmsgfmt.ry".
#

require 'racc/parser'


class PoParser < Racc::Parser

module_eval <<'..end src/rmsgfmt.ry modeval..id5f642c8789', 'src/rmsgfmt.ry', 89
  def parse(str)
    $fuzzy = false
  	str.strip!
    @q = []
    until str.empty? do
      case str
      when /\A\s+/
        str = $'
      when /\Amsgid_plural/
      	@q.push [:MSGID_PLURAL, $&]
        str = $'
      when /\Amsgid/
      	@q.push [:MSGID, $&]
        str = $'
      when /\Amsgstr/
      	@q.push [:MSGSTR, $&]
        str = $'
      when /\A\[(\d+)\]/
      	@q.push [:PLURAL_NUM, $1]
        str = $'
      when /\A\#/
      	@q.push [:COMMENT, $&]
        str = $'
      when /\A\"(.*)\"/
        @q.push [:STRING, $1]
    	  str = $'
      else
    	    c = str[0,1]
          @q.push [:STRING, c]
          str = str[1..-1]
      end
    end 
  	@q.push [false, '$end']
    if $DEBUG
      @q.each do |a,b|
        puts "[#{a}, #{b}]"
      end
    end
    @yydebug = true if $DEBUG
    do_parse
  end

  def next_token
    @q.shift
  end


..end src/rmsgfmt.ry modeval..id5f642c8789

##### racc 1.4.3 generates ###

racc_reduce_table = [
 0, 0, :racc_error,
 0, 9, :_reduce_none,
 2, 9, :_reduce_none,
 2, 9, :_reduce_none,
 4, 11, :_reduce_4,
 5, 11, :_reduce_5,
 2, 13, :_reduce_6,
 1, 13, :_reduce_none,
 3, 14, :_reduce_8,
 2, 10, :_reduce_9,
 1, 10, :_reduce_none,
 2, 12, :_reduce_11,
 1, 12, :_reduce_12 ]

racc_reduce_n = 13

racc_shift_n = 22

racc_action_table = [
     3,     9,     4,     5,    12,    13,    11,    18,    11,    11,
     9,     9,     9,     7,    11,    18,    20,     9,    11 ]

racc_action_check = [
     1,    13,     1,     1,    10,    10,    10,    14,    14,     8,
     4,    12,     5,     3,    15,    16,    18,    20,    21 ]

racc_action_pointer = [
   nil,     0,   nil,    13,     4,     6,   nil,   nil,     3,   nil,
     0,   nil,     5,    -5,     2,     8,    10,   nil,     9,   nil,
    11,    12 ]

racc_action_default = [
    -1,   -13,    -3,   -13,   -10,   -13,    -2,    22,    -9,   -12,
   -13,   -11,   -13,   -13,   -13,    -4,    -5,    -7,   -13,    -6,
   -13,    -8 ]

racc_goto_table = [
     8,    10,    17,     2,    19,     6,    16,     1,    14,    15,
   nil,   nil,   nil,   nil,   nil,   nil,    21 ]

racc_goto_check = [
     4,     4,     6,     3,     6,     2,     5,     1,     4,     4,
   nil,   nil,   nil,   nil,   nil,   nil,     4 ]

racc_goto_pointer = [
   nil,     7,     4,     2,    -4,    -8,   -12 ]

racc_goto_default = [
   nil,   nil,   nil,   nil,   nil,   nil,   nil ]

racc_token_table = {
 false => 0,
 Object.new => 1,
 :COMMENT => 2,
 :MSGID => 3,
 :MSGID_PLURAL => 4,
 :MSGSTR => 5,
 :STRING => 6,
 :PLURAL_NUM => 7 }

racc_use_result_var = true

racc_nt_base = 8

Racc_arg = [
 racc_action_table,
 racc_action_check,
 racc_action_default,
 racc_action_pointer,
 racc_goto_table,
 racc_goto_check,
 racc_goto_default,
 racc_goto_pointer,
 racc_nt_base,
 racc_reduce_table,
 racc_token_table,
 racc_shift_n,
 racc_reduce_n,
 racc_use_result_var ]

Racc_token_to_s_table = [
'$end',
'error',
'COMMENT',
'MSGID',
'MSGID_PLURAL',
'MSGSTR',
'STRING',
'PLURAL_NUM',
'$start',
'msgfmt',
'comment',
'message',
'string_list',
'msgstr_plural',
'msgstr_plural_line']

Racc_debug_parser = true

##### racc system variables end #####

 # reduce 0 omitted

 # reduce 1 omitted

 # reduce 2 omitted

 # reduce 3 omitted

module_eval <<'.,.,', 'src/rmsgfmt.ry', 23
  def _reduce_4( val, _values, result )
			if $fuzzy
        if val[1] != ""
          $stderr.print "Warning: fuzzy message was ignored.\n"
				  $stderr.print "msgid = '#{val[1]}\n"
        else
  				$data[""] = val[3].gsub(/\\n/, "\n")
        end
			  $fuzzy = false
			else
				$data[val[1].gsub(/\\n/, "\n")] = val[3].gsub(/\\n/, "\n")
			end
      result = ""
   result
  end
.,.,

module_eval <<'.,.,', 'src/rmsgfmt.ry', 38
  def _reduce_5( val, _values, result )
			if $fuzzy
        if val[1] != ""
          $stderr.print "Warning: fuzzy message was ignored.\n"
				  $stderr.print "msgid = '#{val[1]}\n"
        else
  				$data[""] = val[3].gsub(/\\n/, "\n")
        end
			  $fuzzy = false
			else
				$data[val[1].gsub(/\\n/, "\n") + "\000" + val[3].gsub(/\\n/, "\n")] = val[4]
			end
      result = ""
   result
  end
.,.,

module_eval <<'.,.,', 'src/rmsgfmt.ry', 56
  def _reduce_6( val, _values, result )
      result = val[0] + "\000" + val[1]
   result
  end
.,.,

 # reduce 7 omitted

module_eval <<'.,.,', 'src/rmsgfmt.ry', 64
  def _reduce_8( val, _values, result )
      result = val[2].gsub(/\\n/, "\n")
   result
  end
.,.,

module_eval <<'.,.,', 'src/rmsgfmt.ry', 70
  def _reduce_9( val, _values, result )
			$fuzzy = true if /fuzzy/ =~ val[1]
   result
  end
.,.,

 # reduce 10 omitted

module_eval <<'.,.,', 'src/rmsgfmt.ry', 78
  def _reduce_11( val, _values, result )
			result = val.delete_if{|item| item == ""}.join
   result
  end
.,.,

module_eval <<'.,.,', 'src/rmsgfmt.ry', 82
  def _reduce_12( val, _values, result )
			result = val[0]
   result
  end
.,.,

 def _reduce_none( val, _values, result )
  result
 end

end   # class PoParser

require 'getoptlong'
require 'gettext'

class RMsgfmt
  VERSION = %w($Revision: 1.1 $)[1].scan(/\d+/).collect {|s| s.to_i}
  DATE = %w($Date: 2003/01/07 14:23:03 $)[1]

  def initialize
    GetText.bindtextdomain("rmsgfmt")
  end

  def start
    opt = check_options
	
    if ARGV.empty?
      print_help
      exit
    end

    parser = PoParser.new
    $data = MOFile.new
    parser.parse(ARGF.read)

    $data.save_to_file(@output_path)
  end

  def print_help
    printf(GetText._("Usage: %s input.po [-o output.mo]\n"), $0)
    print GetText._("Generate binary message catalog from textual translation description.\n\n")
  end

  def check_options
    command_options = [
      ['--help', '-h', GetoptLong::NO_ARGUMENT],
      ['--version', '-v', GetoptLong::NO_ARGUMENT],
      ['--output', '-o', GetoptLong::REQUIRED_ARGUMENT]
    ]
	
    parser = GetoptLong.new
    parser.set_options(*command_options)
	
    opt = Hash.new
    parser.each do |name, arg|
      opt.store(name.sub(/^--/, ""), arg || true)
    end
	
    if opt['version']
      print "#{$0} #{VERSION.join('.')} \(#{DATE}\)\n\n"
      exit
    end
	
    if opt['help']
      print_help
      exit
    end
	
    if opt['output']
      unless FileTest.exist? opt['output']
        @output_path = File.expand_path(opt['output'])
      else
        if $>.tty?
          # FIXME
          printf $stderr, "File '#{opt['output']}' already exists\n"
          exit 1
        else
          printf $stderr, "File '#{opt['output']}' already exists"
          exit 1
        end
      end
    else
      @output_path = "messages.mo"
    end
    opt
  end
end
  
if $0 == __FILE__ then
  RMsgfmt.new.start
end

# Local variables:
# mode: Ruby
# End:
