Debugging area and side bar not showing results

#1

Hey guys,
I’ve been working on the Error Handling chapter and for some reason my debugging area or side bar are not showing anything. I tried just creating a String variable and printing it and it works. Also when an error comes out I get a report in the debugging area, but other than that…nothing. I also tried opening previous chapters code and it works normal. Below is my code. Thanks for your help!

//: Playground - noun: a place where people can play

import Cocoa

enum Token {
    case Number(Int)
    case Plus
}
class Lexer {
    enum ErrorType: Error {
        case InvalidCharacter(Character)
    }
    
    let input: String.CharacterView
    var position: String.CharacterView.Index
    
    init(input: String) {
        self.input = input.characters
        self.position = self.input.startIndex
    }
    func peek() -> Character? {
        guard position < input.endIndex else {
            return nil
        }
        return input[position]
    }
    func advance() {
        assert(position < input.endIndex, "Cannot advance past the end!")
        position = input.index(after: position)
    }
    func getNumber() -> Int {
        var value = 0
        
        while let nextCharacter = peek() {
            switch nextCharacter {
            case "0" ... "9":
                // Another digit - add it into value
                let digitValue = Int(String(nextCharacter))!
                value = 10*value + digitValue
                advance()
                
            default:
                //A non-digit - go back to ragular lexing
                return value
            }
        }
        return value
    }
    func lex() throws -> [Token] {
        var tokens = [Token]()
        
        while let nextCharacter = peek() {
            switch nextCharacter {
            case "0" ... "9":
                let value = getNumber()
                tokens.append(.Number(value))
                //Start of a number - need to grab the rest
                
            case "+":
                tokens.append(.Plus)
                advance()
                
            case " ":
                //Just advance and ingnore spaces
                advance()
                
            default:
                throw ErrorType.InvalidCharacter(nextCharacter)
                //Something unexpected = need to send back an error
                
            }
        }
        return tokens
    }
    
    func evaluate(input: String) {
        print("Evaluation: \(input)")
        let lexer = Lexer(input: input)
        
        do {
            let tokens = try lexer.lex()
            print("Lexer output: \(tokens)")
            
            let parser = Parser(tokens: tokens)
            let result = try parser.parse()
            print("Parser output: \(result)")
            
        } catch Lexer.ErrorType.InvalidCharacter(let character) {
            print("Input contained an invalid character: \(character)")
        } catch Parser.ErrorType.UnexpectedEndOfInput {
            print("Unexpected end of input during parsing")
        } catch Parser.ErrorType.InvalidToken(let token) {
            print("Invalid token during parsing: \(token)")
        } catch {
            print("An error occured: \(error)")
        }
        
        evaluate(input: "10 + 3 + 5")
        evaluate(input: "1 + 2 + ahfas")
    }
    
}

class Parser {
    let tokens: [Token]
    var position = 0
    
    init(tokens: [Token]) {
        self.tokens = tokens
    }
    func parse() throws -> Int {
        //Require a number first
        var value = try getNumber()
        
        while let token = getNextToken() {
            switch token {
                //Getting a Plus after a Number is legal
            case .Plus:
                //After a plus, we must get another number
                let nextNumber = try getNumber()
                value += nextNumber
                
                //Getting a number after a Number is not legal
            case .Number:
                throw ErrorType.InvalidToken(token)
            }
        }
        return value
    }
    
    func getNextToken() -> Token? {
        guard position < tokens.count else {
            return nil
        }
        let token = tokens[position]
        position += 1
        return token
    }
    func getNumber() throws -> Int {
        guard let token = getNextToken() else {
            throw ErrorType.UnexpectedEndOfInput
        }
        switch token {
        case .Number(let value):
            return value
        case .Plus:
            throw ErrorType.InvalidToken(token)
        }
    }
    
    enum ErrorType: Error {
        case UnexpectedEndOfInput
        case InvalidToken(Token)
    }
}
#2

Where is the top-level code, the code that sets off the execution?

Make the evaluate function a top-level function, and move the last two lines out of it.

The structure of your code should look like this:

enum Token {
   ...
}
class Lexer {
   ...
}

class Parser {
   ...
}

// Top-level functions...
//
func evaluate (input: String) {
   ...
}

// Top-level code...
evaluate (input: "10 + 3 + 5")
evaluate (input: "1 + 2 + ahfas")

Every executable program needs some top level code, which starts the execution.

You can become a competent programmer faster than you can imagine.

#3

Thanks very much! I knew that it was something silly!