Skip to content

Commit

Permalink
Spaces around .. and ..< (#31)
Browse files Browse the repository at this point in the history
Although NEP1 suggests not having spaces around these infix operators,
this creates an exception to the normal infix spacing rules.

In spite of this recommendation, lots of code out there maintains spaces
around the operators which makes decision based on "existing practice"
hard.

Adding to the complexity is in order to not break the AST, the code must
take care to remove the spaces only in cases where the infix is not
followed by another operator - this means that we _sometimes_ will put
spaces around these infixes and sometimes not, again leading to
irregularity.

Since there's no consensus in existing code, the rule is irregular and
causes implementation complexity, this PR removes the exception and
formats `..` and `..<` with spaces.
  • Loading branch information
arnetheduck authored Jan 26, 2024
1 parent 3dc181b commit 6065e70
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 181 deletions.
19 changes: 19 additions & 0 deletions docs/src/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,22 @@ proc f(
nextParameter = 1,
)
```

### Infix operators

`nph` puts spaces around infix operators such as `and` and `..`.

Although NEP1 suggests not having spaces around `..` and `..<` in particular,
this creates an exception to the normal infix spacing rules.

In spite of this recommendation, lots of code out there maintains spaces around
the operators which makes decision based on "existing practice" hard.

Adding to the complexity is in order to not break the AST, one would have to
take care to remove the spaces only in cases where the infix is not followed by
another operator (such as `-`) - this means that we _sometimes_ have to put
spaces around these infixes and sometimes not, leading to irregularity.

Since there's no consensus in existing code at the time of writing, the rule is
irregular and causes implementation complexity, `nph` formats `..` and `..<`
with spaces.
6 changes: 3 additions & 3 deletions src/astcmp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ proc equivalent*(a, b: PNode): Outcome =
let
eq =
case a.kind
of nkCharLit..nkUInt64Lit:
of nkCharLit .. nkUInt64Lit:
a.intVal == b.intVal
of nkFloatLit..nkFloat128Lit:
of nkFloatLit .. nkFloat128Lit:
(isNaN(a.floatVal) and isNaN(b.floatVal)) or a.floatVal == b.floatVal
of nkStrLit..nkTripleStrLit:
of nkStrLit .. nkTripleStrLit:
a.strVal == b.strVal
of nkSym:
raiseAssert "Shouldn't eixst in parser"
Expand Down
10 changes: 5 additions & 5 deletions src/astyaml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc addYamlString*(res: var string, s: string) =
res.add "\""
for c in s:
case c
of '\0'..'\x1F', '\x7F'..'\xFF':
of '\0' .. '\x1F', '\x7F' .. '\xFF':
res.add("\\u" & strutils.toHex(ord(c), 4))
of '\"', '\\':
res.add '\\' & c
Expand Down Expand Up @@ -156,11 +156,11 @@ proc treeToYamlAux(
if conf != nil:
res.addf("\n$1info: $2", [istr, lineInfoToStr(conf, n.info)])
case n.kind
of nkCharLit..nkUInt64Lit:
of nkCharLit .. nkUInt64Lit:
res.addf("\n$1intVal: $2", [istr, $(n.intVal)])
of nkFloatLit..nkFloat128Lit:
of nkFloatLit .. nkFloat128Lit:
res.addf("\n$1floatVal: $2", [istr, n.floatVal.toStrMaxPrecision])
of nkStrLit..nkTripleStrLit:
of nkStrLit .. nkTripleStrLit:
res.addf("\n$1strVal: $2", [istr, makeYamlString(n.strVal)])
of nkSym:
res.addf("\n$1sym: ", [istr])
Expand All @@ -173,7 +173,7 @@ proc treeToYamlAux(
else:
if n.len > 0:
res.addf("\n$1sons:", [istr])
for i in 0..<n.len:
for i in 0 ..< n.len:
res.addf("\n$1 - ", [istr])
res.treeToYamlAux(conf, n[i], marker, indent + 1, maxRecDepth - 1)
if n.typ != nil:
Expand Down
86 changes: 43 additions & 43 deletions src/phast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,11 @@ type
info*: TLineInfo
flags*: TNodeFlags
case kind*: TNodeKind
of nkCharLit..nkUInt64Lit:
of nkCharLit .. nkUInt64Lit:
intVal*: BiggestInt
of nkFloatLit..nkFloat128Lit:
of nkFloatLit .. nkFloat128Lit:
floatVal*: BiggestFloat
of nkStrLit..nkTripleStrLit, nkCommentStmt:
of nkStrLit .. nkTripleStrLit, nkCommentStmt:
strVal*: string
of nkSym:
sym*: PSym
Expand Down Expand Up @@ -1335,14 +1335,14 @@ const
tyOpenArray,
tyString,
tyCstring,
tyInt..tyInt64,
tyFloat..tyFloat128,
tyUInt..tyUInt64
tyInt .. tyInt64,
tyFloat .. tyFloat128,
tyUInt .. tyUInt64
}
# types of the expr that may occur in::
# var x = expr
IntegralTypes* = {
tyBool, tyChar, tyEnum, tyInt..tyInt64, tyFloat..tyFloat128, tyUInt..tyUInt64
tyBool, tyChar, tyEnum, tyInt .. tyInt64, tyFloat .. tyFloat128, tyUInt .. tyUInt64
} # weird name because it contains tyFloat
ConstantDataTypes*: TTypeKinds = {tyArray, tySet, tyTuple, tySequence}
NilableTypes*: TTypeKinds = {tyPointer, tyCstring, tyRef, tyPtr, tyProc, tyError}
Expand All @@ -1368,15 +1368,15 @@ const
}
nkIdentKinds* = {nkIdent, nkSym, nkAccQuoted, nkOpenSymChoice, nkClosedSymChoice}
nkPragmaCallKinds* = {nkExprColonExpr, nkCall, nkCallStrLit}
nkLiterals* = {nkCharLit..nkTripleStrLit}
nkFloatLiterals* = {nkFloatLit..nkFloat128Lit}
nkLiterals* = {nkCharLit .. nkTripleStrLit}
nkFloatLiterals* = {nkFloatLit .. nkFloat128Lit}
nkLambdaKinds* = {nkLambda, nkDo}
declarativeDefs* = {nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef}
routineDefs* = declarativeDefs + {nkMacroDef, nkTemplateDef}
procDefs* = nkLambdaKinds + declarativeDefs
callableDefs* = nkLambdaKinds + routineDefs
nkSymChoices* = {nkClosedSymChoice, nkOpenSymChoice}
nkStrKinds* = {nkStrLit..nkTripleStrLit}
nkStrKinds* = {nkStrLit .. nkTripleStrLit}
skLocalVars* = {skVar, skLet, skForVar, skParam, skResult}
skProcKinds* = {
skProc, skFunc, skTemplate, skMacro, skIterator, skMethod, skConverter
Expand Down Expand Up @@ -1479,16 +1479,16 @@ proc len*(n: Indexable): int {.inline.} =

proc safeLen*(n: PNode): int {.inline.} =
## works even for leaves.
if n.kind in {nkNone..nkNilLit}:
if n.kind in {nkNone .. nkNilLit}:
result = 0
else:
result = n.len

proc safeArrLen*(n: PNode): int {.inline.} =
## works for array-like objects (strings passed as openArray in VM).
if n.kind in {nkStrLit..nkTripleStrLit}:
if n.kind in {nkStrLit .. nkTripleStrLit}:
result = n.strVal.len
elif n.kind in {nkNone..nkFloat128Lit}:
elif n.kind in {nkNone .. nkFloat128Lit}:
result = 0
else:
result = n.len
Expand Down Expand Up @@ -1570,7 +1570,7 @@ proc skipPragmaExpr*(n: PNode): PNode =
proc setInfoRecursive*(n: PNode, info: TLineInfo) =
## set line info recursively
if n != nil:
for i in 0..<n.safeLen:
for i in 0 ..< n.safeLen:
setInfoRecursive(n[i], info)

n.info = info
Expand Down Expand Up @@ -1743,21 +1743,21 @@ proc copyStrTable*(dest: var TStrTable, src: TStrTable) =
dest.counter = src.counter

setLen(dest.data, src.data.len)
for i in 0..high(src.data):
for i in 0 .. high(src.data):
dest.data[i] = src.data[i]

proc copyIdTable*(dest: var TIdTable, src: TIdTable) =
dest.counter = src.counter

newSeq(dest.data, src.data.len)
for i in 0..high(src.data):
for i in 0 .. high(src.data):
dest.data[i] = src.data[i]

proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) =
dest.counter = src.counter

setLen(dest.data, src.data.len)
for i in 0..high(src.data):
for i in 0 .. high(src.data):
dest.data[i] = src.data[i]

proc discardSons*(father: PNode) =
Expand Down Expand Up @@ -1930,7 +1930,7 @@ proc assignType*(dest, src: PType) =
dest.sym = src.sym

newSons(dest, src.len)
for i in 0..<src.len:
for i in 0 ..< src.len:
dest[i] = src[i]

proc copyType*(t: PType, id: ItemId, owner: PSym): PType =
Expand Down Expand Up @@ -2072,7 +2072,7 @@ proc delSon*(father: PNode, idx: int) =
if father.len == 0:
return

for i in idx..<father.len - 1:
for i in idx ..< father.len - 1:
father[i] = father[i + 1]

father.sons.setLen(father.len - 1)
Expand All @@ -2094,17 +2094,17 @@ template transitionNodeKindCommon(k: TNodeKind) =
when defined(useNodeIds):
n.id = obj.id

proc transitionSonsKind*(n: PNode, kind: range[nkComesFrom..nkTupleConstr]) =
proc transitionSonsKind*(n: PNode, kind: range[nkComesFrom .. nkTupleConstr]) =
transitionNodeKindCommon(kind)

n.sons = obj.sons

proc transitionIntKind*(n: PNode, kind: range[nkCharLit..nkUInt64Lit]) =
proc transitionIntKind*(n: PNode, kind: range[nkCharLit .. nkUInt64Lit]) =
transitionNodeKindCommon(kind)

n.intVal = obj.intVal

proc transitionIntToFloatKind*(n: PNode, kind: range[nkFloatLit..nkFloat128Lit]) =
proc transitionIntToFloatKind*(n: PNode, kind: range[nkFloatLit .. nkFloat128Lit]) =
transitionNodeKindCommon(kind)

n.floatVal = BiggestFloat(obj.intVal)
Expand Down Expand Up @@ -2143,7 +2143,7 @@ template transitionSymKindCommon*(k: TSymKind) =
proc transitionGenericParamToType*(s: PSym) =
transitionSymKindCommon(skType)

proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) =
proc transitionRoutineSymKind*(s: PSym, kind: range[skProc .. skTemplate]) =
transitionSymKindCommon(kind)

s.gcUnsafetyReason = obj.gcUnsafetyReason
Expand All @@ -2157,14 +2157,14 @@ proc transitionToLet*(s: PSym) =
s.alignment = obj.alignment

proc hasSonWith*(n: PNode, kind: TNodeKind): bool =
for i in 0..<n.len:
for i in 0 ..< n.len:
if n[i].kind == kind:
return true

result = false

proc hasNilSon*(n: PNode): bool =
for i in 0..<n.safeLen:
for i in 0 ..< n.safeLen:
if n[i] == nil:
return true
elif hasNilSon(n[i]):
Expand All @@ -2177,29 +2177,29 @@ proc containsNode*(n: PNode, kinds: TNodeKinds): bool =
return

case n.kind
of nkEmpty..nkNilLit:
of nkEmpty .. nkNilLit:
result = n.kind in kinds
else:
for i in 0..<n.len:
for i in 0 ..< n.len:
if n.kind in kinds or containsNode(n[i], kinds):
return true

proc hasSubnodeWith*(n: PNode, kind: TNodeKind): bool =
case n.kind
of nkEmpty..nkNilLit, nkFormalParams:
of nkEmpty .. nkNilLit, nkFormalParams:
result = n.kind == kind
else:
for i in 0..<n.len:
for i in 0 ..< n.len:
if (n[i].kind == kind) or hasSubnodeWith(n[i], kind):
return true

result = false

proc getInt*(a: PNode): Int128 =
case a.kind
of nkCharLit, nkUIntLit..nkUInt64Lit:
of nkCharLit, nkUIntLit .. nkUInt64Lit:
result = toInt128(cast[uint64](a.intVal))
of nkInt8Lit..nkInt64Lit:
of nkInt8Lit .. nkInt64Lit:
result = toInt128(a.intVal)
of nkIntLit:
# XXX: enable this assert
Expand All @@ -2210,7 +2210,7 @@ proc getInt*(a: PNode): Int128 =

proc getInt64*(a: PNode): int64 {.deprecated: "use getInt".} =
case a.kind
of nkCharLit, nkUIntLit..nkUInt64Lit, nkIntLit..nkInt64Lit:
of nkCharLit, nkUIntLit .. nkUInt64Lit, nkIntLit .. nkInt64Lit:
result = a.intVal
else:
raiseRecoverableError("cannot extract number from invalid AST node")
Expand All @@ -2219,7 +2219,7 @@ proc getFloat*(a: PNode): BiggestFloat =
case a.kind
of nkFloatLiterals:
result = a.floatVal
of nkCharLit, nkUIntLit..nkUInt64Lit, nkIntLit..nkInt64Lit:
of nkCharLit, nkUIntLit .. nkUInt64Lit, nkIntLit .. nkInt64Lit:
result = BiggestFloat a.intVal
else:
raiseRecoverableError("cannot extract number from invalid AST node")
Expand All @@ -2229,7 +2229,7 @@ proc getFloat*(a: PNode): BiggestFloat =

proc getStr*(a: PNode): string =
case a.kind
of nkStrLit..nkTripleStrLit:
of nkStrLit .. nkTripleStrLit:
result = a.strVal
of nkNilLit:
# let's hope this fixes more problems than it creates:
Expand All @@ -2242,9 +2242,9 @@ proc getStr*(a: PNode): string =

proc getStrOrChar*(a: PNode): string =
case a.kind
of nkStrLit..nkTripleStrLit:
of nkStrLit .. nkTripleStrLit:
result = a.strVal
of nkCharLit..nkUInt64Lit:
of nkCharLit .. nkUInt64Lit:
result = $chr(int(a.intVal))
else:
raiseRecoverableError("cannot extract string from invalid AST node")
Expand Down Expand Up @@ -2303,11 +2303,11 @@ proc hasPattern*(s: PSym): bool {.inline.} =
result = isRoutine(s) and s.ast[patternPos].kind != nkEmpty

iterator items*(n: PNode): PNode =
for i in 0..<n.safeLen:
for i in 0 ..< n.safeLen:
yield n[i]

iterator pairs*(n: PNode): tuple[i: int, n: PNode] =
for i in 0..<n.safeLen:
for i in 0 ..< n.safeLen:
yield (i, n[i])

proc isAtom*(n: PNode): bool {.inline.} =
Expand All @@ -2327,7 +2327,7 @@ proc makeStmtList*(n: PNode): PNode =

proc skipStmtList*(n: PNode): PNode =
if n.kind in {nkStmtList, nkStmtListExpr}:
for i in 0..<n.len - 1:
for i in 0 ..< n.len - 1:
if n[i].kind notin {nkEmpty, nkCommentStmt}:
return n

Expand Down Expand Up @@ -2420,7 +2420,7 @@ when false:
if n.isNil:
return true

for i in 0..<n.safeLen:
for i in 0 ..< n.safeLen:
if n[i].containsNil:
return true

Expand Down Expand Up @@ -2506,7 +2506,7 @@ proc canRaise*(fn: PNode): bool =

proc toHumanStrImpl[T](kind: T, num: static int): string =
result = $kind
result = result[num..^1]
result = result[num ..^ 1]
result[0] = result[0].toLowerAscii

proc toHumanStr*(kind: TSymKind): string =
Expand All @@ -2533,8 +2533,8 @@ proc isOutParam*(t: PType): bool {.inline.} =

const
nodesToIgnoreSet* = {
nkNone..pred(nkSym),
succ(nkSym)..nkNilLit,
nkNone .. pred(nkSym),
succ(nkSym) .. nkNilLit,
nkTypeSection,
nkProcDef,
nkConverterDef,
Expand Down
Loading

0 comments on commit 6065e70

Please sign in to comment.