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

@ -12,20 +12,20 @@
import macros, strtabs
type
XmlNode* = ref XmlNodeObj ## an XML tree consists of ``PXmlNode``'s.
XmlNode* = ref XmlNodeObj ## an XML tree consists of ``PXmlNode``'s.
XmlNodeKind* = enum ## different kinds of ``PXmlNode``'s
xnText, ## a text element
xnElement, ## an element with 0 or more children
xnCData, ## a CDATA node
xnEntity, ## an entity (like ``&thing;``)
xnComment ## an XML comment
XmlAttributes* = StringTableRef ## an alias for a string to string mapping
XmlNodeObj {.acyclic.} = object
XmlNodeObj {.acyclic.} = object
case k: XmlNodeKind # private, use the kind() proc to read this field.
of xnText, xnComment, xnCData, xnEntity:
of xnText, xnComment, xnCData, xnEntity:
fText: string
of xnElement:
fTag: string
@ -41,34 +41,34 @@ proc newXmlNode(kind: XmlNodeKind): XmlNode =
new(result)
result.k = kind
proc newElement*(tag: string): XmlNode =
proc newElement*(tag: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnText`` with the given `tag`.
result = newXmlNode(xnElement)
result.fTag = tag
result.s = @[]
# init attributes lazily to safe memory
proc newText*(text: string): XmlNode =
proc newText*(text: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnText`` with the text `text`.
result = newXmlNode(xnText)
result.fText = text
proc newComment*(comment: string): XmlNode =
proc newComment*(comment: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `comment`.
result = newXmlNode(xnComment)
result.fText = comment
proc newCData*(cdata: string): XmlNode =
proc newCData*(cdata: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `cdata`.
result = newXmlNode(xnCData)
result.fText = cdata
proc newEntity*(entity: string): XmlNode =
proc newEntity*(entity: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnEntity`` with the text `entity`.
result = newXmlNode(xnCData)
result.fText = entity
proc text*(n: XmlNode): string {.inline.} =
proc text*(n: XmlNode): string {.inline.} =
## gets the associated text with the node `n`. `n` can be a CDATA, Text,
## comment, or entity node.
assert n.k in {xnText, xnComment, xnCData, xnEntity}
@ -93,16 +93,16 @@ proc innerText*(n: XmlNode): string =
for i in 0 .. n.s.len-1:
if n.s[i].k in {xnText, xnEntity}: result.add(n.s[i].fText)
proc tag*(n: XmlNode): string {.inline.} =
proc tag*(n: XmlNode): string {.inline.} =
## gets the tag name of `n`. `n` has to be an ``xnElement`` node.
assert n.k == xnElement
result = n.fTag
proc add*(father, son: XmlNode) {.inline.} =
proc add*(father, son: XmlNode) {.inline.} =
## adds the child `son` to `father`.
add(father.s, son)
proc len*(n: XmlNode): int {.inline.} =
proc len*(n: XmlNode): int {.inline.} =
## returns the number `n`'s children.
if n.k == xnElement: result = len(n.s)
@ -110,38 +110,38 @@ proc kind*(n: XmlNode): XmlNodeKind {.inline.} =
## returns `n`'s kind.
result = n.k
proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
## returns the `i`'th child of `n`.
assert n.k == xnElement
result = n.s[i]
proc mget* (n: var XmlNode, i: int): var XmlNode {.inline.} =
proc mget* (n: var XmlNode, i: int): var XmlNode {.inline.} =
## returns the `i`'th child of `n` so that it can be modified
assert n.k == xnElement
result = n.s[i]
iterator items*(n: XmlNode): XmlNode {.inline.} =
iterator items*(n: XmlNode): XmlNode {.inline.} =
## iterates over any child of `n`.
assert n.k == xnElement
for i in 0 .. n.len-1: yield n[i]
iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
## iterates over any child of `n`.
assert n.k == xnElement
for i in 0 .. n.len-1: yield mget(n, i)
proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
## gets the attributes belonging to `n`.
## Returns `nil` if attributes have not been initialised for this node.
assert n.k == xnElement
result = n.fAttr
proc `attrs=`*(n: XmlNode, attr: XmlAttributes) {.inline.} =
proc `attrs=`*(n: XmlNode, attr: XmlAttributes) {.inline.} =
## sets the attributes belonging to `n`.
assert n.k == xnElement
n.fAttr = attr
proc attrsLen*(n: XmlNode): int {.inline.} =
proc attrsLen*(n: XmlNode): int {.inline.} =
## returns the number of `n`'s attributes.
assert n.k == xnElement
if not isNil(n.fAttr): result = len(n.fAttr)
@ -151,12 +151,12 @@ proc clientData*(n: XmlNode): int {.inline.} =
## parser and generator.
result = n.fClientData
proc `clientData=`*(n: XmlNode, data: int) {.inline.} =
proc `clientData=`*(n: XmlNode, data: int) {.inline.} =
## sets the client data of `n`. The client data field is used by the HTML
## parser and generator.
n.fClientData = data
proc addEscaped*(result: var string, s: string) =
proc addEscaped*(result: var string, s: string) =
## same as ``result.add(escape(s))``, but more efficient.
for c in items(s):
case c
@ -168,8 +168,8 @@ proc addEscaped*(result: var string, s: string) =
of '/': result.add("/")
else: result.add(c)
proc escape*(s: string): string =
## escapes `s` for inclusion into an XML document.
proc escape*(s: string): string =
## escapes `s` for inclusion into an XML document.
## Escapes these characters:
##
## ------------ -------------------
@ -184,26 +184,26 @@ proc escape*(s: string): string =
## ------------ -------------------
result = newStringOfCap(s.len)
addEscaped(result, s)
proc addIndent(result: var string, indent: int) =
proc addIndent(result: var string, indent: int) =
result.add("\n")
for i in 1..indent: result.add(' ')
proc noWhitespace(n: XmlNode): bool =
#for i in 1..n.len-1:
# if n[i].kind != n[0].kind: return true
for i in 0..n.len-1:
if n[i].kind in {xnText, xnEntity}: return true
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
## adds the textual representation of `n` to `result`.
if n == nil: return
case n.k
of xnElement:
result.add('<')
result.add(n.fTag)
if not isNil(n.fAttr):
for key, val in pairs(n.fAttr):
if not isNil(n.fAttr):
for key, val in pairs(n.fAttr):
result.add(' ')
result.add(key)
result.add("=\"")
@ -217,7 +217,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
# because this would be wrong. For example: ``a<b>b</b>`` is
# different from ``a <b>b</b>``.
for i in 0..n.len-1: result.add(n[i], indent+indWidth, indWidth)
else:
else:
for i in 0..n.len-1:
result.addIndent(indent+indWidth)
result.add(n[i], indent+indWidth, indWidth)
@ -227,7 +227,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
result.add("</")
result.add(n.fTag)
result.add(">")
else:
else:
result.add(" />")
of xnText:
result.addEscaped(n.fText)
@ -245,7 +245,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
result.add(';')
const
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
## header to use for complete XML output
proc `$`*(n: XmlNode): string =
@ -255,21 +255,21 @@ proc `$`*(n: XmlNode): string =
result.add(n)
proc newXmlTree*(tag: string, children: openArray[XmlNode],
attributes: XmlAttributes = nil): XmlNode =
attributes: XmlAttributes = nil): XmlNode =
## creates a new XML tree with `tag`, `children` and `attributes`
result = newXmlNode(xnElement)
result.fTag = tag
newSeq(result.s, children.len)
for i in 0..children.len-1: result.s[i] = children[i]
result.fAttr = attributes
proc xmlConstructor(e: PNimrodNode): PNimrodNode {.compileTime.} =
proc xmlConstructor(e: NimNode): NimNode {.compileTime.} =
expectLen(e, 2)
var a = e[1]
if a.kind == nnkCall:
result = newCall("newXmlTree", toStrLit(a[0]))
var attrs = newNimNode(nnkBracket, a)
var newStringTabCall = newCall("newStringTable", attrs,
var newStringTabCall = newCall("newStringTable", attrs,
newIdentNode("modeCaseSensitive"))
var elements = newNimNode(nnkBracket, a)
for i in 1..a.len-1:
@ -280,13 +280,13 @@ proc xmlConstructor(e: PNimrodNode): PNimrodNode {.compileTime.} =
else:
elements.add(a[i])
result.add(elements)
if attrs.len > 1:
if attrs.len > 1:
#echo repr(newStringTabCall)
result.add(newStringTabCall)
else:
result = newCall("newXmlTree", toStrLit(a))
macro `<>`*(x: expr): expr {.immediate.} =
macro `<>`*(x: expr): expr {.immediate.} =
## Constructor macro for XML. Example usage:
##
## .. code-block:: nim