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

# expect a null-terminated string
def lex(yyinput):
    yycursor = 0
    yylimit = len(yyinput) - 1 # terminating null not included
    count = 0

    while True: 
        yystate = 0
        while True:
            match yystate:
                case 0:
                    yych = yyinput[yycursor]
                    if yych <= 0x20:
                        if yych <= 0x00:
                            if yylimit <= yycursor:
                                yystate = 9
                                continue
                            yycursor += 1
                            yystate = 1
                            continue
                        if yych >= 0x20:
                            yycursor += 1
                            yystate = 3
                            continue
                        yycursor += 1
                        yystate = 1
                        continue
                    else:
                        if yych == 0x27:
                            yycursor += 1
                            yystate = 5
                            continue
                        yycursor += 1
                        yystate = 1
                        continue
                case 1:
                    yystate = 2
                    continue
                case 2:
                    return -1
                case 3:
                    yych = yyinput[yycursor]
                    if yych <= 0x00:
                        yystate = 4
                        continue
                    if yych == 0x20:
                        yycursor += 1
                        yystate = 3
                        continue
                    yystate = 4
                    continue
                case 4:
                    break
                case 5:
                    yymarker = yycursor
                    yych = yyinput[yycursor]
                    if yych >= 0x01:
                        yystate = 7
                        continue
                    if yylimit <= yycursor:
                        yystate = 2
                        continue
                    yycursor += 1
                    yystate = 6
                    continue
                case 6:
                    yych = yyinput[yycursor]
                    yystate = 7
                    continue
                case 7:
                    if yych <= 0x27:
                        if yych <= 0x00:
                            if yylimit <= yycursor:
                                yystate = 10
                                continue
                            yycursor += 1
                            yystate = 6
                            continue
                        if yych <= 0x26:
                            yycursor += 1
                            yystate = 6
                            continue
                        yycursor += 1
                    else:
                        if yych == 0x5C:
                            yycursor += 1
                            yystate = 8
                            continue
                        yycursor += 1
                        yystate = 6
                        continue
                    count += 1
                    break
                case 8:
                    yych = yyinput[yycursor]
                    if yych <= 0x00:
                        if yylimit <= yycursor:
                            yystate = 10
                            continue
                        yycursor += 1
                        yystate = 6
                        continue
                    yycursor += 1
                    yystate = 6
                    continue
                case 9:
                    return count
                case 10:
                    yycursor = yymarker
                    yystate = 2
                    continue
                case _:
                    raise "internal lexer error"


def test(str, count):
    # termunating null not included in `lim`
    assert count == lex(str)

test(b"\0", 0);
test(b"'qu\0tes' 'are' 'fine: \\'' \0", 3);
test(b"'unterminated\\'\0", -1)
