File: mathn.rb

package info (click to toggle)
ruby-parslet 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,260 kB
  • sloc: ruby: 6,157; sh: 8; javascript: 3; makefile: 3
file content (47 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Demonstrates that we have a compatibility fix to mathn's weird idea of 
# integer mathematics. 
# This was contributed by Jonathan Hinkle (https://github.com/hynkle). Thanks!

$:.unshift File.dirname(__FILE__) + "/../lib"

require 'parslet'
require 'parslet/convenience'
include Parslet

def attempt_parse
  possible_whitespace = match['\s'].repeat

  cephalopod =
    str('octopus') |
    str('squid')

  parenthesized_cephalopod =
    str('(') >>
    possible_whitespace >>
    cephalopod >>
    possible_whitespace >>
    str(')')

  parser =
    possible_whitespace >>
    parenthesized_cephalopod >>
    possible_whitespace

  # This parse fails, but that is not the point. When mathn is in the current
  # ruby environment, it modifies integer division in a way that makes 
  # parslet loop indefinitely.
  parser.parse %{(\nsqeed)\n}
rescue Parslet::ParseFailed
end

attempt_parse 
puts 'it terminates before we require mathn'

puts "requiring mathn now"
# mathn was deprecated as of Ruby 2.5
if RUBY_VERSION.gsub(/[^\d]/, '').to_i < 250
  require 'mathn'
end
puts "and trying again (will hang without the fix)"
attempt_parse # but it doesn't terminate after requiring mathn
puts "okay!"