Lexical

public enum Lexical

Lexical is the namespace for Lexical parsers (you can also use shorthand L if you prefer)

  • Errors that could occur while lexical parsing

    See more

    Declaration

    Swift

    public enum Error: ParseError, CustomStringConvertible
  • Parses one Character from a given String

    Declaration

    Swift

    public static let char = Parser<String, Character> { input in
  • Parses a character that matches condition

    Declaration

    Swift

    public static func char(condition: @escaping (Character) -> Bool) -> Parser<String, Character>

    Parameters

    condition

    a function that returns true if the character should be parsed

    Return Value

    a parser that tries to parse a character that matches condition

  • Parses a specific Character from a given String

    Declaration

    Swift

    public static func char(_ character: Character) -> Parser<String, Character>

    Parameters

    character

    the Character that should be parsed

    Return Value

    a parser that parses that one Character from a given String

  • Parses one char that is part of the ascii table

    Declaration

    Swift

    public static let asciiChar = char.filter { parsed in
  • Parses a string that consists only of characters from the ascii range

    Declaration

    Swift

    public static let asciiString = asciiChar.atLeastOne ^^ { String($0) }
  • Parses a lowercase letter (‘a’-‘z’)

    Declaration

    Swift

    public static let lowercaseLetter = char.filter { parsed in
  • Parses an uppercase letter (‘A’-‘Z’)

    Declaration

    Swift

    public static let uppercaseLetter = char.filter { parsed in
  • Parses a letter (‘a’-‘z’ | ‘A’-‘Z’)

    Declaration

    Swift

    public static let letter = (lowercaseLetter | uppercaseLetter).mapError { err in
  • Parses a given String from a String.

    Declaration

    Swift

    public static func string(_ string: String) -> Parser<String, String>

    Parameters

    string

    the String which should be parsed

    Return Value

    a parser that parses that String

  • Parses a string with the given length

    Declaration

    Swift

    public static func string(length: Int) -> Parser<String, String>

    Parameters

    length

    the length the string should have

    Return Value

    a parser that parses a string with exactly the given length

  • Parses a digit [0-9] from a given String

    Declaration

    Swift

    public static let digit = Parser<String, Int> { input in
  • Parses a binary digit (0 or 1)

    Declaration

    Swift

    public static let binaryDigit = digit.filter { parsed in
  • A parser for numbers of the format 0b10110110

    Declaration

    Swift

    public static let binaryNumber = (string("0b") ~> binaryDigit.atLeastOne) ^^ { digits in
  • Parses an octal digit (0 to 7)

    Declaration

    Swift

    public static let octalDigit = digit.filter { parsed in
  • A parser for numbers of the format 0o12372106

    Declaration

    Swift

    public static let octalNumber = (string("0o") ~> octalDigit.atLeastOne) ^^ { digits in
  • Parses a hexadecimal digit (0 to 15)

    Declaration

    Swift

    public static let hexadecimalDigit = char.map { parsed -> Int in
  • A parser for numbers of the format 0xdeadbeaf or 0xDEADBEAF

    Declaration

    Swift

    public static let hexadecimalNumber = ((string("0x") | string("0X")) ~> hexadecimalDigit.atLeastOne) ^^ { digits in
  • Parses a decimal number

    Declaration

    Swift

    public static let decimalNumber = digit.atLeastOne ^^ { digits in
  • Parses a number in hexadecimal, octal, binary or decimal format

    Declaration

    Swift

    public static let number = hexadecimalNumber | octalNumber | binaryNumber | decimalNumber
  • Parses a floating number from String to Double (0.123; 0,123; …)

    Declaration

    Swift

    public static let floatingNumber = "[0-9]+([\\.,][0-9]+)?".r ^^ { input -> Double in
  • Parses the + sign

    Declaration

    Swift

    public static let plus = char("+")
  • Parses the - sign

    Declaration

    Swift

    public static let minus = char("-")
  • Parses the * sign

    Declaration

    Swift

    public static let multiply = char("*")
  • Parses the / sign

    Declaration

    Swift

    public static let divide = char("/")
  • Parses the = sign

    Declaration

    Swift

    public static let assign = char("=")
  • Parses the == sign

    Declaration

    Swift

    public static let equal = (assign ~ assign) ^^ { String([$0, $1]) }
  • Parses one space character

    Declaration

    Swift

    public static let space = char(" ")
  • Parses a new line \n character

    Declaration

    Swift

    public static let newLine = char("\n")
  • Parses a new line \n character

    Declaration

    Swift

    public static let carriageReturn = char("\r\n")
  • tab

    Parses a tab \t character

    Declaration

    Swift

    public static let tab = char("\t")
  • Parses exactly one whitespace

    Declaration

    Swift

    public static let oneWhitespace = space | newLine | carriageReturn | tab
  • Parses at least one whitespace

    Declaration

    Swift

    public static let whitespaces = oneWhitespace.atLeastOne