documented optional indentation for 'case' statements/'case' objects
This commit is contained in:
parent
09499b3822
commit
48847b5616
5 changed files with 35 additions and 4 deletions
|
|
@ -1279,7 +1279,13 @@ proc parseObjectCase(p: var TParser): PNode =
|
||||||
addSon(a, parseTypeDesc(p))
|
addSon(a, parseTypeDesc(p))
|
||||||
addSon(a, ast.emptyNode)
|
addSon(a, ast.emptyNode)
|
||||||
addSon(result, a)
|
addSon(result, a)
|
||||||
|
if p.tok.tokType == tkColon: getTok(p)
|
||||||
skipComment(p, result)
|
skipComment(p, result)
|
||||||
|
var wasIndented = false
|
||||||
|
if p.tok.tokType == tkInd:
|
||||||
|
pushInd(p.lex, p.tok.indent)
|
||||||
|
getTok(p)
|
||||||
|
wasIndented = true
|
||||||
while true:
|
while true:
|
||||||
if p.tok.tokType == tkSad: getTok(p)
|
if p.tok.tokType == tkSad: getTok(p)
|
||||||
var b: PNode
|
var b: PNode
|
||||||
|
|
@ -1300,6 +1306,9 @@ proc parseObjectCase(p: var TParser): PNode =
|
||||||
addSon(b, fields)
|
addSon(b, fields)
|
||||||
addSon(result, b)
|
addSon(result, b)
|
||||||
if b.kind == nkElse: break
|
if b.kind == nkElse: break
|
||||||
|
if wasIndented:
|
||||||
|
eat(p, tkDed)
|
||||||
|
popInd(p.lex)
|
||||||
|
|
||||||
proc parseObjectPart(p: var TParser): PNode =
|
proc parseObjectPart(p: var TParser): PNode =
|
||||||
case p.tok.tokType
|
case p.tok.tokType
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,8 @@ proc getOrdValue(n: PNode): biggestInt =
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkCharLit..nkInt64Lit: result = n.intVal
|
of nkCharLit..nkInt64Lit: result = n.intVal
|
||||||
of nkNilLit: result = 0
|
of nkNilLit: result = 0
|
||||||
else:
|
of nkHiddenStdConv: result = getOrdValue(n.sons[1])
|
||||||
|
else:
|
||||||
LocalError(n.info, errOrdinalTypeExpected)
|
LocalError(n.info, errOrdinalTypeExpected)
|
||||||
result = 0
|
result = 0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -932,7 +932,10 @@ An example:
|
||||||
As can been seen from the example, an advantage to an object hierarchy is that
|
As can been seen from the example, an advantage to an object hierarchy is that
|
||||||
no casting between different object types is needed. Yet, access to invalid
|
no casting between different object types is needed. Yet, access to invalid
|
||||||
object fields raises an exception.
|
object fields raises an exception.
|
||||||
|
|
||||||
|
The syntax of ``case`` in an object declaration follows closely the syntax of
|
||||||
|
the ``case`` statement: The branches in a ``case`` section may be indented too.
|
||||||
|
|
||||||
|
|
||||||
Set type
|
Set type
|
||||||
~~~~~~~~
|
~~~~~~~~
|
||||||
|
|
@ -1732,7 +1735,16 @@ Example:
|
||||||
of "delete-everything", "restart-computer":
|
of "delete-everything", "restart-computer":
|
||||||
echo("permission denied")
|
echo("permission denied")
|
||||||
of "go-for-a-walk": echo("please yourself")
|
of "go-for-a-walk": echo("please yourself")
|
||||||
else: echo("unknown command")
|
else: echo("unknown command")
|
||||||
|
|
||||||
|
# indentation of the branches is also allowed; and so is an optional colon
|
||||||
|
# after the selecting expression:
|
||||||
|
case readline(stdin):
|
||||||
|
of "delete-everything", "restart-computer":
|
||||||
|
echo("permission denied")
|
||||||
|
of "go-for-a-walk": echo("please yourself")
|
||||||
|
else: echo("unknown command")
|
||||||
|
|
||||||
|
|
||||||
The `case`:idx: statement is similar to the if statement, but it represents
|
The `case`:idx: statement is similar to the if statement, but it represents
|
||||||
a multi-branch selection. The expression after the keyword ``case`` is
|
a multi-branch selection. The expression after the keyword ``case`` is
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,16 @@ type
|
||||||
d, e, f: char
|
d, e, f: char
|
||||||
else: nil
|
else: nil
|
||||||
n: bool
|
n: bool
|
||||||
|
|
||||||
|
type
|
||||||
|
TMyObject = object of TObject
|
||||||
|
case disp: range[0..4]:
|
||||||
|
of 0: arg: char
|
||||||
|
of 1: s: string
|
||||||
|
else: wtf: bool
|
||||||
|
|
||||||
|
var
|
||||||
|
x: TMyObject
|
||||||
|
|
||||||
var
|
var
|
||||||
global: int
|
global: int
|
||||||
|
|
|
||||||
1
todo.txt
1
todo.txt
|
|
@ -11,7 +11,6 @@ New pragmas:
|
||||||
- implement ``byCopy`` pragma
|
- implement ``byCopy`` pragma
|
||||||
|
|
||||||
- ``borrow`` needs to take type classes into account
|
- ``borrow`` needs to take type classes into account
|
||||||
- complete and document optional indentation for 'case' statement
|
|
||||||
- make templates hygienic by default: try to gensym() everything in the 'block'
|
- make templates hygienic by default: try to gensym() everything in the 'block'
|
||||||
of a template; find a better solution for gensym instead of `*ident`
|
of a template; find a better solution for gensym instead of `*ident`
|
||||||
- ``bind`` for overloaded symbols does not work apparently
|
- ``bind`` for overloaded symbols does not work apparently
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue