// Code generated by re2v, DO NOT EDIT.
//line "codegen/v/loop_switch/advanced.re":1
// re2v $INPUT -o $OUTPUT -cf --loop-switch -Wno-nondeterministic-tags

import arrays
import log
import os

//line "codegen/v/loop_switch/advanced.v":10
const yycmedia_type = 0
const yycheader = 22
//line "codegen/v/loop_switch/advanced.re":7


// Use a small buffer to cover the case when a lexeme doesn't fit.
// In real world use a larger buffer.
const bufsize = 100

const mtag_root = -1
const tag_none = -1

// An m-tag tree is a way to store histories with an O(1) copy operation.
// Histories naturally form a tree, as they have common start and fork at some
// point. The tree is stored as an array of pairs (tag value, link to parent).
// An m-tag is represented with a single link in the tree (array index).
struct MtagElem {
    elem int
    pred int
}
type MtagTrie = []MtagElem

// Append a single value to an m-tag history.
fn add_mtag(mut trie &MtagTrie, mtag int, value int) int {
    trie = arrays.concat(trie, MtagElem{value, mtag})
    return trie.len - 1
}

// Recursively unwind tag histories and collect version components.
fn unwind(trie MtagTrie, x int, y int, str []u8) []string {
    // Reached the root of the m-tag tree, stop recursion.
    if x == mtag_root && y == mtag_root {
        return []
    }

    // Unwind history further.
    mut result := unwind(trie, trie[x].pred, trie[y].pred, str)

    // Get tag values. Tag histories must have equal length.
    if x == mtag_root || y == mtag_root {
        panic("tag histories have different length")
    }
    ex := trie[x].elem
    ey := trie[y].elem

    if ex != tag_none && ey != tag_none {
        // Both tags are valid string indices, extract component.
        result = arrays.concat(result, str[ex..ey].str())
    } else if !(ex == tag_none && ey == tag_none) {
        panic("both tags should be tagNone")
    }
    return result
}

struct State {
mut:
    file   os.File
    buf    []u8
    cur    int
    mar    int
    tok    int
    lim    int
    cond   int
    state  int
    trie   MtagTrie
    
//line "codegen/v/loop_switch/advanced.v":77

	yyt1 int
	yyt2 int
//line "codegen/v/loop_switch/advanced.re":69

    
//line "codegen/v/loop_switch/advanced.v":84

	yytm1 int
	yytm10 int
	yytm2 int
	yytm3 int
	yytm4 int
	yytm5 int
	yytm6 int
	yytm7 int
	yytm8 int
	yytm9 int
//line "codegen/v/loop_switch/advanced.re":70

    accept int
}

enum Status {
    lex_end
    lex_ready
    lex_waiting
    lex_bad_packet
    lex_big_packet
}

fn fill(mut st &State) Status {
    shift := st.tok
    used := st.lim - st.tok
    free := bufsize - used

    // Error: no space. In real life can reallocate a larger buffer.
    if free < 1 { return .lex_big_packet }

    // Shift buffer contents (discard already processed data).
    copy(mut &st.buf, st.buf[shift..shift+used])
    st.cur -= shift
    st.mar -= shift
    st.lim -= shift
    st.tok -= shift
    
//line "codegen/v/loop_switch/advanced.v":124

	if st.yyt1 != tag_none { st.yyt1 -= shift };
	if st.yyt2 != tag_none { st.yyt2 -= shift };
//line "codegen/v/loop_switch/advanced.re":96


    // Fill free space at the end of buffer with new data.
    pos := st.file.tell() or { 0 }
    if n := st.file.read_bytes_into(u64(pos), mut st.buf[st.lim..bufsize]) {
        st.lim += n
    }
    st.buf[st.lim] = 0 // append sentinel symbol

    return .lex_ready
}

