File: parse_u32_conditions.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (53 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download | duplicates (2)
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
# re2py $INPUT -o $OUTPUT -c

%{conditions %}

def parse_u32(yyinput):
    yycursor = 0
    yycond = YYC_INIT
    num = 0

    while True: %{
        re2c:yyfill:enable = 0;
        re2c:indent:top = 2;

        <INIT> '0b' / [01]        :=> BIN
        <INIT> "0"                :=> OCT
        <INIT> "" / [1-9]         :=> DEC
        <INIT> '0x' / [0-9a-fA-F] :=> HEX
        <INIT> * { return None }

        <BIN> [01] {
            num = num * 2 + (yyinput[yycursor - 1] - 48)
            break
        }
        <OCT> [0-7] {
            num = num * 8 + (yyinput[yycursor - 1] - 48)
            break
        }
        <DEC> [0-9] {
            num = num * 10 + (yyinput[yycursor - 1] - 48)
            break
        }
        <HEX> [0-9] {
            num = num * 16 + (yyinput[yycursor - 1] - 48)
            break
        }
        <HEX> [a-f] {
            num = num * 16 + (yyinput[yycursor - 1] - 87)
            break
        }
        <HEX> [A-F] {
            num = num * 16 + (yyinput[yycursor - 1] - 55)
            break
        }

        <BIN, OCT, DEC, HEX> * { return num }
    %}

assert parse_u32(b"\0") == None
assert parse_u32(b"1234567890\0") == 1234567890
assert parse_u32(b"0b1101\0") == 13
assert parse_u32(b"0x7Fe\0") == 2046
assert parse_u32(b"0644\0") == 420
assert parse_u32(b"9999999999\0") == 9999999999