Substitutions.swift 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
private enum ParseState {
    case Initial
    case Escaped
    case Dollar
    case SubstitutionName
}

private func evaluateSubstitution(substitution: String, package: Package) -> String {
    //"prefix-like" substitions here
    //collect sources substition
    if substitution.hasPrefix("collect_sources:") {
12
        let taskName = String(substitution.characters[substitution.characters.index(substitution.characters.startIndex, offsetBy: 16)..<substitution.characters.endIndex])
13 14 15 16 17 18 19 20 21 22 23 24 25
        guard let task = package.tasks[taskName] else {
            fatalError("Cannot find task named \(taskName) for substition.")
        }
        guard let sources = task["sources"]?.vector else {
            fatalError("No sources for task named \(taskName) for substition.")
        }
        var str_sources: [String] = []
        for str in sources {
            guard let str_s = str.string else {
                fatalError("Non-string source \(str) for substition.")
            }
            str_sources.append(str_s)
        }
26
        let collectedSources = collectSources(sourceDescriptions: str_sources, taskForCalculatingPath: task)
27 28
        var output = ""
        for (idx, source) in collectedSources.enumerated() {
29
            output += source.description
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            if idx != collectedSources.count - 1 { output += " "}
        }
        return output
    }

    //"constant" substitions here
    switch(substitution) {
        case "test_substitution":
        return "test_substitution"
        default:
        fatalError("Unknown substitiution \(substitution)")
    }
}

public func evaluateSubstitutions(input: String, package: Package)-> String {
    var output = ""
    var currentSubstitionName = ""
    var parseState = ParseState.Initial
    //introducing, the world's shittiest parser
    for char in input.characters {
        switch(parseState) {
            case .Initial:
                switch(char) {
                    case "$":
                        parseState = .Dollar
                    case "\\":
                        parseState = .Escaped
                    default:
                        output.characters.append(char)
                }

61
            case .Escaped:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                output.characters.append(char)
                parseState = .Initial

            case .Dollar:
                switch(char) {
                    case "{":
                        parseState = .SubstitutionName
                    case "\\":
                        output.characters.append("$")
                        parseState = .Escaped
                    default:
                        output.characters.append("$")
                        output.characters.append(char)
                        parseState = .Initial

                }
            case .SubstitutionName:
                switch(char) {
                    case "}":
81
                        output += evaluateSubstitution(substitution: currentSubstitionName, package: package)
82 83 84 85 86 87 88 89 90
                        currentSubstitionName = ""
                        parseState = .Initial
                    default:
                        currentSubstitionName.characters.append(char)
                }
        }
    }
    return output
}