implemented backslash for continuation comments

This commit is contained in:
Araq
2012-08-23 08:45:40 +02:00
parent c4c0c41d61
commit bdf3bee055
6 changed files with 46 additions and 10 deletions

View File

@@ -172,7 +172,7 @@ proc tokToStr*(tok: TToken): string =
of tkParLe..tkColon, tkEof, tkInd, tkSad, tkDed, tkAccent:
result = tokTypeToStr[tok.tokType]
else:
if (tok.ident != nil):
if tok.ident != nil:
result = tok.ident.s
else:
InternalError("tokToStr")
@@ -654,19 +654,29 @@ proc scanComment(L: var TLexer, tok: var TToken) =
# column after only whitespace
tok.tokType = tkComment
var col = getColNumber(L, pos)
while true:
while not (buf[pos] in {CR, LF, lexbase.EndOfFile}):
while true:
var lastBackslash = -1
while buf[pos] notin {CR, LF, lexbase.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, lexbase.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):
tok.literal = tok.literal & "\n"
else:
if buf[pos] == '#' and (col == indent or lastBackslash > 0):
tok.literal.add "\n"
else:
if buf[pos] > ' ':
L.indentAhead = indent
inc(L.dedent)

View File

@@ -135,6 +135,16 @@ aligned to the preceding one, it does not start a new comment:
# statement.
i = i + 1 # This a new comment that is NOT
echo(i) # continued here, because this comment refers to the echo statement
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

View File

@@ -130,6 +130,17 @@ comments need to be aligned at the same column:
i = i + 1 # This a new comment that is NOT
echo(i) # continued here, because this comment refers to the echo statement
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.

View File

@@ -648,8 +648,8 @@ proc parseBackslash(p: var TRstParser, father: PRstNode) =
# XXX: Unicode?
inc(p.idx)
if p.tok[p.idx].kind != tkWhite: add(father, newLeaf(p))
inc(p.idx)
else:
if p.tok[p.idx].kind != tkEof: inc(p.idx)
else:
add(father, newLeaf(p))
inc(p.idx)

View File

@@ -3,7 +3,6 @@ version 0.9.0
- make 'bind' default for templates and introduce 'mixin'
- implement 'bind' for macros
- use ``\`` for comment continuations
- ``final`` should be the default for objects
- implement "closure tuple consists of a single 'ref'" optimization
- implement for loop transformation for first class iterators

View File

@@ -51,7 +51,7 @@ Library Additions
- Added ``system.clamp`` to limit a value within an interval ``[a, b]``.
- Added ``strutils.continuesWith``.
- Added ``system.getStackTrace``.
- Added ``system.||`` for parallel for loop support.
- Added ``system.||`` for parallel ``for`` loop support.
- The GC supports (soft) realtime systems via ``GC_setMaxPause``
and ``GC_step`` procs.
- The sockets module now supports ssl through the OpenSSL library, ``recvLine``
@@ -106,6 +106,8 @@ Changes affecting backwards compatibility
Activate the warning ``ImplicitClosure`` to make the compiler list the
occurances of proc types which are affected.
- The Nimrod type system now distinguishes ``openarray`` from ``varargs``.
- Templates are now ``hygienic``. Use the ``dirty`` pragma to get the old
behaviour.
Compiler Additions
@@ -151,6 +153,10 @@ Language Additions
line: ``inc i; inc j``.
- ``bind`` supports overloaded symbols and operators.
- A ``distinct`` type can now borrow from generic procs.
- Added the pragmas ``gensym``, ``inject`` and ``dirty`` for hygiene
in templates.
- Comments can be continued with a backslash continuation character so that
comment pieces don't have to align on the same column.
2012-02-09 Version 0.8.14 released