# @shexjs/parser

> Shape Expressions Compact Syntax (ShExC) parser.

Latest version **1.0.0-alpha.33** (published 2026-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @shexjs/parser
pnpm add @shexjs/parser
yarn add @shexjs/parser
bun add @shexjs/parser
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.33 |
| Published | 2026-09-14 |
| First published | 2019-03-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 4 |
| Unpacked size | 311.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 62 |
| Author | Eric Prud'hommeaux |
| Maintainers | ericprud, justinwb |
| Keywords | shex, shape expressions, rdf, query, parser |

## Links

- npm: https://www.npmjs.com/package/@shexjs/parser
- Repository: https://github.com/shexjs/shex.js
- Homepage: https://github.com/shexjs/shex.js#readme
- Issues: https://github.com/shexSpec/grammar/issues
- npm.io page: https://npm.io/package/@shexjs/parser

## Dependencies (4)

- [@shexjs/term](https://npm.io/package/@shexjs/term.md) ^1.0.0-alpha.28
- [@types/shexj](https://npm.io/package/@types/shexj.md) ^2.1.7
- [@ts-jison/lexer](https://npm.io/package/@ts-jison/lexer.md) ^0.4.1-alpha.1
- [@ts-jison/parser](https://npm.io/package/@ts-jison/parser.md) ^0.4.1-alpha.3

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.0-alpha.33 (latest) — 2026-09-14
- 1.0.0-alpha.31 — 2026-09-06
- 1.0.0-alpha.30 — 2026-08-31
- 1.0.0-alpha.28 — 2023-11-05
- 1.0.0-alpha.27 — 2023-10-23
- 1.0.0-alpha.26 — 2023-04-25
- 1.0.0-alpha.25 — 2023-03-21
- 1.0.0-alpha.24 — 2022-09-16
- 1.0.0-alpha.23 — 2022-08-23
- 1.0.0-alpha.22 — 2022-08-18
- 1.0.0-alpha.21 — 2022-03-08
- 1.0.0-alpha.20 — 2022-01-12
- 1.0.0-alpha.19 — 2021-12-22
- 1.0.0-alpha.15 — 2021-07-18
- 1.0.0-alpha.14 — 2021-06-22
- … 12 more at https://npm.io/package/@shexjs/parser/versions

## README

# @shexjs/parser

[![npm version](https://img.shields.io/npm/v/@shexjs/parser)](https://www.npmjs.com/package/@shexjs/parser)
[![CI](https://github.com/shexjs/shex.js/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/shexjs/shex.js/actions/workflows/ci.yml)

Parse [ShExC](https://shex.io/shex-semantics/#shexc), return [ShExJ](https://shex.io/shex-semantics/#shexj)

## Install

``` shell
npm install @shexjs/parser
```

## Quick start
Invoke from the command line:
``` sh
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:
``` json
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/S1",
      "type": "ShapeDecl",
      "shapeExpr": {
        "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](https://developer.mozilla.org/en-US/docs/Web/API/URL)) allows you to parse schemas with relative URLs for e.g. shape and property names:
``` sh
node -e 'console.log(
  JSON.stringify(require("@shexjs/parser")
    .construct("http://a.example/")
    .parse("<S1> { <p1> [1 2] }"), null, 2)
)'
```
``` json
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/S1",
      "type": "ShapeDecl",
      "shapeExpr": {
        "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:
``` sh
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)
)'
```
``` json
{
  "type": "Schema",
  "shapes": [
    {
      "id": "http://a.example/path/S1",
      "type": "ShapeDecl",
      "shapeExpr": {
        "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, indexes the labeled shape declarations and triple expressions (`._index`), and records where each declaration was parsed (`._locations`) — what editor tooling wants:
``` sh
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)
)'
```
``` json
{
  "type": "Schema",
  "shapes": [ … as above … ],
  "_base": "http://a.example/path/path3",
  "_prefixes": {
    "": "http://a.example/path/path3#"
  },
  "_index": {
    "shapeExprs": {
      "http://a.example/path/S1": { … the ShapeDecl above … }
    },
    "tripleExprs": {}
  },
  "_sourceMap": null,
  "_locations": {
    "http://a.example/path/S1": {
      "filename": "http://a.example/path/path2/",
      "first_line": 3,
      "first_column": 0,
      "last_line": 3,
      "last_column": 24
    }
  },
  "_exprLocations": {}
}
```

---

`@shexjs/parser` is one of the [shex.js](https://github.com/shexjs/shex.js#readme) packages; installing [`shex`](https://www.npmjs.com/package/shex) pulls in the whole suite, and [its README](https://github.com/shexjs/shex.js/tree/main/packages/shex#the-shexjs-packages) maps them.

---
_Source: https://npm.io/package/@shexjs/parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
