Rename PNimrodNode to NimNode

This commit is contained in:
def 2015-03-17 17:50:32 +01:00
commit fd4e629905
30 changed files with 508 additions and 518 deletions

View file

@ -8,8 +8,8 @@
#
## This module implements a simple high performance `JSON`:idx:
## parser. JSON (JavaScript Object Notation) is a lightweight
## data-interchange format that is easy for humans to read and write
## parser. JSON (JavaScript Object Notation) is a lightweight
## data-interchange format that is easy for humans to read and write
## (unlike XML). It is easy for machines to parse and generate.
## JSON is based on a subset of the JavaScript Programming Language,
## Standard ECMA-262 3rd Edition - December 1999.
@ -50,10 +50,10 @@
## }
## ]
import
import
hashes, strutils, lexbase, streams, unicode, macros
type
type
JsonEventKind* = enum ## enumeration of all events that may occur when parsing
jsonError, ## an error occurred during parsing
jsonEof, ## end of file reached
@ -67,7 +67,7 @@ type
jsonObjectEnd, ## end of an object: the ``}`` token
jsonArrayStart, ## start of an array: the ``[`` token
jsonArrayEnd ## start of an array: the ``]`` token
TTokKind = enum # must be synchronized with TJsonEventKind!
tkError,
tkEof,
@ -83,7 +83,7 @@ type
tkBracketRi,
tkColon,
tkComma
JsonError* = enum ## enumeration that lists all errors that can occur
errNone, ## no error
errInvalidToken, ## invalid token
@ -96,8 +96,8 @@ type
errEOC_Expected, ## ``*/`` expected
errEofExpected, ## EOF expected
errExprExpected ## expr expected
ParserState = enum
ParserState = enum
stateEof, stateStart, stateObject, stateArray, stateExpectArrayComma,
stateExpectObjectComma, stateExpectColon, stateExpectValue
@ -111,7 +111,7 @@ type
{.deprecated: [TJsonEventKind: JsonEventKind, TJsonError: JsonError,
TJsonParser: JsonParser].}
const
errorMessages: array [JsonError, string] = [
"no error",
@ -146,56 +146,56 @@ proc open*(my: var JsonParser, input: Stream, filename: string) =
my.state = @[stateStart]
my.kind = jsonError
my.a = ""
proc close*(my: var JsonParser) {.inline.} =
proc close*(my: var JsonParser) {.inline.} =
## closes the parser `my` and its associated input stream.
lexbase.close(my)
proc str*(my: JsonParser): string {.inline.} =
## returns the character data for the events: ``jsonInt``, ``jsonFloat``,
proc str*(my: JsonParser): string {.inline.} =
## returns the character data for the events: ``jsonInt``, ``jsonFloat``,
## ``jsonString``
assert(my.kind in {jsonInt, jsonFloat, jsonString})
return my.a
proc getInt*(my: JsonParser): BiggestInt {.inline.} =
proc getInt*(my: JsonParser): BiggestInt {.inline.} =
## returns the number for the event: ``jsonInt``
assert(my.kind == jsonInt)
return parseBiggestInt(my.a)
proc getFloat*(my: JsonParser): float {.inline.} =
proc getFloat*(my: JsonParser): float {.inline.} =
## returns the number for the event: ``jsonFloat``
assert(my.kind == jsonFloat)
return parseFloat(my.a)
proc kind*(my: JsonParser): JsonEventKind {.inline.} =
proc kind*(my: JsonParser): JsonEventKind {.inline.} =
## returns the current event type for the JSON parser
return my.kind
proc getColumn*(my: JsonParser): int {.inline.} =
proc getColumn*(my: JsonParser): int {.inline.} =
## get the current column the parser has arrived at.
result = getColNumber(my, my.bufpos)
proc getLine*(my: JsonParser): int {.inline.} =
proc getLine*(my: JsonParser): int {.inline.} =
## get the current line the parser has arrived at.
result = my.lineNumber
proc getFilename*(my: JsonParser): string {.inline.} =
proc getFilename*(my: JsonParser): string {.inline.} =
## get the filename of the file that the parser processes.
result = my.filename
proc errorMsg*(my: JsonParser): string =
proc errorMsg*(my: JsonParser): string =
## returns a helpful error message for the event ``jsonError``
assert(my.kind == jsonError)
result = "$1($2, $3) Error: $4" % [
my.filename, $getLine(my), $getColumn(my), errorMessages[my.err]]
proc errorMsgExpected*(my: JsonParser, e: string): string =
proc errorMsgExpected*(my: JsonParser, e: string): string =
## returns an error message "`e` expected" in the same format as the
## other error messages
## other error messages
result = "$1($2, $3) Error: $4" % [
my.filename, $getLine(my), $getColumn(my), e & " expected"]
proc handleHexChar(c: char, x: var int): bool =
proc handleHexChar(c: char, x: var int): bool =
result = true # Success
case c
of '0'..'9': x = (x shl 4) or (ord(c) - ord('0'))
@ -208,8 +208,8 @@ proc parseString(my: var JsonParser): TTokKind =
var pos = my.bufpos + 1
var buf = my.buf
while true:
case buf[pos]
of '\0':
case buf[pos]
of '\0':
my.err = errQuoteExpected
result = tkError
break
@ -218,21 +218,21 @@ proc parseString(my: var JsonParser): TTokKind =
break
of '\\':
case buf[pos+1]
of '\\', '"', '\'', '/':
of '\\', '"', '\'', '/':
add(my.a, buf[pos+1])
inc(pos, 2)
of 'b':
add(my.a, '\b')
inc(pos, 2)
inc(pos, 2)
of 'f':
add(my.a, '\f')
inc(pos, 2)
inc(pos, 2)
of 'n':
add(my.a, '\L')
inc(pos, 2)
inc(pos, 2)
of 'r':
add(my.a, '\C')
inc(pos, 2)
inc(pos, 2)
of 't':
add(my.a, '\t')
inc(pos, 2)
@ -244,15 +244,15 @@ proc parseString(my: var JsonParser): TTokKind =
if handleHexChar(buf[pos], r): inc(pos)
if handleHexChar(buf[pos], r): inc(pos)
add(my.a, toUTF8(Rune(r)))
else:
else:
# don't bother with the error
add(my.a, buf[pos])
inc(pos)
of '\c':
of '\c':
pos = lexbase.handleCR(my, pos)
buf = my.buf
add(my.a, '\c')
of '\L':
of '\L':
pos = lexbase.handleLF(my, pos)
buf = my.buf
add(my.a, '\L')
@ -260,25 +260,25 @@ proc parseString(my: var JsonParser): TTokKind =
add(my.a, buf[pos])
inc(pos)
my.bufpos = pos # store back
proc skip(my: var JsonParser) =
proc skip(my: var JsonParser) =
var pos = my.bufpos
var buf = my.buf
while true:
while true:
case buf[pos]
of '/':
if buf[pos+1] == '/':
of '/':
if buf[pos+1] == '/':
# skip line comment:
inc(pos, 2)
while true:
case buf[pos]
of '\0':
case buf[pos]
of '\0':
break
of '\c':
of '\c':
pos = lexbase.handleCR(my, pos)
buf = my.buf
break
of '\L':
of '\L':
pos = lexbase.handleLF(my, pos)
buf = my.buf
break
@ -288,44 +288,44 @@ proc skip(my: var JsonParser) =
# skip long comment:
inc(pos, 2)
while true:
case buf[pos]
of '\0':
case buf[pos]
of '\0':
my.err = errEOC_Expected
break
of '\c':
of '\c':
pos = lexbase.handleCR(my, pos)
buf = my.buf
of '\L':
of '\L':
pos = lexbase.handleLF(my, pos)
buf = my.buf
of '*':
inc(pos)
if buf[pos] == '/':
if buf[pos] == '/':
inc(pos)
break
else:
inc(pos)
else:
else:
break
of ' ', '\t':
of ' ', '\t':
inc(pos)
of '\c':
of '\c':
pos = lexbase.handleCR(my, pos)
buf = my.buf
of '\L':
of '\L':
pos = lexbase.handleLF(my, pos)
buf = my.buf
else:
break
my.bufpos = pos
proc parseNumber(my: var JsonParser) =
proc parseNumber(my: var JsonParser) =
var pos = my.bufpos
var buf = my.buf
if buf[pos] == '-':
if buf[pos] == '-':
add(my.a, '-')
inc(pos)
if buf[pos] == '.':
if buf[pos] == '.':
add(my.a, "0.")
inc(pos)
else:
@ -350,7 +350,7 @@ proc parseNumber(my: var JsonParser) =
inc(pos)
my.bufpos = pos
proc parseName(my: var JsonParser) =
proc parseName(my: var JsonParser) =
var pos = my.bufpos
var buf = my.buf
if buf[pos] in IdentStartChars:
@ -359,11 +359,11 @@ proc parseName(my: var JsonParser) =
inc(pos)
my.bufpos = pos
proc getTok(my: var JsonParser): TTokKind =
proc getTok(my: var JsonParser): TTokKind =
setLen(my.a, 0)
skip(my) # skip whitespace, comments
case my.buf[my.bufpos]
of '-', '.', '0'..'9':
of '-', '.', '0'..'9':
parseNumber(my)
if {'.', 'e', 'E'} in my.a:
result = tkFloat
@ -393,17 +393,17 @@ proc getTok(my: var JsonParser): TTokKind =
result = tkEof
of 'a'..'z', 'A'..'Z', '_':
parseName(my)
case my.a
case my.a
of "null": result = tkNull
of "true": result = tkTrue
of "false": result = tkFalse
else: result = tkError
else:
else:
inc(my.bufpos)
result = tkError
my.tok = result
proc next*(my: var JsonParser) =
proc next*(my: var JsonParser) =
## retrieves the first/next event. This controls the parser.
var tk = getTok(my)
var i = my.state.len-1
@ -416,13 +416,13 @@ proc next*(my: var JsonParser) =
else:
my.kind = jsonError
my.err = errEofExpected
of stateStart:
# tokens allowed?
of stateStart:
# tokens allowed?
case tk
of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
my.state[i] = stateEof # expect EOF next!
my.kind = JsonEventKind(ord(tk))
of tkBracketLe:
of tkBracketLe:
my.state.add(stateArray) # we expect any
my.kind = jsonArrayStart
of tkCurlyLe:
@ -433,12 +433,12 @@ proc next*(my: var JsonParser) =
else:
my.kind = jsonError
my.err = errEofExpected
of stateObject:
of stateObject:
case tk
of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
my.state.add(stateExpectColon)
my.kind = JsonEventKind(ord(tk))
of tkBracketLe:
of tkBracketLe:
my.state.add(stateExpectColon)
my.state.add(stateArray)
my.kind = jsonArrayStart
@ -457,7 +457,7 @@ proc next*(my: var JsonParser) =
of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
my.state.add(stateExpectArrayComma) # expect value next!
my.kind = JsonEventKind(ord(tk))
of tkBracketLe:
of tkBracketLe:
my.state.add(stateExpectArrayComma)
my.state.add(stateArray)
my.kind = jsonArrayStart
@ -472,8 +472,8 @@ proc next*(my: var JsonParser) =
my.kind = jsonError
my.err = errBracketRiExpected
of stateExpectArrayComma:
case tk
of tkComma:
case tk
of tkComma:
discard my.state.pop()
next(my)
of tkBracketRi:
@ -484,8 +484,8 @@ proc next*(my: var JsonParser) =
my.kind = jsonError
my.err = errBracketRiExpected
of stateExpectObjectComma:
case tk
of tkComma:
case tk
of tkComma:
discard my.state.pop()
next(my)
of tkCurlyRi:
@ -495,9 +495,9 @@ proc next*(my: var JsonParser) =
else:
my.kind = jsonError
my.err = errCurlyRiExpected
of stateExpectColon:
case tk
of tkColon:
of stateExpectColon:
case tk
of tkColon:
my.state[i] = stateExpectValue
next(my)
else:
@ -508,7 +508,7 @@ proc next*(my: var JsonParser) =
of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
my.state[i] = stateExpectObjectComma
my.kind = JsonEventKind(ord(tk))
of tkBracketLe:
of tkBracketLe:
my.state[i] = stateExpectObjectComma
my.state.add(stateArray)
my.kind = jsonArrayStart
@ -532,8 +532,8 @@ type
JString,
JObject,
JArray
JsonNode* = ref JsonNodeObj ## JSON node
JsonNode* = ref JsonNodeObj ## JSON node
JsonNodeObj* {.acyclic.} = object
case kind*: JsonNodeKind
of JString:
@ -644,7 +644,7 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
newSeq(result.elems, elements.len)
for i, p in pairs(elements): result.elems[i] = p
proc toJson(x: PNimrodNode): PNimrodNode {.compiletime.} =
proc toJson(x: NimNode): NimNode {.compiletime.} =
case x.kind
of nnkBracket:
result = newNimNode(nnkBracket)
@ -672,7 +672,7 @@ proc `==`* (a,b: JsonNode): bool =
if a.isNil:
if b.isNil: return true
return false
elif b.isNil or a.kind != b.kind:
elif b.isNil or a.kind != b.kind:
return false
else:
return case a.kind
@ -709,7 +709,7 @@ proc hash* (n:JsonNode): THash =
of JNull:
result = hash(0)
proc len*(n: JsonNode): int =
proc len*(n: JsonNode): int =
## If `n` is a `JArray`, it returns the number of elements.
## If `n` is a `JObject`, it returns the number of pairs.
## Else it returns 0.
@ -727,7 +727,7 @@ proc `[]`*(node: JsonNode, name: string): JsonNode =
if key == name:
return item
return nil
proc `[]`*(node: JsonNode, index: int): JsonNode =
## Gets the node at `index` in an Array. Result is undefined if `index`
## is out of bounds
@ -744,12 +744,12 @@ proc hasKey*(node: JsonNode, key: string): bool =
proc existsKey*(node: JsonNode, key: string): bool {.deprecated.} = node.hasKey(key)
## Deprecated for `hasKey`
proc add*(father, child: JsonNode) =
## Adds `child` to a JArray node `father`.
proc add*(father, child: JsonNode) =
## Adds `child` to a JArray node `father`.
assert father.kind == JArray
father.elems.add(child)
proc add*(obj: JsonNode, key: string, val: JsonNode) =
proc add*(obj: JsonNode, key: string, val: JsonNode) =
## Adds ``(key, val)`` pair to the JObject node `obj`. For speed
## reasons no check for duplicate keys is performed!
## But ``[]=`` performs the check.
@ -760,7 +760,7 @@ proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) =
## Sets a field from a `JObject`. Performs a check for duplicate keys.
assert(obj.kind == JObject)
for i in 0..obj.fields.len-1:
if obj.fields[i].key == key:
if obj.fields[i].key == key:
obj.fields[i].val = val
return
obj.fields.add((key, val))
@ -815,17 +815,17 @@ proc copy*(p: JsonNode): JsonNode =
# ------------- pretty printing ----------------------------------------------
proc indent(s: var string, i: int) =
proc indent(s: var string, i: int) =
s.add(spaces(i))
proc newIndent(curr, indent: int, ml: bool): int =
if ml: return curr + indent
else: return indent
proc nl(s: var string, ml: bool) =
proc nl(s: var string, ml: bool) =
if ml: s.add("\n")
proc escapeJson*(s: string): string =
proc escapeJson*(s: string): string =
## Converts a string `s` to its JSON representation.
result = newStringOfCap(s.len + s.len shr 3)
result.add("\"")
@ -842,13 +842,13 @@ proc escapeJson*(s: string): string =
result.add(toHex(r, 4))
result.add("\"")
proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
lstArr = false, currIndent = 0) =
case node.kind
of JObject:
if currIndent != 0 and not lstArr: result.nl(ml)
result.indent(currIndent) # Indentation
if node.fields.len > 0:
if node.fields.len > 0:
result.add("{")
result.nl(ml) # New line
for i in 0..len(node.fields)-1:
@ -856,17 +856,17 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
result.add(", ")
result.nl(ml) # New Line
# Need to indent more than {
result.indent(newIndent(currIndent, indent, ml))
result.indent(newIndent(currIndent, indent, ml))
result.add(escapeJson(node.fields[i].key))
result.add(": ")
toPretty(result, node.fields[i].val, indent, ml, false,
toPretty(result, node.fields[i].val, indent, ml, false,
newIndent(currIndent, indent, ml))
result.nl(ml)
result.indent(currIndent) # indent the same as {
result.add("}")
else:
result.add("{}")
of JString:
of JString:
if lstArr: result.indent(currIndent)
result.add(escapeJson(node.str))
of JInt:
@ -934,11 +934,11 @@ iterator mpairs*(node: var JsonNode): var tuple[key: string, val: JsonNode] =
for keyVal in mitems(node.fields):
yield keyVal
proc eat(p: var JsonParser, tok: TTokKind) =
proc eat(p: var JsonParser, tok: TTokKind) =
if p.tok == tok: discard getTok(p)
else: raiseParseErr(p, tokToStr[tok])
proc parseJson(p: var JsonParser): JsonNode =
proc parseJson(p: var JsonParser): JsonNode =
## Parses JSON from a JSON Parser `p`.
case p.tok
of tkString:
@ -955,17 +955,17 @@ proc parseJson(p: var JsonParser): JsonNode =
of tkTrue:
result = newJBool(true)
discard getTok(p)
of tkFalse:
of tkFalse:
result = newJBool(false)
discard getTok(p)
of tkNull:
of tkNull:
result = newJNull()
discard getTok(p)
of tkCurlyLe:
of tkCurlyLe:
result = newJObject()
discard getTok(p)
while p.tok != tkCurlyRi:
if p.tok != tkString:
while p.tok != tkCurlyRi:
if p.tok != tkString:
raiseParseErr(p, "string literal as key expected")
var key = p.a
discard getTok(p)
@ -978,7 +978,7 @@ proc parseJson(p: var JsonParser): JsonNode =
of tkBracketLe:
result = newJArray()
discard getTok(p)
while p.tok != tkBracketRi:
while p.tok != tkBracketRi:
result.add(parseJson(p))
if p.tok != tkComma: break
discard getTok(p)
@ -1098,10 +1098,10 @@ when false:
of jsonObjectEnd: echo("}")
of jsonArrayStart: echo("[")
of jsonArrayEnd: echo("]")
close(x)
# { "json": 5 }
# { "json": 5 }
# To get that we shall use, obj["json"]
when isMainModule: