From 8cb07bc2099d7f0b88202c713b9c64f741b91618 Mon Sep 17 00:00:00 2001 From: Andy Davidoff Date: Sat, 26 Dec 2020 15:07:30 -0500 Subject: [PATCH] switch to ups for sanitization --- .gitmodules | 4 ++ nimph.nimble | 1 + nimph/sanitize.nim | 111 --------------------------------------------- nimph/spec.nim | 2 +- ups | 1 + 5 files changed, 7 insertions(+), 112 deletions(-) delete mode 100644 nimph/sanitize.nim create mode 160000 ups diff --git a/.gitmodules b/.gitmodules index 82213fd..650cd37 100644 --- a/.gitmodules +++ b/.gitmodules @@ -28,3 +28,7 @@ [submodule "testes"] path = testes url = https://github.com/disruptek/testes +[submodule "ups"] + path = ups + url = https://github.com/disruptek/ups + branch = master diff --git a/nimph.nimble b/nimph.nimble index 0862311..d766a33 100644 --- a/nimph.nimble +++ b/nimph.nimble @@ -4,6 +4,7 @@ license = "MIT" requires "https://github.com/disruptek/github >= 2.0.3 & < 3.0.0" requires "https://github.com/c-blake/cligen >= 0.9.46 & < 2.0.0" requires "https://github.com/disruptek/bump >= 1.8.18 & < 2.0.0" +requires "https://github.com/disruptek/ups < 2.0.0" requires "https://github.com/zevv/npeg >= 0.21.3 & < 1.0.0" requires "https://github.com/disruptek/jsonconvert < 2.0.0" requires "https://github.com/disruptek/badresults < 2.0.0" diff --git a/nimph/sanitize.nim b/nimph/sanitize.nim deleted file mode 100644 index 9772dcc..0000000 --- a/nimph/sanitize.nim +++ /dev/null @@ -1,111 +0,0 @@ -import std/macros -import std/options -import std/strutils -import std/hashes - -type - NimIdentifier* = distinct string ## a valid nim identifier - -const - elideUnderscoresInIdentifiers {.booldefine.} = false - -when nimvm: - discard -else: - import cutelog - -proc isValidNimIdentifier*(s: string): bool = - ## true for strings that are valid identifier names - block complete: - if s.len > 0 and s[0] in IdentStartChars: - if s.len > 1 and '_' in [s[0], s[^1]]: - break complete - for i in 1..s.len-1: - if s[i] notin IdentChars: - break complete - if s[i] == '_' and s[i-1] == '_': - break complete - result = true - -template cappableAdd(s: var string; c: char) = - ## add a char to a string, perhaps capitalizing it - if s.len > 0 and s[^1] == '_': - s.add c.toUpperAscii() - else: - s.add c - -proc sanitizeIdentifier*(name: string; capsOkay = false): Option[NimIdentifier] = - ## convert any string to a valid nim identifier in camel_Case - assert name == strip(name), "please strip whitespace" - var id = "" - block sanitized: - if name.len == 0: - break sanitized - for c in name: - if id.len == 0: - if c in IdentStartChars: - id.cappableAdd c - continue - elif c in IdentChars: - id.cappableAdd c - continue - # help differentiate words case-insensitively - id.add '_' - when not elideUnderscoresInIdentifiers: - while "__" in id: - id = id.replace("__", "_") - if id.len > 1: - id.removeSuffix {'_'} - id.removePrefix {'_'} - # if we need to lowercase the first letter, we'll lowercase - # until we hit a word boundary (_, digit, or lowercase char) - if not capsOkay and id[0].isUpperAscii: - for i in id.low..id.high: - if id[i] in ['_', id[i].toLowerAscii]: - break - id[i] = id[i].toLowerAscii - # ensure we're not, for example, starting with a digit - if id[0] notin IdentStartChars: - when nimvm: - warning "identifiers cannot start with `" & id[0] & "`" - else: - discard - # warn "identifiers cannot start with `" & id[0] & "`" - break sanitized - when elideUnderscoresInIdentifiers: - if id.len > 1: - while "_" in id: - id = id.replace("_", "") - if not id.isValidNimIdentifier: - when nimvm: - warning "bad identifier: " & id - else: - discard - # warn "bad identifier: " & id - break sanitized - result = some(id.NimIdentifier) - -proc `$`*(name: NimIdentifier): string {.borrow.} -proc len*(name: NimIdentifier): int {.borrow.} - -proc hash*(name: NimIdentifier): Hash = - ## hash an identifier in such a way that two names that are - ## stylistically different but refer to the same identifier will have - ## the same hash - var name = name.string - assert name.isValidNimIdentifier - var s = $name[0] - if len(name) > 1: - s.add toLowerAscii(name[1 .. ^1]) - if s[0] != '_': # the name might be simply `_` - s = s.replace("_") # otherwise, elide underscores - assert s.isValidNimIdentifier - var h: Hash = 0 - h = h !& hash(s) - result = !$h - -proc `==`*(a, b: NimIdentifier): bool = - result = cmpIgnoreStyle(a.string, b.string) == 0 - -proc `<`*(a, b: NimIdentifier): bool = - result = cmpIgnoreStyle(a.string, b.string) == -1 diff --git a/nimph/spec.nim b/nimph/spec.nim index 2cedc3f..564fa04 100644 --- a/nimph/spec.nim +++ b/nimph/spec.nim @@ -11,7 +11,7 @@ import cutelog export cutelog import nimph/paths -import nimph/sanitize +import ups/sanitize type Flag* {.pure.} = enum diff --git a/ups b/ups new file mode 160000 index 0000000..7e149ca --- /dev/null +++ b/ups @@ -0,0 +1 @@ +Subproject commit 7e149ca87fd9d11d8727a83beef05a2c77aa270c