@shexjs/parser
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.28 • Public • Published

NPM Version ShapeExpressions Gitter chat https://gitter.im/shapeExpressions/Lobby DOI

@shexjs/parser

Parse ShExC, return ShExJ

install

npm install --save @shexjs/parser

Quick Start

Invoke from the command line:

node -e 'console.log(
  JSON.stringify(require("@shexjs/parser")
    .construct()
    .parse("<http://a.example/S1> { <http://a.example/p1> [1 2] }"), null, 2)
)'

The result is a ShExJ expression of the input schema:

{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/S1",
      "type": "Shape",
      "expression": {
        "type": "TripleConstraint",
        "predicate": "http://a.example/p1",
        "valueExpr": {
          "type": "NodeConstraint",
          "values": [
            {
              "value": "1",
              "type": "http://www.w3.org/2001/XMLSchema#integer"
            },
            {
              "value": "2",
              "type": "http://www.w3.org/2001/XMLSchema#integer"
            }
          ]
        }
      }
    }
  ]
}

Base IRI

Providing a Base IRI (see MDN docs for URL) allows you to parse schemas with relative URLs for e.g. shape and property names:

node -e 'console.log(
  JSON.stringify(require("@shexjs/parser")
    .construct("http://a.example/")
    .parse("<S1> { <p1> [1 2] }"), null, 2)
)'
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/S1",
      "type": "Shape",
      "expression": {
        "type": "TripleConstraint",
        "predicate": "http://a.example/p1",
        "valueExpr": {
          "type": "NodeConstraint",
          "values": [
            {
              "value": "1",
              "type": "http://www.w3.org/2001/XMLSchema#integer"
            },
            {
              "value": "2",
              "type": "http://www.w3.org/2001/XMLSchema#integer"
            }
          ]
        }
      }
    }
  ]
}

Pre-loaded prefixes

A second parameter to construct is a map for prefixes that are not defined in the schema:

node -e 'console.log(
  JSON.stringify(require("@shexjs/parser")
    .construct("http://a.example/path/path2/", {v: "http://a.example/vocab#"})
    .parse("BASE <../path3>\nPREFIX : <#>\n<S1> { :p1 [v:v1 v:v2] }"), null, 2)
)'
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/path/S1",
      "type": "Shape",
      "expression": {
        "type": "TripleConstraint",
        "predicate": "http://a.example/path/path3#p1",
        "valueExpr": {
          "type": "NodeConstraint",
          "values": [
            "http://a.example/vocab#v1",
            "http://a.example/vocab#v2"
          ]
        }
      }
    }
  ]
}

Index option

The third construct parameter is for passing parsing options. One handy one is index, which returns the final base (._base) and prefix mapping (._prefixes) encountered during parsing. The ._index provides indexes into the ShExJ's labled shape declarations and triple expressions :

node -e 'console.log(
  JSON.stringify(require("@shexjs/parser")
    .construct("http://a.example/path/path2/", {v: "http://a.example/vocab#"}, {index:true})
    .parse("BASE <../path3>\nPREFIX : <#>\n<S1> { :p1 [v:v1 v:v2] }"), null, 2)
)'
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/path/S1",
      "type": "Shape",
      "expression": {
        "type": "TripleConstraint",
        "predicate": "http://a.example/path/path3#p1",
        "valueExpr": {
          "type": "NodeConstraint",
          "values": [
            "http://a.example/vocab#v1",
            "http://a.example/vocab#v2"
          ]
        }
      }
    }
  ],
  "_base": "http://a.example/path/path3",
  "_prefixes": {
    "": "http://a.example/path/path3#"
  },
  "_index": {
    "shapeExprs": {
      "http://a.example/path/S1": <ref to S1 above>
    },
    "tripleExprs": {}
  }
}

Lerna Monorepo

This repo uses lerna to manage multiple NPM packages. These packages are located in packages/*:

Package Sidebar

Install

npm i @shexjs/parser

Weekly Downloads

253

Version

1.0.0-alpha.28

License

MIT

Unpacked Size

297 kB

Total Files

9

Last publish

Collaborators

  • ericprud
  • justinwb