Skip to content

Commit 7d0269d

Browse files
committed
fix comment after if/while/case (fixes #71)
When a comment appears after keywords with conditionals, it's an open question how to format them: they could stay there but this causes trouble with indent of long/non-trivial conditions - instead of coming up with a clever solution, this PR simply moves the comment out of there.
1 parent cf3369e commit 7d0269d

File tree

5 files changed

+557
-3
lines changed

5 files changed

+557
-3
lines changed

src/phparser.nim

+7-2
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ proc exprEqExpr(p: var Parser): PNode =
482482
proc exprList(p: var Parser, endTok: TokType, result: PNode) =
483483
#| exprList = expr ^+ comma
484484
getTok(p)
485+
splitLookahead(p, result, clMid)
485486
optInd(p, result)
486487
# progress guaranteed
487488
var a = parseExpr(p)
@@ -498,6 +499,7 @@ proc exprList(p: var Parser, endTok: TokType, result: PNode) =
498499
proc optionalExprList(p: var Parser, endTok: TokType, result: PNode) =
499500
#| optionalExprList = expr ^* comma
500501
getTok(p)
502+
splitLookahead(p, result, clMid)
501503
optInd(p, result)
502504
# progress guaranteed
503505
while (p.tok.tokType != endTok) and (p.tok.tokType != tkEof):
@@ -1894,9 +1896,10 @@ proc parseIfOrWhen(p: var Parser, kind: TNodeKind): PNode =
18941896
#| ifStmt = 'if' condStmt
18951897
#| whenStmt = 'when' condStmt
18961898
result = newNodeP(kind, p)
1899+
18971900
while true:
1898-
getTok(p) # skip `if`, `when`, `elif`
18991901
var branch = newNodeP(nkElifBranch, p)
1902+
getTok(p) # skip `if`, `when`, `elif`
19001903
splitLookahead(p, branch, clMid)
19011904
optInd(p, branch)
19021905
branch.add(parseExpr(p))
@@ -1920,8 +1923,8 @@ proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode =
19201923
#| whenExpr = 'when' condExpr
19211924
result = newNodeP(kind, p)
19221925
while true:
1923-
getTok(p) # skip `if`, `when`, `elif`
19241926
var branch = newNodeP(nkElifExpr, p)
1927+
getTok(p) # skip `if`, `when`, `elif`
19251928
splitLookahead(p, branch, clMid)
19261929
optInd(p, branch)
19271930
branch.add(parseExpr(p))
@@ -1983,11 +1986,13 @@ proc parseCase(p: var Parser): PNode =
19831986
inElif = true
19841987
b = newNodeP(nkElifBranch, p)
19851988
getTok(p)
1989+
splitLookahead(p, b, clMid)
19861990
optInd(p, b)
19871991
b.add(parseExpr(p))
19881992
of tkElse:
19891993
b = newNodeP(nkElse, p)
19901994
getTok(p)
1995+
splitLookahead(p, b, clMid)
19911996
else:
19921997
break
19931998
b.add(parseColComStmt(p, b, clMid))

tests/after/comments.nim

+26
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,29 @@ block:
473473
block:
474474
discard
475475
# dedented comment post discard
476+
477+
block:
478+
if 2 >= 1 and 2 >= 1 and 2 >= 1: # some conditions:
479+
discard
480+
elif 2 >= 1 and 2 >= 1 and 2 >= 1: # some elif conds
481+
discard
482+
483+
if 2 >= 1 and 2 >= 1 and 2 >= 1:
484+
# some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc
485+
discard
486+
487+
if 2 >= 1 and
488+
# some conditions
489+
2 >= 1 and 2 >= 1:
490+
discard
491+
492+
while 2 >= 1 and 2 >= 1 and 2 >= 1: # some conditions:
493+
discard
494+
495+
case
496+
# comment
497+
true
498+
of true: # comment
499+
discard
500+
else: #comment
501+
discard

tests/after/comments.nim.nph.yaml

+242
Original file line numberDiff line numberDiff line change
@@ -1796,3 +1796,245 @@ sons:
17961796
- kind: "nkEmpty"
17971797
- kind: "nkCommentStmt"
17981798
"comment": "# dedented comment post discard"
1799+
- kind: "nkBlockStmt"
1800+
sons:
1801+
- kind: "nkEmpty"
1802+
- kind: "nkStmtList"
1803+
sons:
1804+
- kind: "nkIfStmt"
1805+
sons:
1806+
- kind: "nkElifBranch"
1807+
mid:
1808+
- "# some conditions:"
1809+
sons:
1810+
- kind: "nkInfix"
1811+
sons:
1812+
- kind: "nkIdent"
1813+
ident: "and"
1814+
- kind: "nkInfix"
1815+
sons:
1816+
- kind: "nkIdent"
1817+
ident: "and"
1818+
- kind: "nkInfix"
1819+
sons:
1820+
- kind: "nkIdent"
1821+
ident: ">="
1822+
- kind: "nkIntLit"
1823+
intVal: 2
1824+
- kind: "nkIntLit"
1825+
intVal: 1
1826+
- kind: "nkInfix"
1827+
sons:
1828+
- kind: "nkIdent"
1829+
ident: ">="
1830+
- kind: "nkIntLit"
1831+
intVal: 2
1832+
- kind: "nkIntLit"
1833+
intVal: 1
1834+
- kind: "nkInfix"
1835+
sons:
1836+
- kind: "nkIdent"
1837+
ident: ">="
1838+
- kind: "nkIntLit"
1839+
intVal: 2
1840+
- kind: "nkIntLit"
1841+
intVal: 1
1842+
- kind: "nkStmtList"
1843+
sons:
1844+
- kind: "nkDiscardStmt"
1845+
sons:
1846+
- kind: "nkEmpty"
1847+
- kind: "nkElifBranch"
1848+
mid:
1849+
- "# some elif conds"
1850+
sons:
1851+
- kind: "nkInfix"
1852+
sons:
1853+
- kind: "nkIdent"
1854+
ident: "and"
1855+
- kind: "nkInfix"
1856+
sons:
1857+
- kind: "nkIdent"
1858+
ident: "and"
1859+
- kind: "nkInfix"
1860+
sons:
1861+
- kind: "nkIdent"
1862+
ident: ">="
1863+
- kind: "nkIntLit"
1864+
intVal: 2
1865+
- kind: "nkIntLit"
1866+
intVal: 1
1867+
- kind: "nkInfix"
1868+
sons:
1869+
- kind: "nkIdent"
1870+
ident: ">="
1871+
- kind: "nkIntLit"
1872+
intVal: 2
1873+
- kind: "nkIntLit"
1874+
intVal: 1
1875+
- kind: "nkInfix"
1876+
sons:
1877+
- kind: "nkIdent"
1878+
ident: ">="
1879+
- kind: "nkIntLit"
1880+
intVal: 2
1881+
- kind: "nkIntLit"
1882+
intVal: 1
1883+
- kind: "nkStmtList"
1884+
sons:
1885+
- kind: "nkDiscardStmt"
1886+
sons:
1887+
- kind: "nkEmpty"
1888+
- kind: "nkIfStmt"
1889+
sons:
1890+
- kind: "nkElifBranch"
1891+
sons:
1892+
- kind: "nkInfix"
1893+
sons:
1894+
- kind: "nkIdent"
1895+
ident: "and"
1896+
- kind: "nkInfix"
1897+
sons:
1898+
- kind: "nkIdent"
1899+
ident: "and"
1900+
- kind: "nkInfix"
1901+
sons:
1902+
- kind: "nkIdent"
1903+
ident: ">="
1904+
- kind: "nkIntLit"
1905+
intVal: 2
1906+
- kind: "nkIntLit"
1907+
intVal: 1
1908+
- kind: "nkInfix"
1909+
sons:
1910+
- kind: "nkIdent"
1911+
ident: ">="
1912+
- kind: "nkIntLit"
1913+
intVal: 2
1914+
- kind: "nkIntLit"
1915+
intVal: 1
1916+
- kind: "nkInfix"
1917+
sons:
1918+
- kind: "nkIdent"
1919+
ident: ">="
1920+
- kind: "nkIntLit"
1921+
intVal: 2
1922+
- kind: "nkIntLit"
1923+
intVal: 1
1924+
- kind: "nkStmtList"
1925+
sons:
1926+
- kind: "nkCommentStmt"
1927+
"comment": "# some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc"
1928+
- kind: "nkDiscardStmt"
1929+
sons:
1930+
- kind: "nkEmpty"
1931+
- kind: "nkIfStmt"
1932+
sons:
1933+
- kind: "nkElifBranch"
1934+
sons:
1935+
- kind: "nkInfix"
1936+
sons:
1937+
- kind: "nkIdent"
1938+
ident: "and"
1939+
- kind: "nkInfix"
1940+
sons:
1941+
- kind: "nkIdent"
1942+
ident: "and"
1943+
- kind: "nkInfix"
1944+
sons:
1945+
- kind: "nkIdent"
1946+
ident: ">="
1947+
- kind: "nkIntLit"
1948+
intVal: 2
1949+
- kind: "nkIntLit"
1950+
intVal: 1
1951+
- kind: "nkInfix"
1952+
sons:
1953+
- kind: "nkIdent"
1954+
ident: ">="
1955+
- kind: "nkIntLit"
1956+
prefix:
1957+
- "# some conditions"
1958+
intVal: 2
1959+
- kind: "nkIntLit"
1960+
intVal: 1
1961+
- kind: "nkInfix"
1962+
sons:
1963+
- kind: "nkIdent"
1964+
ident: ">="
1965+
- kind: "nkIntLit"
1966+
intVal: 2
1967+
- kind: "nkIntLit"
1968+
intVal: 1
1969+
- kind: "nkStmtList"
1970+
sons:
1971+
- kind: "nkDiscardStmt"
1972+
sons:
1973+
- kind: "nkEmpty"
1974+
- kind: "nkWhileStmt"
1975+
mid:
1976+
- "# some conditions:"
1977+
sons:
1978+
- kind: "nkInfix"
1979+
sons:
1980+
- kind: "nkIdent"
1981+
ident: "and"
1982+
- kind: "nkInfix"
1983+
sons:
1984+
- kind: "nkIdent"
1985+
ident: "and"
1986+
- kind: "nkInfix"
1987+
sons:
1988+
- kind: "nkIdent"
1989+
ident: ">="
1990+
- kind: "nkIntLit"
1991+
intVal: 2
1992+
- kind: "nkIntLit"
1993+
intVal: 1
1994+
- kind: "nkInfix"
1995+
sons:
1996+
- kind: "nkIdent"
1997+
ident: ">="
1998+
- kind: "nkIntLit"
1999+
intVal: 2
2000+
- kind: "nkIntLit"
2001+
intVal: 1
2002+
- kind: "nkInfix"
2003+
sons:
2004+
- kind: "nkIdent"
2005+
ident: ">="
2006+
- kind: "nkIntLit"
2007+
intVal: 2
2008+
- kind: "nkIntLit"
2009+
intVal: 1
2010+
- kind: "nkStmtList"
2011+
sons:
2012+
- kind: "nkDiscardStmt"
2013+
sons:
2014+
- kind: "nkEmpty"
2015+
- kind: "nkCaseStmt"
2016+
sons:
2017+
- kind: "nkIdent"
2018+
prefix:
2019+
- "# comment"
2020+
ident: "true"
2021+
- kind: "nkOfBranch"
2022+
mid:
2023+
- "# comment"
2024+
sons:
2025+
- kind: "nkIdent"
2026+
ident: "true"
2027+
- kind: "nkStmtList"
2028+
sons:
2029+
- kind: "nkDiscardStmt"
2030+
sons:
2031+
- kind: "nkEmpty"
2032+
- kind: "nkElse"
2033+
mid:
2034+
- "#comment"
2035+
sons:
2036+
- kind: "nkStmtList"
2037+
sons:
2038+
- kind: "nkDiscardStmt"
2039+
sons:
2040+
- kind: "nkEmpty"

tests/before/comments.nim

+40-1
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,43 @@ discard
453453
block:
454454
block:
455455
discard
456-
# dedented comment post discard
456+
# dedented comment post discard
457+
458+
block:
459+
if # some conditions:
460+
2 >= 1 and
461+
2 >= 1 and
462+
2 >= 1:
463+
discard
464+
elif # some elif conds
465+
2 >= 1 and
466+
2 >= 1 and
467+
2 >= 1:
468+
discard
469+
470+
if # some conditions with a very long comment that wont fit on a line abacacsdcasdcasdsdcsdcsdc
471+
2 >= 1 and
472+
2 >= 1 and
473+
2 >= 1:
474+
discard
475+
476+
if
477+
2 >= 1 and
478+
# some conditions
479+
2 >= 1 and
480+
2 >= 1:
481+
discard
482+
483+
while # some conditions:
484+
2 >= 1 and
485+
2 >= 1 and
486+
2 >= 1:
487+
discard
488+
489+
case # comment
490+
true
491+
of # comment
492+
true:
493+
discard
494+
else #comment
495+
: discard

0 commit comments

Comments
 (0)