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) ⇒ StringsParser

Returns a new instance of StringsParser.



6
7
8
# File 'lib/babelyoda/strings_parser.rb', line 6

def initialize(lexer)
  @lexer = lexer
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



10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
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 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