grammar changes for table constructor: second part
This commit is contained in:
parent
f00072fd55
commit
1985ac6995
3 changed files with 29 additions and 24 deletions
|
|
@ -355,6 +355,24 @@ proc exprColonEqExprList(p: var TParser, kind, elemKind: TNodeKind,
|
||||||
result = newNodeP(kind, p)
|
result = newNodeP(kind, p)
|
||||||
exprColonEqExprListAux(p, elemKind, endTok, sepTok, result)
|
exprColonEqExprListAux(p, elemKind, endTok, sepTok, result)
|
||||||
|
|
||||||
|
proc setOrTableConstr(p: var TParser): PNode =
|
||||||
|
result = newNodeP(nkCurly, p)
|
||||||
|
getTok(p) # skip '{'
|
||||||
|
optInd(p, result)
|
||||||
|
if p.tok.tokType == tkColon:
|
||||||
|
getTok(p) # skip ':'
|
||||||
|
result.kind = nkTableConstr
|
||||||
|
else:
|
||||||
|
while p.tok.tokType notin {tkCurlyRi, tkEof, tkSad, tkInd}:
|
||||||
|
var a = exprColonEqExpr(p, nkExprColonExpr, tkColon)
|
||||||
|
if a.kind == nkExprColonExpr: result.kind = nkTableConstr
|
||||||
|
addSon(result, a)
|
||||||
|
if p.tok.tokType != tkComma: break
|
||||||
|
getTok(p)
|
||||||
|
optInd(p, a)
|
||||||
|
optPar(p)
|
||||||
|
eat(p, tkCurlyRi) # skip '}'
|
||||||
|
|
||||||
proc parseCast(p: var TParser): PNode =
|
proc parseCast(p: var TParser): PNode =
|
||||||
result = newNodeP(nkCast, p)
|
result = newNodeP(nkCast, p)
|
||||||
getTok(p)
|
getTok(p)
|
||||||
|
|
@ -460,7 +478,7 @@ proc identOrLiteral(p: var TParser): PNode =
|
||||||
result = exprColonEqExprList(p, nkPar, nkExprColonExpr, tkParRi, tkColon)
|
result = exprColonEqExprList(p, nkPar, nkExprColonExpr, tkParRi, tkColon)
|
||||||
of tkCurlyLe:
|
of tkCurlyLe:
|
||||||
# {} constructor
|
# {} constructor
|
||||||
result = exprColonEqExprList(p, nkCurly, nkRange, tkCurlyRi, tkDotDot)
|
result = setOrTableConstr(p)
|
||||||
of tkBracketLe:
|
of tkBracketLe:
|
||||||
# [] constructor
|
# [] constructor
|
||||||
result = exprColonEqExprList(p, nkBracket, nkExprColonExpr, tkBracketRi,
|
result = exprColonEqExprList(p, nkBracket, nkExprColonExpr, tkBracketRi,
|
||||||
|
|
|
||||||
|
|
@ -339,6 +339,8 @@ proc lsub(n: PNode): int =
|
||||||
of nkCommand: result = lsub(n.sons[0]) + lcomma(n, 1) + 1
|
of nkCommand: result = lsub(n.sons[0]) + lcomma(n, 1) + 1
|
||||||
of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(n) + 3
|
of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(n) + 3
|
||||||
of nkPar, nkCurly, nkBracket: result = lcomma(n) + 2
|
of nkPar, nkCurly, nkBracket: result = lcomma(n) + 2
|
||||||
|
of nkTableConstr:
|
||||||
|
result = if n.len > 0: lcomma(n) + 2 else: len("{:}")
|
||||||
of nkSymChoice: result = lsons(n) + len("()") + sonsLen(n) - 1
|
of nkSymChoice: result = lsons(n) + len("()") + sonsLen(n) - 1
|
||||||
of nkTupleTy: result = lcomma(n) + len("tuple[]")
|
of nkTupleTy: result = lcomma(n) + len("tuple[]")
|
||||||
of nkDotExpr: result = lsons(n) + 1
|
of nkDotExpr: result = lsons(n) + 1
|
||||||
|
|
@ -750,7 +752,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
|
||||||
put(g, tkCurlyLe, "{")
|
put(g, tkCurlyLe, "{")
|
||||||
gcomma(g, n, c)
|
gcomma(g, n, c)
|
||||||
put(g, tkCurlyRi, "}")
|
put(g, tkCurlyRi, "}")
|
||||||
of nkBracket:
|
of nkTableConstr:
|
||||||
|
put(g, tkCurlyLe, "{")
|
||||||
|
if n.len > 0: gcomma(g, n, c)
|
||||||
|
else: put(g, tkColon, ":")
|
||||||
|
put(g, tkCurlyRi, "}")
|
||||||
|
of nkBracket:
|
||||||
put(g, tkBracketLe, "[")
|
put(g, tkBracketLe, "[")
|
||||||
gcomma(g, n, c)
|
gcomma(g, n, c)
|
||||||
put(g, tkBracketRi, "]")
|
put(g, tkBracketRi, "]")
|
||||||
|
|
@ -1059,7 +1066,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
|
||||||
gcomma(g, n)
|
gcomma(g, n)
|
||||||
put(g, tkBracketRi, "]")
|
put(g, tkBracketRi, "]")
|
||||||
else:
|
else:
|
||||||
#nkNone, nkMetaNode, nkTableConstr, nkExplicitTypeListCall:
|
#nkNone, nkMetaNode, nkExplicitTypeListCall:
|
||||||
InternalError(n.info, "rnimsyn.gsub(" & $n.kind & ')')
|
InternalError(n.info, "rnimsyn.gsub(" & $n.kind & ')')
|
||||||
|
|
||||||
proc renderTree(n: PNode, renderFlags: TRenderFlags = {}): string =
|
proc renderTree(n: PNode, renderFlags: TRenderFlags = {}): string =
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Nimrod's Runtime Library
|
# Nimrod's Runtime Library
|
||||||
# (c) Copyright 2009 Andreas Rumpf
|
# (c) Copyright 2011 Andreas Rumpf
|
||||||
#
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
|
|
@ -13,25 +13,6 @@
|
||||||
|
|
||||||
## .. include:: ../doc/astspec.txt
|
## .. include:: ../doc/astspec.txt
|
||||||
|
|
||||||
#[[[cog
|
|
||||||
#def toEnum(name, elems):
|
|
||||||
# body = ""
|
|
||||||
# counter = 0
|
|
||||||
# for e in elems:
|
|
||||||
# if counter % 4 == 0: p = "\n "
|
|
||||||
# else: p = ""
|
|
||||||
# body = body + p + 'n' + e + ', '
|
|
||||||
# counter = counter + 1
|
|
||||||
#
|
|
||||||
# return (" TNimrod%s* = enum%s\n TNim%ss* = set[TNimrod%s]\n" %
|
|
||||||
# (name, body[:-2], name, name))
|
|
||||||
#
|
|
||||||
#enums = eval(open("data/ast.yml").read())
|
|
||||||
#cog.out("type\n")
|
|
||||||
#for key, val in enums.items():
|
|
||||||
# if key[-4:] == "Flag": continue
|
|
||||||
# cog.out(toEnum(key, val))
|
|
||||||
#]]]
|
|
||||||
type
|
type
|
||||||
TNimrodNodeKind* = enum
|
TNimrodNodeKind* = enum
|
||||||
nnkNone, nnkEmpty, nnkIdent, nnkSym,
|
nnkNone, nnkEmpty, nnkIdent, nnkSym,
|
||||||
|
|
@ -86,7 +67,6 @@ type
|
||||||
nskEnumField, nskForVar, nskModule, nskLabel,
|
nskEnumField, nskForVar, nskModule, nskLabel,
|
||||||
nskStub
|
nskStub
|
||||||
TNimSymKinds* = set[TNimrodSymKind]
|
TNimSymKinds* = set[TNimrodSymKind]
|
||||||
#[[[end]]]
|
|
||||||
|
|
||||||
type
|
type
|
||||||
TNimrodIdent* = object of TObject
|
TNimrodIdent* = object of TObject
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue