Skip to content

Commit

Permalink
[REFACTOR] Remove ptree implementation
Browse files Browse the repository at this point in the history
I somehow managed to trigger another generic sandwich or whatever it is
called, with symboms very similar to
nim-lang/Nim#16128 - can't get string from
`Table[string, string]`, things worked until I had another instantiation of
the table before one in ptree.

For some reason `std/tables` is the only module that triggers that kind of
bugs for me, though considering /how/ it is implemented internally it
should not come out as a surprise. Basicallyt an ideal environment to
trigger things like that - multiple spliced `{.dirty.}` template
instantiations, generics on top of generics, things are partially shared
with multiple other modules (like hashes etc).
  • Loading branch information
haxscramper committed Aug 29, 2021
1 parent 21ccd06 commit 9596eb1
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 214 deletions.
2 changes: 1 addition & 1 deletion src/hmisc/algo/hlex_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1290,4 +1290,4 @@ macro scanSlice*(str; pattern: varargs[untyped]): untyped =

result.add newCall("popSlice", str)

echo result.repr()
# echo result.repr()
26 changes: 1 addition & 25 deletions src/hmisc/hasts/mustache_template.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import
../core/all,
../algo/[hlex_base, hparse_base],
../types/ptree
../algo/[hlex_base, hparse_base]

import std/[options, streams, tables]
export hparse_base
Expand Down Expand Up @@ -33,7 +32,6 @@ type
MLexer = HsLexer[MTok]
MTree = HsTokTree[MustacheAstKind, MTok]
MState = HsLexerState[bool]
MPContext = PTree[string, string]


proc newMustacheLexer*(): MLexer =
Expand Down Expand Up @@ -148,25 +146,3 @@ proc mustacheParse*(str: string): MTree =
return parseStmtList(lexer)


proc writeTemplate*(stream: Stream, tree: MTree, ctx: MPcontext) =
case tree.kind:
of makContent:
stream.write(tree.token.str)

of makStmtList:
for sub in items(tree):
stream.writeTemplate(sub, ctx)

of makSection:
var get = ctx
for key in tree[0]:
get = get.getKey(key.strVal())

for value in items(get):
stream.writeTemplate(tree[1], value)

of makGetExpr:
stream.write ctx.getKey(tree[0].strVal()).getVal()

else:
raise newImplementKindError(tree.kind)
8 changes: 2 additions & 6 deletions src/hmisc/macros/ast_spec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,7 @@ proc instImpl[N, K](
of akDirectSlice:
var subIdx = 0
for item in arange.start .. arange.finish:
let name =
if arange.name.len == 0:
ident(name.strVal() & "_" & $subIdx)
else:
name
let name = ident(name.strVal() & "_" & $subIdx)

subnodes.add nnkIdentDefs.newTree(name, typeName, newEmptyNode())
inc subIdx
Expand Down Expand Up @@ -815,7 +811,7 @@ proc instImpl[N, K](
newEmptyNode(),
impl)

# echo result.repr()
echo result.repr()


template generateConstructors*[N; K: enum](
Expand Down
8 changes: 4 additions & 4 deletions src/hmisc/macros/nim_ast_aux.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const
nimAstSpec* = astSpec(NimNode, NimNodeKind):
nnkInfix:
0 as "op": nnkIdent
1 .. 2: _
?3: nnkStmtList
1 .. 2 as "operands": _
?3 as "body": nnkStmtList


nnkTryStmt:
0 .. ^1:
0 .. ^1 as "exceptions":
nnkExceptBranch:
1 .. ^2:
1 .. ^2 as "capture":
nnkSym
nnkIdent
nnkInfix:
Expand Down
101 changes: 0 additions & 101 deletions src/hmisc/types/ptree.nim

This file was deleted.

79 changes: 2 additions & 77 deletions tests/tMustache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ testFileStarted()

import
hmisc/hasts/mustache_template,
hmisc/types/[ptree, colorstring],
hmisc/types/[colorstring],
hmisc/preludes/unittest

import std/[streams, db_sqlite]
Expand All @@ -26,80 +26,5 @@ suite "Simple template instantiation":

echo treeRepr(tree)

var ctx = newPTree({
"names": newPTree([
newPTree({"name": "test-name"}),
newPTree({"name": "test-name"}),
newPTree({"name": "test-name"})
])
})

let s = newFileStream(stdout)
s.writeTemplate(tree, ctx)

suite "Data providers":
test "SQlite provider":
type EKind = enum ekModule, ekProc, ekType

var db = open(":memory:", "", "", "")

db.exec(sql lit3"""
CREATE TABLE entries (
id INT PRIMARY KEY UNIQUE,
name TEXT,
kind INT,
category STRING
)""")

db.exec(sql("CREATE TABLE nested (id INT, parent INT)"))

for (id, name, kind, category) in [
(1, "io", ekModule, "#top"),
]:
db.exec(sql(lit3"""
INSERT INTO entries
(id, name, kind, category)
VALUES
(?, ?, ?, ?)
"""), id, name, kind.int, category)

for x in db.fastRows(sql lit3"""
SELECT name
FROM sqlite_master
WHERE type ='table' AND name NOT LIKE 'sqlite_%';
"""):
echo x

const base = lit3"""
{{#kind.ekModule}}
<h2>{{name}}</h2>
<table>
<tr><th>Category</th><th>Functions</th></tr>
{{#category}}
<tr><th>{{name}}</th><th>{{kind}}</th></tr>
{{/category}}
</table>
{{/kind.ekModule}}
"""


let tree = mustacheParse(base)
echo treeRepr(tree)

var ptree = newPTree({
"kind": newPTree({
"ekModule": newPTree([
newPTree({
"name": newPTree[string, string]("test"),
"category": newPTree([newPTree({
"name": "Category-value",
"kind": "Category-name"
})])})])})})

let s = newFileStream(stdout)
s.writeTemplate(tree, ptree)

db.close()


testFileEnded()
testFileEnded()

0 comments on commit 9596eb1

Please sign in to comment.