@cirru/parser.nim

Cirru parser compiled from Nim code

Usage no npm install needed!

<script type="module">
  import cirruParserNim from 'https://cdn.skypack.dev/@cirru/parser.nim';
</script>

README

Nim Cirru Parser

Parser for Cirru Syntax.

nimble install cirru-parser
import cirruParser

# parse code
parseCirru "a b"

which returns:

(kind: cirruSeq, list: @[(kind: cirruSeq, list: @[(kind: cirruString, text: "a"), (kind: cirruString, text: "b")])])  : CirruNode

CirruNode is the type of exprssions and tokens parsed from Cirru code. Browse types.nim for definitions.

A quick way to create Cirru nodes is creating nodes from JSON via toCirru function:

let a4 = %* ["a", "quot;, "quot;, "b"]
toCirru(a4)

To compare Cirru nodes, use ==. Notice that this overloaded == only checks types and values. Nodes are equal even they contain different line or column fields.

To format errors, use:

formatParserFailure(code, error.msg, "filename", error.line, error.column)

Browse tests/ you will find examples of all public APIs.

JavaScript library

Since Nim can be compiled to JavaScript, this library is also published on npm:

yarn add @cirru/parser.nim

Nim strings is different from JavaScript string. To call this library, convert string to number[] first:

a = require '@cirru/parser.nim'
code = 'a b'
codes = code.split('').map (x) -> x.charCodeAt(0)
cirru.parseCirru codes

For return value "b", it's also represented in number[]:

{
  "line": 1,
  "column": 3,
  "kind": 0,
  "text": [
    98
  ],
  "list": null
}

License

MIT