Commit 7962eb95 authored by David Owens II's avatar David Owens II

Start of the parsing implementation.

parent 6ec945ff
Pipeline #674 passed with stage
......@@ -13,12 +13,48 @@
// limitations under the License.
import atpkgmodel
import Foundation
public enum PackageParserError: ErrorType {
case PackageFileDoesNotExist
case PackageFileDoesNotExist(filename: String)
case MissingPackageDeclaration
case InvalidPackageFile
case UnexpectedToken(expected: Token, actual: Token)
case MissingToken(expected: Token)
}
extension Lexer {
func parseableNext() -> Token? {
while true {
guard let token = self.next() else { return nil }
if case .Comment = token {}
else if case .Terminal = token {}
else { return self.peek() }
}
}
func take(expected: Token) throws {
guard let token = self.parseableNext() else { throw PackageParserError.MissingToken(expected: expected) }
if !Token.isEquivalent(expected, to: token) { throw PackageParserError.UnexpectedToken(expected: expected, actual: token) }
}
}
public func parsePackageDefinition(filepath: String) throws -> Package {
throw PackageParserError.PackageFileDoesNotExist
}
\ No newline at end of file
guard let content: String = try? NSString(contentsOfFile: filepath, encoding: NSUTF8StringEncoding) as String else {
throw PackageParserError.PackageFileDoesNotExist(filename: filepath)
}
let scanner = Scanner(content: content)
let lexer = Lexer(scanner: scanner)
try lexer.take(.OpenParen(line: 0, column: 0))
try lexer.take(.Identifier("package", line: 0, column: 0))
// TODO: Parse the properties of the package definition.
try lexer.take(.CloseParen(line: 0, column: 0))
try lexer.take(.EOF)
let package = Package(name: "nope")
return package
}
......@@ -64,6 +64,33 @@ public enum Token {
default: return false
}
}
public static func isEquivalent(lhs: Token?, to rhs: Token?) -> Bool {
if lhs == nil && rhs == nil { return true }
guard let lhs = lhs else { return false }
guard let rhs = rhs else { return false }
switch (lhs, rhs) {
case let (.Identifier(l0, _, _), .Identifier(r0, _, _)): return l0 == r0
case let (.StringLiteral(l0, _, _), .StringLiteral(r0, _, _)): return l0 == r0
case let (.Comment(l0, _, _), .Comment(r0, _, _)): return l0 == r0
case let (.Unhandled(l0, _, _), .Unhandled(r0, _, _)): return l0 == r0
case (.OpenParen, .OpenParen): return true
case (.CloseParen, .CloseParen): return true
case (.OpenBracket, .OpenBracket): return true
case (.CloseBracket, .CloseBracket): return true
case (.OpenBrace, .OpenBrace): return true
case (.CloseBrace, .CloseBrace): return true
case (.Terminal, .Terminal): return true
case (.Colon, .Colon): return true
case (.EOF, .EOF): return true
default: return false
}
}
}
func isCharacterPartOfSet(c: Character?, set: NSCharacterSet) -> Bool {
......@@ -180,7 +207,7 @@ public class Lexer {
return self.current
}
public func tokenize() -> [Token] {
func tokenize() -> [Token] {
var tokens = [Token]()
while let token = self.next() { tokens.append(token) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment