mutating func lex()

in Sources/Markdown/Parser/BlockDirectiveParser.swift [336:381]


    mutating func lex(until stop: (Character) -> Lex.Continuation,
                      allowEscape: Bool = false,
                      allowQuote: Bool = false,
                      allowEmpty: Bool = false) -> Lex? {
        var takeCount = 0
        var prefix = text.makeIterator()
        var isEscaped = false
        var isQuoted = false

        while let c = prefix.next() {
            if isEscaped {
                isEscaped = false
                takeCount += 1
                continue
            }
            if allowEscape,
               c == "\\" {
                isEscaped = true
                takeCount += 1
                continue
            }
            if isQuoted {
                if c == "\"" {
                    isQuoted = false
                }
                takeCount += 1
                continue
            }
            if allowQuote,
               c == "\"" {
                isQuoted = true
                takeCount += 1
                continue
            }
            if case .stop = stop(c) {
                break
            }
            takeCount += 1
        }

        guard allowEmpty || takeCount > 0 else {
            return nil
        }

        return take(takeCount, allowEmpty: allowEmpty)
    }