File: prim.rb

package info (click to toggle)
ruby-rsec 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 2,130; lisp: 13; makefile: 3
file content (102 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (3)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module Rsec

  # primitive base
  module Prim
    def sign_strategy_to_pattern sign_strategy
      case sign_strategy
      when 3; '[\+\-]?'
      when 2; '\+?'
      when 1; '\-?'
      when 0; ''
      end
    end
  end

  # double precision float parser
  class PDouble < Binary
    include Prim

    def float_pattern sign_strategy, is_hex
      sign = sign_strategy_to_pattern sign_strategy
      if is_hex
        /#{sign}0x[\da-f]+(\.[\da-f]+)?/i
      else
        /#{sign}\d+(\.\d+)?(e[\+\-]?\d+)?/i
      end
    end

    def initialize sign_strategy, is_hex
      self.left = float_pattern sign_strategy, is_hex
    end

    def _parse ctx
      if (d = ctx.scan left)
        d = Float(d)
        return d if d.finite?
      end
      INVALID
    end
  end

  # primitive int parser commons
  class PInt < Binary
    include Prim

    def int_pattern sign_strategy, base
      sign = sign_strategy_to_pattern sign_strategy
      if base > 10
        d_hi = 9
        char_range = "a-#{('a'.ord + base - 11).chr}"
      else
        d_hi = base - 1
        char_range = ''
      end
      /#{sign}[0-#{d_hi}#{char_range}]+/i
    end

    def _parse ctx
      if (d = ctx.scan left)
        d = d.to_i @base
        return d if right.include?(d)
      end
      INVALID
    end
  end

  # 32-bit int parser
  class PInt32 < PInt
    def initialize sign_strategy, base
      @base = base
      self.left = int_pattern sign_strategy, base
      self.right = (-(1<<31))..((1<<31)-1)
    end
  end

  # unsigned 32 bit int parser
  class PUnsignedInt32 < PInt
    def initialize sign_strategy, base
      @base = base
      self.left = int_pattern sign_strategy, base
      self.right = 0...(1<<32)
    end
  end

  # 64-bit int parser
  class PInt64 < PInt
    def initialize sign_strategy, base
      @base = base
      self.left = int_pattern sign_strategy, base
      self.right = (-(1<<63))..((1<<63)-1)
    end
  end

  # unsigned 64-bit int parser
  class PUnsignedInt64 < PInt
    def initialize sign_strategy, base
      @base = base
      self.left = int_pattern sign_strategy, base
      self.right = 0...(1<<64)
    end
  end

end