# 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
                        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:
                yytl1 = yyt1
                yytl2 = yyt2
                yytr2 = yyt3
                yytl3 = yyt5
                yytr3 = yyt4
                yytl0 = yyt1
                yytr0 = yycursor
                yytr1 = yyt2
                yytr1 -= 1
                major = int(yyinput[yytl1:yytr1])
                minor = int(yyinput[yytl2:yytr2])
                patch = 0 if yytl3 == NONE else int(yyinput[yytl3 + 1:yytr3])
                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
