language addition: colon-block for expressions in let/var context
This commit is contained in:
parent
6323b09f32
commit
d587b6a25f
3 changed files with 62 additions and 10 deletions
|
|
@ -696,7 +696,7 @@ proc primarySuffix(p: var TParser, r: PNode, baseIndent: int): PNode =
|
||||||
result = namedParams(p, result, nkCall, tkParRi)
|
result = namedParams(p, result, nkCall, tkParRi)
|
||||||
if result.len > 1 and result.sons[1].kind == nkExprColonExpr:
|
if result.len > 1 and result.sons[1].kind == nkExprColonExpr:
|
||||||
result.kind = nkObjConstr
|
result.kind = nkObjConstr
|
||||||
else:
|
elif p.tok.tokType == tkDo:
|
||||||
parseDoBlocks(p, result)
|
parseDoBlocks(p, result)
|
||||||
of tkDo:
|
of tkDo:
|
||||||
# progress guaranteed
|
# progress guaranteed
|
||||||
|
|
@ -972,10 +972,8 @@ proc optPragmas(p: var TParser): PNode =
|
||||||
else:
|
else:
|
||||||
result = ast.emptyNode
|
result = ast.emptyNode
|
||||||
|
|
||||||
proc parseDoBlock(p: var TParser): PNode =
|
proc parseDoBlock(p: var TParser; info: TLineInfo): PNode =
|
||||||
#| doBlock = 'do' paramListArrow pragmas? colcom stmt
|
#| doBlock = 'do' paramListArrow pragmas? colcom stmt
|
||||||
let info = parLineInfo(p)
|
|
||||||
getTok(p)
|
|
||||||
let params = parseParamList(p, retColon=false)
|
let params = parseParamList(p, retColon=false)
|
||||||
let pragmas = optPragmas(p)
|
let pragmas = optPragmas(p)
|
||||||
colcom(p, result)
|
colcom(p, result)
|
||||||
|
|
@ -985,11 +983,10 @@ proc parseDoBlock(p: var TParser): PNode =
|
||||||
|
|
||||||
proc parseDoBlocks(p: var TParser, call: PNode) =
|
proc parseDoBlocks(p: var TParser, call: PNode) =
|
||||||
#| doBlocks = doBlock ^* IND{=}
|
#| doBlocks = doBlock ^* IND{=}
|
||||||
if p.tok.tokType == tkDo:
|
|
||||||
#withInd(p):
|
|
||||||
# addSon(call, parseDoBlock(p))
|
|
||||||
while sameOrNoInd(p) and p.tok.tokType == tkDo:
|
while sameOrNoInd(p) and p.tok.tokType == tkDo:
|
||||||
addSon(call, parseDoBlock(p))
|
let info = parLineInfo(p)
|
||||||
|
getTok(p)
|
||||||
|
addSon(call, parseDoBlock(p, info))
|
||||||
|
|
||||||
proc parseProcExpr(p: var TParser, isExpr: bool): PNode =
|
proc parseProcExpr(p: var TParser, isExpr: bool): PNode =
|
||||||
#| procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)?
|
#| procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)?
|
||||||
|
|
@ -1889,9 +1886,18 @@ proc parseVarTuple(p: var TParser): PNode =
|
||||||
addSon(result, parseExpr(p))
|
addSon(result, parseExpr(p))
|
||||||
|
|
||||||
proc parseVariable(p: var TParser): PNode =
|
proc parseVariable(p: var TParser): PNode =
|
||||||
#| variable = (varTuple / identColonEquals) indAndComment
|
#| colonBody = colcom stmt doBlocks?
|
||||||
|
#| variable = (varTuple / identColonEquals) colonBody? indAndComment
|
||||||
if p.tok.tokType == tkParLe: result = parseVarTuple(p)
|
if p.tok.tokType == tkParLe: result = parseVarTuple(p)
|
||||||
else: result = parseIdentColonEquals(p, {withPragma})
|
else: result = parseIdentColonEquals(p, {withPragma})
|
||||||
|
if p.tok.tokType == tkColon and p.tok.indent < 0:
|
||||||
|
let last = result.len-1
|
||||||
|
let ex = result.sons[last]
|
||||||
|
if ex.kind != nkEmpty:
|
||||||
|
let call = makeCall(ex)
|
||||||
|
call.add parseDoBlock(p, parLineInfo(p))
|
||||||
|
parseDoBlocks(p, call)
|
||||||
|
result.sons[last] = call
|
||||||
indAndComment(p, result)
|
indAndComment(p, result)
|
||||||
|
|
||||||
proc parseBind(p: var TParser, k: TNodeKind): PNode =
|
proc parseBind(p: var TParser, k: TNodeKind): PNode =
|
||||||
|
|
|
||||||
33
tests/parser/tletcolon.nim
Normal file
33
tests/parser/tletcolon.nim
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
discard """
|
||||||
|
output: '''boo
|
||||||
|
3
|
||||||
|
44 3
|
||||||
|
more body code
|
||||||
|
yes
|
||||||
|
yes'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
template x(body): untyped =
|
||||||
|
body
|
||||||
|
44
|
||||||
|
|
||||||
|
template y(val, body): untyped =
|
||||||
|
body
|
||||||
|
val
|
||||||
|
|
||||||
|
proc mana =
|
||||||
|
let foo = x:
|
||||||
|
echo "boo"
|
||||||
|
var foo2 = y 3:
|
||||||
|
echo "3"
|
||||||
|
echo foo, " ", foo2
|
||||||
|
|
||||||
|
mana()
|
||||||
|
let other = x:
|
||||||
|
echo "more body code"
|
||||||
|
if true:
|
||||||
|
echo "yes"
|
||||||
|
else:
|
||||||
|
echo "no"
|
||||||
|
let outer = y(5):
|
||||||
|
echo "yes"
|
||||||
|
|
@ -96,6 +96,19 @@ remove the need for the ``newException`` template.
|
||||||
- A new pragma ``.used`` can be used for symbols to prevent
|
- A new pragma ``.used`` can be used for symbols to prevent
|
||||||
the "declared but not used" warning. More details can be
|
the "declared but not used" warning. More details can be
|
||||||
found `here <http://nim-lang.org/docs/manual.html#pragmas-used-pragma>`_.
|
found `here <http://nim-lang.org/docs/manual.html#pragmas-used-pragma>`_.
|
||||||
|
- The popular "colon block of statements" syntax is now also supported for
|
||||||
|
``let`` and ``var`` statements:
|
||||||
|
|
||||||
|
.. code-block:: nim
|
||||||
|
template ve(value, effect): untyped =
|
||||||
|
effect
|
||||||
|
val
|
||||||
|
|
||||||
|
let x = ve(4):
|
||||||
|
echo "welcome to Nim!"
|
||||||
|
|
||||||
|
This is particularly useful for DSLs that help in tree construction.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Bugfixes
|
Bugfixes
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue