class Babelyoda::StringsParser

Constants

Bit

Public Class Methods

new(lexer) click to toggle source
# File lib/babelyoda/strings_parser.rb, line 6
def initialize(lexer)
  @lexer = lexer
end

Public Instance Methods

cleanup_comment(str) click to toggle source
# File lib/babelyoda/strings_parser.rb, line 60
def cleanup_comment(str)
  if str.match(/^\/\/\s*/)
    str.sub(/^\/\/\s*/, '')
  else
    str.sub(/^\/\*\s*/, '').sub(/\s*\*\/$/, '')
  end
end
cleanup_string(str) click to toggle source
# File lib/babelyoda/strings_parser.rb, line 68
def cleanup_string(str)
  str.sub(/^\"/, '').sub(/\"$/, '')
end
match_bs(bs, *tokens) { |shift.map { |bit| bit }| ... } click to toggle source
# File lib/babelyoda/strings_parser.rb, line 52
def match_bs(bs, *tokens)
  return unless bs.size >= tokens.size
  tokens.each_with_index do |token, idx|
    return unless bs[idx][:token] == token 
  end
  yield bs.shift(tokens.size).map { |bit| bit[:value] }
end
parse(str, &block) click to toggle source
# File lib/babelyoda/strings_parser.rb, line 10
def parse(str, &block)
  @block = block
  bitstream = []
  @lexer.lex(str) do | token, value |
    bitstream << Bit.new(token, value)
  end
  while bitstream.size > 0
    record = produce(bitstream)
    @block.call(record[:string_for_key], record[:value], record[:comment]) if record
  end
end
produce(bs) click to toggle source
# File lib/babelyoda/strings_parser.rb, line 22
def produce(bs)
  match_bs(bs, :multiline_comment, :string, :equal_sign, :string, :semicolon) do |bits|
    result = {}
    result[:string_for_key] = bits[1]
    result[:comment] = bits[0]
    result[:value] = bits[3]
    return result
  end
  match_bs(bs, :singleline_comment, :string, :equal_sign, :string, :semicolon) do |bits|
    result = {}
    result[:string_for_key] = bits[1]
    result[:comment] = bits[0]
    result[:value] = bits[3]
    return result
  end
  match_bs(bs, :string, :equal_sign, :string, :semicolon) do |bits|        
    result = {}
    result[:string_for_key] = bits[0]
    result[:value] = bits[2]
    return result
  end
  match_bs(bs, :singleline_comment) do |bits|
    return nil
  end
  match_bs(bs, :multiline_comment) do |bits|
    return nil
  end
  raise "Syntax error: #{bs.shift(5).inspect}"
end