easier comment handling; breaks code
This commit is contained in:
parent
0049a2a388
commit
e6cad814a3
8 changed files with 32 additions and 73 deletions
|
|
@ -593,6 +593,7 @@ proc genStringCase(p: BProc, t: PNode, d: var TLoc) =
|
||||||
else:
|
else:
|
||||||
# else statement: nothing to do yet
|
# else statement: nothing to do yet
|
||||||
# but we reserved a label, which we use later
|
# but we reserved a label, which we use later
|
||||||
|
discard
|
||||||
linefmt(p, cpsStmts, "switch (#hashString($1) & $2) {$n",
|
linefmt(p, cpsStmts, "switch (#hashString($1) & $2) {$n",
|
||||||
rdLoc(a), toRope(bitMask))
|
rdLoc(a), toRope(bitMask))
|
||||||
for j in countup(0, high(branches)):
|
for j in countup(0, high(branches)):
|
||||||
|
|
|
||||||
|
|
@ -654,36 +654,23 @@ proc getOperator(L: var TLexer, tok: var TToken) =
|
||||||
|
|
||||||
proc scanComment(L: var TLexer, tok: var TToken) =
|
proc scanComment(L: var TLexer, tok: var TToken) =
|
||||||
var pos = L.bufpos
|
var pos = L.bufpos
|
||||||
var buf = L.buf
|
var buf = L.buf
|
||||||
# a comment ends if the next line does not start with the # on the same
|
assert buf[pos+1] == '#'
|
||||||
# column after only whitespace
|
|
||||||
tok.tokType = tkComment
|
tok.tokType = tkComment
|
||||||
# iNumber contains the number of '\n' in the token
|
# iNumber contains the number of '\n' in the token
|
||||||
tok.iNumber = 0
|
tok.iNumber = 0
|
||||||
var col = getColNumber(L, pos)
|
|
||||||
while true:
|
while true:
|
||||||
var lastBackslash = -1
|
|
||||||
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}:
|
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}:
|
||||||
if buf[pos] == '\\': lastBackslash = pos+1
|
|
||||||
add(tok.literal, buf[pos])
|
add(tok.literal, buf[pos])
|
||||||
inc(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)
|
pos = handleCRLF(L, pos)
|
||||||
buf = L.buf
|
buf = L.buf
|
||||||
var indent = 0
|
var indent = 0
|
||||||
while buf[pos] == ' ':
|
while buf[pos] == ' ':
|
||||||
inc(pos)
|
inc(pos)
|
||||||
inc(indent)
|
inc(indent)
|
||||||
if buf[pos] == '#' and (col == indent or lastBackslash > 0):
|
if buf[pos] == '#' and buf[pos+1] == '#':
|
||||||
tok.literal.add "\n"
|
tok.literal.add "\n"
|
||||||
col = indent
|
|
||||||
inc tok.iNumber
|
inc tok.iNumber
|
||||||
else:
|
else:
|
||||||
if buf[pos] > ' ':
|
if buf[pos] > ' ':
|
||||||
|
|
@ -700,7 +687,7 @@ proc skip(L: var TLexer, tok: var TToken) =
|
||||||
of ' ':
|
of ' ':
|
||||||
inc(pos)
|
inc(pos)
|
||||||
inc(tok.strongSpaceA)
|
inc(tok.strongSpaceA)
|
||||||
of Tabulator:
|
of '\t':
|
||||||
lexMessagePos(L, errTabulatorsAreNotAllowed, pos)
|
lexMessagePos(L, errTabulatorsAreNotAllowed, pos)
|
||||||
inc(pos)
|
inc(pos)
|
||||||
of CR, LF:
|
of CR, LF:
|
||||||
|
|
@ -711,10 +698,14 @@ proc skip(L: var TLexer, tok: var TToken) =
|
||||||
inc(pos)
|
inc(pos)
|
||||||
inc(indent)
|
inc(indent)
|
||||||
tok.strongSpaceA = 0
|
tok.strongSpaceA = 0
|
||||||
if buf[pos] > ' ':
|
if buf[pos] > ' ' and (buf[pos] != '#' or buf[pos+1] == '#'):
|
||||||
tok.indent = indent
|
tok.indent = indent
|
||||||
L.currLineIndent = indent
|
L.currLineIndent = indent
|
||||||
break
|
break
|
||||||
|
of '#':
|
||||||
|
# do not skip documentation comment:
|
||||||
|
if buf[pos+1] == '#': break
|
||||||
|
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}: inc(pos)
|
||||||
else:
|
else:
|
||||||
break # EndOfFile also leaves the loop
|
break # EndOfFile also leaves the loop
|
||||||
L.bufpos = pos
|
L.bufpos = pos
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ proc commandCompileToC =
|
||||||
# echo "BEFORE CHECK DEP"
|
# echo "BEFORE CHECK DEP"
|
||||||
# discard checkDepMem(gProjectMainIdx)
|
# discard checkDepMem(gProjectMainIdx)
|
||||||
# echo "CHECK DEP COMPLETE"
|
# echo "CHECK DEP COMPLETE"
|
||||||
|
discard
|
||||||
|
|
||||||
compileProject()
|
compileProject()
|
||||||
cgenWriteModules()
|
cgenWriteModules()
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,7 @@ proc checkConvertible(c: PContext, castDest, src: PType): TConvStatus =
|
||||||
elif (skipTypes(castDest, abstractVarRange).kind in IntegralTypes) and
|
elif (skipTypes(castDest, abstractVarRange).kind in IntegralTypes) and
|
||||||
(skipTypes(src, abstractVarRange-{tyTypeDesc}).kind in IntegralTypes):
|
(skipTypes(src, abstractVarRange-{tyTypeDesc}).kind in IntegralTypes):
|
||||||
# accept conversion between integral types
|
# accept conversion between integral types
|
||||||
|
discard
|
||||||
else:
|
else:
|
||||||
# we use d, s here to speed up that operation a bit:
|
# we use d, s here to speed up that operation a bit:
|
||||||
case cmpTypes(c, d, s)
|
case cmpTypes(c, d, s)
|
||||||
|
|
@ -2060,6 +2061,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
|
||||||
of nkClosedSymChoice, nkOpenSymChoice:
|
of nkClosedSymChoice, nkOpenSymChoice:
|
||||||
# handling of sym choices is context dependent
|
# handling of sym choices is context dependent
|
||||||
# the node is left intact for now
|
# the node is left intact for now
|
||||||
|
discard
|
||||||
of nkStaticExpr:
|
of nkStaticExpr:
|
||||||
result = semStaticExpr(c, n)
|
result = semStaticExpr(c, n)
|
||||||
of nkAsgn: result = semAsgn(c, n)
|
of nkAsgn: result = semAsgn(c, n)
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,7 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
# symbol lookup ...
|
# symbol lookup ...
|
||||||
of skUnknown, skParam:
|
of skUnknown, skParam:
|
||||||
# Leave it as an identifier.
|
# Leave it as an identifier.
|
||||||
|
discard
|
||||||
of skProc, skMethod, skIterators, skConverter:
|
of skProc, skMethod, skIterators, skConverter:
|
||||||
result.sons[0] = symChoice(c, n.sons[0], s, scOption)
|
result.sons[0] = symChoice(c, n.sons[0], s, scOption)
|
||||||
first = 1
|
first = 1
|
||||||
|
|
|
||||||
|
|
@ -385,6 +385,7 @@ proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
|
||||||
case kind
|
case kind
|
||||||
of skType:
|
of skType:
|
||||||
# process pragmas later, because result.typ has not been set yet
|
# process pragmas later, because result.typ has not been set yet
|
||||||
|
discard
|
||||||
of skField: pragma(c, result, n.sons[1], fieldPragmas)
|
of skField: pragma(c, result, n.sons[1], fieldPragmas)
|
||||||
of skVar: pragma(c, result, n.sons[1], varPragmas)
|
of skVar: pragma(c, result, n.sons[1], varPragmas)
|
||||||
of skLet: pragma(c, result, n.sons[1], letPragmas)
|
of skLet: pragma(c, result, n.sons[1], letPragmas)
|
||||||
|
|
|
||||||
|
|
@ -154,35 +154,22 @@ Comments
|
||||||
|
|
||||||
`Comments`:idx: start anywhere outside a string or character literal with the
|
`Comments`:idx: start anywhere outside a string or character literal with the
|
||||||
hash character ``#``.
|
hash character ``#``.
|
||||||
|
|
||||||
Comments consist of a concatenation of `comment pieces`:idx:. A comment piece
|
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
|
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
|
belong to the piece. If the next line only consists of a comment piece with
|
||||||
aligned to the preceding one, it does not start a new comment:
|
no other tokens between it and the preceding one, it does not start a new
|
||||||
|
comment:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
|
|
||||||
i = 0 # This is a single comment over multiple lines belonging to the
|
i = 0 # This is a single comment over multiple lines.
|
||||||
# assignment statement. The scanner merges these two pieces.
|
# The scanner merges these two pieces.
|
||||||
# This is a new comment belonging to the current block, but to no particular
|
# The comment continues here.
|
||||||
# statement.
|
|
||||||
i = i + 1 # This a new comment that is NOT
|
|
||||||
echo(i) # continued here, because this comment refers to the echo statement
|
|
||||||
|
|
||||||
|
`Documentation comments`:idx: are comments that start with two ``##``.
|
||||||
The alignment requirement does not hold if the preceding comment piece ends in
|
Documentation comments are tokens; they are only allowed at certain places in
|
||||||
a backslash (followed by optional whitespace):
|
the input file as they belong to the syntax tree!
|
||||||
|
|
||||||
.. 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.
|
|
||||||
|
|
||||||
|
|
||||||
Identifiers & Keywords
|
Identifiers & Keywords
|
||||||
|
|
@ -5688,7 +5675,7 @@ Spawn
|
||||||
|
|
||||||
Nimrod has a builtin thread pool that can be used for CPU intensive tasks. For
|
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
|
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
|
.. code-block:: nimrod
|
||||||
proc processLine(line: string) =
|
proc processLine(line: string) =
|
||||||
|
|
|
||||||
37
doc/tut1.txt
37
doc/tut1.txt
|
|
@ -111,41 +111,16 @@ Comments
|
||||||
--------
|
--------
|
||||||
|
|
||||||
`Comments`:idx: start anywhere outside a string or character literal with the
|
`Comments`:idx: start anywhere outside a string or character literal with the
|
||||||
hash character ``#``. Documentation comments start with ``##``. Multiline
|
hash character ``#``. Documentation comments start with ``##``:
|
||||||
comments need to be aligned at the same column:
|
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
|
# A comment.
|
||||||
|
|
||||||
i = 0 # This is a single comment over multiple lines belonging to the
|
var myVariable: int ## a documentation comment
|
||||||
# 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
|
|
||||||
|
|
||||||
|
Documentation comments are tokens; they are only allowed at certain places in
|
||||||
The alignment requirement does not hold if the preceding comment piece ends in
|
the input file as they belong to the syntax tree! This feature enables simpler
|
||||||
a backslash:
|
documentation generators.
|
||||||
|
|
||||||
.. 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!
|
|
||||||
|
|
||||||
**Note**: To comment out a large piece of code, it is often better to use a
|
**Note**: To comment out a large piece of code, it is often better to use a
|
||||||
``when false:`` statement.
|
``when false:`` statement.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue