easier comment handling; breaks code

This commit is contained in:
Araq 2014-04-25 21:41:56 +02:00
commit e6cad814a3
8 changed files with 32 additions and 73 deletions

View file

@ -593,6 +593,7 @@ proc genStringCase(p: BProc, t: PNode, d: var TLoc) =
else:
# else statement: nothing to do yet
# but we reserved a label, which we use later
discard
linefmt(p, cpsStmts, "switch (#hashString($1) & $2) {$n",
rdLoc(a), toRope(bitMask))
for j in countup(0, high(branches)):

View file

@ -654,36 +654,23 @@ proc getOperator(L: var TLexer, tok: var TToken) =
proc scanComment(L: var TLexer, tok: var TToken) =
var pos = L.bufpos
var buf = L.buf
# a comment ends if the next line does not start with the # on the same
# column after only whitespace
var buf = L.buf
assert buf[pos+1] == '#'
tok.tokType = tkComment
# iNumber contains the number of '\n' in the token
tok.iNumber = 0
var col = getColNumber(L, pos)
while true:
var lastBackslash = -1
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}:
if buf[pos] == '\\': lastBackslash = pos+1
add(tok.literal, buf[pos])
inc(pos)
if lastBackslash > 0:
# a backslash is a continuation character if only followed by spaces
# plus a newline:
while buf[lastBackslash] == ' ': inc(lastBackslash)
if buf[lastBackslash] notin {CR, LF, nimlexbase.EndOfFile}:
# false positive:
lastBackslash = -1
pos = handleCRLF(L, pos)
buf = L.buf
var indent = 0
while buf[pos] == ' ':
inc(pos)
inc(indent)
if buf[pos] == '#' and (col == indent or lastBackslash > 0):
if buf[pos] == '#' and buf[pos+1] == '#':
tok.literal.add "\n"
col = indent
inc tok.iNumber
else:
if buf[pos] > ' ':
@ -700,7 +687,7 @@ proc skip(L: var TLexer, tok: var TToken) =
of ' ':
inc(pos)
inc(tok.strongSpaceA)
of Tabulator:
of '\t':
lexMessagePos(L, errTabulatorsAreNotAllowed, pos)
inc(pos)
of CR, LF:
@ -711,10 +698,14 @@ proc skip(L: var TLexer, tok: var TToken) =
inc(pos)
inc(indent)
tok.strongSpaceA = 0
if buf[pos] > ' ':
if buf[pos] > ' ' and (buf[pos] != '#' or buf[pos+1] == '#'):
tok.indent = indent
L.currLineIndent = indent
break
of '#':
# do not skip documentation comment:
if buf[pos+1] == '#': break
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}: inc(pos)
else:
break # EndOfFile also leaves the loop
L.bufpos = pos

View file

@ -69,6 +69,7 @@ proc commandCompileToC =
# echo "BEFORE CHECK DEP"
# discard checkDepMem(gProjectMainIdx)
# echo "CHECK DEP COMPLETE"
discard
compileProject()
cgenWriteModules()

View file

@ -166,6 +166,7 @@ proc checkConvertible(c: PContext, castDest, src: PType): TConvStatus =
elif (skipTypes(castDest, abstractVarRange).kind in IntegralTypes) and
(skipTypes(src, abstractVarRange-{tyTypeDesc}).kind in IntegralTypes):
# accept conversion between integral types
discard
else:
# we use d, s here to speed up that operation a bit:
case cmpTypes(c, d, s)
@ -2060,6 +2061,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
of nkClosedSymChoice, nkOpenSymChoice:
# handling of sym choices is context dependent
# the node is left intact for now
discard
of nkStaticExpr:
result = semStaticExpr(c, n)
of nkAsgn: result = semAsgn(c, n)

View file

