146146# # .. _Sphinx directives: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html
147147
148148import
149- os, strutils, rstast, algorithm, lists, sequtils
149+ os, strutils, rstast, std/ enumutils, algorithm, lists, sequtils,
150+ std/ private/ miscdollars
150151
151152type
152153 RstParseOption * = enum # # options for the RST parser
@@ -477,17 +478,18 @@ type
477478 EParseError * = object of ValueError
478479
479480const
480- LineRstInit * = 1 # # Initial line number for standalone RST text
481- ColRstInit * = 0 # # Initial column number for standalone RST text
482- # # (Nim global reporting adds ColOffset=1)
481+ LineRstInit * = 1 # # Initial line number for standalone RST text
482+ ColRstInit * = 0 # # Initial column number for standalone RST text
483+ # # (Nim global reporting adds ColOffset=1)
484+ ColRstOffset * = 1 # # 1: a replica of ColOffset for internal use
483485
484486template currentTok (p: RstParser ): Token = p.tok[p.idx]
485487template prevTok (p: RstParser ): Token = p.tok[p.idx - 1 ]
486488template nextTok (p: RstParser ): Token = p.tok[p.idx + 1 ]
487489
488490proc whichMsgClass * (k: MsgKind ): MsgClass =
489491 # # returns which message class `k` belongs to.
490- case ( $ k) [1 ]
492+ case k.symbolName [1 ]
491493 of 'e' , 'E' : result = mcError
492494 of 'w' , 'W' : result = mcWarning
493495 of 'h' , 'H' : result = mcHint
@@ -497,7 +499,9 @@ proc defaultMsgHandler*(filename: string, line, col: int, msgkind: MsgKind,
497499 arg: string ) =
498500 let mc = msgkind.whichMsgClass
499501 let a = $ msgkind % arg
500- let message = " $1($2, $3) $4: $5" % [filename, $ line, $ col, $ mc, a]
502+ var message: string
503+ toLocation (message, filename, line, col + ColRstOffset )
504+ message.add " $1: $2" % [$ mc, a]
501505 if mc == mcError: raise newException (EParseError , message)
502506 else : writeLine (stdout, message)
503507
@@ -1973,25 +1977,32 @@ proc parseEnumList(p: var RstParser): PRstNode =
19731977 if match (p, p.idx, wildcards[w]): break
19741978 inc w
19751979 assert w < wildcards.len
1980+
19761981 proc checkAfterNewline (p: RstParser , report: bool ): bool =
1982+ # # If no indentation on the next line then parse as a normal paragraph
1983+ # # according to the RST spec. And report a warning with suggestions
19771984 let j = tokenAfterNewline (p, start= p.idx+ 1 )
1985+ let requiredIndent = p.tok[p.idx+ wildToken[w]].col
19781986 if p.tok[j].kind notin {tkIndent, tkEof} and
1979- p.tok[j].col < p.tok[p.idx + wildToken[w]].col and
1987+ p.tok[j].col < requiredIndent and
19801988 (p.tok[j].col > col or
19811989 (p.tok[j].col == col and not match (p, j, wildcards[w]))):
19821990 if report:
19831991 let n = p.line + p.tok[j].line
19841992 let msg = " \n " & """
19851993 not enough indentation on line $2
1986- (if it's continuation of enumeration list),
1994+ (should be at column $3 if it's a continuation of enum. list),
19871995 or no blank line after line $1 (if it should be the next paragraph),
19881996 or no escaping \ at the beginning of line $1
19891997 (if lines $1..$2 are a normal paragraph, not enum. list) """ .
19901998 unindent (8 )
1991- rstMessage (p, mwRstStyle, msg % [$ (n- 1 ), $ n])
1999+ let c = p.col + requiredIndent + ColRstOffset
2000+ rstMessage (p, mwRstStyle, msg % [$ (n- 1 ), $ n, $ c],
2001+ p.tok[j].line, p.tok[j].col)
19922002 result = false
19932003 else :
19942004 result = true
2005+
19952006 if not checkAfterNewline (p, report = true ):
19962007 return nil
19972008 result = newRstNodeA (p, rnEnumList)
0 commit comments