<commit_before>#! /usr/bin/env ruby

require 'rubygems'
require 'rattler'

class Calculator < Rattler::Runtime::ExtendedPackratParser
  grammar %{
    %whitespace SPACE*

    start   <-  (expr / quit) EOF

    expr    <-  expr ~'+' term                  {|a,b| a + b }
              / expr ~'-' term                  {|a,b| a - b }
              / term

    term    <-  term ~'*' primary               {|a,b| a * b }
              / term ~'/' primary               {|a,b| a / b }
              / primary

    primary <-  ~'(' expr ~')'
              / @('-'? DIGIT+ ('.' DIGIT+)?)    { _.to_f }

    quit    <-  'quit'                          { abort "\\n" }
  }
end

loop do
  begin
    puts
    print "> "
    puts Calculator.parse!(gets)
  rescue Rattler::Runtime::SyntaxError => e
    puts e
  end
end<commit_msg>calc example exits cleanly on end of input<commit_after>#! /usr/bin/env ruby

require 'rubygems'
require 'rattler'

class Calculator < Rattler::Runtime::ExtendedPackratParser
  grammar %{
    %whitespace SPACE*

    start   <-  (expr / quit) EOF

    expr    <-  expr ~'+' term                  {|a,b| a + b }
              / expr ~'-' term                  {|a,b| a - b }
              / term

    term    <-  term ~'*' primary               {|a,b| a * b }
              / term ~'/' primary               {|a,b| a / b }
              / primary

    primary <-  ~'(' expr ~')'
              / @('-'? DIGIT+ ('.' DIGIT+)?)    { _.to_f }

    quit    <-  'quit'                          { abort "\\n" }
  }
end

loop do
  begin
    puts
    print "> "
    break unless (s = gets)
    puts Calculator.parse!(s)
  rescue Rattler::Runtime::SyntaxError => e
    puts e
  end
end
puts
{'fraction_non_alphanumeric': 0.15162241887905606, 'fraction_numerical': 0.0, 'mean_word_length': 1.3043478260869565, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5347980', 'n_tokens_mistral': 610, 'n_tokens_neox': 566, 'n_words': 141}