Removed json support from minimin, updated tasks.
h3rald h3rald@h3rald.com
Fri, 11 Dec 2020 22:14:44 +0100
9 files changed,
114 insertions(+),
83 deletions(-)
M
core/baseutils.nim
→
core/baseutils.nim
@@ -20,7 +20,31 @@ if dirEnd < 0:
return s return s[0..dirEnd] + proc escapeEx*(s: string, unquoted = false): string = + for c in s: + case c + of '\L': result.add("\\n") + of '\b': result.add("\\b") + of '\f': result.add("\\f") + of '\t': result.add("\\t") + of '\v': result.add("\\u000b") + of '\r': result.add("\\r") + of '"': result.add("\\\"") + of '\0'..'\7': result.add("\\u000" & $ord(c)) + of '\14'..'\31': result.add("\\u00" & toHex(ord(c), 2)) + of '\\': result.add("\\\\") + else: result.add(c) + if unquoted: + return result + return "\"" & result & "\"" + else: - import os + import os, json + proc parentDirEx*(s: string): string = return s.parentDir + + proc escapeEx*(s: string, unquoted = false): string = + if unquoted: + return s.escapeJsonUnquoted + return s.escapeJson
M
core/parser.nim
→
core/parser.nim
@@ -3,11 +3,11 @@ import
lexbase, strutils, sequtils, - json, oids, streams, - critbits - + critbits, + baseutils + import unicode except strip type@@ -665,7 +665,7 @@ of tkFalse:
result = @[op&"MinValue(kind: minBool, boolVal: false)"] discard getToken(p) of tkString: - result = @[op&"MinValue(kind: minString, strVal: "&p.a.escapeJson&")"] + result = @[op&"MinValue(kind: minString, strVal: "&p.a.escapeEx&")"] p.a = "" discard getToken(p) of tkInt:@@ -719,7 +719,7 @@ raiseInvalid("Invalid dictionary")
result.add indent&"var "&scopevar&" = newScopeRef(nil)" result.add op&"MinValue(kind: minDictionary, scope: "&scopevar&")" of tkSymbol: - result = @[op&"MinValue(kind: minSymbol, symVal: "&p.a.escapeJson&", column: " & $p.getColumn & ", line: " & $p.lineNumber & ", filename: "&p.filename.escapeJson&")"] + result = @[op&"MinValue(kind: minSymbol, symVal: "&p.a.escapeEx&", column: " & $p.getColumn & ", line: " & $p.lineNumber & ", filename: "&p.filename.escapeEx&")"] p.a = "" discard getToken(p) else:
M
core/utils.nim
→
core/utils.nim
@@ -1,13 +1,16 @@
import strutils, - critbits, - json + critbits import baseutils, parser, value, scope, interpreter + +when not defined(mini): + import + json # Library methods@@ -40,8 +43,6 @@ if name != "":
scope.previous.symbols[name] = MinOperator(kind: minProcOp, prc: op) # Dictionary Methods - -proc `%`*(i: In, a: MinValue): JsonNode {.extern:"min_exported_symbol_percent_2".} proc dget*(i: In, q: MinValue, s: MinValue): MinValue {.extern:"min_exported_symbol_$1".}= if not q.isDictionary:@@ -119,61 +120,63 @@ r.add key.newVal
r.add value.val return r.newVal -# JSON interop +when not defined(mini): -proc `%`*(i: In, a: MinValue): JsonNode {.extern:"min_exported_symbol_percent_2".}= - case a.kind: - of minBool: - return %a.boolVal - of minSymbol: - return %(";sym:$1" % [a.getstring]) - of minString: - return %a.strVal - of minInt: - return %a.intVal - of minFloat: - return %a.floatVal - of minQuotation: - result = newJArray() - for it in a.qVal: - result.add(i%it) - of minDictionary: - result = newJObject() - for it in a.dVal.pairs: - result[it.key] = i%i.dget(a, it.key) + # JSON interop -proc fromJson*(i: In, json: JsonNode): MinValue {.extern:"min_exported_symbol_$1".}= - case json.kind: - of JNull: - result = newSeq[MinValue](0).newVal - of JBool: - result = json.getBool.newVal - of JInt: - result = json.getBiggestInt.newVal - of JFloat: - result = json.getFloat.newVal - of JString: - let s = json.getStr - if s.startsWith(";sym:"): - result = s.replace(";sym:", "").newSym - else: - result = json.getStr.newVal - of JObject: - var res = newDict(i.scope) - for key, value in json.pairs: - var first = $key[0] - var rest = "" - if key.len > 1: - rest = key[1..key.len-1] - #first = sgregex.replace(first, peg"[^a-zA-Z0-9_]", "_") - #rest = sgregex.replace(rest, peg"[^a-zA-Z0-9/!?+*._-]", "_") - discard i.dset(res, first&rest, i.fromJson(value)) - return res - of JArray: - var res = newSeq[MinValue](0) - for value in json.items: - res.add i.fromJson(value) - return res.newVal + proc `%`*(i: In, a: MinValue): JsonNode {.extern:"min_exported_symbol_percent_2".}= + case a.kind: + of minBool: + return %a.boolVal + of minSymbol: + return %(";sym:$1" % [a.getstring]) + of minString: + return %a.strVal + of minInt: + return %a.intVal + of minFloat: + return %a.floatVal + of minQuotation: + result = newJArray() + for it in a.qVal: + result.add(i%it) + of minDictionary: + result = newJObject() + for it in a.dVal.pairs: + result[it.key] = i%i.dget(a, it.key) + + proc fromJson*(i: In, json: JsonNode): MinValue {.extern:"min_exported_symbol_$1".}= + case json.kind: + of JNull: + result = newSeq[MinValue](0).newVal + of JBool: + result = json.getBool.newVal + of JInt: + result = json.getBiggestInt.newVal + of JFloat: + result = json.getFloat.newVal + of JString: + let s = json.getStr + if s.startsWith(";sym:"): + result = s.replace(";sym:", "").newSym + else: + result = json.getStr.newVal + of JObject: + var res = newDict(i.scope) + for key, value in json.pairs: + var first = $key[0] + var rest = "" + if key.len > 1: + rest = key[1..key.len-1] + first = sgregex.replace(first, peg"[^a-zA-Z0-9_]", "_") + rest = sgregex.replace(rest, peg"[^a-zA-Z0-9/!?+*._-]", "_") + discard i.dset(res, first&rest, i.fromJson(value)) + return res + of JArray: + var res = newSeq[MinValue](0) + for value in json.items: + res.add i.fromJson(value) + return res.newVal # Validators
M
lib/min_lang.nim
→
lib/min_lang.nim
@@ -3,8 +3,7 @@ critbits,
strutils, sequtils, parseopt, - algorithm, - json + algorithm when defined(mini): import rdstdin,@@ -12,6 +11,7 @@ ../core/minilogger
else: import os, + json, logging, ../packages/niftylogger, ../packages/nimline/nimline,@@ -112,10 +112,7 @@ i.push(dict)
except: raiseInvalid("Invalid/unsupported YAML object (only dictionaries with string values are supported)") - def.symbol("from-json") do (i: In): - let vals = i.expect("string") - let s = vals[0] - i.push i.fromJson(s.getString.parseJson) + def.symbol("to-yaml") do (i: In): let vals = i.expect "a"@@ -134,11 +131,6 @@ i.push(yaml.strip.newVal)
except: raiseInvalid(err) - def.symbol("to-json") do (i: In): - let vals = i.expect "a" - let q = vals[0] - i.push(($((i%q).pretty)).newVal) - def.symbol("loglevel") do (i: In): let vals = i.expect("'sym") let s = vals[0]@@ -252,6 +244,17 @@ let s = vals[0]
i.push i.parse s.strVal when not defined(mini): + + def.symbol("from-json") do (i: In): + let vals = i.expect("string") + let s = vals[0] + i.push i.fromJson(s.getString.parseJson) + + def.symbol("to-json") do (i: In): + let vals = i.expect "a" + let q = vals[0] + i.push(($((i%q).pretty)).newVal) + # Save/load symbols def.symbol("save-symbol") do (i: In) {.gcsafe.}:
M
lib/min_str.nim
→
lib/min_str.nim
@@ -1,11 +1,11 @@
import strutils, - sequtils, - json + sequtils import ../core/parser, ../core/value, ../core/interpreter, + ../core/baseutils, ../core/utils when not defined(mini):@@ -215,9 +215,7 @@
def.symbol("escape") do (i: In): let vals = i.expect("'sym") let a = vals[0].getString - var s = "" - a.escapeJsonUnquoted(s) - i.push s.newVal + i.push a.escapeEx(true).newVal def.symbol("prefix") do (i: In): let vals = i.expect("'sym", "'sym")
M
next-release.md
→
next-release.md
@@ -1,4 +1,4 @@
-* Added the possibility to "compile" min files into sibgle executables. This is achieved by converting the specified min file to its raw nim code equivalent and then calling the Nim compiler (which in turns calls the C compiler). +* Added the possibility to "compile" min files into single executables. This is achieved by converting the specified min file to its raw Nim code equivalent and then calling the Nim compiler (which in turns calls the C compiler). * Added **compiled?** symbol which returns true if the program has been compiled. * Added the possibility of including a path containing additional **.min** files to compile along with the main file (**-m**, **--module-path**). * Added the possibility to compile a bare-bones version of min specifying the **-d:mini** compilation flag.
M
site/contents/download.md
→
site/contents/download.md
@@ -62,6 +62,8 @@ * The {#link-module||sys#}
* The following operators: * {#link-operator||lang||load#} * {#link-operator||lang||read#} + * {#link-operator||lang||to-json#} + * {#link-operator||lang||from-json#} * {#link-operator||lang||raw-args#} * {#link-operator||lang||save-symbol#} * {#link-operator||lang||load-symbol#}
M
tasks/release.min
→
tasks/release.min
@@ -21,8 +21,9 @@ build-tasks ^macosx-lite
build-tasks ^macosx-mini github-tasks ^update github-tasks ^upload +) %default +( ssh-tasks ^build ssh-tasks ^h3rald - "All done!" notice -) %default +) %sites +release-tasks