/* Generated by re2swift */
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 1)
// re2swift $INPUT -o $OUTPUT
import Foundation

func lex(_ str: Data) -> Int? {
  var cursor = 0, limit = str.count, marker = 0
  var count = 0

  loop: while true {
    
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.swift", line: 13)
  var yych: UInt8 = 0
  var yystate: UInt = 0
  yyl: while true {
    switch yystate {
      case 0:
        yych = cursor < limit ? str[cursor] : 0
        switch yych {
          case 0x20:
            cursor += 1
            yystate = 3
            continue yyl
          case 0x27:
            cursor += 1
            yystate = 5
            continue yyl
          default:
            if limit <= cursor {
              yystate = 10
              continue yyl
            }
            cursor += 1
            yystate = 1
            continue yyl
        }
      case 1:
        yystate = 2
        continue yyl
      case 2:
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 27)
        return nil
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.swift", line: 44)
      case 3:
        yych = cursor < limit ? str[cursor] : 0
        switch yych {
          case 0x20:
            cursor += 1
            yystate = 3
            continue yyl
          default:
            yystate = 4
            continue yyl
        }
      case 4:
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 25)
        continue loop
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.swift", line: 59)
      case 5:
        marker = cursor
        yych = cursor < limit ? str[cursor] : 0
        if yych >= 0x01 {
          yystate = 7
          continue yyl
        }
        if limit <= cursor {
          yystate = 2
          continue yyl
        }
        cursor += 1
        yystate = 6
        continue yyl
      case 6:
        yych = cursor < limit ? str[cursor] : 0
        yystate = 7
        continue yyl
      case 7:
        switch yych {
          case 0x27:
            cursor += 1
            yystate = 8
            continue yyl
          case 0x5C:
            cursor += 1
            yystate = 9
            continue yyl
          default:
            if limit <= cursor {
              yystate = 11
              continue yyl
            }
            cursor += 1
            yystate = 6
            continue yyl
        }
      case 8:
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 21)
        
        count += 1
        continue loop

#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.swift", line: 103)
      case 9:
        yych = cursor < limit ? str[cursor] : 0
        if yych <= 0x00 {
          if limit <= cursor {
            yystate = 11
            continue yyl
          }
          cursor += 1
          yystate = 6
          continue yyl
        }
        cursor += 1
        yystate = 6
        continue yyl
      case 10:
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 26)
        return count
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.swift", line: 121)
      case 11:
        cursor = marker
        yystate = 2
        continue yyl
      default: fatalError("internal lexer error")
    }
  }
#sourceLocation(file: "swift/eof/05_fake_sentinel_eof_rule.re", line: 28)

  }
}

func test(_ str: String, _ expect: Int?) {
  // For the sake of example create a string without terminating null.
  assert(lex(Data(str.utf8)) == expect)
}

test("", 0)
test("'qu\0tes' 'are' 'fine: \\'' ", 3)
test("'unterminated\\'", nil)
