summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-04-13 01:09:15 +0200
committerAraq <rumpf_a@web.de>2013-04-13 01:09:15 +0200
commit18fbaab216e3dc59011b871849ebcf4bfab94afd (patch)
treeb564b6b4ce28e551484b36a2659762af10e25ea7
parent3cb3813eed378d753807a07f434234ce2d4c5159 (diff)
downloadNim-18fbaab216e3dc59011b871849ebcf4bfab94afd.tar.gz
fixes #310
-rw-r--r--compiler/lexer.nim3
-rw-r--r--compiler/parser.nim4
-rw-r--r--lib/packages/docutils/rst.nim38
-rw-r--r--tests/compile/tmatrix2.nim22
4 files changed, 45 insertions, 22 deletions
diff --git a/compiler/lexer.nim b/compiler/lexer.nim
index 02f63aee8..9cf5ccb2b 100644
--- a/compiler/lexer.nim
+++ b/compiler/lexer.nim
@@ -675,6 +675,8 @@ proc scanComment(L: var TLexer, tok: var TToken) =
   # a comment ends if the next line does not start with the # on the same
   # column after only whitespace
   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
@@ -699,6 +701,7 @@ proc scanComment(L: var TLexer, tok: var TToken) =
     if buf[pos] == '#' and (col == indent or lastBackslash > 0):
       tok.literal.add "\n"
       col = indent
+      inc tok.iNumber
     else:
       if buf[pos] > ' ': 
         L.indentAhead = indent
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 81adecac2..769aa7a3e 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -1207,9 +1207,9 @@ proc parseRoutine(p: var TParser, kind: TNodeKind): PNode =
     addSon(result, ast.emptyNode)
   indAndComment(p, result)    # XXX: document this in the grammar!
   
-proc newCommentStmt(p: var TParser): PNode = 
+proc newCommentStmt(p: var TParser): PNode =
   result = newNodeP(nkCommentStmt, p)
-  result.info.line = result.info.line - int16(1)
+  result.info.line = result.info.line - int16(1) - int16(p.tok.iNumber)
 
 type 
   TDefParser = proc (p: var TParser): PNode {.nimcall.}
diff --git a/lib/packages/docutils/rst.nim b/lib/packages/docutils/rst.nim
index 09daed77d..54c17367f 100644
--- a/lib/packages/docutils/rst.nim
+++ b/lib/packages/docutils/rst.nim
@@ -180,11 +180,11 @@ proc getIndentAux(L: var TLexer, start: int): int =
   L.bufpos = pos              # no need to set back buf
   
 proc getIndent(L: var TLexer, tok: var TToken) = 
-  inc(L.line)
-  tok.line = L.line
   tok.col = 0
   tok.kind = tkIndent         # skip the newline (but include it in the token!)
   tok.ival = getIndentAux(L, L.bufpos)
+  inc L.line
+  tok.line = L.line
   L.col = tok.ival
   tok.ival = max(tok.ival - L.baseIndent, 0)
   tok.symbol = "\n" & repeatChar(tok.ival)
@@ -220,21 +220,26 @@ proc rawGetTok(L: var TLexer, tok: var TToken) =
       inc(L.col)
   tok.col = max(tok.col - L.baseIndent, 0)
 
-proc getTokens(buffer: string, skipPounds: bool, tokens: var TTokenSeq) = 
+proc getTokens(buffer: string, skipPounds: bool, tokens: var TTokenSeq): int = 
   var L: TLexer
   var length = len(tokens)
   L.buf = cstring(buffer)
-  L.line = 1                  # skip UTF-8 BOM
+  L.line = 0                  # skip UTF-8 BOM
   if (L.buf[0] == '\xEF') and (L.buf[1] == '\xBB') and (L.buf[2] == '\xBF'): 
     inc(L.bufpos, 3)
   L.skipPounds = skipPounds
   if skipPounds: 
-    if L.buf[L.bufpos] == '#': inc(L.bufpos)
-    if L.buf[L.bufpos] == '#': inc(L.bufpos)
+    if L.buf[L.bufpos] == '#': 
+      inc(L.bufpos)
+      inc(result)
+    if L.buf[L.bufpos] == '#': 
+      inc(L.bufpos)
+      inc(result)
     L.baseIndent = 0
     while L.buf[L.bufpos] == ' ': 
       inc(L.bufpos)
       inc(L.baseIndent)
+      inc(result)
   while true: 
     inc(length)
     setlen(tokens, length)
@@ -314,7 +319,7 @@ proc rstMessage(p: TRstParser, msgKind: TMsgKind, arg: string) =
                              p.col + p.tok[p.idx].col, msgKind, arg)
 
 proc rstMessage(p: TRstParser, msgKind: TMsgKind, arg: string, line, col: int) = 
