Skip to content

Commit 1927cef

Browse files
committed
fix(sugar): @a.b(1) not compile
1 parent 4d33844 commit 1927cef

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/pylib/pysugar/stmt/class.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ then you will find it compile but `O.f()` gives `0` instead of `3`
173173
template clsType: NimNode =
174174
nnkBracketExpr.newTree(ident"typedesc", curClass())
175175

176+
if decor.name.len != 0:
177+
return false
176178
case $decor.name
177179
of "staticmethod":
178180
purgeBase()
@@ -283,9 +285,14 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
283285
for def in items(body):
284286
var pragmas = defPragmas # will be set as empty if `isConstruct` or not base
285287
case def.kind
286-
of nnkCall: # attr define, e.g. a: int / a: int = 1
287-
let tup = parseDeclWithType(def)
288-
addAttr tup.name, tup.typ, tup.val
288+
of nnkCall:
289+
# - attr define, e.g. a: int / a: int = 1
290+
# - dotted called decorator, e.g. @unittest.skipIf(COND, MSG)
291+
if def.len == 2 and def[0].kind == nnkIdent:
292+
let tup = parseDeclWithType(def)
293+
addAttr tup.name, tup.typ, tup.val
294+
else:
295+
parser.pushDecorator extractDottedCalledDecorator def
289296
of nnkAsgn: # a = 1
290297
addAttr def[0], emptyn, def[1]
291298
of nnkCommand: # TODO: support async

src/pylib/pysugar/stmt/decorator.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ proc consumeDecorator*(mparser; procDef: NimNode): NimNode =
6363
let blk = newBlockStmt blkExpr
6464
result = newLetStmt(procName, blk)
6565

66+
proc extractDottedCalledDecorator*(decorator: NimNode): NimNode =
67+
## Extract dotted call from `decorator`.
68+
## e.g. `@a.b.c(1, 2)` -> `a.b.c(1, 2)`
69+
##
70+
## .. note:: Currently `decorator` is to be modified in place, while result is still returned
71+
expectKind decorator, nnkCall
72+
expectMinLen decorator, 2
73+
result = decorator
74+
var cur = decorator
75+
while cur[0].kind != nnkPrefix:
76+
cur = cur[0]
77+
assert cur[0][0].eqIdent "@"
78+
cur[0] = cur[0][1]

src/pylib/pysugar/stmt/tonim.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ proc parsePyStmt*(mparser; statement: NimNode): NimNode =
120120
# TODO: impl by define such class in global but mangling its name
121121
# It has to be global as class's def is implemented via `method`,
122122
# which is only allowed at global scope
123+
of "@":
124+
# dotted called decorator, e.g. @unittest.skipIf(COND, MSG)
125+
mparser.pushDecorator extractDottedCalledDecorator statement
123126
else:
124127
var cmd = newNimNode nnkCommand
125128
for i in statement:

0 commit comments

Comments
 (0)