Module scoping for embedded type and refs

This commit is contained in:
Emery Hemingway 2021-09-06 16:49:17 +02:00
parent 2d64dae0dd
commit cef2116e96
1 changed files with 19 additions and 7 deletions

View File

@ -65,7 +65,10 @@ proc typeIdent(sn: SchemaNode): PNode =
of snkNamed:
sn.pattern.typeIdent
of snkRef:
ident($sn)
var id = ident sn.refPath[sn.refPath.high]
for i in countDown(sn.refPath.high.pred, 0):
id = nn(nkDotExpr, ident(sn.refPath[i].toLowerAscii), id)
id
else:
stderr.writeLine("no typeIdent for " & $sn.kind & " " & $sn)
ident"Preserve"
@ -109,7 +112,7 @@ proc nimTypeOf(known: var TypeTable; sn: SchemaNode; name = ""): PNode =
else:
let
enumName = name.nimIdentNormalize & "Kind"
enumIdent = nn(nkPostFix, ident"*", ident(enumName))
enumIdent = ident(enumName)
if enumName notin known:
known[enumName] = toEnumDef(enumName, sn)
let recCase = nkRecCase.newNode.add(
@ -229,7 +232,7 @@ proc nimTypeOf(known: var TypeTable; sn: SchemaNode; name = ""): PNode =
of snkNamed:
result = nimTypeOf(known, sn.pattern, name)
of snkRef:
result = ident $sn
result = typeIdent(sn)
else:
result = nkCommentStmt.newNode
result.comment.add("Missing type generator for " & $sn.kind & " " & $sn)
@ -376,7 +379,7 @@ proc collectRefImports(imports: PNode; sn: SchemaNode) =
case sn.kind
of snkRef:
if sn.refPath.len > 1:
imports.add ident(sn.refPath[0])
imports.add ident(sn.refPath[0].toLowerAscii)
else:
for child in sn.items:
collectRefImports(imports, child)
@ -384,10 +387,19 @@ proc collectRefImports(imports: PNode; sn: SchemaNode) =
proc collectRefImports(imports: PNode; scm: Schema) =
if scm.embeddedType.contains {'.'}:
let m = split(scm.embeddedType, '.', 1 )[0]
imports.add ident(m)
imports.add ident(m.toLowerAscii)
for _, def in scm.definitions:
collectRefImports(imports, def)
proc moduleScopedIdent(s: string): PNode =
var id: string
let items = split(s, {'.'})
for i in 0..<items.high:
id.add items[i].toLowerAscii
id.add '.'
id.add items[items.high]
ident(id)
proc generateNimFile*(scm: Schema; path: string) =
var
knownTypes: TypeTable
@ -403,13 +415,13 @@ proc generateNimFile*(scm: Schema; path: string) =
typeSection.add nn(nkTypeDef,
ident"EmbeddedType",
newEmpty(),
ident(scm.embeddedType))
scm.embeddedType.moduleScopedIdent)
typeSection.add nn(nkTypeDef,
ident"Preserve",
newEmpty(),
nn(nkBracketExpr,
ident"PreserveGen",
ident(scm.embeddedType)))
ident"EmbeddedType"))
for name, def in scm.definitions.pairs:
if def.isConst:
constSection.add toConst(name, def)