<commit_before>require 'cgi'

module FlavourSaver
  UnknownNodeTypeException          = Class.new(StandardError)
  UnknownContextException           = Class.new(StandardError)
  InappropriateUseOfElseException   = Class.new(StandardError)
  UndefinedPrivateVariableException = Class.new(StandardError)
  UnknownHelperException            = Class.new(RuntimeError)
  class Runtime

    attr_accessor :context, :parent, :ast

    def self.run(ast, context, locals={}, helpers=[])
      self.new(ast, context, locals, helpers).to_s
    end

    def initialize(ast, context=nil, locals={}, helpers=[],parent=nil)
      @ast = ast
      @locals = locals
      @helpers = helpers
      @context = context
      @parent = parent
      @privates = {}
    end

    def to_s(tmp_context = nil,privates={})
      result = nil
      if tmp_context
        old_context = @context
        @context = tmp_context
        old_privates = @privates
        @privates = @privates.dup.merge(privates) if privates.any?
        result = evaluate_node(@ast)
        @privates = old_privates
        @context = old_context
      else
        result = evaluate_node(@ast)
      end
      result
    end

    def private_variable_set(name,value)
      @privates[name.to_s] = value
    end

    def private_variable_get(name)
      begin
        @privates.fetch(name)
      rescue KeyError => e
        raise UndefinedPrivateVariableException, "private variable not found @#{name}"
      end
    end

    def strip(tmp_context = nil)
      self.to_s(tmp_context).gsub(/[\s\r\n]+/,' ').strip
    end

    def evaluate_node(node)
      case node
      when TemplateNode
        node.items.map { |n| evaluate_node(n) }.join('')
      when BlockExpressionNode
        evaluate_block(node).to_s
      when OutputNode
        node.value
      when NumberNode
        if node.value =~ /\./
          node.value.to_f
        else
          node.value.to_i
        end
      when ValueNode
        node.value
      when SafeExpressionNode
        evaluate_expression(node).to_s
      when ExpressionNode
        escape(evaluate_expression(node).to_s)
      when CallNode
        evaluate_call(node)
      when Hash
        node.each do |key,value|
          node[key] = evaluate_argument(value)
        end
        node
      when CommentNode
        ''
      when PartialNode
        evaluate_partial(node)
      else
        raise UnknownNodeTypeException, "Don't know how to deal with a node of type #{node.class.to_s.inspect}."
      end
    end

    def parent
      raise UnknownContextException, "No parent context in which to evaluate the parentiness of the context" unless @parent
      @parent
    end

    def parent?
      !!@parent
    end

    def evaluate_partial(node)
      _context = context
      _context = evaluate_argument(node.context) if node.context
      if defined?(::Rails)
        context.send(:render, :partial => node.name, :object => _context)
      else
        partial = Partial.fetch(node.name)
        if partial.respond_to? :call
          partial.call(_context)
        else
          create_child_runtime(partial).to_s(_context)
        end
      end
    end

    def evaluate_call(call, context=context, &block)
      context = Helpers.decorate_with(context,@helpers,@locals) unless context.is_a? Helpers::Decorator
      case call
      when ParentCallNode
        depth = call.depth
        (2..depth).inject(parent) { |p| p.parent }.evaluate_call(call.to_callnode,&block)<commit_msg>fixes circular arg ref on ruby 2.2.1<commit_after>require 'cgi'

module FlavourSaver
  UnknownNodeTypeException          = Class.new(StandardError)
  UnknownContextException           = Class.new(StandardError)
  InappropriateUseOfElseException   = Class.new(StandardError)
  UndefinedPrivateVariableException = Class.new(StandardError)
  UnknownHelperException            = Class.new(RuntimeError)
  class Runtime

    attr_accessor :context, :parent, :ast

    def self.run(ast, context, locals={}, helpers=[])
      self.new(ast, context, locals, helpers).to_s
    end

    def initialize(ast, context=nil, locals={}, helpers=[],parent=nil)
      @ast = ast
      @locals = locals
      @helpers = helpers
      @context = context
      @parent = parent
      @privates = {}
    end

    def to_s(tmp_context = nil,privates={})
      result = nil
      if tmp_context
        old_context = @context
        @context = tmp_context
        old_privates = @privates
        @privates = @privates.dup.merge(privates) if privates.any?
        result = evaluate_node(@ast)
        @privates = old_privates
        @context = old_context
      else
        result = evaluate_node(@ast)
      end
      result
    end

    def private_variable_set(name,value)
      @privates[name.to_s] = value
    end

    def private_variable_get(name)
      begin
        @privates.fetch(name)
      rescue KeyError => e
        raise UndefinedPrivateVariableException, "private variable not found @#{name}"
      end
    end

    def strip(tmp_context = nil)
      self.to_s(tmp_context).gsub(/[\s\r\n]+/,' ').strip
    end

    def evaluate_node(node)
      case node
      when TemplateNode
        node.items.map { |n| evaluate_node(n) }.join('')
      when BlockExpressionNode
        evaluate_block(node).to_s
      when OutputNode
        node.value
      when NumberNode
        if node.value =~ /\./
          node.value.to_f
        else
          node.value.to_i
        end
      when ValueNode
        node.value
      when SafeExpressionNode
        evaluate_expression(node).to_s
      when ExpressionNode
        escape(evaluate_expression(node).to_s)
      when CallNode
        evaluate_call(node)
      when Hash
        node.each do |key,value|
          node[key] = evaluate_argument(value)
        end
        node
      when CommentNode
        ''
      when PartialNode
        evaluate_partial(node)
      else
        raise UnknownNodeTypeException, "Don't know how to deal with a node of type #{node.class.to_s.inspect}."
      end
    end

    def parent
      raise UnknownContextException, "No parent context in which to evaluate the parentiness of the context" unless @parent
      @parent
    end

    def parent?
      !!@parent
    end

    def evaluate_partial(node)
      _context = context
      _context = evaluate_argument(node.context) if node.context
      if defined?(::Rails)
        context.send(:render, :partial => node.name, :object => _context)
      else
        partial = Partial.fetch(node.name)
        if partial.respond_to? :call
          partial.call(_context)
        else
          create_child_runtime(partial).to_s(_context)
        end
      end
    end

    def evaluate_call(call, context=self.context, &block)
      context = Helpers.decorate_with(context,@helpers,@locals) unless context.is_a? Helpers::Decorator
      case call
      when ParentCallNode
        depth = call.depth
        (2..depth).inject(parent) { |p| p.parent }.evaluate_call(call.to_callnode,&block)
{'fraction_non_alphanumeric': 0.08889840182648402, 'fraction_numerical': 0.0007134703196347032, 'mean_word_length': 2.3281101614434947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5497606', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2028, 'n_words': 483}