From e6cad814a3185cd5669776bc3e89cf022b950a1c Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 25 Apr 2014 21:41:56 +0200 Subject: [PATCH] easier comment handling; breaks code --- compiler/ccgstmts.nim | 1 + compiler/lexer.nim | 27 +++++++++------------------ compiler/main.nim | 1 + compiler/semexprs.nim | 2 ++ compiler/semgnrc.nim | 1 + compiler/semtypes.nim | 1 + doc/manual.txt | 35 +++++++++++------------------------ doc/tut1.txt | 37 ++++++------------------------------- 8 files changed, 32 insertions(+), 73 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 311149cb3..a8cfa57e4 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -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)): diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 2bfd8d1eb..ec6721033 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -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 diff --git a/compiler/main.nim b/compiler/main.nim index f6d11d960..0e8540556 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -69,6 +69,7 @@ proc commandCompileToC = # echo "BEFORE CHECK DEP" # discard checkDepMem(gProjectMainIdx) # echo "CHECK DEP COMPLETE" + discard compileProject() cgenWriteModules() diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 652e1dd4b..9ea93a15e 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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) diff --git a/compiler/semgnrc.nim b/compiler/semgnrc.nim index da4a2a132..63beb15b0 100644 --- a/compiler/semgnrc.nim +++ b/compiler/semgnrc.nim @@ -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 diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 6289ecc85..8fcb6ea99 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -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) diff --git a/doc/manual.txt b/doc/manual.txt index 39e2bad2a..826267ffd 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -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) = diff --git a/doc/tut1.txt b/doc/tut1.txt index 46eda7ae3..96c9f1267 100644 --- a/doc/tut1.txt +++ b/doc/tut1.txt @@ -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.