No description
Find a file
SkyAo 83fa0ef394
Merge pull request #31 from Cirru/renaming
rename types to reduce inconsistency of APIs
2021-01-25 13:53:16 +08:00
.github/workflows trying tests with actions 2020-08-20 11:57:11 +08:00
scripts get JavaScript backend of library working 2019-12-22 00:26:57 +08:00
src rename types to reduce inconsistency of APIs; start 0.3.0 2021-01-24 22:14:25 +08:00
tests rename types to reduce inconsistency of APIs; start 0.3.0 2021-01-24 22:14:25 +08:00
.gitignore switching to linked list for performance 2020-10-10 16:37:15 +08:00
.npmignore trying alpha with a npm module from nim 2019-12-15 17:31:50 +08:00
cirru_parser.nimble rename types to reduce inconsistency of APIs; start 0.3.0 2021-01-24 22:14:25 +08:00
package.json change naming convention, use underscores; bump 0.1.4 2020-09-29 18:06:32 +08:00
README.md rename types to reduce inconsistency of APIs; start 0.3.0 2021-01-24 22:14:25 +08:00

Nim Cirru Parser

Parser for Cirru Syntax.

nimble install cirru_parser
import cirru_parser

# parse code
parseCirru "a b"

which returns:

(kind: cirruList, list: @[(kind: cirruList, list: @[(kind: cirruToken, token: "a"), (kind: cirruToken, token: "b")])])  : CirruNode

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

type
  CirruNodeKind* = enum
    cirruToken,
    cirruList

  CirruNode* = object
    line*: int
    column*: int
    case kind*: CirruNodeKind
    of cirruToken:
      token*: string
    of cirruList:
      list*: DoublyLinkedList[CirruNode]

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

let a4 = %* ["a", "$", "$", "b"]
let a5 = toCirru(a4)

toJson(a5) # converts back

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