@shexjs/parser
v1.0.0-alpha.28
Published
Shape Expressions Compact Syntax (ShExC) parser.
Downloads
2,106
Readme
@shexjs/parser
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/*
:
shape-map
-- a ShapeMap parser@shexjs/parser
-- parse ShExC into ShExJ@shexjs/writer
-- serialize ShExK as ShExC@shexjs/term
-- RDF terms uses in ShEx@shexjs/util
-- some utilities for transforming schemas or validation output@shexjs/visitor
-- a visitor for schemas@shexjs/validator
-- validate nodes in an RDF graph against shapes in a schema@shexjs/eval-validator-api
-- API called by@shexjs/validator
for validating Shapes, with tripleExpressions and EXTENDS etc.@shexjs/eval-simple-1err
-- Implementation of@shexjs/eval-validator-api
which reports only one error.@shexjs/eval-threaded-nerr
-- Implementation of@shexjs/eval-validator-api
which exhaustively enumerate combinations of ways the data fails to satisfy a shape's expression.@shexjs/loader
-- an API for loading and using ShEx schemas@shexjs/node
-- additional API functionality for a node environment@shexjs/cli
-- a set of command line tools for transformaing and validating with schemas@shexjs/webapp
-- the shex-simple WEBApp@shexjs/shape-path-query
-- traverse ShEx schemas with a path language@shexjs/extension-test
-- a small language for testing semantic actions in ShEx implementations (more)@shexjs/extension-map
-- an extension for transforming data from one schema to another (more)@shexjs/extension-eval
-- simple extension which evaluates Javascript semantic action code (more)