-  p.s.msgHandler(p.filename, p.line + line, 
+  p.s.msgHandler(p.filename, p.line + line,
                              p.col + col, msgKind, arg)
 
 proc rstMessage(p: TRstParser, msgKind: TMsgKind) = 
@@ -692,6 +697,7 @@ proc parseUntil(p: var TRstParser, father: PRstNode, postfix: string,
   let
     line = p.tok[p.idx].line
     col = p.tok[p.idx].col
+  inc p.idx
   while true: 
     case p.tok[p.idx].kind
     of tkPunct: 
@@ -710,7 +716,7 @@ proc parseUntil(p: var TRstParser, father: PRstNode, postfix: string,
       add(father, newRstNode(rnLeaf, " "))
       inc(p.idx)
       if p.tok[p.idx].kind == tkIndent: 
-        rstMessage(p, meExpected, postfix)
+        rstMessage(p, meExpected, postfix, line, col)
         break 
     of tkWhite: 
       add(father, newRstNode(rnLeaf, " "))
@@ -751,17 +757,14 @@ proc parseInline(p: var TRstParser, father: PRstNode) =
   case p.tok[p.idx].kind
   of tkPunct: 
     if isInlineMarkupStart(p, "***"):
-      inc(p.idx)
       var n = newRstNode(rnTripleEmphasis)
       parseUntil(p, n, "***", true)
       add(father, n)
     elif isInlineMarkupStart(p, "**"): 
-      inc(p.idx)
       var n = newRstNode(rnStrongEmphasis)
       parseUntil(p, n, "**", true)
       add(father, n)
     elif isInlineMarkupStart(p, "*"): 
-      inc(p.idx)
       var n = newRstNode(rnEmphasis)
       parseUntil(p, n, "*", true)
       add(father, n)
@@ -769,18 +772,15 @@ proc parseInline(p: var TRstParser, father: PRstNode) =
       inc(p.idx)
       add(father, parseMarkdownCodeblock(p))
     elif isInlineMarkupStart(p, "``"):
-      inc(p.idx)
       var n = newRstNode(rnInlineLiteral)
       parseUntil(p, n, "``", false)
       add(father, n)
     elif isInlineMarkupStart(p, "`"): 
-      inc(p.idx)
       var n = newRstNode(rnInterpretedText)
       parseUntil(p, n, "`", true)
       n = parsePostfix(p, n)
       add(father, n)
     elif isInlineMarkupStart(p, "|"): 
-      inc(p.idx)
       var n = newRstNode(rnSubstitutionReferences)
       parseUntil(p, n, "|", false)
       add(father, n)
@@ -873,7 +873,6 @@ proc parseSection(p: var TRstParser, result: PRstNode)
 proc parseField(p: var TRstParser): PRstNode = 
   result = newRstNode(rnField)
   var col = p.tok[p.idx].col
-  inc(p.idx)                  # skip :
   var fieldname = newRstNode(rnFieldname)
   parseUntil(p, fieldname, ":", false)
   var fieldbody = newRstNode(rnFieldbody)
@@ -1156,7 +1155,7 @@ proc parseSimpleTable(p: var TRstParser): PRstNode =
       q.col = cols[j]
       q.line = line - 1
       q.filename = p.filename
-      getTokens(row[j], false, q.tok)
+      q.col += getTokens(row[j], false, q.tok)
       b = newRstNode(rnTableDataCell)
       add(b, parseDoc(q))
       add(a, b)
@@ -1459,11 +1458,11 @@ proc dirInclude(p: var TRstParser): PRstNode =
     if getFieldValue(n, "literal") != "": 
       result = newRstNode(rnLiteralBlock)
       add(result, newRstNode(rnLeaf, readFile(path)))
-    else: 
+    else:
       var q: TRstParser
       initParser(q, p.s)
       q.filename = filename
-      getTokens(readFile(path), false, q.tok) 
+      q.col += getTokens(readFile(path), false, q.tok) 
       # workaround a GCC bug; more like the interior pointer bug?
       #if find(q.tok[high(q.tok)].symbol, "\0\x01\x02") > 0:
       #  InternalError("Too many binary zeros in include file")
@@ -1634,7 +1633,6 @@ proc rstParse*(text, filename: string,
   initParser(p, newSharedState(options, findFile, msgHandler))
   p.filename = filename
   p.line = line
-  p.col = column
-  getTokens(text, roSkipPounds in options, p.tok)
+  p.col = column + getTokens(text, roSkipPounds in options, p.tok)
   result = resolveSubs(p, parseDoc(p))
   hasToc = p.hasToc
diff --git a/tests/compile/tmatrix2.nim b/tests/compile/tmatrix2.nim
new file mode 100644
index 000000000..442096e93
--- /dev/null
+++ b/tests/compile/tmatrix2.nim
@@ -0,0 +1,22 @@
+discard """
+  output: "5.0000000000000000e+00"
+"""
+
+type
+  TMatrixNM*[M, N, T] = object 
+    aij*: T
+  TVectorN*[N, T] = TMatrixNM[range[0..0], N, T]
+  TVector3*[T] = TVectorN[range[0..2], T]
+
+proc coeffRef*[M, N, T] (matrix: var TMatrixNM[M, N, T], a: M, b: N): var T =
+  return matrix.aij
+
+proc coeffRef*[N, T] (vector: var TVectorN[N, T], i: N): var T = vector.aij
+
+var 
+  testVar: TVector3[float]
+
+testVar.aij = 2.0
+testVar.coeffRef(1) = 5.0
+
+echo testVar.aij