Class: Babelyoda::StringsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/babelyoda/strings_parser.rb

Defined Under Namespace

Classes: Bit

Instance Method Summary collapse

Constructor Details

#initialize(lexer, language) ⇒ StringsParser

Returns a new instance of StringsParser.



8
9
10
# File 'lib/babelyoda/strings_parser.rb', line 8

def initialize(lexer, language)
  @lexer, @language = lexer, language
end

Instance Method Details

#cleanup_comment(str) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
# File 'lib/babelyoda/strings_parser.rb', line 68

def cleanup_string(str)
  str.sub(/^\"/, '').sub(/\"$/, '')
end

#match_bs(bs, *tokens) {|bs.shift(tokens.size).map { |bit| bit[:value] }| ... } ⇒ Object

Yields:

  • (bs.shift(tokens.size).map { |bit| bit[:value] })


52
53
54
55
56
57
58
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/babelyoda/strings_parser.rb', line 12

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) if record
  end
end

#produce(bs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/babelyoda/strings_parser.rb', line 24

def produce(bs)
  match_bs(bs, :multiline_comment, :string, :equal_sign, :string, :semicolon) do |bits|
    localization_key = LocalizationKey.new(cleanup_string(bits[1]), cleanup_comment(bits[0]))
    localization_value = LocalizationValue.new(@language, cleanup_string(bits[3]))
    localization_key << localization_value
    return localization_key
  end
  match_bs(bs, :singleline_comment, :string, :equal_sign, :string, :semicolon) do |bits|
    localization_key = LocalizationKey.new(cleanup_string(bits[1]), cleanup_comment(bits[0]))
    localization_value = LocalizationValue.new(@language, cleanup_string(bits[3]))
    localization_key << localization_value
    return localization_key
  end
  match_bs(bs, :string, :equal_sign, :string, :semicolon) do |bits|
    localization_key = LocalizationKey.new(cleanup_string(bits[0]), nil)
    localization_value = LocalizationValue.new(@language, cleanup_string(bits[2]))
    localization_key << localization_value
    return localization_key
  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