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

This commit is contained in:
Araq 2012-06-22 17:47:03 +02:00
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, 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

View file

@ -97,6 +97,7 @@ 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
of nkHiddenStdConv: result = getOrdValue(n.sons[1])
else: else:
LocalError(n.info, errOrdinalTypeExpected) LocalError(n.info, errOrdinalTypeExpected)
result = 0 result = 0

View file

@ -933,6 +933,9 @@ 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
~~~~~~~~ ~~~~~~~~
@ -1734,6 +1737,15 @@ Example:
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
evaluated and if its value is in a *slicelist* the corresponding statements evaluated and if its value is in a *slicelist* the corresponding statements

View file

@ -12,6 +12,16 @@ type
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

View file

@ -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