O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

var str = "Hello, playground"

enum Token {
    case number(Int)
    case plus
}

Next, start building your lexer. To lex an input string, you will ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required