Lexing an Input String

The first phase of your expression-evaluating compiler is lexing. Lexing is the process of turning some input into a sequence of tokens. A token is something with meaning, like a number or a plus sign (the two tokens your compiler will recognize). Lexing is sometimes referred to as “tokenizing” because you are turning some meaningless-to-the-compiler input (like a string) into a sequence of meaningful tokens.

Create a new playground named ErrorHandling. Define an enumeration that has cases for the two kinds of token.

Listing 20.1  Declaring the Token type

import Cocoa

enum Token {
    case Number(Int)
    case Plus
}

Next, start building your lexer. To lex an input string, you will need to access the individual ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.