1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 04:41:47 +03:00
semantic/prototype/doubt-difftool/Argument.swift

61 lines
1.4 KiB
Swift
Raw Normal View History

2015-11-02 20:04:09 +03:00
/// A list of arguments for the difftool.
2015-11-02 19:36:20 +03:00
enum Argument {
2015-11-02 19:36:52 +03:00
indirect case File(Source, Argument)
2015-11-02 19:44:52 +03:00
indirect case OutputFlag(Output, Argument)
2015-11-02 19:36:52 +03:00
case End
2015-11-02 19:37:54 +03:00
2015-11-02 20:03:51 +03:00
private var rest: Argument? {
2015-11-02 19:44:18 +03:00
switch self {
case let .File(_, rest):
return rest
2015-11-02 19:44:52 +03:00
case let .OutputFlag(_, rest):
return rest
2015-11-02 19:44:18 +03:00
case .End:
return nil
}
}
2015-11-02 19:37:54 +03:00
var files: [Source] {
switch self {
case let .File(a, rest):
return [a] + rest.files
default:
return rest?.files ?? []
2015-11-02 19:37:54 +03:00
}
}
2015-11-02 19:44:42 +03:00
2015-11-02 20:07:38 +03:00
var output: Output {
func output(argument: Argument, defaultingTo: Output = .Split) -> Output {
switch argument {
case let .OutputFlag(f, rest):
return output(rest, defaultingTo: f)
default:
return rest.map { output($0, defaultingTo: defaultingTo) } ?? defaultingTo
}
}
return output(self)
}
2015-11-02 19:44:42 +03:00
enum Output {
case Unified
case Split
}
2015-11-02 19:36:20 +03:00
}
2015-11-02 19:35:32 +03:00
2015-11-02 19:51:13 +03:00
private let flag: Madness.Parser<[String], Argument.Output>.Function =
const(Argument.Output.Unified) <^> satisfy { $0 == "--unified" }
<|> const(Argument.Output.Split) <^> satisfy { $0 == "--split" }
2015-11-02 20:57:41 +03:00
<|> pure(Argument.Output.Split)
2015-11-02 19:46:57 +03:00
private let source: Madness.Parser<[String], Source>.Function =
{ try! Source($0) } <^> satisfy { !$0.hasPrefix("--") }
2015-11-02 20:50:34 +03:00
let argumentsParser: Madness.Parser<[String], Argument>.Function = satisfy(const(true)) *> fix { rest in
(curry(Argument.OutputFlag) <^> flag <|> curry(Argument.File) <^> source)
<*> rest
2015-11-02 20:03:16 +03:00
<|> pure(Argument.End)
2015-11-02 20:01:33 +03:00
}
2015-11-02 19:35:41 +03:00
import Madness
2015-11-02 19:51:13 +03:00
import Prelude