Commit 67cdcee2 authored by David Owens II's avatar David Owens II

Package model now convertible from parsed files.

Also removed the annoying wrapping strings from Token.StringLiteral.
parent c2586a40
Pipeline #680 passed with stage
......@@ -21,13 +21,14 @@ public enum OutputType {
public class FilePath {
public var path: String
public var expandedItems: [String] {
get { return [] }
}
public init(path: String) {
self.path = path
}
init?(value: ParseValue) {
guard let str = value.stringLiteral else { return nil }
self.path = str
}
}
public class Dependency {
......@@ -36,6 +37,11 @@ public class Dependency {
public init(name: String) {
self.name = name
}
init?(value: ParseValue) {
guard let str = value.stringLiteral else { return nil }
self.name = str
}
}
public class Task {
......@@ -45,8 +51,7 @@ public class Task {
// The optional properties. All optional properties must have a default value.
public var dependencies: [Dependency] = []
public var tool: String = "atllbuild"
public var source: [FilePath] = []
public var version: String = ""
public var sources: [FilePath] = []
public var bootstrapOnly: Bool = false
public var llbuildyaml: String = ""
public var linkSDK: Bool = false
......@@ -57,13 +62,94 @@ public class Task {
public init(name: String) {
self.name = name
}
init?(value: ParseValue, name: String) {
guard let kvp = value.map else { return nil }
if let value = kvp["name"]?.stringLiteral { self.name = value }
else {
print("ERROR: Name is a required property on task.")
return nil
}
if let value = kvp["tool"]?.stringLiteral { self.tool = value }
if let value = kvp["bootstrapOnly"]?.boolLiteral { self.bootstrapOnly = value }
if let value = kvp["llbuildyaml"]?.stringLiteral { self.llbuildyaml = value }
if let value = kvp["linkSDK"]?.boolLiteral { self.linkSDK = value }
if let value = kvp["outputType"]?.stringLiteral {
switch value {
case "lib": self.outputType = .StaticLibrary
case "static-library": self.outputType = .StaticLibrary
case "dylib": self.outputType = .DynamicLibrary
case "dynamic-library": self.outputType = .DynamicLibrary
case "exe": self.outputType = .Executable
case "executable": self.outputType = .Executable
default: print("ERROR: unsupported outputType: \(value), defaulting to: \(self.outputType)")
}
}
if let values = kvp["dependencies"]?.vector {
for value in values {
if let dep = Dependency(value: value) { self.dependencies.append(dep) }
}
}
if let values = kvp["sources"]?.vector {
for value in values {
if let filepath = FilePath(value: value) { self.sources.append(filepath) }
}
}
if let values = kvp["source"]?.vector {
for value in values {
if let filepath = FilePath(value: value) { self.sources.append(filepath) }
}
}
if let values = kvp["compilerOptions"]?.vector {
for value in values {
if let value = value.stringLiteral { self.compilerOptions.append(value) }
}
}
if let values = kvp["linkWithProduct"]?.vector {
for value in values {
if let value = value.stringLiteral { self.linkWithProduct.append(value) }
}
}
}
}
public class Package {
// The required properties.
public var name: String
public var tasks: [Task] = []
// The optional properties. All optional properties must have a default value.
public var version: String = ""
public var tasks: [String:Task] = [:]
public init(name: String) {
self.name = name
}
public init?(type: ParseType) {
if type.name != "package" { return nil }
if let value = type.properties["name"]?.stringLiteral { self.name = value }
else {
print("ERROR: No name specified for the package.")
return nil
}
if let value = type.properties["version"]?.stringLiteral { self.version = value }
if let parsedTasks = type.properties["tasks"]?.map {
for (key, value) in parsedTasks {
if let task = Task(value: value, name: key) {
self.tasks[key] = task
}
}
}
}
}
\ No newline at end of file
......@@ -30,6 +30,39 @@ public enum ParseValue {
case Vector([ParseValue])
}
extension ParseValue {
public var stringLiteral: String? {
if case let .StringLiteral(value) = self { return value }
return nil
}
public var integerLiteral: Int? {
if case let .IntegerLiteral(value) = self { return value }
return nil
}
public var floatLiteral: Double? {
if case let .FloatLiteral(value) = self { return value }
return nil
}
public var boolLiteral: Bool? {
if case let .BoolLiteral(value) = self { return value }
return nil
}
public var map: [String:ParseValue]? {
if case let .Map(value) = self { return value }
return nil
}
public var vector: [ParseValue]? {
if case let .Vector(value) = self { return value }
return nil
}
}
public class ParseType {
public var name: String = ""
public var properties: [String:ParseValue] = [:]
......
......@@ -150,11 +150,10 @@ public class Lexer {
return Token(type: .Comment, value: comment, line: line, column: column)
}
else if next.character == "\"" {
var content = String(next.character!)
var content = ""
while let info = scanner.next() where info.character != "\"" {
content.append(info.character!)
}
content.append(scanner.peek()!.character!)
return Token(type: .StringLiteral, value: content, line: next.line, column: next.column)
}
......
......@@ -54,7 +54,8 @@ let tests: [Test] = [
ScannerTests(),
LexerTests(),
ParserTests()
ParserTests(),
PackageTests()
]
for test in tests {
......
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import atpkg
class PackageTests: Test {
required init() {}
let tests = [
PackageTests.testBasic
]
let filename = __FILE__
static func testBasic() throws {
let filepath = "./atpkg/tests/collateral/basic.atpkg"
guard let parser = Parser(filepath: filepath) else {
try test.assert(false); return
}
let result = try parser.parse()
guard let package = Package(type: result) else { try test.assert(false); return }
try test.assert(package.name == "basic")
try test.assert(package.version == "0.1.0-dev")
try test.assert(package.tasks.count == 1)
for (key, task) in package.tasks {
try test.assert(key == "build")
try test.assert(task.tool == "lldb-build")
try test.assert(task.name == "json-swift")
try test.assert(task.outputType == .StaticLibrary)
try test.assert(task.sources.count == 1)
try test.assert(task.sources[0].path == "src/**.swift")
}
// (package
// :name "basic"
// :version "0.1.0-dev"
// :tasks {:build {:tool "lldb-build"
// :name "json-swift"
// :output-type "lib"
// :source [ "src/**.swift" ]}}
// )
// ; End of the sample.
}
}
......@@ -24,11 +24,10 @@ func outputBaseline(lexer: Lexer) {
switch token.type {
case .Terminal: value = "\\n"
case .StringLiteral: value = token.value.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
default: value = token.value
}
let output = "try test.assert(lexer.next() == Token(type: \(type), value: \"\(value)\", line: \(token.line), column: \(token.column)))"
let output = "try test.assert(lexer.next() == Token(type: \(type), value: \(value), line: \(token.line), column: \(token.column)))"
print(output)
}
print("--- end baseline ---")
......@@ -56,11 +55,11 @@ class LexerTests: Test {
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 2, column: 8))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 3, column: 2))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "name", line: 3, column: 3))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"basic\"", line: 3, column: 8))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "basic", line: 3, column: 8))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 3, column: 15))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 4, column: 2))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "version", line: 4, column: 3))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"0.1.0-dev\"", line: 4, column: 11))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "0.1.0-dev", line: 4, column: 11))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 4, column: 22))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 5, column: 2))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 6, column: 2))
......@@ -71,20 +70,20 @@ class LexerTests: Test {
try test.assert(lexer.next() == Token(type: TokenType.OpenBrace, value: "{", line: 6, column: 17))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 6, column: 18))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "tool", line: 6, column: 19))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"lldb-build\"", line: 6, column: 24))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "lldb-build", line: 6, column: 24))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 6, column: 36))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 7, column: 18))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "name", line: 7, column: 19))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"json-swift\"", line: 7, column: 24))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "json-swift", line: 7, column: 24))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 7, column: 36))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 8, column: 18))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "output-type", line: 8, column: 19))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"lib\"", line: 8, column: 31))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "lib", line: 8, column: 31))
try test.assert(lexer.next() == Token(type: TokenType.Terminal, value: "\n", line: 8, column: 37))
try test.assert(lexer.next() == Token(type: TokenType.Colon, value: ":", line: 9, column: 18))
try test.assert(lexer.next() == Token(type: TokenType.Identifier, value: "source", line: 9, column: 19))
try test.assert(lexer.next() == Token(type: TokenType.OpenBracket, value: "[", line: 9, column: 26))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "\"src/**.swift\"", line: 9, column: 28))
try test.assert(lexer.next() == Token(type: TokenType.StringLiteral, value: "src/**.swift", line: 9, column: 28))
try test.assert(lexer.next() == Token(type: TokenType.CloseBracket, value: "]", line: 9, column: 43))
try test.assert(lexer.next() == Token(type: TokenType.CloseBrace, value: "}", line: 9, column: 44))
try test.assert(lexer.next() == Token(type: TokenType.CloseBrace, value: "}", line: 9, column: 45))
......
......@@ -16,23 +16,6 @@
import Foundation
import atpkg
extension ParseValue {
var stringLiteral: String? {
if case let .StringLiteral(value) = self { return value }
return nil
}
var map: [String:ParseValue]? {
if case let .Map(value) = self { return value }
return nil
}
var vector: [ParseValue]? {
if case let .Vector(value) = self { return value }
return nil
}
}
class ParserTests: Test {
required init() {}
let tests = [
......@@ -51,11 +34,11 @@ class ParserTests: Test {
let name = result.properties["name"]
try test.assert(name != nil)
try test.assert(name?.stringLiteral == "\"basic\"")
try test.assert(name?.stringLiteral == "basic")
let version = result.properties["version"]
try test.assert(version != nil)
try test.assert(version?.stringLiteral == "\"0.1.0-dev\"")
try test.assert(version?.stringLiteral == "0.1.0-dev")
let tasks = result.properties["tasks"]
try test.assert(tasks != nil)
......@@ -65,19 +48,19 @@ class ParserTests: Test {
let tool = build?.map?["tool"]
try test.assert(tool != nil)
try test.assert(tool?.stringLiteral == "\"lldb-build\"")
try test.assert(tool?.stringLiteral == "lldb-build")
let buildName = build?.map?["name"]
try test.assert(buildName != nil)
try test.assert(buildName?.stringLiteral == "\"json-swift\"")
try test.assert(buildName?.stringLiteral == "json-swift")
let outputType = build?.map?["output-type"]
try test.assert(outputType != nil)
try test.assert(outputType?.stringLiteral == "\"lib\"")
try test.assert(outputType?.stringLiteral == "lib")
let source = build?.map?["source"]
try test.assert(source != nil)
try test.assert(source?.vector != nil)
try test.assert(source?.vector?[0].stringLiteral == "\"src/**.swift\"")
try test.assert(source?.vector?[0].stringLiteral == "src/**.swift")
}
}
\ No newline at end of file
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