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

#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 6)
let yymaxfill = 1
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 3)


func lex(_ str: UnsafeBufferPointer<UInt8>) -> Int? {
  let length = str.count

  // Make a copy of the string with YYMAXFILL zeroes at the end.
  let yyinput = ContiguousArray<UInt8>(str) + repeatElement(0, count: yymaxfill)

  var yycursor = 0, yylimit = length + yymaxfill
  var count = 0

  loop: while true {
    
#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 22)
  var yych: UInt8 = 0
  var yystate: UInt = 0
  yyl: while true {
    switch yystate {
      case 0:
        if yylimit <= yycursor {
          return nil
        }
        yych = yyinput[yycursor]
        yycursor += 1
        switch yych {
          case 0x00:
            yystate = 1
            continue yyl
          case 0x20:
            yystate = 3
            continue yyl
          case 0x27:
            yystate = 5
            continue yyl
          default:
            yystate = 2
            continue yyl
        }
      case 1:
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 24)
        
        // Check that it's the sentinel, not some unexpected null.
        if yycursor - 1 == length { return count } else { return nil }

#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 53)
      case 2:
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 29)
        return nil
#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 57)
      case 3:
        if yylimit <= yycursor {
          return nil
        }
        yych = yyinput[yycursor]
        switch yych {
          case 0x20:
            yycursor += 1
            yystate = 3
            continue yyl
          default:
            yystate = 4
            continue yyl
        }
      case 4:
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 28)
        continue loop
#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 75)
      case 5:
        if yylimit <= yycursor {
          return nil
        }
        yych = yyinput[yycursor]
        yycursor += 1
        switch yych {
          case 0x27:
            yystate = 6
            continue yyl
          case 0x5C:
            yystate = 7
            continue yyl
          default:
            yystate = 5
            continue yyl
        }
      case 6:
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 20)
        
        count += 1
        continue loop

#sourceLocation(file: "swift/eof/02_bounds_checking.swift", line: 99)
      case 7:
        if yylimit <= yycursor {
          return nil
        }
        yycursor += 1
        yystate = 5
        continue yyl
      default: fatalError("internal lexer error")
    }
  }
#sourceLocation(file: "swift/eof/02_bounds_checking.re", line: 30)

  }
}

func test(_ str: StaticString, _ expect: Int?) {
  str.withUTF8Buffer {
    assert(lex($0) == expect)
  }
}

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