File: 04_posix_captures.py

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 (168 lines) | stat: -rw-r--r-- 5,261 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Generated by re2py
# re2py $INPUT -o $OUTPUT

from collections import namedtuple

SemVer = namedtuple('SemVer', 'major minor patch')

# Maximum number of capturing groups among all rules.
YYMAXNMATCH = 4

NONE = -1

def parse(yyinput):
    yycursor = 0

    # A list for capturing parentheses (twice the number of groups).
    yypmatch = [None] * (YYMAXNMATCH * 2)


    yystate = 0
    while True:
        match yystate:
            case 0:
                yych = yyinput[yycursor]
                if yych <= 0x2F:
                    yycursor += 1
                    yystate = 1
                    continue
                if yych <= 0x39:
                    yyt1 = yycursor
                    yycursor += 1
                    yystate = 3
                    continue
                yycursor += 1
                yystate = 1
                continue
            case 1:
                yystate = 2
                continue
            case 2:
                return None
            case 3:
                yymarker = yycursor
                yych = yyinput[yycursor]
                if yych == 0x2E:
                    yycursor += 1
                    yystate = 4
                    continue
                if yych <= 0x2F:
                    yystate = 2
                    continue
                if yych <= 0x39:
                    yycursor += 1
                    yystate = 6
                    continue
                yystate = 2
                continue
            case 4:
                yych = yyinput[yycursor]
                if yych <= 0x2F:
                    yystate = 5
                    continue
                if yych <= 0x39:
                    yyt2 = yycursor
                    yycursor += 1
                    yystate = 7
                    continue
                yystate = 5
                continue
            case 5:
                yycursor = yymarker
                yystate = 2
                continue
            case 6:
                yych = yyinput[yycursor]
                if yych == 0x2E:
                    yycursor += 1
                    yystate = 4
                    continue
                if yych <= 0x2F:
                    yystate = 5
                    continue
                if yych <= 0x39:
                    yycursor += 1
                    yystate = 6
                    continue
                yystate = 5
                continue
            case 7:
                yych = yyinput[yycursor]
                if yych <= 0x2E:
                    if yych <= 0x00:
                        yyt3 = yycursor
                        yyt4 = -1
                        yyt5 = -1
                        yycursor += 1
                        yystate = 8
                        continue
                    if yych <= 0x2D:
                        yystate = 5
                        continue
                    yyt3 = yycursor
                    yyt5 = yycursor
                    yycursor += 1
                    yystate = 9
                    continue
                else:
                    if yych <= 0x2F:
                        yystate = 5
                        continue
                    if yych <= 0x39:
                        yycursor += 1
                        yystate = 7
                        continue
                    yystate = 5
                    continue
            case 8:
                yynmatch = 4
                yypmatch[2] = yyt1
                yypmatch[4] = yyt2
                yypmatch[5] = yyt3
                yypmatch[6] = yyt5
                yypmatch[7] = yyt4
                yypmatch[0] = yyt1
                yypmatch[1] = yycursor
                yypmatch[3] = yyt2
                yypmatch[3] -= 1
                # `yynmatch` is the number of capturing groups
                assert yynmatch == 4
                # Even `yypmatch` values are for opening parentheses, odd values
                # are for closing parentheses, the first group is the whole match.
                major = int(yyinput[yypmatch[2]:yypmatch[3]])
                minor = int(yyinput[yypmatch[4]:yypmatch[5]])
                patch = 0 if yypmatch[6] == NONE else int(yyinput[yypmatch[6] + 1:yypmatch[7]])
                return SemVer(major, minor, patch)
            case 9:
                yych = yyinput[yycursor]
                if yych <= 0x00:
                    yystate = 5
                    continue
                yystate = 11
                continue
            case 10:
                yych = yyinput[yycursor]
                yystate = 11
                continue
            case 11:
                if yych <= 0x00:
                    yyt4 = yycursor
                    yycursor += 1
                    yystate = 8
                    continue
                if yych <= 0x2F:
                    yystate = 5
                    continue
                if yych <= 0x39:
                    yycursor += 1
                    yystate = 10
                    continue
                yystate = 5
                continue
            case _:
                raise "internal lexer error"


assert parse(b"23.34\0") == SemVer(23, 34, 0)
assert parse(b"1.2.99999\0") == SemVer(1, 2, 99999)
assert parse(b"1.a\0") == None