From 06eb09ffc6dbf596d46c8723659596cd9fd0f024 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2016 08:52:16 -0800 Subject: [PATCH 1/7] Add an Xcode project --- Sibil.xcodeproj/project.pbxproj | 257 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + Sibil/main.swift | 12 + 3 files changed, 276 insertions(+) create mode 100644 Sibil.xcodeproj/project.pbxproj create mode 100644 Sibil.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Sibil/main.swift diff --git a/Sibil.xcodeproj/project.pbxproj b/Sibil.xcodeproj/project.pbxproj new file mode 100644 index 0000000..6dc8e83 --- /dev/null +++ b/Sibil.xcodeproj/project.pbxproj @@ -0,0 +1,257 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + C0496EB51E04533D0000E33E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0496EB41E04533D0000E33E /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + C0496EAF1E04533D0000E33E /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* 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 = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + C0496EAE1E04533D0000E33E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + C0496EA81E04533D0000E33E = { + isa = PBXGroup; + children = ( + C0496EB31E04533D0000E33E /* Sibil */, + C0496EB21E04533D0000E33E /* Products */, + ); + sourceTree = ""; + }; + C0496EB21E04533D0000E33E /* Products */ = { + isa = PBXGroup; + children = ( + C0496EB11E04533D0000E33E /* Sibil */, + ); + name = Products; + sourceTree = ""; + }; + C0496EB31E04533D0000E33E /* Sibil */ = { + isa = PBXGroup; + children = ( + C0496EB41E04533D0000E33E /* main.swift */, + ); + path = Sibil; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + C0496EB01E04533D0000E33E /* Sibil */ = { + isa = PBXNativeTarget; + buildConfigurationList = C0496EB81E04533D0000E33E /* Build configuration list for PBXNativeTarget "Sibil" */; + buildPhases = ( + C0496EAD1E04533D0000E33E /* Sources */, + C0496EAE1E04533D0000E33E /* Frameworks */, + C0496EAF1E04533D0000E33E /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Sibil; + productName = Sibil; + productReference = C0496EB11E04533D0000E33E /* Sibil */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + C0496EA91E04533D0000E33E /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0810; + LastUpgradeCheck = 0810; + ORGANIZATIONNAME = "Eryn Wells"; + TargetAttributes = { + C0496EB01E04533D0000E33E = { + CreatedOnToolsVersion = 8.1; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = C0496EAC1E04533D0000E33E /* Build configuration list for PBXProject "Sibil" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = C0496EA81E04533D0000E33E; + productRefGroup = C0496EB21E04533D0000E33E /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + C0496EB01E04533D0000E33E /* Sibil */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + C0496EAD1E04533D0000E33E /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C0496EB51E04533D0000E33E /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + C0496EB61E04533D0000E33E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + C0496EB71E04533D0000E33E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + C0496EB91E04533D0000E33E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Debug; + }; + C0496EBA1E04533D0000E33E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + C0496EAC1E04533D0000E33E /* Build configuration list for PBXProject "Sibil" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C0496EB61E04533D0000E33E /* Debug */, + C0496EB71E04533D0000E33E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + C0496EB81E04533D0000E33E /* Build configuration list for PBXNativeTarget "Sibil" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C0496EB91E04533D0000E33E /* Debug */, + C0496EBA1E04533D0000E33E /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = C0496EA91E04533D0000E33E /* Project object */; +} diff --git a/Sibil.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Sibil.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..8ccf6d7 --- /dev/null +++ b/Sibil.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Sibil/main.swift b/Sibil/main.swift new file mode 100644 index 0000000..fcebf74 --- /dev/null +++ b/Sibil/main.swift @@ -0,0 +1,12 @@ +// +// main.swift +// Sibil +// +// Created by Eryn Wells on 12/16/16. +// Copyright © 2016 Eryn Wells. All rights reserved. +// + +import Foundation + +print("Hello, World!") + From 3840f9ee024871302393d6b1d7c7587008375158 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2016 09:44:05 -0800 Subject: [PATCH 2/7] 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 + } +} From a0eb34f9a22af32538c07bd4cf48a5159d09564a Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2016 10:10:05 -0800 Subject: [PATCH 3/7] Lex simple parentheses --- Sibil/Lexer.swift | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Sibil/Lexer.swift b/Sibil/Lexer.swift index 32757e3..eec7719 100644 --- a/Sibil/Lexer.swift +++ b/Sibil/Lexer.swift @@ -9,7 +9,7 @@ import Foundation -struct Token { +struct Token: CustomDebugStringConvertible { enum Kind { case LeftParen case RightParen @@ -17,24 +17,38 @@ struct Token { let kind: Kind let value: String + + // MARK: CustomDebugStringConvertible + + var debugDescription: String { + return "Token(kind: .\(kind), value: \"\(value)\")" + } } -class Lexer: IteratorProtocol { - typealias Element = Token - +class Lexer { let input: String - private var index: String.Index + var index: String.Index init(input: String) { self.input = input self.index = input.startIndex } +} - // MARK: IteratorProtocol +extension Lexer: Sequence, IteratorProtocol { + typealias Element = Token + + func makeIterator() -> Lexer { + return self + } func next() -> Token? { + guard index != input.endIndex else { + return nil + } + var token: Token? while token == nil { let c = input[index] @@ -46,6 +60,7 @@ class Lexer: IteratorProtocol { default: break } + index = input.index(after: index) } return token } From aed0d3e43d370a9210b67cef44c7c9f2cea337da Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2016 10:10:13 -0800 Subject: [PATCH 4/7] Simple test harness --- Sibil/main.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sibil/main.swift b/Sibil/main.swift index fcebf74..b72e9f7 100644 --- a/Sibil/main.swift +++ b/Sibil/main.swift @@ -8,5 +8,8 @@ import Foundation -print("Hello, World!") +let l = Lexer(input: "(())") +for t in l { + print(t) +} From 35a5cf1dc8b9014617a0627280b9bdb299ba1d21 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 17 Dec 2016 10:04:35 -0800 Subject: [PATCH 5/7] Fiddling with state, I dunno --- Sibil/Lexer.swift | 87 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/Sibil/Lexer.swift b/Sibil/Lexer.swift index eec7719..b3b9788 100644 --- a/Sibil/Lexer.swift +++ b/Sibil/Lexer.swift @@ -40,6 +40,12 @@ class Lexer { extension Lexer: Sequence, IteratorProtocol { typealias Element = Token + private enum State { + case Initial + case Identifier + case Emit + } + func makeIterator() -> Lexer { return self } @@ -49,19 +55,84 @@ extension Lexer: Sequence, IteratorProtocol { return nil } + var state = State.Initial var token: Token? - while token == nil { + var forward = index + + let toState = { (nextState: State) in + state = nextState + } + + let retract = { + forward = self.input.index(before: forward) + } + + let advance = { + forward = self.input.index(after: forward) + } + + let emit = { (kind: Token.Kind) in + let valueRange = Range(uncheckedBounds: (lower: self.index, upper: forward)) + let value = self.input.substring(with: valueRange) + token = Token(kind: kind, value: value) + toState(.Emit) + } + + while state != .Emit { let c = input[index] - switch c { - case "(": - token = Token(kind: .LeftParen, value: String(c)) - case ")": - token = Token(kind: .RightParen, value: String(c)) - default: + switch state { + case .Initial: + if c.isLeftParen { + + } + else if c.isRightParen { + + } + else if c.isIdentifierInitial { + advance() + toState(.Identifier) + } + case .Identifier: + if c.isIdentifierSubsequent + case .Emit: + // Nothing to do for this state break } - index = input.index(after: index) } + return token } } + +extension Character { + static let identifierInitialSet: CharacterSet = { + let letters = CharacterSet.letters + let extras = CharacterSet(charactersIn: "!$%&*/:<=>?~_^") + let initials = letters.union(extras) + return initials + }() + + static let identifierSubsequentSet: CharacterSet = { + let initials = Character.identifierInitialSet + let digits = CharacterSet.decimalDigits + let extras = CharacterSet(charactersIn: ".+-") + let subsequents = initials.union(digits).union(extras) + return subsequents + }() + + var isLeftParen: Bool { + return self == "(" + } + + var isRightParen: Bool { + return self == ")" + } + + var isIdentifierInitial: Bool { + return false + } + + var isIdentifierSubsequent: Bool { + Character.identifierSubsequentSet.contains(<#T##member: UnicodeScalar##UnicodeScalar#>) + } +} From 59e55a39e492f660570f7463c81b3dc41249bc36 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 19 Dec 2016 08:57:59 -0800 Subject: [PATCH 6/7] Straighten out Characters and CharacterSets --- Sibil.xcodeproj/project.pbxproj | 5 +++ Sibil/Characters.swift | 60 +++++++++++++++++++++++++++++++++ Sibil/Lexer.swift | 35 +------------------ 3 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 Sibil/Characters.swift diff --git a/Sibil.xcodeproj/project.pbxproj b/Sibil.xcodeproj/project.pbxproj index cc82376..a9c0e4f 100644 --- a/Sibil.xcodeproj/project.pbxproj +++ b/Sibil.xcodeproj/project.pbxproj @@ -9,6 +9,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 */; }; + C0C60EC11E084631004C1559 /* Characters.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C60EC01E084631004C1559 /* Characters.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -27,6 +28,7 @@ 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 = ""; }; + C0C60EC01E084631004C1559 /* Characters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Characters.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -61,6 +63,7 @@ children = ( C0496EB41E04533D0000E33E /* main.swift */, C0496EBB1E04534F0000E33E /* Lexer.swift */, + C0C60EC01E084631004C1559 /* Characters.swift */, ); path = Sibil; sourceTree = ""; @@ -124,6 +127,7 @@ buildActionMask = 2147483647; files = ( C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */, + C0C60EC11E084631004C1559 /* Characters.swift in Sources */, C0496EB51E04533D0000E33E /* main.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -254,6 +258,7 @@ C0496EBA1E04533D0000E33E /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Sibil/Characters.swift b/Sibil/Characters.swift new file mode 100644 index 0000000..58f82fb --- /dev/null +++ b/Sibil/Characters.swift @@ -0,0 +1,60 @@ +// +// Characters.swift +// Sibil +// +// Created by Eryn Wells on 12/19/16. +// Copyright © 2016 Eryn Wells. All rights reserved. +// + +import Foundation + + +extension CharacterSet { + static let asciiLetters: CharacterSet = { + return CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + }() + + static let asciiDigits: CharacterSet = { + return CharacterSet(charactersIn: "1234567890") + }() + + static let identifierInitials: CharacterSet = { + let letters = CharacterSet.asciiLetters + let extras = CharacterSet(charactersIn: "!$%&*/:<=>?~_^") + let initials = letters.union(extras) + return initials + }() + + static let identifierSubsequents: CharacterSet = { + let initials = CharacterSet.identifierInitials + let digits = CharacterSet.asciiDigits + let extras = CharacterSet(charactersIn: ".+-") + let subsequents = initials.union(digits).union(extras) + return subsequents + }() + + func contains(_ char: Character) -> Bool { + let cSet = CharacterSet(charactersIn: String(char)) + let isSuperset = self.isSuperset(of: cSet) + return isSuperset + } +} + + +extension Character { + var isLeftParen: Bool { + return self == "(" + } + + var isRightParen: Bool { + return self == ")" + } + + var isIdentifierInitial: Bool { + return false + } + + var isIdentifierSubsequent: Bool { + return CharacterSet.identifierSubsequents.contains(self) + } +} diff --git a/Sibil/Lexer.swift b/Sibil/Lexer.swift index b3b9788..8f91546 100644 --- a/Sibil/Lexer.swift +++ b/Sibil/Lexer.swift @@ -93,7 +93,7 @@ extension Lexer: Sequence, IteratorProtocol { toState(.Identifier) } case .Identifier: - if c.isIdentifierSubsequent + break case .Emit: // Nothing to do for this state break @@ -103,36 +103,3 @@ extension Lexer: Sequence, IteratorProtocol { return token } } - -extension Character { - static let identifierInitialSet: CharacterSet = { - let letters = CharacterSet.letters - let extras = CharacterSet(charactersIn: "!$%&*/:<=>?~_^") - let initials = letters.union(extras) - return initials - }() - - static let identifierSubsequentSet: CharacterSet = { - let initials = Character.identifierInitialSet - let digits = CharacterSet.decimalDigits - let extras = CharacterSet(charactersIn: ".+-") - let subsequents = initials.union(digits).union(extras) - return subsequents - }() - - var isLeftParen: Bool { - return self == "(" - } - - var isRightParen: Bool { - return self == ")" - } - - var isIdentifierInitial: Bool { - return false - } - - var isIdentifierSubsequent: Bool { - Character.identifierSubsequentSet.contains(<#T##member: UnicodeScalar##UnicodeScalar#>) - } -} From ed274947839e3f2936dd9b5f295b7c07422d04b9 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 19 Dec 2016 09:46:58 -0800 Subject: [PATCH 7/7] Try to lex identifiers and parens --- Sibil/Characters.swift | 10 +++++----- Sibil/Lexer.swift | 18 +++++++++++++++--- Sibil/main.swift | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Sibil/Characters.swift b/Sibil/Characters.swift index 58f82fb..6942d87 100644 --- a/Sibil/Characters.swift +++ b/Sibil/Characters.swift @@ -33,10 +33,10 @@ extension CharacterSet { return subsequents }() - func contains(_ char: Character) -> Bool { - let cSet = CharacterSet(charactersIn: String(char)) - let isSuperset = self.isSuperset(of: cSet) - return isSuperset + func contains(_ c: Character) -> Bool { + let cSet = CharacterSet(charactersIn: String(c)) + let containsC = isSuperset(of: cSet) + return containsC } } @@ -51,7 +51,7 @@ extension Character { } var isIdentifierInitial: Bool { - return false + return CharacterSet.identifierInitials.contains(self) } var isIdentifierSubsequent: Bool { diff --git a/Sibil/Lexer.swift b/Sibil/Lexer.swift index 8f91546..e6005ce 100644 --- a/Sibil/Lexer.swift +++ b/Sibil/Lexer.swift @@ -13,6 +13,7 @@ struct Token: CustomDebugStringConvertible { enum Kind { case LeftParen case RightParen + case Identifier } let kind: Kind @@ -79,20 +80,28 @@ extension Lexer: Sequence, IteratorProtocol { } while state != .Emit { - let c = input[index] + let c = input[forward] + print("processing '\(c)' in \(state)") switch state { case .Initial: if c.isLeftParen { - + emit(.LeftParen) } else if c.isRightParen { - + emit(.RightParen) } else if c.isIdentifierInitial { advance() toState(.Identifier) } case .Identifier: + if c.isIdentifierSubsequent { + advance() + } + else { + retract() + emit(.Identifier) + } break case .Emit: // Nothing to do for this state @@ -100,6 +109,9 @@ extension Lexer: Sequence, IteratorProtocol { } } + // Set up for the next token. + index = input.index(after: forward) + return token } } diff --git a/Sibil/main.swift b/Sibil/main.swift index b72e9f7..300d31f 100644 --- a/Sibil/main.swift +++ b/Sibil/main.swift @@ -8,7 +8,7 @@ import Foundation -let l = Lexer(input: "(())") +let l = Lexer(input: "((abc))") for t in l { print(t) }