fn lex(mut st &State) Status {
    mut yych := u8(0)
    mut l1 := tag_none
    mut l2 := tag_none
    mut f1 := mtag_root
    mut f2 := mtag_root
    mut p1 := mtag_root
    mut p2 := mtag_root
    mut p3 := mtag_root
    mut p4 := mtag_root
loop:
    
//line "codegen/v/loop_switch/advanced.v":153
    mut yystate := int(st.state)
yyl:
    for {
        match yystate {
            -1...0 {
                yych = st.buf[st.cur]
                match yych {
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.yyt1 = st.cur
                        st.cur += 1
                        yystate = 3
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 44
                            return .lex_waiting
                        }
                        st.cur += 1
                        yystate = 1
                        continue yyl
                    }
                }
            }
            1 {
                yystate = 2
                continue yyl
            }
            2 {
                st.state = yycmedia_type
//line "codegen/v/loop_switch/advanced.re":189
                return .lex_bad_packet
//line "codegen/v/loop_switch/advanced.v":186
            }
            3 {
                st.mar = st.cur
                yych = st.buf[st.cur]
                match yych {
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        yystate = 5
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 45
                            return .lex_waiting
                        }
                        yystate = 2
                        continue yyl
                    }
                }
            }
            4 {
                yych = st.buf[st.cur]
                yystate = 5
                continue yyl
            }
            5 {
                match yych {
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.cur += 1
                        yystate = 4
                        continue yyl
                    }
                    0x2F {
                        st.cur += 1
                        yystate = 7
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 46
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            6 {
                st.cur = st.mar
                yystate = 2
                continue yyl
            }
            7 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x09, 0x0D, 0x20, 0x3B {
                        if st.lim <= st.cur {
                            st.state = 47
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                    else {
                        yystate = 9
                        continue yyl
                    }
                }
            }
            8 {
                yych = st.buf[st.cur]
                yystate = 9
                continue yyl
            }
            9 {
                match yych {
                    0x09, 0x20 {
                        st.yytm6 = st.yytm10
                        st.yytm6 = add_mtag(mut &st.trie, st.yytm6, tag_none)
                        st.yytm5 = st.yytm9
                        st.yytm5 = add_mtag(mut &st.trie, st.yytm5, tag_none)
                        st.yytm4 = st.yytm8
                        st.yytm4 = add_mtag(mut &st.trie, st.yytm4, tag_none)
                        st.yytm3 = st.yytm7
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, tag_none)
                        st.yyt2 = st.cur
                        st.cur += 1
                        yystate = 10
                        continue yyl
                    }
                    0x0D {
                        st.yytm6 = st.yytm10
                        st.yytm6 = add_mtag(mut &st.trie, st.yytm6, tag_none)
                        st.yytm5 = st.yytm9
                        st.yytm5 = add_mtag(mut &st.trie, st.yytm5, tag_none)
                        st.yytm4 = st.yytm8
                        st.yytm4 = add_mtag(mut &st.trie, st.yytm4, tag_none)
                        st.yytm3 = st.yytm7
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, tag_none)
                        st.yyt2 = st.cur
                        st.cur += 1
                        yystate = 11
                        continue yyl
                    }
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.cur += 1
                        yystate = 8
                        continue yyl
                    }
                    0x3B {
                        st.yyt2 = st.cur
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 48
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            10 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 10
                        continue yyl
                    }
                    0x0D {
                        st.cur += 1
                        yystate = 11
                        continue yyl
                    }
                    0x3B {
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 49
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            11 {
                yych = st.buf[st.cur]
                match yych {
                    0x0A {
                        st.cur += 1
                        yystate = 13
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 50
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            12 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.yytm7 = add_mtag(mut &st.trie, st.yytm7, st.cur)
                        st.cur += 1
                        yystate = 14
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 51
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            13 {
                l1 = st.yyt1
                l2 = st.yyt2
                p1 = st.yytm3
                p2 = st.yytm4
                p3 = st.yytm5
                p4 = st.yytm6
                st.state = yycmedia_type
//line "codegen/v/loop_switch/advanced.re":167
                
            mt := st.buf[l1..l2].str()
            log.debug("media type: $mt")

            pnames := unwind(st.trie, p1, p2, st.buf)
            log.debug("pnames: $pnames")

            pvals := unwind(st.trie, p3, p4, st.buf)
            log.debug("pvals: $pvals")

            st.tok = st.cur
            unsafe { goto loop }

//line "codegen/v/loop_switch/advanced.v":403
            }
            14 {
                yych = st.buf[st.cur]
                match yych {
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.cur += 1
                        yystate = 14
                        continue yyl
                    }
                    0x3D {
                        st.yytm8 = add_mtag(mut &st.trie, st.yytm8, st.cur)
                        st.cur += 1
                        yystate = 15
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 52
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            15 {
                yych = st.buf[st.cur]
                match yych {
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.yytm9 = add_mtag(mut &st.trie, st.yytm9, st.cur)
                        st.cur += 1
                        yystate = 16
                        continue yyl
                    }
                    0x22 {
                        st.yytm9 = add_mtag(mut &st.trie, st.yytm9, st.cur)
                        st.cur += 1
                        yystate = 17
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 53
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            16 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.yytm10 = add_mtag(mut &st.trie, st.yytm10, st.cur)
                        st.cur += 1
                        yystate = 18
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = st.yytm7
                        st.yytm4 = st.yytm8
                        st.yytm5 = st.yytm9
                        st.yytm6 = st.yytm10
                        st.yytm6 = add_mtag(mut &st.trie, st.yytm6, st.cur)
                        st.cur += 1
                        yystate = 11
                        continue yyl
                    }
                    0x21, 0x23...0x27, 0x2A...0x2B, 0x2D...0x2E, 0x30...0x39, 0x41...0x5A, 0x5E...0x7A, 0x7C, 0x7E {
                        st.cur += 1
                        yystate = 16
                        continue yyl
                    }
                    0x3B {
                        st.yytm10 = add_mtag(mut &st.trie, st.yytm10, st.cur)
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 54
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            17 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x1F, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 55
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                    0x22 {
                        st.cur += 1
                        yystate = 19
                        continue yyl
                    }
                    0x5C {
                        st.cur += 1
                        yystate = 20
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 17
                        continue yyl
                    }
                }
            }
            18 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 18
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = st.yytm7
                        st.yytm4 = st.yytm8
                        st.yytm5 = st.yytm9
                        st.yytm6 = st.yytm10
                        st.cur += 1
                        yystate = 11
                        continue yyl
                    }
                    0x3B {
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 56
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            19 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.yytm10 = add_mtag(mut &st.trie, st.yytm10, st.cur)
                        st.cur += 1
                        yystate = 18
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = st.yytm7
                        st.yytm4 = st.yytm8
                        st.yytm5 = st.yytm9
                        st.yytm6 = st.yytm10
                        st.yytm6 = add_mtag(mut &st.trie, st.yytm6, st.cur)
                        st.cur += 1
                        yystate = 11
                        continue yyl
                    }
                    0x3B {
                        st.yytm10 = add_mtag(mut &st.trie, st.yytm10, st.cur)
                        st.cur += 1
                        yystate = 12
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 57
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                }
            }
            20 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 58
                            return .lex_waiting
                        }
                        yystate = 6
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 17
                        continue yyl
                    }
                }
            }
            21 {
                st.state = yycmedia_type
//line "codegen/v/loop_switch/advanced.re":190
                unsafe { goto end }
//line "codegen/v/loop_switch/advanced.v":611
            }
            22 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 59
                            return .lex_waiting
                        }
                        st.cur += 1
                        yystate = 23
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, st.cur)
                        st.cur += 1
                        yystate = 25
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 26
                        continue yyl
                    }
                }
            }
            23 {
                yystate = 24
                continue yyl
            }
            24 {
                st.state = yycheader
//line "codegen/v/loop_switch/advanced.re":189
                return .lex_bad_packet
//line "codegen/v/loop_switch/advanced.v":646
            }
            25 {
                st.accept = 0
                st.mar = st.cur
                yych = st.buf[st.cur]
                match yych {
                    0x0A {
                        st.cur += 1
                        yystate = 27
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 60
                            return .lex_waiting
                        }
                        yystate = 24
                        continue yyl
                    }
                }
            }
            26 {
                st.accept = 0
                st.mar = st.cur
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 61
                            return .lex_waiting
                        }
                        yystate = 24
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 29
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, st.cur)
                        st.cur += 1
                        yystate = 30
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 31
                        continue yyl
                    }
                }
            }
            27 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 32
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 62
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                }
            }
            28 {
                st.cur = st.mar
                if st.accept == 0 {
                    yystate = 24
                    continue yyl
                } else {
                    yystate = 37
                    continue yyl
                }
            }
            29 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 63
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 29
                        continue yyl
                    }
                    0x20 {
                        st.cur += 1
                        yystate = 31
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 33
                        continue yyl
                    }
                }
            }
            30 {
                yych = st.buf[st.cur]
                match yych {
                    0x0A {
                        st.cur += 1
                        yystate = 27
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 64
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                }
            }
            31 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 65
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 29
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, st.cur)
                        st.cur += 1
                        yystate = 30
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 31
                        continue yyl
                    }
                }
            }
            32 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 66
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 32
                        continue yyl
                    }
                    0x0D {
                        st.yytm5 = add_mtag(mut &st.trie, st.yytm5, st.cur)
                        st.yytm4 = st.yytm3
                        st.yytm4 = add_mtag(mut &st.trie, st.yytm4, st.cur)
                        st.cur += 1
                        yystate = 34
                        continue yyl
                    }
                    else {
                        st.yytm5 = add_mtag(mut &st.trie, st.yytm5, st.cur)
                        st.cur += 1
                        yystate = 35
                        continue yyl
                    }
                }
            }
            33 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 67
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x0D {
                        st.yytm3 = add_mtag(mut &st.trie, st.yytm3, st.cur)
                        st.cur += 1
                        yystate = 30
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 31
                        continue yyl
                    }
                }
            }
            34 {
                yych = st.buf[st.cur]
                match yych {
                    0x0A {
                        st.cur += 1
                        yystate = 36
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 68
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                }
            }
            35 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 69
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 38
                        continue yyl
                    }
                    0x0D {
                        st.yytm4 = st.yytm3
                        st.yytm4 = add_mtag(mut &st.trie, st.yytm4, st.cur)
                        st.cur += 1
                        yystate = 34
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 35
                        continue yyl
                    }
                }
            }
            36 {
                st.accept = 1
                st.mar = st.cur
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.yytm1 = st.yytm3
                        st.yytm2 = st.yytm5
                        st.yytm3 = st.yytm4
                        st.cur += 1
                        yystate = 32
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 70
                            return .lex_waiting
                        }
                        st.yytm1 = st.yytm3
                        st.yytm2 = st.yytm5
                        yystate = 37
                        continue yyl
                    }
                }
            }
            37 {
                f1 = st.yytm1
                f2 = st.yytm2
                st.state = yycheader
//line "codegen/v/loop_switch/advanced.re":181
                
            folds := unwind(st.trie, f1, f2, st.buf)
            log.debug("folds: $folds")

            st.tok = st.cur
            unsafe { goto loop }

//line "codegen/v/loop_switch/advanced.v":943
            }
            38 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 71
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 38
                        continue yyl
                    }
                    0x0D {
                        st.cur += 1
                        yystate = 39
                        continue yyl
                    }
                    0x20 {
                        st.cur += 1
                        yystate = 35
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 40
                        continue yyl
                    }
                }
            }
            39 {
                yych = st.buf[st.cur]
                match yych {
                    0x0A {
                        st.cur += 1
                        yystate = 41
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 72
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                }
            }
            40 {
                yych = st.buf[st.cur]
                match yych {
                    0x00, 0x01...0x08, 0x0A...0x0C, 0x0E...0x1E, 0x7F {
                        if st.lim <= st.cur {
                            st.state = 73
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                    0x09 {
                        st.cur += 1
                        yystate = 42
                        continue yyl
                    }
                    0x0D {
                        st.yytm4 = st.yytm3
                        st.yytm4 = add_mtag(mut &st.trie, st.yytm4, st.cur)
                        st.cur += 1
                        yystate = 34
                        continue yyl
                    }
                    else {
                        st.cur += 1
                        yystate = 35
                        continue yyl
                    }
                }
            }
            41 {
                st.yytm1 = st.yytm3
                st.yytm2 = st.yytm5
                yystate = 37
                continue yyl
            }
            42 {
                yych = st.buf[st.cur]
                match yych {
                    0x09, 0x20 {
                        st.cur += 1
                        yystate = 42
                        continue yyl
                    }
                    0x0D {
                        st.cur += 1
                        yystate = 39
                        continue yyl
                    }
                    else {
                        if st.lim <= st.cur {
                            st.state = 74
                            return .lex_waiting
                        }
                        yystate = 28
                        continue yyl
                    }
                }
            }
            43 {
                st.state = yycheader
//line "codegen/v/loop_switch/advanced.re":190
                unsafe { goto end }
//line "codegen/v/loop_switch/advanced.v":1059
            }
            44 {
                if st.lim <= st.cur {
                    yystate = 21
                    continue yyl
                }
                yystate = 0
                continue yyl
            }
            45 {
                if st.lim <= st.cur {
                    yystate = 2
                    continue yyl
                }
                yystate = 3
                continue yyl
            }
            46 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 4
                continue yyl
            }
            47 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 7
                continue yyl
            }
            48 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 8
                continue yyl
            }
            49 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 10
                continue yyl
            }
            50 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 11
                continue yyl
            }
            51 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 12
                continue yyl
            }
            52 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 14
                continue yyl
            }
            53 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 15
                continue yyl
            }
            54 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 16
                continue yyl
            }
            55 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 17
                continue yyl
            }
            56 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 18
                continue yyl
            }
            57 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 19
                continue yyl
            }
            58 {
                if st.lim <= st.cur {
                    yystate = 6
                    continue yyl
                }
                yystate = 20
                continue yyl
            }
            59 {
                if st.lim <= st.cur {
                    yystate = 43
                    continue yyl
                }
                yystate = 22
                continue yyl
            }
            60 {
                if st.lim <= st.cur {
                    yystate = 24
                    continue yyl
                }
                yystate = 25
                continue yyl
            }
            61 {
                if st.lim <= st.cur {
                    yystate = 24
                    continue yyl
                }
                yystate = 26
                continue yyl
            }
            62 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 27
                continue yyl
            }
            63 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 29
                continue yyl
            }
            64 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 30
                continue yyl
            }
            65 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 31
                continue yyl
            }
            66 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 32
                continue yyl
            }
            67 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 33
                continue yyl
            }
            68 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 34
                continue yyl
            }
            69 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 35
                continue yyl
            }
            70 {
                if st.lim <= st.cur {
                    st.yytm1 = st.yytm3
                    st.yytm2 = st.yytm5
                    yystate = 37
                    continue yyl
                }
                yystate = 36
                continue yyl
            }
            71 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 38
                continue yyl
            }
            72 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 39
                continue yyl
            }
            73 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 40
                continue yyl
            }
            74 {
                if st.lim <= st.cur {
                    yystate = 28
                    continue yyl
                }
                yystate = 42
                continue yyl
            }
            else { panic("internal lexer error") }
        }
    }
