code: def parse_sequence(
      state: parse_state,
      src: const_char_p,
      rule_name: str,
      out_elements: std.vector[LlamaGrammarElement],
      is_nested: bool,
  ) -> const_char_p:
      # size_t last_sym_start = out_elements.size();
      # const char * pos = src;
      last_sym_start = out_elements.size()  # type: int
      pos = const_char_p(src)  # type: const_char_p
      # while (*pos) {
      while pos[0]:
          # if (*pos == '"') { // literal string
          #     pos++;
          #     last_sym_start = out_elements.size();
          #     while (*pos != '"') {
          #         auto char_pair = parse_char(pos);
          #                 pos       = char_pair.second;
          #         out_elements.push_back({LLAMA_GRETYPE_CHAR, char_pair.first});
          #     }
          #     pos = parse_space(pos + 1, is_nested);
          if pos[0] == '"':  # literal string
              pos += 1
              last_sym_start = out_elements.size()
              while pos[0] != '"':
                  char_pair = parse_char(
                      pos
                  )  # type: Tuple[int, const_char_p]
                  pos = char_pair[1]
                  out_elements.push_back(
                      LlamaGrammarElement(
                          llama_gretype.LLAMA_GRETYPE_CHAR, char_pair[0]
                      )
                  )
              pos = parse_space(pos + 1, is_nested)
          # } else if (*pos == '[') { // char range(s)
          #     pos++;
          #     enum llama_gretype start_type = LLAMA_GRETYPE_CHAR;
          elif pos[0] == "[":  # char range(s)
              pos += 1
              start_type = (
                  llama_gretype.LLAMA_GRETYPE_CHAR
              )  # type: llama_gretype
              # if (*pos == '^') {
              #     pos++;
              #     start_type = LLAMA_GRETYPE_CHAR_NOT;
              # }
              # last_sym_start = out_elements.size();
              if pos[0] == "^":
                  pos += 1
                  start_type = llama_gretype.LLAMA_GRETYPE_CHAR_NOT
              last_sym_start = out_elements.size()
              # while (*pos != ']') {
              #     auto char_pair = parse_char(pos);
              #             pos       = char_pair.second;
              #     enum llama_gretype type = last_sym_start < out_elements.size()
              #         ? LLAMA_GRETYPE_CHAR_ALT
              #         : start_type;
              #     out_elements.push_back({type, char_pair.first});
              while pos[0] != "]":
                  char_pair = parse_char(
                      pos
                  )  # type: Tuple[int, const_char_p]
                  pos = char_pair[1]
                  type = (
                      llama_gretype.LLAMA_GRETYPE_CHAR_ALT
                      if last_sym_start < out_elements.size()
                      else start_type
                  )  # type: llama_gretype
                  out_elements.push_back(
                      LlamaGrammarElement(type, char_pair[0])
                  )
                  #     if (pos[0] == '-' && pos[1] != ']') {
                  #         auto endchar_pair = parse_char(pos + 1);
                  #                 pos          = endchar_pair.second;
                  #         out_elements.push_back({LLAMA_GRETYPE_CHAR_RNG_UPPER, endchar_pair.first});
                  #     }
                  # }
                  if pos[0] == "-" and pos[1] != "]":
                      endchar_pair = parse_char(
                          pos + 1
                      )  # type: Tuple[int, const_char_p]
                      pos = endchar_pair[1]
                      out_elements.push_back(
                          LlamaGrammarElement(
                              llama_gretype.LLAMA_GRETYPE_CHAR_RNG_UPPER,
                              endchar_pair[0],
                          )
                      )
              # pos = parse_space(pos + 1, is_nested);
              pos = parse_space(pos + 1, is_nested)
          # } else if (is_word_char(*pos)) { // rule reference
          #     const char * name_end    = parse_name(pos);
          #     uint32_t     ref_rule_id = get_symbol_id(state, pos, name_end - pos);
          #     pos = parse_space(name_end, is_nested);
          #     last_sym_start = out_elements.size();
          #     out_elements.push_back({LLAMA_GRETYPE_RULE_REF, ref_rule_id});
          elif is_word_char(pos[0]):  # rule reference
              name_end = parse_name(pos)  # type: const_char_p
              ref_rule_id = get_symbol_id(
                  state, pos, name_end - pos
              )  # type: int
              pos = parse_space(name_end, is_nested)
              last_sym_start = out_elements.size()
              out_elements.push_back(
                  LlamaGrammarElement(
                      llama_gretype.LLAMA_GRETYPE_RULE_REF, ref_rule_id
                  )
              )
          # } else if (*pos == '(') { // grouping
          #     // parse nested alternates into synthesized rule
          #     pos = parse_space(pos + 1, true);
          #     uint32_t sub_rule_id = generate_symbol_id(state, rule_name);
          #     pos = parse_alternates(state, pos, rule_name, sub_rule_id, true);
          #     last_sym_start = out_elements.size();
          #     // output reference to synthesized rule
          #     out_elements.push_back({LLAMA_GRETYPE_RULE_REF, sub_rule_id});
          #     if (*pos != ')') {
          #         throw std::runtime_error(std::string("expecting ')' at ") + pos);
          #     }
          #     pos = parse_space(pos + 1, is_nested);
          elif pos[0] == "(":  # grouping
              # parse nested alternates into synthesized rule
              pos = parse_space(pos + 1, True)
              sub_rule_id = generate_symbol_id(
                  state, rule_name
              )  # type: int
              pos = parse_alternates(
                  state, pos, rule_name, sub_rule_id, True
              )
              last_sym_start = out_elements.size()
              # output reference to synthesized rule
              out_elements.push_back(
                  LlamaGrammarElement(
                      llama_gretype.LLAMA_GRETYPE_RULE_REF, sub_rule_id
                  )
              )
              if pos[0] != ")":
                  raise RuntimeError("expecting ')' at " + str(pos))
              pos = parse_space(pos + 1, is_nested)
          # } else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
          #     if (last_sym_start == out_elements.size()) {
          #         throw std::runtime_error(std::string("expecting preceeding item to */+/? at ") + pos);
          #     }
          elif pos[0] in ("*", "+", "?"):  # repetition operator
              if last_sym_start == out_elements.size():
                  raise RuntimeError(
                      "expecting preceding item to */+/? at " + str(pos)
                  )
              # // apply transformation to previous symbol (last_sym_start to end) according to
              # // rewrite rules:
              # // S* --> S' ::= S S' |
              # // S+ --> S' ::= S S' | S
              # // S? --> S' ::= S |
              # uint32_t sub_rule_id = generate_symbol_id(state, rule_name);
              # std::vector<llama_grammar_element> sub_rule;
              # // add preceding symbol to generated rule
              # sub_rule.insert(
              #     sub_rule.end(), out_elements.begin() + last_sym_start, out_elements.end());
              sub_rule_id = generate_symbol_id(
                  state, rule_name
              )  # type: int
              sub_rule = std.vector[
                  LlamaGrammarElement
              ]()  # type: std.vector[LlamaGrammarElement]
              sub_rule.insert(
                  sub_rule.end(),
                  out_elements.begin() + last_sym_start,
                  out_elements.end(),
              )
              # if (*pos == '*' || *pos == '+') {
              #     // cause generated rule to recurse
              #     sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, sub_rule_id});
              # }
              # // mark start of alternate def
              # sub_rule.push_back({LLAMA_GRETYPE_ALT, 0});
              if pos[0] in ("*", "+"):
                  sub_rule.push_back(
                      LlamaGrammarElement(
                          llama_gretype.LLAMA_GRETYPE_RULE_REF,
                          sub_rule_id,
                      )
                  )
              sub_rule.push_back(
                  LlamaGrammarElement(
                      llama_gretype.LLAMA_GRETYPE_ALT, 0
                  )
              )
              # if (*pos == '+') {
              #     // add preceding symbol as alternate only for '+' (otherwise empty)
              #     sub_rule.insert(
              #         sub_rule.end(), out_elements.begin() + last_sym_start, out_elements.end());
              # }
              # sub_rule.push_back({LLAMA_GRETYPE_END, 0});
              # add_rule(state, sub_rule_id, sub_rule);
              # // in original rule, replace previous symbol with reference to generated rule
              # out_elements.resize(last_sym_start);
              # out_elements.push_back({LLAMA_GRETYPE_RULE_REF, sub_rule_id});
              # pos = parse_space(pos + 1, is_nested);
              if pos[0] == "+":
                  # add preceding symbol as alternate only for '+' (otherwise empty)
                  sub_rule.insert(
                      sub_rule.end(),
                      out_elements.begin() + last_sym_start,
                      out_elements.end(),
                  )
              sub_rule.push_back(
                  LlamaGrammarElement(
                      llama_gretype.LLAMA_GRETYPE_END, 0
                  )
              )
              add_rule(state, sub_rule_id, sub_rule)
              # in original rule, replace previous symbol with reference to generated rule
              out_elements.resize(last_sym_start)
              out_elements.push_back(
                  LlamaGrammarElement(
                      llama_gretype.LLAMA_GRETYPE_RULE_REF, sub_rule_id
                  )
              )
              pos = parse_space(pos + 1, is_nested)
          # } else {
          #     break;
          # }
          else:
              break
      #     }
      #     return pos;
      # }
      return pos
  
imports: from pathlib import Path
  import sys
  from ctypes import *
  from enum import Enum
  from itertools import islice
  from typing import Any, Callable, Dict, Generic, List, Optional, OrderedDict, TextIO, Tuple, TypeVar, Union, overload
  import llama_cpp.llama_cpp
  import json
  import re
  from typing import List, Optional
label_desc: Use image node_id 10 for calling a global function with example usage: parse_sequence(state, src, rule_name, out_elements, is_nested) and returns: pos