diff modules/json.tp @ 377:93c28eee141e default tip

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 15 Aug 2015 22:45:33 -0700
parents e2c1309ab750
children
line wrap: on
line diff
--- a/modules/json.tp	Sat Aug 15 22:45:24 2015 -0700
+++ b/modules/json.tp	Sat Aug 15 22:45:33 2015 -0700
@@ -18,7 +18,7 @@
 	t <- "t" byte: 0
 	f <- "f" byte: 0
 
-	parseNumAt <- :str :at :recvResult {
+	parseNumAt <- :str at {
 		num <- 0
 		l <- str length
 		minus <- false
@@ -56,12 +56,50 @@
 		}
 	}
 
-	parseStrAt <- :src :at :recvResult {
-		//TODO: Deal with escaped characters
-		end <- src find: "\"" startingAt: at + 1 else: { src length }
+	parseStrAt <- :src at {
+		//TODO: Deal with unicode escapes
+		bslash <- "\\" byte: 0
+		quote <- "\"" byte: 0
+		escapes <- dict hash
+		escapes set: bslash "\\"
+		escapes set: quote "\""
+		escapes set: ("n" byte: 0) "\n"
+		escapes set: ("t" byte: 0) "\t"
+		escapes set: ("r" byte: 0) "\r"
+		i <- at + 1
+		chunks <- #[]
+		start <- i
+		continue <- true
+		while: { continue && i < (src byte_length) } do: {
+			b <- src byte: i
+			if: b = bslash {
+				if: i > start {
+					chunks append: (src from: start withLength: i-start)
+				}
+				if: i + 1 < (src byte_length) {
+					i <- i + 1
+					b <- src byte: i
+					start <- i + 1
+					escapes ifget: b :v {
+						chunks append: v
+					} else: {
+						//not a recognized escape, just copy it raw
+						chunks append: (src from: i-1 withLength: 2)
+					}
+				}
+			} else: {
+				if: b = quote {
+					if: i > start {
+						chunks append: (src from: start withLength: i-start)
+					}
+					continue <- false
+				}
+			}
+			i <- i + 1
+		}
 		#{
-			value <- src from: (at + 1) withLength: (end - at - 1)
-			after <- end + 1
+			value <- chunks join: ""
+			after <- i
 		}
 	}
 
@@ -69,10 +107,10 @@
 		ret <- false
 		b <- text byte: cur
 		if: b = neg || b >= zero && b <= nine {
-			text parseNumAt: cur
+			parseNumAt: text cur
 		} else: {
 			if: b = quote {
-				text parseStrAt: cur
+				parseStrAt: text cur
 			} else: {
 				if: b = startArr {
 					len <- text length
@@ -101,7 +139,7 @@
 				} else: {
 					if: b = startObj {
 						len <- text length
-						val <- dict linear
+						val <- dict hash
 						cur <- cur + 1
 						aft <- -1
 						expectKey <- true