W3cubDocs

/Nim

Module sexp

Imports

hashes, strutils, lexbase, streams, unicode, macros

Types

SexpEventKind = enum
  sexpError,                  ## an error occurred during parsing
  sexpEof,                    ## end of file reached
  sexpString,                 ## a string literal
  sexpSymbol,                 ## a symbol
  sexpInt,                    ## an integer literal
  sexpFloat,                  ## a float literal
  sexpNil,                    ## the value ``nil``
  sexpDot,                    ## the dot to separate car/cdr
  sexpListStart,              ## start of a list: the ``(`` token
  sexpListEnd                 ## end of a list: the ``)`` token
enumeration of all events that may occur when parsing
SexpError = enum
  errNone,                    ## no error
  errInvalidToken,            ## invalid token
  errParensRiExpected,        ## ``)`` expected
  errQuoteExpected,           ## ``"`` expected
  errEofExpected              ## EOF expected
enumeration that lists all errors that can occur
SexpParser = object of BaseLexer
  a: string
  tok: TTokKind
  kind: SexpEventKind
  err: SexpError
the parser object.
SexpNodeKind = enum
  SNil, SInt, SFloat, SString, SSymbol, SList, SCons
possible SEXP node types
SexpNode = ref SexpNodeObj
SEXP node
SexpNodeObj = object
  case kind*: SexpNodeKind
  of SString:
      str*: string

  of SSymbol:
      symbol*: string

  of SInt:
      num*: BiggestInt

  of SFloat:
      fnum*: float

  of SList:
      elems*: seq[SexpNode]

  of SCons:
      car: SexpNode
      cdr: SexpNode

  of SNil:
      nil
SexpParsingError = object of ValueError
is raised for a SEXP error

Procs

proc close(my: var SexpParser) {.inline, raises: [Exception], tags: [].}
closes the parser my and its associated input stream.
proc str(my: SexpParser): string {.inline, raises: [], tags: [].}
returns the character data for the events: sexpInt, sexpFloat, sexpString
proc getInt(my: SexpParser): BiggestInt {.inline, raises: [ValueError], tags: [].}
returns the number for the event: sexpInt
proc getFloat(my: SexpParser): float {.inline, raises: [ValueError], tags: [].}
returns the number for the event: sexpFloat
proc kind(my: SexpParser): SexpEventKind {.inline, raises: [], tags: [].}
returns the current event type for the SEXP parser
proc getColumn(my: SexpParser): int {.inline, raises: [], tags: [].}
get the current column the parser has arrived at.
proc getLine(my: SexpParser): int {.inline, raises: [], tags: [].}
get the current line the parser has arrived at.
proc errorMsg(my: SexpParser): string {.raises: [ValueError], tags: [].}
returns a helpful error message for the event sexpError
proc errorMsgExpected(my: SexpParser; e: string): string {.raises: [ValueError],
    tags: [].}
returns an error message "e expected" in the same format as the other error messages
proc raiseParseErr(p: SexpParser; msg: string) {.noinline, noreturn,
    raises: [SexpParsingError, ValueError], tags: [].}
raises an ESexpParsingError exception.
proc newSString(s: string): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SString SexpNode.
proc newSInt(n: BiggestInt): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SInt SexpNode.
proc newSFloat(n: float): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SFloat SexpNode.
proc newSNil(): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SNil SexpNode.
proc newSCons(car, cdr: SexpNode): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SCons SexpNode
proc newSList(): SexpNode {.procvar, raises: [], tags: [].}
Creates a new SList SexpNode
proc newSSymbol(s: string): SexpNode {.procvar, raises: [], tags: [].}
proc getStr(n: SexpNode; default: string = ""): string {.raises: [], tags: [].}

Retrieves the string value of a SString SexpNode.

Returns default if n is not a SString.

proc getNum(n: SexpNode; default: BiggestInt = 0): BiggestInt {.raises: [], tags: [].}

