documented optional indentation for 'case' statements/'case' objects

This commit is contained in:
Araq
2012-06-22 17:47:03 +02:00
parent 09499b3822
commit 48847b5616
5 changed files with 35 additions and 4 deletions

View File

@@ -1279,7 +1279,13 @@ proc parseObjectCase(p: var TParser): PNode =
addSon(a, parseTypeDesc(p))
addSon(a, ast.emptyNode)
addSon(result, a)
if p.tok.tokType == tkColon: getTok(p)
skipComment(p, result)
var wasIndented = false
if p.tok.tokType == tkInd:
pushInd(p.lex, p.tok.indent)
getTok(p)
wasIndented = true
while true:
if p.tok.tokType == tkSad: getTok(p)
var b: PNode
@@ -1300,6 +1306,9 @@ proc parseObjectCase(p: var TParser): PNode =
addSon(b, fields)
addSon(result, b)
if b.kind == nkElse: break
if wasIndented:
eat(p, tkDed)
popInd(p.lex)
proc parseObjectPart(p: var TParser): PNode =
case p.tok.tokType

View File

@@ -97,7 +97,8 @@ proc getOrdValue(n: PNode): biggestInt =
case n.kind
of nkCharLit..nkInt64Lit: result = n.intVal
of nkNilLit: result = 0
else:
of nkHiddenStdConv: result = getOrdValue(n.sons[1])
else:
LocalError(n.info, errOrdinalTypeExpected)
result = 0

View File

@@ -932,7 +932,10 @@ An example:
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
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
~~~~~~~~
@@ -1732,7 +1735,16 @@ Example:
of "delete-everything", "restart-computer":
echo("permission denied")
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
a multi-branch selection. The expression after the keyword ``case`` is

View File

@@ -11,6 +11,16 @@ type
d, e, f: char
else: nil
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
global: int

View File

@@ -11,7 +11,6 @@ New pragmas:
- implement ``byCopy`` pragma
- ``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'
of a template; find a better solution for gensym instead of `*ident`
- ``bind`` for overloaded symbols does not work apparently