Implemented enhanced tokenizer for symbols.
h3rald h3rald@h3rald.com
Sun, 27 Oct 2024 10:30:52 +0100
4 files changed,
64 insertions(+),
2 deletions(-)
M
minpkg/core/shell.nim
→
minpkg/core/shell.nim
@@ -7,6 +7,7 @@ critbits,
algorithm, streams, terminal, + json, os ]@@ -110,12 +111,26 @@ stdout.write(s)
else: stdout.styledWrite(color, s) +proc printSymbol(s: string) = + let pS = processSymbolValue(s) + if pS.len == 0: + p(s, fgCyan) + else: + for part in pS.items: + if part["subtype"].getStr == "tkDict": + p(part["value"].getStr, fgBlue) + elif ["tkDot", "tkAutopop", "tkSystemSigil"].contains part[ + "subtype"].getStr: + p(part["value"].getStr, fgRed) + else: + p(part["value"].getStr, fgCyan) + proc pv(item: MinValue) = case item.kind of minNull, minBool: p($item, fgGreen) of minSymbol: - p($item, fgCyan) + printSymbol($item) of minString: p($item, fgYellow) of minFloat, minInt:
M
minpkg/core/utils.nim
→
minpkg/core/utils.nim
@@ -119,7 +119,7 @@ var r = newSeq[MinValue](0)
for key, value in q.dVal.pairs: if value.kind == minProcOp: raiseInvalid("Dictionary contains operators that cannot be accessed.") - var p = newSeq[MinValue](0) + var p = newSeq[MinValue](0) p.add value.val p.add key.newVal r.add p.newVal@@ -406,3 +406,40 @@ a = i.pop
b = i.pop if not (a.isQuotation and b.isQuotation or a.isString and b.isString): raiseInvalid("Two quotations or two strings are required on the stack") + +const SYSTEM_SIGILS* = @[':', '\'', '?', '~', '@', '^'] + +proc processSymbolValue*(v: string): JsonNode = + result = newJArray() + var sym = v + if SYSTEM_SIGILS.contains(v[0]): + sym = v[1..^1] + var sigil = newJObject() + sigil["subtype"] = %"tkSystemSigil" + sigil["value"] = %($v[0]) + result.add sigil + var syms = sym.split('.') + var count = 0 + for s in syms: + sym = s + var symbol = newJObject() + var subtype = "tkDict" + if syms.len == 1 or count >= syms.len-1: + subtype = "tkSymbol" + symbol["subtype"] = %subtype + symbol["value"] = %sym + result.add symbol + if count < syms.len-1: + var dot = newJObject() + dot["subtype"] = %"tkDot" + dot["value"] = %"." + result.add dot + count += 1 + if sym.len > 0 and sym[^1] == '!': + # Shorten previous symbol + let lastVal = result[^1]["value"].getStr + result[^1]["value"] = %lastVal[0..^2] + var autopop = newJObject() + autopop["subtype"] = %"tkAutopop" + autopop["value"] = %"!" + result.add autopop
M
minpkg/lib/min_global.nim
→
minpkg/lib/min_global.nim
@@ -1191,12 +1191,20 @@ var q = newSeq[MinValue](0)
var dict = newDict(i.scope) i.dset(dict, "type", newVal($t)) i.dset(dict, "value", p.a.processTokenValue(t).newVal) + if t == tkSymbol: + let processedSymbol = processSymbolValue(p.a) + if processedSymbol.len > 0: + i.dset(dict, "processedSymbol", i.fromJson processedSymbol) q.add dict while t != tkEof: t = p.getToken() var dict = newDict(i.scope) i.dset(dict, "type", newVal($t)) i.dset(dict, "value", p.a.processTokenValue(t).newVal) + if t == tkSymbol: + let processedSymbol = processSymbolValue(p.a) + if processedSymbol.len > 0: + i.dset(dict, "processedSymbol", i.fromJson processedSymbol) q.add dict i.push q.newVal
M
next-release.md
→
next-release.md
@@ -6,4 +6,6 @@ ### Fixes and Improvements
* Implemented `define-sigil` (was documented but not actually implemented). * The `help` symbol now correctly displays help for namespaced symbols. +* Enhanced the `tokenizer` symbol to provide additional information for symbols. +* Enhanced min shell highlighting to support dot notation, sigils, autopop.