Retrieves the int value of a SInt SexpNode.

Returns default if n is not a SInt.

proc getFNum(n: SexpNode; default: float = 0.0): float {.raises: [], tags: [].}

Retrieves the float value of a SFloat SexpNode.

Returns default if n is not a SFloat.

proc getSymbol(n: SexpNode; default: string = ""): string {.raises: [], tags: [].}

Retrieves the int value of a SList SexpNode.

Returns default if n is not a SList.

proc getElems(n: SexpNode; default: seq[SexpNode] = @[]): seq[SexpNode] {.raises: [],
    tags: [].}

Retrieves the int value of a SList SexpNode.

Returns default if n is not a SList.

proc getCons(n: SexpNode; defaults: Cons = (newSNil(), newSNil())): Cons {.raises: [],
    tags: [].}

Retrieves the cons value of a SList SexpNode.

Returns default if n is not a SList.

proc sexp(s: string): SexpNode {.raises: [], tags: [].}
Generic constructor for SEXP data. Creates a new SString SexpNode.
proc sexp(n: BiggestInt): SexpNode {.raises: [], tags: [].}
Generic constructor for SEXP data. Creates a new SInt SexpNode.
proc sexp(n: float): SexpNode {.raises: [], tags: [].}
Generic constructor for SEXP data. Creates a new SFloat SexpNode.
proc sexp(b: bool): SexpNode {.raises: [], tags: [].}
Generic constructor for SEXP data. Creates a new SSymbol SexpNode with value t or SNil SexpNode.
proc sexp(elements: openArray[SexpNode]): SexpNode {.raises: [], tags: [].}
Generic constructor for SEXP data. Creates a new SList SexpNode
proc sexp(s: SexpNode): SexpNode {.raises: [], tags: [].}
proc `==`(a, b: SexpNode): bool {.raises: [], tags: [].}
Check two nodes for equality
proc hash(n: SexpNode): Hash {.raises: [], tags: [].}
Compute the hash for a SEXP node
proc len(n: SexpNode): int {.raises: [], tags: [].}
If n is a SList, it returns the number of elements. If n is a JObject, it returns the number of pairs. Else it returns 0.
proc `[]`(node: SexpNode; index: int): SexpNode {.raises: [], tags: [].}
Gets the node at index in a List. Result is undefined if index is out of bounds
proc add(father, child: SexpNode) {.raises: [], tags: [].}
Adds child to a SList node father.
proc escapeJson(s: string): string {.raises: [], tags: [].}
Converts a string s to its JSON representation.
proc copy(p: SexpNode): SexpNode {.raises: [], tags: [].}
Performs a deep copy of a.
proc pretty(node: SexpNode; indent = 2): string {.raises: [], tags: [].}
Converts node to its Sexp Representation, with indentation and on multiple lines.
proc `$`(node: SexpNode): string {.raises: [], tags: [].}
Converts node to its SEXP Representation on one line.
proc open(my: var SexpParser; input: Stream) {.raises: [Exception], tags: [ReadIOEffect].}
initializes the parser with an input stream.
proc parseSexp(s: Stream): SexpNode {.raises: [Exception, ValueError, SexpParsingError],
                                  tags: [ReadIOEffect].}
Parses from a buffer s into a SexpNode.
proc parseSexp(buffer: string): SexpNode {.raises: [Exception, ValueError,
    SexpParsingError], tags: [ReadIOEffect].}
Parses Sexp from buffer.

Iterators

iterator items(node: SexpNode): SexpNode {.raises: [], tags: [].}
Iterator for the items of node. node has to be a SList.
iterator mitems(node: var SexpNode): var SexpNode {.raises: [], tags: [].}
Iterator for the items of node. node has to be a SList. Items can be modified.

Macros

macro convertSexp(x: untyped): untyped
Convert an expression to a SexpNode directly, without having to specify % for every element.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/sexp.html