@ -141,6 +141,7 @@ proc semGenericStmt(c: PContext, n: PNode,
# symbol lookup ...
of skUnknown, skParam:
# Leave it as an identifier.
discard
of skProc, skMethod, skIterators, skConverter:
result.sons[0] = symChoice(c, n.sons[0], s, scOption)
first = 1

View file

@ -385,6 +385,7 @@ proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
case kind
of skType:
# process pragmas later, because result.typ has not been set yet
discard
of skField: pragma(c, result, n.sons[1], fieldPragmas)
of skVar: pragma(c, result, n.sons[1], varPragmas)
of skLet: pragma(c, result, n.sons[1], letPragmas)

View file

@ -154,35 +154,22 @@ Comments
`Comments`:idx: start anywhere outside a string or character literal with the
hash character ``#``.
Comments consist of a concatenation of `comment pieces`:idx:. A comment piece
starts with ``#`` and runs until the end of the line. The end of line characters
belong to the piece. If the next line only consists of a comment piece which is
aligned to the preceding one, it does not start a new comment:
belong to the piece. If the next line only consists of a comment piece with
no other tokens between it and the preceding one, it does not start a new
comment:
.. code-block:: nimrod
i = 0 # This is a single comment over multiple lines belonging to the
# assignment statement. The scanner merges these two pieces.
# This is a new comment belonging to the current block, but to no particular
# statement.
i = i + 1 # This a new comment that is NOT
echo(i) # continued here, because this comment refers to the echo statement
i = 0 # This is a single comment over multiple lines.
# The scanner merges these two pieces.
# The comment continues here.
The alignment requirement does not hold if the preceding comment piece ends in
a backslash (followed by optional whitespace):
.. code-block:: nimrod
type
TMyObject {.final, pure, acyclic.} = object # comment continues: \
# we have lots of space here to comment 'TMyObject'.
# This line belongs to the comment as it's properly aligned.
Comments are tokens; they are only allowed at certain places in the input file
as they belong to the syntax tree! This feature enables perfect source-to-source
transformations (such as pretty-printing) and superior documentation generators.
A nice side-effect is that the human reader of the code always knows exactly
which code snippet the comment refers to.
`Documentation comments`:idx: are comments that start with two ``##``.
Documentation comments are tokens; they are only allowed at certain places in
the input file as they belong to the syntax tree!
Identifiers & Keywords
@ -5688,7 +5675,7 @@ Spawn
Nimrod has a builtin thread pool that can be used for CPU intensive tasks. For
IO intensive tasks the upcoming ``async`` and ``await`` features should be
used instead. `spawn`:idx: is used to pass a task to the thread pool:
used. `spawn`:idx: is used to pass a task to the thread pool:
.. code-block:: nimrod
proc processLine(line: string) =

View file

@ -111,41 +111,16 @@ Comments
--------
`Comments`:idx: start anywhere outside a string or character literal with the
hash character ``#``. Documentation comments start with ``##``. Multiline
comments need to be aligned at the same column:
hash character ``#``. Documentation comments start with ``##``:
.. code-block:: nimrod
# A comment.
i = 0 # This is a single comment over multiple lines belonging to the
# assignment statement.
# This is a new comment belonging to the current block, but to no particular
# statement.
i = i + 1 # This a new comment that is NOT
echo(i) # continued here, because this comment refers to the echo statement
var myVariable: int ## a documentation comment
The alignment requirement does not hold if the preceding comment piece ends in
a backslash:
.. code-block:: nimrod
type
TMyObject {.final, pure, acyclic.} = object # comment continues: \
# we have lots of space here to comment 'TMyObject'.
# This line belongs to the comment as it's properly aligned.
Comments are tokens; they are only allowed at certain places in the input file
as they belong to the syntax tree! This feature enables perfect source-to-source
transformations (such as pretty-printing) and simpler documentation generators.
A nice side-effect is that the human reader of the code always knows exactly
which code snippet the comment refers to. Since comments are a proper part of
the syntax, watch their indentation:
.. code-block::
echo("Hello!")
# comment has the same indentation as above statement -> fine
echo("Hi!")
# comment has not the correct indentation level -> syntax error!
Documentation comments are tokens; they are only allowed at certain places in
the input file as they belong to the syntax tree! This feature enables simpler
documentation generators.
**Note**: To comment out a large piece of code, it is often better to use a
``when false:`` statement.