<commit_before># A small example that demonstrates the power of tree pattern matching. Also
# uses '.as(:name)' to construct a tree that can reliably be matched
# afterwards. 

$:.unshift '../lib'

require 'pp'
require 'parslet'

module LISP # as in 'lots of insipid and stupid parenthesis'
  class Parser < Parslet::Parser
    rule(:balanced) {
      str('(').as(:l) >> balanced.maybe.as(:m) >> str(')').as(:r)
    }
  
    root(:balanced)
  end

  class Transform < Parslet::Transform
    rule(:l => '(', :m => simple(:x), :r => ')') { 
      # innermost :m will contain nil
      x.nil? ? 1 : x+1
    }
  end
end

parser = LISP::Parser.new
transform = LISP::Transform.new
%w!
  ()
  (())
  ((((()))))
  ((())
!.each do |pexp|
  begin
    result = parser.parse(pexp)
    puts "#{"%20s"%pexp}: #{result.inspect} (#{transform.apply(result)} parens)"
  rescue Parslet::ParseFailed => m
    puts "#{"%20s"%pexp}: #{m}"
  end
  puts
end
<commit_msg>. fix example parslet loading<commit_after># A small example that demonstrates the power of tree pattern matching. Also
# uses '.as(:name)' to construct a tree that can reliably be matched
# afterwards. 

$:.unshift File.join(
  File.dirname(__FILE__), 
  '/../lib')

require 'pp'
require 'parslet'

module LISP # as in 'lots of insipid and stupid parenthesis'
  class Parser < Parslet::Parser
    rule(:balanced) {
      str('(').as(:l) >> balanced.maybe.as(:m) >> str(')').as(:r)
    }
  
    root(:balanced)
  end

  class Transform < Parslet::Transform
    rule(:l => '(', :m => simple(:x), :r => ')') { 
      # innermost :m will contain nil
      x.nil? ? 1 : x+1
    }
  end
end

parser = LISP::Parser.new
transform = LISP::Transform.new
%w!
  ()
  (())
  ((((()))))
  ((())
!.each do |pexp|
  begin
    result = parser.parse(pexp)
    puts "#{"%20s"%pexp}: #{result.inspect} (#{transform.apply(result)} parens)"
  rescue Parslet::ParseFailed => m
    puts "#{"%20s"%pexp}: #{m}"
  end
  puts
end

{'fraction_non_alphanumeric': 0.1882051282051282, 'fraction_numerical': 0.006153846153846154, 'mean_word_length': 3.2598253275109172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '1499975', 'n_tokens_mistral': 713, 'n_tokens_neox': 690, 'n_words': 190}