# Generated by re2py
# re2py $INPUT -o $OUTPUT

from collections import namedtuple

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

NONE = -1

def parse(yyinput):
    yycursor = 0

    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
                        yycursor += 1
                        yystate = 8
                        continue
                    if yych <= 0x2D:
                        yystate = 5
                        continue
                    yyt3 = 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:
                t1 = yyt1
                t3 = yyt2
                t4 = yyt3
                t5 = yyt4
                t2 = yyt2
                t2 -= 1
                major = int(yyinput[t1:t2])
                minor = int(yyinput[t3:t4])
                patch = int(yyinput[t5:yycursor - 1]) if t5 != NONE else 0
                return SemVer(major, minor, patch)
            case 9:
                yych = yyinput[yycursor]
                if yych <= 0x2F:
                    yystate = 5
                    continue
                if yych >= 0x3A:
                    yystate = 5
                    continue
                yyt4 = yycursor
                yycursor += 1
                yystate = 10
                continue
            case 10:
                yych = yyinput[yycursor]
                if yych <= 0x00:
                    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
