Class: Changit::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/changit/lexer.rb,
lib/changit/lexer/section_token.rb,
lib/changit/lexer/key_value_token.rb

Defined Under Namespace

Classes: KeyValueToken, SectionToken

Constant Summary collapse

RULES =
[
  [/\[(.*?)\]/, SectionToken].freeze,
  [/^[^(=|\[|\s)]*\s*\=\s*[^(=|\[|\])]*/, KeyValueToken].freeze
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Lexer



10
11
12
13
14
# File 'lib/changit/lexer.rb', line 10

def initialize(expression)
  @original_expression = expression
  @tokens = tokenize
  @token_hash = to_hash
end

Instance Attribute Details

#original_expressionObject (readonly)

Returns the value of attribute original_expression.



8
9
10
# File 'lib/changit/lexer.rb', line 8

def original_expression
  @original_expression
end

#token_hashObject (readonly)

Returns the value of attribute token_hash.



8
9
10
# File 'lib/changit/lexer.rb', line 8

def token_hash
  @token_hash
end

#tokensObject (readonly)

Returns the value of attribute tokens.



8
9
10
# File 'lib/changit/lexer.rb', line 8

def tokens
  @tokens
end

Instance Method Details

#reconstruct_tokens_from_hash!Object



20
21
22
23
24
25
26
27
28
# File 'lib/changit/lexer.rb', line 20

def reconstruct_tokens_from_hash!
  tokens = []
  @token_hash.each do |section, key_value|
    tokens << SectionToken.new(section)
    key_value.each { |lhs, rhs| tokens << KeyValueToken.new("#{lhs} = #{rhs}") }
  end

  @tokens = tokens
end

#to_sObject



16
17
18
# File 'lib/changit/lexer.rb', line 16

def to_s
  @tokens.map(&:to_s).join
end