mirror of
https://github.com/github/semantic.git
synced 2024-12-27 00:44:57 +03:00
Parameterize CofreeJSON by SourcePos
This commit is contained in:
parent
92c4141b44
commit
dd8d4bac2c
@ -10,7 +10,7 @@ import Foundation
|
||||
import Madness
|
||||
import Prelude
|
||||
|
||||
public typealias CofreeJSON = Cofree<JSONLeaf, Range<String.CharacterView.Index>>
|
||||
public typealias CofreeJSON = Cofree<JSONLeaf, (SourcePos<String.Index>, Range<String.Index>)>
|
||||
public typealias JSONParser = Parser<String.CharacterView, CofreeJSON>.Function
|
||||
|
||||
// Inlined for performance reasons
|
||||
@ -29,16 +29,16 @@ typealias MembersParser = Parser<String.CharacterView, [(String, CofreeJSON)]>.F
|
||||
|
||||
// Parses an array of (String, CofreeJSON) object members
|
||||
func members(json: JSONParser) -> MembersParser {
|
||||
let keyAndKeyTerm: Parser<String.CharacterView, (String, CofreeJSON)>.Function = quoted --> { (_, range, key) in
|
||||
(key, Cofree(range, .Leaf(.String(key))))
|
||||
let keyAndKeyTerm: Parser<String.CharacterView, (String, CofreeJSON)>.Function = quoted --> { (pos, range, key) in
|
||||
(key, Cofree((pos, range), .Leaf(.String(key))))
|
||||
}
|
||||
let pairs: Parser<String.CharacterView, (String, CofreeJSON)>.Function = (curry(pair) <^>
|
||||
keyAndKeyTerm
|
||||
<* whitespace
|
||||
<* %":"
|
||||
<* whitespace
|
||||
<*> json) --> { (_, range, values) in
|
||||
(values.0.0, Cofree(range, .Fixed([values.0.1, values.1])))
|
||||
<*> json) --> { (pos, range, values) in
|
||||
(values.0.0, Cofree((pos, range), .Fixed([values.0.1, values.1])))
|
||||
}
|
||||
|
||||
return sepBy(pairs, whitespace <* %"," <* whitespace)
|
||||
@ -46,7 +46,7 @@ func members(json: JSONParser) -> MembersParser {
|
||||
|
||||
public let json: JSONParser = fix { json in
|
||||
let string: JSONParser = quoted --> {
|
||||
Cofree($1, .Leaf(.String($2)))
|
||||
Cofree(($0, $1), .Leaf(.String($2)))
|
||||
} <?> "string"
|
||||
|
||||
let array: JSONParser = %"["
|
||||
@ -55,7 +55,7 @@ public let json: JSONParser = fix { json in
|
||||
<* whitespace
|
||||
<* %"]"
|
||||
--> {
|
||||
Cofree($1, .Indexed($2))
|
||||
Cofree(($0, $1), .Indexed($2))
|
||||
} <?> "array"
|
||||
|
||||
let object: JSONParser = %"{"
|
||||
@ -63,21 +63,21 @@ public let json: JSONParser = fix { json in
|
||||
*> members(json)
|
||||
<* whitespace
|
||||
<* %"}"
|
||||
--> { (_, range, values: [(String, CofreeJSON)]) in
|
||||
Cofree(range, .Keyed(Dictionary(elements: values)))
|
||||
--> { (pos, range, values: [(String, CofreeJSON)]) in
|
||||
Cofree((pos, range), .Keyed(Dictionary(elements: values)))
|
||||
} <?> "object"
|
||||
|
||||
let numberParser: JSONParser = (number --> { _, range, value in
|
||||
Cofree(range, .Leaf(JSONLeaf.Number(value)))
|
||||
let numberParser: JSONParser = (number --> { pos, range, value in
|
||||
Cofree((pos, range), .Leaf(JSONLeaf.Number(value)))
|
||||
}) <?> "number"
|
||||
|
||||
let null: JSONParser = %"null" --> { (_, range, value) in
|
||||
return Cofree(range, .Leaf(.Null))
|
||||
let null: JSONParser = %"null" --> { pos, range, value in
|
||||
return Cofree((pos, range), .Leaf(.Null))
|
||||
} <?> "null"
|
||||
|
||||
let boolean: JSONParser = %"false" <|> %"true" --> { (_, range, value) in
|
||||
let boolean: JSONParser = %"false" <|> %"true" --> { pos, range, value in
|
||||
let boolean = value == "true"
|
||||
return Cofree(range, .Leaf(.Boolean(boolean)))
|
||||
return Cofree((pos, range), .Leaf(.Boolean(boolean)))
|
||||
} <?> "boolean"
|
||||
|
||||
// TODO: This should be JSON = dict <|> array and
|
||||
|
Loading…
Reference in New Issue
Block a user