<commit_before> #
 #     _|      _|  _|              _|
 #     _|_|  _|_|        _|_|_|  _|_|_|_|
 #     _|  _|  _|  _|  _|_|        _|
 #     _|      _|  _|      _|_|    _|
 #     _|      _|  _|  _|_|_|        _|_|
 #
 #             MIST BUILD SYSTEM
 # Copyright (c) 2015 On Demand Solutions, inc.

MistPostParser = require './mist-post-parser'
chalk = require 'chalk' # XXX DEBUG

## XXX DEBUG
inspect = (v)->console.log require('util').inspect v, colors: on, depth: null

expand = (str, vars)->
  str.replace /\$\(\s*([a-z0-9_]+)\s*\)/gi, (m, name, offset)->
    if not vars[name]? then throw {
      message: "variable not defined: #{name}"
      column: offset + 3 # +1 for 1-based offset, +2 for $(
    }
    return vars[name]

doVariableAssignment = (line, enabled, vars)->
  
  return line

filters = [
  doVariableAssignment
]

processLines = (lines, map, options)->
  enabled = [yes]
  enabled.isEnabled = -> @[@.length - 1]
  vars = options.vars || options.vars = {}
  lines = lines.map (line, i)->
    try
      line = expand line, vars
    catch e
      e.line = i
      throw e
    for filter in filters
      line = filter line, enabled, vars, options
      if not line then return null
    return line
  lines = lines.filter (line, i)-><commit_msg>added (recursive) variable assignment/expansion<commit_after> #
 #     _|      _|  _|              _|
 #     _|_|  _|_|        _|_|_|  _|_|_|_|
 #     _|  _|  _|  _|  _|_|        _|
 #     _|      _|  _|      _|_|    _|
 #     _|      _|  _|  _|_|_|        _|_|
 #
 #             MIST BUILD SYSTEM
 # Copyright (c) 2015 On Demand Solutions, inc.

MistPostParser = require './mist-post-parser'
chalk = require 'chalk' # XXX DEBUG

## XXX DEBUG
inspect = (v)->console.log require('util').inspect v, colors: on, depth: null

expand = (str, vars)->
  perform = ->
    str = str.replace /\$\(\s*([a-z0-9_]+)\s*\)/gi, (m, name, offset)->
      if not vars[name]? then throw {
        message: "variable not defined: #{name}"
        column: offset + 3 # +1 for 1-based offset, +2 for $(
      }
      return vars[name]
  loop
    break if str is perform()
  return str

doVariableAssignment = (line, enabled, vars)->
  m = line.match /^[\s\t]*([a-z0-9_]+)[\s\t]*(\+?=)[\s\t]*(.+?)[\s\t]*$/i
  if m
    switch m[2]
      when '=' then vars[m[1]] = m[3]
      when '+=' then vars[m[1]] = "#{vars[m[1]]} #{m[3]}"
  return line

filters = [
  doVariableAssignment
]

processLines = (lines, map, options)->
  enabled = [yes]
  enabled.isEnabled = -> @[@.length - 1]
  vars = options.vars || options.vars = {}
  lines = lines.map (line, i)->
    try
      line = expand line, vars
    catch e
      e.line = i
      throw e
    for filter in filters
      line = filter line, enabled, vars, options
      if not line then return null
    return line
  lines = lines.filter (line, i)->
{'fraction_non_alphanumeric': 0.1691928093056045, 'fraction_numerical': 0.010574550581600282, 'mean_word_length': 2.0915032679738563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '4803265', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1013, 'n_words': 258}