Add very rudimentary Lexer: it matches parens!

This commit is contained in:
Eryn Wells 2016-12-16 09:44:05 -08:00
parent 06eb09ffc6
commit 3840f9ee02
2 changed files with 56 additions and 0 deletions

View file

@ -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 = "<group>"; };
C0496EBB1E04534F0000E33E /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -58,6 +60,7 @@
isa = PBXGroup;
children = (
C0496EB41E04533D0000E33E /* main.swift */,
C0496EBB1E04534F0000E33E /* Lexer.swift */,
);
path = Sibil;
sourceTree = "<group>";
@ -120,6 +123,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */,
C0496EB51E04533D0000E33E /* main.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

52
Sibil/Lexer.swift Normal file
View file

@ -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
}
}