From 3840f9ee024871302393d6b1d7c7587008375158 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2016 09:44:05 -0800 Subject: [PATCH] Add very rudimentary Lexer: it matches parens! --- Sibil.xcodeproj/project.pbxproj | 4 +++ Sibil/Lexer.swift | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Sibil/Lexer.swift diff --git a/Sibil.xcodeproj/project.pbxproj b/Sibil.xcodeproj/project.pbxproj index 6dc8e83..cc82376 100644 --- a/Sibil.xcodeproj/project.pbxproj +++ b/Sibil.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ C0496EB51E04533D0000E33E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0496EB41E04533D0000E33E /* main.swift */; }; + C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0496EBB1E04534F0000E33E /* Lexer.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +26,7 @@ /* Begin PBXFileReference section */ C0496EB11E04533D0000E33E /* Sibil */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Sibil; sourceTree = BUILT_PRODUCTS_DIR; }; C0496EB41E04533D0000E33E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + C0496EBB1E04534F0000E33E /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +60,7 @@ isa = PBXGroup; children = ( C0496EB41E04533D0000E33E /* main.swift */, + C0496EBB1E04534F0000E33E /* Lexer.swift */, ); path = Sibil; sourceTree = ""; @@ -120,6 +123,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */, C0496EB51E04533D0000E33E /* main.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sibil/Lexer.swift b/Sibil/Lexer.swift new file mode 100644 index 0000000..32757e3 --- /dev/null +++ b/Sibil/Lexer.swift @@ -0,0 +1,52 @@ +// +// Lexer.swift +// Sibil +// +// Created by Eryn Wells on 12/16/16. +// Copyright © 2016 Eryn Wells. All rights reserved. +// + +import Foundation + + +struct Token { + enum Kind { + case LeftParen + case RightParen + } + + let kind: Kind + let value: String +} + + +class Lexer: IteratorProtocol { + typealias Element = Token + + let input: String + + private var index: String.Index + + init(input: String) { + self.input = input + self.index = input.startIndex + } + + // MARK: IteratorProtocol + + func next() -> Token? { + var token: Token? + while token == nil { + let c = input[index] + switch c { + case "(": + token = Token(kind: .LeftParen, value: String(c)) + case ")": + token = Token(kind: .RightParen, value: String(c)) + default: + break + } + } + return token + } +}