Lexer

public struct Lexer : IteratorProtocol, Sequence, Sendable

A type that tokenize a source file.

  • The Hylo source being tokenized.

    Declaration

    Swift

    public let sourceCode: SourceFile
  • The current position in the source file.

    Declaration

    Swift

    private(set) var index: String.Index { get }
  • Creates a lexer generating tokens from the contents of source.

    Declaration

    Swift

    public init(tokenizing source: SourceFile)
  • The current location of the lexer in sourceCode.

    Declaration

    Swift

    public var location: SourcePosition { get }
  • true iff the last scanned token was .int.

    Declaration

    Swift

    private var previousTokenWasInt: Bool
  • Advances to the next token and returns it, or returns nil if no next token exists.

    Declaration

    Swift

    public mutating func next() -> Token?
  • Discards count characters from the stream.

    Declaration

    Swift

    private mutating func discard(_ count: Int = 1)
  • Returns the next character in the stream without consuming it, if any.

    Declaration

    Swift

    private func peek() -> Character?
  • Returns the current index and consumes character from the stream, or returns nil if the stream starts with a different character.

    Declaration

    Swift

    public mutating func take(_ character: Character) -> String.Index?
  • Returns the current index and consumes prefix from the stream, or returns nil if the stream starts with a different prefix.

    Declaration

    Swift

    private mutating func take<T: Sequence>(prefix: T) -> String.Index?
    where T.Element == Character
  • Consumes the longest substring that satisfies the given predicate.

    Declaration

    Swift

    private mutating func take(while predicate: (Character) -> Bool) -> Substring
  • Consumes an integral literal and returns its kind, or returns nil if it fails to scan a valid integer.

    Declaration

    Swift

    private mutating func scanIntegralLiteral(allowingPlus allowPlus: Bool = false) -> Token.Kind?

    Parameters

    allowingPlus

    If set to true, allows the integral literal to begin with a “+” sign. If set to false (the default), only allows “-” or no sign at the beginning.