//line "codegen/v/loop_switch/advanced.re":191

end:
    return .lex_end
}

fn test(expect Status, packets []string) {
    // Create a "socket" (open the same file for reading and writing).
    fname := "pipe"
    mut fw := os.create(fname) or { panic("cannot create file") }
    mut fr := os.open(fname) or { panic("cannot open file") }

    // Initialize lexer state: `state` value is -1, all offsets are at the end
    // of buffer.
    mut st := &State{
        file:   fr,
        // Sentinel at `lim` offset is set to zero, which triggers YYFILL.
        buf:    []u8{len: bufsize + 1},
        cur:    bufsize,
        mar:    bufsize,
        tok:    bufsize,
        lim:    bufsize,
        cond:   yycmedia_type,
        state:  -1,
        trie:   []MtagElem{},
        
//line "codegen/v/loop_switch/advanced.v":1340

		yyt1: tag_none,
		yyt2: tag_none,
//line "codegen/v/loop_switch/advanced.re":215

        
//line "codegen/v/loop_switch/advanced.v":1347

		yytm1: mtag_root,
		yytm10: mtag_root,
		yytm2: mtag_root,
		yytm3: mtag_root,
		yytm4: mtag_root,
		yytm5: mtag_root,
		yytm6: mtag_root,
		yytm7: mtag_root,
		yytm8: mtag_root,
		yytm9: mtag_root,
//line "codegen/v/loop_switch/advanced.re":216

        accept: 0,
    }
    // buf is zero-initialized, no need to write sentinel

    // Main loop. The buffer contains incomplete data which appears packet by
    // packet. When the lexer needs more input it saves its internal state and
    // returns to the caller which should provide more input and resume lexing.
    mut status := Status.lex_ready
    mut send := 0
    for {
        status = lex(mut &st)
        if status == .lex_end {
            break
        } else if status == .lex_waiting {
            if send < packets.len {
                log.debug("sending packet $send")
                fw.write_string(packets[send]) or { panic("cannot write to file") }
                fw.flush()
                send += 1
            }
            status = fill(mut &st)
            log.debug("filled buffer $st.buf, status $status")
            if status != .lex_ready {
                break
            }
        } else if status == .lex_bad_packet {
            break
        }
    }

    // Check results.
    if status != expect {
        panic("expected $expect, got $status")
    }

    // Cleanup: remove input file.
    fr.close()
    fw.close()
    os.rm(fname) or { panic("cannot remove file") }
}

fn main() {
    //log.set_level(.debug)
    test(.lex_end, ["ap", "plication/j", "son;", " charset=\"", "utf\\\"-8\"\r", "\n", ""])
}
