Add very rudimentary Lexer: it matches parens!
This commit is contained in:
parent
06eb09ffc6
commit
3840f9ee02
2 changed files with 56 additions and 0 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
C0496EB51E04533D0000E33E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0496EB41E04533D0000E33E /* main.swift */; };
|
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 */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXCopyFilesBuildPhase section */
|
/* Begin PBXCopyFilesBuildPhase section */
|
||||||
|
@ -25,6 +26,7 @@
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
C0496EB11E04533D0000E33E /* Sibil */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Sibil; sourceTree = BUILT_PRODUCTS_DIR; };
|
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>"; };
|
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 */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
|
@ -58,6 +60,7 @@
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
C0496EB41E04533D0000E33E /* main.swift */,
|
C0496EB41E04533D0000E33E /* main.swift */,
|
||||||
|
C0496EBB1E04534F0000E33E /* Lexer.swift */,
|
||||||
);
|
);
|
||||||
path = Sibil;
|
path = Sibil;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
@ -120,6 +123,7 @@
|
||||||
isa = PBXSourcesBuildPhase;
|
isa = PBXSourcesBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */,
|
||||||
C0496EB51E04533D0000E33E /* main.swift in Sources */,
|
C0496EB51E04533D0000E33E /* main.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
|
52
Sibil/Lexer.swift
Normal file
52
Sibil/Lexer.swift
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue