mirror of
https://github.com/github/semantic.git
synced 2024-12-27 17:05:33 +03:00
commit
1d012a91a4
@ -24,7 +24,6 @@
|
||||
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5351B5AE2D0004E581F /* Array.swift */; };
|
||||
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5371B5AE2D0004E581F /* Doc.swift */; };
|
||||
D4AAE5451B5AE2D0004E581F /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53A1B5AE2D0004E581F /* Operators.swift */; };
|
||||
D4AAE5461B5AE2D0004E581F /* Parse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53B1B5AE2D0004E581F /* Parse.swift */; };
|
||||
D4AAE5471B5AE2D0004E581F /* Prelude.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53C1B5AE2D0004E581F /* Prelude.swift */; };
|
||||
D4AAE5481B5AE2D0004E581F /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53D1B5AE2D0004E581F /* String.swift */; };
|
||||
D4AAE5491B5AE2D0004E581F /* StringLiteralConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */; };
|
||||
@ -108,7 +107,6 @@
|
||||
D4AAE5351B5AE2D0004E581F /* Array.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = "<group>"; };
|
||||
D4AAE5371B5AE2D0004E581F /* Doc.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Doc.swift; sourceTree = "<group>"; };
|
||||
D4AAE53A1B5AE2D0004E581F /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
|
||||
D4AAE53B1B5AE2D0004E581F /* Parse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parse.swift; sourceTree = "<group>"; };
|
||||
D4AAE53C1B5AE2D0004E581F /* Prelude.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Prelude.swift; sourceTree = "<group>"; };
|
||||
D4AAE53D1B5AE2D0004E581F /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
|
||||
D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringLiteralConvertible.swift; sourceTree = "<group>"; };
|
||||
@ -198,7 +196,6 @@
|
||||
D4AAE5351B5AE2D0004E581F /* Array.swift */,
|
||||
D4AAE5371B5AE2D0004E581F /* Doc.swift */,
|
||||
D4AAE53A1B5AE2D0004E581F /* Operators.swift */,
|
||||
D4AAE53B1B5AE2D0004E581F /* Parse.swift */,
|
||||
D4AAE53C1B5AE2D0004E581F /* Prelude.swift */,
|
||||
D4AAE53D1B5AE2D0004E581F /* String.swift */,
|
||||
D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */,
|
||||
@ -442,7 +439,6 @@
|
||||
D49FCBC61BBF214300C5E9C3 /* Patch.swift in Sources */,
|
||||
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */,
|
||||
D45A36CD1BBC75DF00BE3DDE /* Info.swift in Sources */,
|
||||
D4AAE5461B5AE2D0004E581F /* Parse.swift in Sources */,
|
||||
D49FCBC81BBF2C4300C5E9C3 /* Algorithm.swift in Sources */,
|
||||
D4DF970C1BC5DF9E0040F41F /* BoundsCheckedArray.swift in Sources */,
|
||||
D432D4751BA9D6A400F3FABC /* Memo.swift in Sources */,
|
||||
|
@ -1,38 +1,3 @@
|
||||
infix operator <|> {
|
||||
associativity left
|
||||
precedence 120
|
||||
}
|
||||
|
||||
infix operator <*> {
|
||||
associativity left
|
||||
precedence 130
|
||||
}
|
||||
|
||||
infix operator <* {
|
||||
associativity left
|
||||
precedence 130
|
||||
}
|
||||
|
||||
infix operator *> {
|
||||
associativity left
|
||||
precedence 130
|
||||
}
|
||||
|
||||
infix operator <^> {
|
||||
associativity left
|
||||
precedence 130
|
||||
}
|
||||
|
||||
infix operator >>- {
|
||||
associativity left
|
||||
precedence 100
|
||||
}
|
||||
|
||||
infix operator &&& {
|
||||
associativity left
|
||||
precedence 120
|
||||
}
|
||||
|
||||
infix operator <> {
|
||||
associativity right
|
||||
precedence 140
|
||||
@ -52,9 +17,3 @@ infix operator >>> {
|
||||
associativity right
|
||||
precedence 170
|
||||
}
|
||||
|
||||
prefix operator ^ {}
|
||||
|
||||
postfix operator * {}
|
||||
|
||||
postfix operator + {}
|
||||
|
@ -1,97 +0,0 @@
|
||||
public struct State<T> {
|
||||
public let rest: String
|
||||
public let value: T
|
||||
}
|
||||
|
||||
public prefix func ^(string: String)(_ input: String) -> State<String>? {
|
||||
return input.characters.startsWith(string.characters)
|
||||
? State(rest: input.from(string.characters.count), value: string)
|
||||
: nil
|
||||
}
|
||||
|
||||
public prefix func ^(strings: [String])(_ input: String) -> State<String>? {
|
||||
return strings.indexOf { input.characters.startsWith($0.characters) }
|
||||
.flatMap { index in
|
||||
let string = strings[index]
|
||||
return (^string)(input)
|
||||
}
|
||||
}
|
||||
|
||||
public prefix func ^<S where S: SequenceType, S.Generator.Element == Character>(strings: S) -> String -> State<String>? {
|
||||
return ^strings.map { String($0) }
|
||||
}
|
||||
|
||||
|
||||
/// Parse the full string with parser or fail.
|
||||
public func full<A>(parser: String -> State<A>?)(_ input: String) -> A? {
|
||||
return parser(input).flatMap { $0.rest.isEmpty ? $0.value : nil }
|
||||
}
|
||||
|
||||
|
||||
/// A convenience for use while developing parsers.
|
||||
public func never<T>(_: String) -> State<T>? {
|
||||
return nil
|
||||
}
|
||||
|
||||
//// Matches a single character that is not matched by `parser`.
|
||||
public func not<T>(parser: String -> State<T>?)(_ input: String) -> State<String>? {
|
||||
if input.isEmpty { return nil }
|
||||
return parser(input).map(const(nil)) ?? State(rest: input.from(1), value: input.to(1))
|
||||
}
|
||||
|
||||
|
||||
/// Delays evaluation of a parser.
|
||||
public func delay<A>(thunk: () -> String -> State<A>?) -> String -> State<A>? {
|
||||
return { thunk()($0) }
|
||||
}
|
||||
|
||||
|
||||
public func parseWhile(predicate: Character -> Bool)(_ input: String) -> State<String>? {
|
||||
return input.characters.count > 0 && predicate(input.characters[input.startIndex])
|
||||
? parseWhile(predicate)(input.from(1)).map { State(rest: $0.rest, value: input.to(1) + $0.value) } ?? State(rest: input.from(1), value: input.to(1))
|
||||
: nil
|
||||
}
|
||||
|
||||
public postfix func * <T>(parser: String -> State<T>?) -> String -> State<[T]>? {
|
||||
return (Array.cons <^> (parser <*> { (parser*)($0) })) <|> pure([])
|
||||
}
|
||||
|
||||
public postfix func + <T> (parser: String -> State<T>?) -> String -> State<[T]>? {
|
||||
return (Array.cons <^> (parser <*> parser*))
|
||||
}
|
||||
|
||||
public func interpolate<Separator, Element>(element: String -> State<Element>?, _ separator: String -> State<Separator>?) -> String -> State<[Element]>? {
|
||||
return Array.cons <^> (element <*> (separator *> element)*) <|> pure([])
|
||||
}
|
||||
|
||||
public func <*> <T, U> (left: String -> State<T>?, right: String -> State<U>?)(_ input: String) -> State<(T, U)>? {
|
||||
return left(input).flatMap { l in
|
||||
right(l.rest).map { r in
|
||||
State(rest: r.rest, value: (l.value, r.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func <* <T, U> (left: String -> State<T>?, right: String -> State<U>?) -> String -> State<T>? {
|
||||
return left >>- { const($0) <^> right }
|
||||
}
|
||||
|
||||
public func *> <T, U> (left: String -> State<T>?, right: String -> State<U>?) -> String -> State<U>? {
|
||||
return left >>- const(right)
|
||||
}
|
||||
|
||||
public func <|> <T> (left: String -> State<T>?, right: String -> State<T>?)(_ input: String) -> State<T>? {
|
||||
return left(input) ?? right(input)
|
||||
}
|
||||
|
||||
public func <^> <T, U> (left: T -> U, right: String -> State<T>?) -> String -> State<U>? {
|
||||
return right >>- { pure(left($0)) }
|
||||
}
|
||||
|
||||
public func >>- <T, U> (left: String -> State<T>?, right: T -> String -> State<U>?)(_ input: String) -> State<U>? {
|
||||
return left(input).flatMap { state in right(state.value)(state.rest) }
|
||||
}
|
||||
|
||||
public func pure<A>(value: A)(_ input: String) -> State<A>? {
|
||||
return State(rest: input, value: value)
|
||||
}
|
Loading…
Reference in New Issue
Block a user