[backport] run nimpretty on parsers

This commit is contained in:
narimiran 2019-09-27 11:01:51 +02:00
commit b17ed2ca9c
11 changed files with 207 additions and 193 deletions

View file

@ -1945,7 +1945,8 @@ proc untilElementEnd(x: var XmlParser, result: XmlNode,
adderr(expected(x, result)) adderr(expected(x, result))
# this seems to do better match error corrections in browsers: # this seems to do better match error corrections in browsers:
while x.kind in {xmlElementEnd, xmlWhitespace}: while x.kind in {xmlElementEnd, xmlWhitespace}:
if x.kind == xmlElementEnd and cmpIgnoreCase(x.elemName, result.tag) == 0: if x.kind == xmlElementEnd and cmpIgnoreCase(x.elemName,
result.tag) == 0:
break break
next(x) next(x)
next(x) next(x)

View file

@ -406,7 +406,7 @@ macro `%*`*(x: untyped): untyped =
## `%` for every element. ## `%` for every element.
result = toJson(x) result = toJson(x)
proc `==`* (a, b: JsonNode): bool = proc `==`*(a, b: JsonNode): bool =
## Check two nodes for equality ## Check two nodes for equality
if a.isNil: if a.isNil:
if b.isNil: return true if b.isNil: return true
@ -502,7 +502,8 @@ proc contains*(node: JsonNode, val: JsonNode): bool =
assert(node.kind == JArray) assert(node.kind == JArray)
find(node.elems, val) >= 0 find(node.elems, val) >= 0
proc existsKey*(node: JsonNode, key: string): bool {.deprecated: "use 'hasKey' instead".} = proc existsKey*(node: JsonNode, key: string): bool {.
deprecated: "use 'hasKey' instead".} =
node.hasKey(key) node.hasKey(key)
proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode = proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
@ -538,7 +539,8 @@ proc getOrDefault*(node: JsonNode, key: string): JsonNode =
if not isNil(node) and node.kind == JObject: if not isNil(node) and node.kind == JObject:
result = node.fields.getOrDefault(key) result = node.fields.getOrDefault(key)
template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key) template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode,
key: string): JsonNode = node.getOrDefault(key)
proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) = proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =
## Traverses the node and tries to set the value at the given location ## Traverses the node and tries to set the value at the given location
@ -691,7 +693,7 @@ proc pretty*(node: JsonNode, indent = 2): string =
## Similar to prettyprint in Python. ## Similar to prettyprint in Python.
runnableExamples: runnableExamples:
let j = %* {"name": "Isaac", "books": ["Robot Dreams"], let j = %* {"name": "Isaac", "books": ["Robot Dreams"],
"details": {"age":35, "pi":3.1415}} "details": {"age": 35, "pi": 3.1415}}
doAssert pretty(j) == """ doAssert pretty(j) == """
{ {
"name": "Isaac", "name": "Isaac",
@ -1331,7 +1333,8 @@ proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
( (
var map = `tableInit`[`tableKeyType`, `tableValueType`](); var map = `tableInit`[`tableKeyType`, `tableValueType`]();
verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`)); verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`));
for `forLoopKey` in keys(`jsonNode`.fields): map[`forLoopKey`] = `constructorNode`; for `forLoopKey` in keys(`jsonNode`.fields): map[
`forLoopKey`] = `constructorNode`;
map map
) )
of "ref": of "ref":
@ -1374,7 +1377,8 @@ proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
( (
var list: `typeSym`; var list: `typeSym`;
verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`)); verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
for `forLoopI` in 0 ..< `jsonNode`.len: list[`forLoopI`] =`constructorNode`; for `forLoopI` in 0 ..< `jsonNode`.len: list[
`forLoopI`] = `constructorNode`;
list list
) )
of "tuple": of "tuple":
@ -1640,11 +1644,11 @@ when isMainModule:
except: except:
doAssert(false, "IndexError thrown for valid index") doAssert(false, "IndexError thrown for valid index")
doAssert(testJson{"b"}.getStr()=="asd", "Couldn't fetch a singly nested key with {}") doAssert(testJson{"b"}.getStr() == "asd", "Couldn't fetch a singly nested key with {}")
doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil") doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")
doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil") doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil") doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
doAssert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found") doAssert(testJson{"a"} == parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found")
doAssert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil") doAssert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil")
# Generator: # Generator:
@ -1669,10 +1673,10 @@ when isMainModule:
const hisAge = 31 const hisAge = 31
var j3 = %* var j3 = %*
[ { "name": "John" [ {"name": "John"
, "age": herAge , "age": herAge
} }
, { "name": "Susan" , {"name": "Susan"
, "age": hisAge , "age": hisAge
} }
] ]
@ -1708,7 +1712,8 @@ when isMainModule:
except IndexError: doAssert(true) except IndexError: doAssert(true)
var parsed2 = parseFile("tests/testdata/jsontest2.json") var parsed2 = parseFile("tests/testdata/jsontest2.json")
doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}") doAssert(parsed2{"repository", "description"}.str ==
"IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")
doAssert escapeJsonUnquoted("\10Foo🎃barÄ") == "\\nFoo🎃barÄ" doAssert escapeJsonUnquoted("\10Foo🎃barÄ") == "\\nFoo🎃barÄ"
doAssert escapeJsonUnquoted("\0\7\20") == "\\u0000\\u0007\\u0014" # for #7887 doAssert escapeJsonUnquoted("\0\7\20") == "\\u0000\\u0007\\u0014" # for #7887

View file

@ -197,7 +197,7 @@ when declared(os.paramCount):
# we cannot provide this for NimRtl creation on Posix, because we can't # we cannot provide this for NimRtl creation on Posix, because we can't
# access the command line arguments then! # access the command line arguments then!
proc initOptParser*(cmdline = "", shortNoVal: set[char]={}, proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
longNoVal: seq[string] = @[]; longNoVal: seq[string] = @[];
allowWhitespaceAfterColon = true): OptParser = allowWhitespaceAfterColon = true): OptParser =
## Initializes the command line parser. ## Initializes the command line parser.
@ -235,7 +235,7 @@ when declared(os.paramCount):
result.key = TaintedString"" result.key = TaintedString""
result.val = TaintedString"" result.val = TaintedString""
proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char]={}, proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char] = {},
longNoVal: seq[string] = @[]; longNoVal: seq[string] = @[];
allowWhitespaceAfterColon = true): OptParser = allowWhitespaceAfterColon = true): OptParser =
## Initializes the command line parser. ## Initializes the command line parser.
@ -345,7 +345,8 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
inc(i) inc(i)
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i) while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
# if we're at the end, use the next command line option: # if we're at the end, use the next command line option:
if i >= p.cmds[p.idx].len and p.idx < p.cmds.len and p.allowWhitespaceAfterColon: if i >= p.cmds[p.idx].len and p.idx < p.cmds.len and
p.allowWhitespaceAfterColon:
inc p.idx inc p.idx
i = 0 i = 0
if p.idx < p.cmds.len: if p.idx < p.cmds.len:
@ -403,7 +404,8 @@ proc remainingArgs*(p: OptParser): seq[TaintedString] {.rtl, extern: "npo$1".} =
result = @[] result = @[]
for i in p.idx..<p.cmds.len: result.add TaintedString(p.cmds[i]) for i in p.idx..<p.cmds.len: result.add TaintedString(p.cmds[i])
iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] = iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
val: TaintedString] =
## Convenience iterator for iterating over the given ## Convenience iterator for iterating over the given
## `OptParser<#OptParser>`_. ## `OptParser<#OptParser>`_.
## ##
@ -443,7 +445,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedSt
when declared(initOptParser): when declared(initOptParser):
iterator getopt*(cmdline: seq[TaintedString] = commandLineParams(), iterator getopt*(cmdline: seq[TaintedString] = commandLineParams(),
shortNoVal: set[char]={}, longNoVal: seq[string] = @[]): shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
tuple[kind: CmdLineKind, key, val: TaintedString] = tuple[kind: CmdLineKind, key, val: TaintedString] =
## Convenience iterator for iterating over command line arguments. ## Convenience iterator for iterating over command line arguments.
## ##
@ -484,7 +486,8 @@ when declared(initOptParser):
## if filename == "": ## if filename == "":
## # no filename has been written, so we show the help ## # no filename has been written, so we show the help
## writeHelp() ## writeHelp()
var p = initOptParser(cmdline, shortNoVal=shortNoVal, longNoVal=longNoVal) var p = initOptParser(cmdline, shortNoVal = shortNoVal,
longNoVal = longNoVal)
while true: while true:
next(p) next(p)
if p.kind == cmdEnd: break if p.kind == cmdEnd: break

View file

@ -162,7 +162,7 @@ proc skip(c: var SqlLexer) =
break break
of '/': of '/':
if c.buf[pos+1] == '*': if c.buf[pos+1] == '*':
inc(pos,2) inc(pos, 2)
while true: while true:
case c.buf[pos] case c.buf[pos]
of '\0': break of '\0': break
@ -264,12 +264,13 @@ proc getSymbol(c: var SqlLexer, tok: var Token) =
while true: while true:
add(tok.literal, c.buf[pos]) add(tok.literal, c.buf[pos])
inc(pos) inc(pos)
if c.buf[pos] notin {'a'..'z','A'..'Z','0'..'9','_','$', '\128'..'\255'}: if c.buf[pos] notin {'a'..'z', 'A'..'Z', '0'..'9', '_', '$',
'\128'..'\255'}:
break break
c.bufpos = pos c.bufpos = pos
tok.kind = tkIdentifier tok.kind = tkIdentifier
proc getQuotedIdentifier(c: var SqlLexer, tok: var Token, quote='\"') = proc getQuotedIdentifier(c: var SqlLexer, tok: var Token, quote = '\"') =
var pos = c.bufpos + 1 var pos = c.bufpos + 1
tok.kind = tkQuotedIdentifier tok.kind = tkQuotedIdentifier
while true: while true:
@ -403,7 +404,7 @@ proc getTok(c: var SqlLexer, tok: var Token) =
of 'x', 'X': of 'x', 'X':
if c.buf[c.bufpos + 1] == '\'': if c.buf[c.bufpos + 1] == '\'':
tok.kind = tkHexStringConstant tok.kind = tkHexStringConstant
getBitHexString(c, tok, {'a'..'f','A'..'F','0'..'9'}) getBitHexString(c, tok, {'a'..'f', 'A'..'F', '0'..'9'})
else: else:
getSymbol(c, tok) getSymbol(c, tok)
of '$': getDollarString(c, tok) of '$': getDollarString(c, tok)
@ -714,7 +715,8 @@ proc identOrLiteral(p: var SqlParser): SqlNode =
getTok(p) # we must consume a token here to prevent endless loops! getTok(p) # we must consume a token here to prevent endless loops!
proc primary(p: var SqlParser): SqlNode = proc primary(p: var SqlParser): SqlNode =
if (p.tok.kind == tkOperator and (p.tok.literal == "+" or p.tok.literal == "-")) or isKeyw(p, "not"): if (p.tok.kind == tkOperator and (p.tok.literal == "+" or p.tok.literal ==
"-")) or isKeyw(p, "not"):
result = newNode(nkPrefix) result = newNode(nkPrefix)
result.add(newNode(nkIdent, p.tok.literal)) result.add(newNode(nkIdent, p.tok.literal))
getTok(p) getTok(p)
@ -1439,7 +1441,7 @@ proc ra(n: SqlNode, s: var SqlWriter) =
s.addKeyw("enum") s.addKeyw("enum")
rs(n, s) rs(n, s)
proc renderSQL*(n: SqlNode, upperCase=false): string = proc renderSQL*(n: SqlNode, upperCase = false): string =
## Converts an SQL abstract syntax tree to its string representation. ## Converts an SQL abstract syntax tree to its string representation.
var s: SqlWriter var s: SqlWriter
s.buffer = "" s.buffer = ""
@ -1493,7 +1495,7 @@ when not defined(js):
finally: finally:
close(p) close(p)
proc parseSQL*(input: string, filename=""): SqlNode = proc parseSQL*(input: string, filename = ""): SqlNode =
## parses the SQL from `input` into an AST and returns the AST. ## parses the SQL from `input` into an AST and returns the AST.
## `filename` is only used for error messages. ## `filename` is only used for error messages.
## Syntax errors raise an `SqlParseError` exception. ## Syntax errors raise an `SqlParseError` exception.

View file

@ -50,7 +50,7 @@
{.deadCodeElim: on.} # dce option deprecated {.deadCodeElim: on.} # dce option deprecated
{.push debugger:off .} # the user does not want to trace a part {.push debugger: off.} # the user does not want to trace a part
# of the standard library! # of the standard library!
include "system/inclrtl" include "system/inclrtl"
@ -64,8 +64,8 @@ const
proc toLower(c: char): char {.inline.} = proc toLower(c: char): char {.inline.} =
result = if c in {'A'..'Z'}: chr(ord(c)-ord('A')+ord('a')) else: c result = if c in {'A'..'Z'}: chr(ord(c)-ord('A')+ord('a')) else: c
proc parseBin*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0): int proc parseBin*[T: SomeInteger](s: string, number: var T, start = 0,
{.noSideEffect.} = maxLen = 0): int {.noSideEffect.} =
## Parses a binary number and stores its value in ``number``. ## Parses a binary number and stores its value in ``number``.
## ##
## Returns the number of the parsed characters or 0 in case of an error. ## Returns the number of the parsed characters or 0 in case of an error.
@ -111,8 +111,8 @@ proc parseBin*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0):
number = output number = output
result = i - start result = i - start
proc parseOct*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0): int proc parseOct*[T: SomeInteger](s: string, number: var T, start = 0,
{.noSideEffect.} = maxLen = 0): int {.noSideEffect.} =
## Parses an octal number and stores its value in ``number``. ## Parses an octal number and stores its value in ``number``.
## ##
## Returns the number of the parsed characters or 0 in case of an error. ## Returns the number of the parsed characters or 0 in case of an error.
@ -158,8 +158,8 @@ proc parseOct*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0):
number = output number = output
result = i - start result = i - start
proc parseHex*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0): int proc parseHex*[T: SomeInteger](s: string, number: var T, start = 0,
{.noSideEffect.} = maxLen = 0): int {.noSideEffect.} =
## Parses a hexadecimal number and stores its value in ``number``. ## Parses a hexadecimal number and stores its value in ``number``.
## ##
## Returns the number of the parsed characters or 0 in case of an error. ## Returns the number of the parsed characters or 0 in case of an error.
@ -468,7 +468,7 @@ proc parseInt*(s: string, number: var int, start = 0): int {.
number = int(res) number = int(res)
proc parseSaturatedNatural*(s: string, b: var int, start = 0): int {. proc parseSaturatedNatural*(s: string, b: var int, start = 0): int {.
raises: [].}= raises: [].} =
## Parses a natural number into ``b``. This cannot raise an overflow ## Parses a natural number into ``b``. This cannot raise an overflow
## error. ``high(int)`` is returned for an overflow. ## error. ``high(int)`` is returned for an overflow.
## The number of processed character is returned. ## The number of processed character is returned.

View file

@ -399,7 +399,7 @@ proc parseComment(my: var XmlParser) =
my.bufpos = pos my.bufpos = pos
my.kind = xmlComment my.kind = xmlComment
proc parseWhitespace(my: var XmlParser, skip=false) = proc parseWhitespace(my: var XmlParser, skip = false) =
var pos = my.bufpos var pos = my.bufpos
while true: while true:
case my.buf[pos] case my.buf[pos]
@ -562,7 +562,7 @@ proc parseTag(my: var XmlParser) =
my.kind = xmlCharData my.kind = xmlCharData
add(my.a, '<') add(my.a, '<')
return return
parseWhitespace(my, skip=true) parseWhitespace(my, skip = true)
if my.buf[my.bufpos] in NameStartChar: if my.buf[my.bufpos] in NameStartChar:
# an attribute follows: # an attribute follows:
my.kind = xmlElementOpen my.kind = xmlElementOpen
@ -588,7 +588,7 @@ proc parseEndTag(my: var XmlParser) =
my.bufpos = lexbase.handleRefillChar(my, my.bufpos+1) my.bufpos = lexbase.handleRefillChar(my, my.bufpos+1)
#inc(my.bufpos, 2) #inc(my.bufpos, 2)
parseName(my, my.a) parseName(my, my.a)
parseWhitespace(my, skip=true) parseWhitespace(my, skip = true)
if my.buf[my.bufpos] == '>': if my.buf[my.bufpos] == '>':
inc(my.bufpos) inc(my.bufpos)
else: else:
@ -606,7 +606,7 @@ proc parseAttribute(my: var XmlParser) =
return return
let startPos = my.bufpos let startPos = my.bufpos
parseWhitespace(my, skip=true) parseWhitespace(my, skip = true)
if my.buf[my.bufpos] != '=': if my.buf[my.bufpos] != '=':
if allowEmptyAttribs notin my.options or if allowEmptyAttribs notin my.options or
(my.buf[my.bufpos] != '>' and my.bufpos == startPos): (my.buf[my.bufpos] != '>' and my.bufpos == startPos):
@ -614,7 +614,7 @@ proc parseAttribute(my: var XmlParser) =
return return
inc(my.bufpos) inc(my.bufpos)
parseWhitespace(my, skip=true) parseWhitespace(my, skip = true)
var pos = my.bufpos var pos = my.bufpos
if my.buf[pos] in {'\'', '"'}: if my.buf[pos] in {'\'', '"'}:
@ -678,7 +678,7 @@ proc parseAttribute(my: var XmlParser) =
add(my.b, my.buf[pos]) add(my.b, my.buf[pos])
inc pos inc pos
my.bufpos = pos my.bufpos = pos
parseWhitespace(my, skip=true) parseWhitespace(my, skip = true)
proc parseCharData(my: var XmlParser) = proc parseCharData(my: var XmlParser) =
var pos = my.bufpos var pos = my.bufpos
@ -711,8 +711,9 @@ proc rawGetTok(my: var XmlParser) =
of '/': of '/':
parseEndTag(my) parseEndTag(my)
of '!': of '!':
if my.buf[pos+2] == '[' and my.buf[pos+3] == 'C' and my.buf[pos+4] == 'D' and if my.buf[pos+2] == '[' and my.buf[pos+3] == 'C' and
my.buf[pos+5] == 'A' and my.buf[pos+6] == 'T' and my.buf[pos+7] == 'A' and my.buf[pos+4] == 'D' and my.buf[pos+5] == 'A' and
my.buf[pos+6] == 'T' and my.buf[pos+7] == 'A' and
my.buf[pos+8] == '[': my.buf[pos+8] == '[':
parseCDATA(my) parseCDATA(my)
elif my.buf[pos+2] == '-' and my.buf[pos+3] == '-': elif my.buf[pos+2] == '-' and my.buf[pos+3] == '-':
@ -742,7 +743,8 @@ proc getTok(my: var XmlParser) =
of xmlComment: of xmlComment:
if my.options.contains(reportComments): break if my.options.contains(reportComments): break
of xmlWhitespace: of xmlWhitespace:
if my.options.contains(reportWhitespace) or lastKind in {xmlCharData, xmlComment, xmlEntity}: if my.options.contains(reportWhitespace) or lastKind in {xmlCharData,
xmlComment, xmlEntity}:
break break
else: break else: break

View file

@ -169,4 +169,5 @@ when isMainModule:
block bug1518: block bug1518:
var err: seq[string] = @[] var err: seq[string] = @[]
assert $parsexml(newStringStream"<tag>One &amp; two</tag>", "temp.xml", err) == "<tag>One &amp; two</tag>" assert $parsexml(newStringStream"<tag>One &amp; two</tag>", "temp.xml",
err) == "<tag>One &amp; two</tag>"