Compare commits

...
Sign in to create a new pull request.

7 commits

5 changed files with 465 additions and 0 deletions

View file

@ -0,0 +1,266 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* 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 */
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 = "<group>"; };
C0496EBB1E04534F0000E33E /* Lexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lexer.swift; sourceTree = "<group>"; };
C0C60EC01E084631004C1559 /* Characters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Characters.swift; sourceTree = "<group>"; };
/* 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 = "<group>";
};
C0496EB21E04533D0000E33E /* Products */ = {
isa = PBXGroup;
children = (
C0496EB11E04533D0000E33E /* Sibil */,
);
name = Products;
sourceTree = "<group>";
};
C0496EB31E04533D0000E33E /* Sibil */ = {
isa = PBXGroup;
children = (
C0496EB41E04533D0000E33E /* main.swift */,
C0496EBB1E04534F0000E33E /* Lexer.swift */,
C0C60EC01E084631004C1559 /* Characters.swift */,
);
path = Sibil;
sourceTree = "<group>";
};
/* 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 = (
C0496EBC1E04534F0000E33E /* Lexer.swift in Sources */,
C0C60EC11E084631004C1559 /* Characters.swift in Sources */,
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;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = C0496EA91E04533D0000E33E /* Project object */;
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:Sibil.xcodeproj">
</FileRef>
</Workspace>

60
Sibil/Characters.swift Normal file
View file

@ -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(_ c: Character) -> Bool {
let cSet = CharacterSet(charactersIn: String(c))
let containsC = isSuperset(of: cSet)
return containsC
}
}
extension Character {
var isLeftParen: Bool {
return self == "("
}
var isRightParen: Bool {
return self == ")"
}
var isIdentifierInitial: Bool {
return CharacterSet.identifierInitials.contains(self)
}
var isIdentifierSubsequent: Bool {
return CharacterSet.identifierSubsequents.contains(self)
}
}

117
Sibil/Lexer.swift Normal file
View file

@ -0,0 +1,117 @@
//
// Lexer.swift
// Sibil
//
// Created by Eryn Wells on 12/16/16.
// Copyright © 2016 Eryn Wells. All rights reserved.
//
import Foundation
struct Token: CustomDebugStringConvertible {
enum Kind {
case LeftParen
case RightParen
case Identifier
}
let kind: Kind
let value: String
// MARK: CustomDebugStringConvertible
var debugDescription: String {
return "Token(kind: .\(kind), value: \"\(value)\")"
}
}
class Lexer {
let input: String
var index: String.Index
init(input: String) {
self.input = input
self.index = input.startIndex
}
}
extension Lexer: Sequence, IteratorProtocol {
typealias Element = Token
private enum State {
case Initial
case Identifier
case Emit
}
func makeIterator() -> Lexer {
return self
}
func next() -> Token? {
guard index != input.endIndex else {
return nil
}
var state = State.Initial
var token: Token?
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[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
break
}
}
// Set up for the next token.
index = input.index(after: forward)
return token
}
}

15
Sibil/main.swift Normal file
View file

@ -0,0 +1,15 @@
//
// main.swift
// Sibil
//
// Created by Eryn Wells on 12/16/16.
// Copyright © 2016 Eryn Wells. All rights reserved.
//
import Foundation
let l = Lexer(input: "((abc))")
for t in l {
print(t)
}