From 8c341fc129079ff331ed9085e0354e8b842847b8 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 16 Oct 2015 11:09:20 -0400 Subject: [PATCH] Terms annotated with ranges over a string can be printed to JSON. --- prototype/Doubt/Cofree.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/prototype/Doubt/Cofree.swift b/prototype/Doubt/Cofree.swift index e9fc3c44d..e4ba01733 100644 --- a/prototype/Doubt/Cofree.swift +++ b/prototype/Doubt/Cofree.swift @@ -124,5 +124,21 @@ public protocol CofreeType { extension Cofree: CofreeType {} +extension CofreeType where Self.Annotation == Range { + public func JSON(source: String) -> Doubt.JSON { + return unwrap.JSON( + ifLeaf: { _ in .String(source[extract]) }, + ifRecur: { + [ + "range": [ + "offset": .Number(Double(source.startIndex.distanceTo($0.extract.startIndex))), + "length": .Number(Double($0.extract.count)), + ], + "unwrap": $0.JSON(source) + ] + }) + } +} + import Prelude