Skip to content

Commit

Permalink
fix do in call in dotexpr, format long lines and files
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck committed Feb 4, 2024
1 parent 13a4822 commit bb68318
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/phlexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1425,8 +1425,8 @@ proc rawGetTok*(L: var Lexer, tok: var Token) =
template atTokenEnd() {.dirty.} =
L.previousTokenEnd.line = L.tokenEnd.line
L.previousTokenEnd.col = L.tokenEnd.col
L.tokenEnd.line = tok.line.uint16
L.tokenEnd.col = getColNumber(L, L.bufpos).int16
L.tokenEnd.line = tok.line
L.tokenEnd.col = getColNumber(L, L.bufpos)

let lineB = tok.lineB
reset(tok)
Expand Down
10 changes: 2 additions & 8 deletions src/phlineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,8 @@ type

FileIndex* = distinct int32
TLineInfo* = object
# This is designed to be as small as possible,
# because it is used
# in syntax nodes. We save space here by using
# two int16 and an int32.
# On 64 bit and on 32 bit systems this is
# only 8 bytes.
line*: uint16
col*: int16
line*: int
col*: int
fileIndex*: FileIndex
offsetA*, offsetB*: int

Expand Down
11 changes: 2 additions & 9 deletions src/phmsgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,8 @@ proc fileInfoIdx*(conf: ConfigRef, filename: RelativeFile): FileIndex =

proc newLineInfo*(fileInfoIdx: FileIndex, line, col: int): TLineInfo =
result.fileIndex = fileInfoIdx
if line < int high(uint16):
result.line = uint16(line)
else:
result.line = high(uint16)

if col < int high(int16):
result.col = int16(col)
else:
result.col = -1
result.line = line
result.col = col

proc newLineInfo*(
conf: ConfigRef, filename: AbsoluteFile, line, col: int
Expand Down
4 changes: 2 additions & 2 deletions src/phparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type
inSemiStmtList*: int
emptyNode: PNode
skipped*: seq[Token]
endLine: uint16
endLine: int

SymbolMode = enum
smNormal
Expand Down Expand Up @@ -219,7 +219,7 @@ template setEndInfo(node: PNode) =
# for statement lists, which is the only place where we retain whitespace
node.endInfo = TLineInfo(
fileIndex: p.lex.fileIdx,
line: uint16 p.lineNumberPrevious,
line: p.lineNumberPrevious,
col: p.lex.previousTokenEnd.col,
)

Expand Down
33 changes: 26 additions & 7 deletions src/phrenderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const
type
TRenderTok* = object
kind*: TokType
length*: int16
length*: int
sym*: PSym

TRenderTokSeq* = seq[TRenderTok]
Expand Down Expand Up @@ -101,6 +101,7 @@ type
sfOneLine ## Use single-line formatting (if possible)
sfStackDot ## Stack multiple dot-calls
sfStackDotInCall ## Stacked dotting opportunity
sfParDo ## Add parens to `do` to avoid dot-expr ambiguity

SubFlags = set[SubFlag]
TOutput = TSrcGen | TSrcLen
Expand Down Expand Up @@ -211,11 +212,11 @@ proc containsNL(s: string): bool =

proc addTok(g: var TSrcLen, kind: TokType, s: string) =
g.nl = g.nl or containsNL(s)
g.tokens.add TRenderTok(kind: kind, length: int16(s.len))
g.tokens.add TRenderTok(kind: kind, length: s.len)

proc addTok(g: var TSrcGen, kind: TokType, s: string) =
# debugEcho "addTok ", kind, " " , s.len, " ", s
g.tokens.add TRenderTok(kind: kind, length: int16(s.len))
g.tokens.add TRenderTok(kind: kind, length: s.len)

g.buf.add(s)
g.line += count(s, "\n")
Expand Down Expand Up @@ -1495,6 +1496,18 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
put(g, tkNil, atom(g, n)) # complex expressions
of nkCall, nkConv, nkPattern, nkObjConstr:
if n.len > 1 and n.lastSon.kind in postExprBlocks:
let
doPars =
if n.lastSon.kind == nkDo and sfParDo in flags:
# A dot-expr that ends with a call with a `do` needs an extra set of
# parens to highlight where the `do` ends.
put(g, tkParLe, $tkParLe)
optNL(g)
true
else:
false
ind = condIndent(g, doPars)

accentedName(g, n[0], flags = (flags * {sfStackDot}) + {sfStackDotInCall})

var i = 1
Expand All @@ -1507,6 +1520,10 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
)

postStatements(g, n, i, sfSkipDo in flags)
dedent(g, ind)

if n.lastSon.kind == nkDo and sfParDo in flags:
put(g, tkParRi, $tkParRi)
elif n.len >= 1:
accentedName(g, n[0], flags = (flags * {sfStackDot}) + {sfStackDotInCall})
glist(g, n, tkParLe, start = 1, flags = {lfLongSepAtEnd})
Expand Down Expand Up @@ -1629,10 +1646,12 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
)
stackNL = stackDot and sfStackDotInCall in flags
subFlags =
if stackDot:
{sfStackDot}
else:
{}
{sfParDo} + (
if stackDot:
{sfStackDot}
else:
{}
)

gsub(g, n[0], flags = subFlags)

Expand Down
8 changes: 8 additions & 0 deletions tests/after/postexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ of a:
discard
else:
discard

discard
(
aaa.bbb
.exec do(res: int64):
size = res
)
.ccc()
45 changes: 45 additions & 0 deletions tests/after/postexprs.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,48 @@ sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkDiscardStmt"
sons:
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "aaa"
- kind: "nkIdent"
ident: "bbb"
- kind: "nkIdent"
ident: "exec"
- kind: "nkDo"
sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkEmpty"
- kind: "nkIdentDefs"
sons:
- kind: "nkIdent"
ident: "res"
- kind: "nkIdent"
ident: "int64"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkAsgn"
sons:
- kind: "nkIdent"
ident: "size"
- kind: "nkIdent"
ident: "res"
- kind: "nkIdent"
ident: "ccc"
5 changes: 4 additions & 1 deletion tests/before/postexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ command "llllllllllllllllooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnngggggggggggggg

command param:
of a: discard
else: discard
else: discard

discard (aaa.bbb.exec do(res: int64):
size = res).ccc()
45 changes: 45 additions & 0 deletions tests/before/postexprs.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,48 @@ sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkDiscardStmt"
sons:
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkCall"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "aaa"
- kind: "nkIdent"
ident: "bbb"
- kind: "nkIdent"
ident: "exec"
- kind: "nkDo"
sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkEmpty"
- kind: "nkIdentDefs"
sons:
- kind: "nkIdent"
ident: "res"
- kind: "nkIdent"
ident: "int64"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkAsgn"
sons:
- kind: "nkIdent"
ident: "size"
- kind: "nkIdent"
ident: "res"
- kind: "nkIdent"
ident: "ccc"

0 comments on commit bb68318

Please sign in to comment.