Sha256: 75bdd94e936336e11f3bd2415ed18a209aaf3803009908468271395ccd3adbe7

Contents?: true

Size: 1.59 KB

Versions: 41

Compression:

Stored size: 1.59 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Transforms

    class VariableRule < Rule
      class Parser < TwitterCldr::Parsers::Parser
        private

        def do_parse(options)
          var_name = name
          next_token(:equals)
          var_value = value
          [var_name, var_value]
        end

        def name
          current_token.value.tap do
            next_token(:variable)
          end
        end

        def value
          [].tap do |value_parts|
            until current_token.type == :semicolon
              value_parts << current_token
              next_token(current_token.type)
            end
          end
        end
      end

      class << self
        def parse(rule_text, symbol_table, index)
          cleaned_rule_text = Rule.remove_comment(rule_text)
          tokens = tokenizer.tokenize(cleaned_rule_text)
          var_name, value_tokens = parser.parse(tokens)

          VariableRule.new(
            var_name, replace_symbols(
              value_tokens, symbol_table
            )
          )
        end

        def accepts?(rule_text)
          !!(rule_text =~ /\A[\s]*\$[\w]+[\s]*=/)
        end

        private

        def parser
          @parser ||= Parser.new
        end

        def tokenizer
          @tokenizer ||= TwitterCldr::Transforms::Tokenizer.new
        end
      end

      attr_reader :name, :value_tokens

      def initialize(name, value)
        @name = name
        @value_tokens = value
      end

      def is_variable?
        true
      end
    end

  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
twitter_cldr-6.12.1 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.12.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.5 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.4 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.3 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.2 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.1 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.11.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.10.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.9.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.8.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.7.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.6.2 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.6.1 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.6.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.5.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.4.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.3.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.2.0 lib/twitter_cldr/transforms/variable_rule.rb
twitter_cldr-6.1.0 lib/twitter_cldr/transforms/variable_rule.rb