@faubulous/mentor-rdf-parsers
v1.7.4
Published
Chevrotain parsers for RDF languages such as N-Triples, N-Quads, Turtle and Trig.
Downloads
783
Maintainers
Readme
Mentor RDF Parsers
Standards-compliant, fault-tolerant parsers for RDF languages, built with Chevrotain. Designed for IDEs and language tools where error recovery and concrete syntax tree (CST) access are essential.
Features
- Standards Compliance — Implements grammar productions from W3C specifications
- Fault Tolerance — Recovers from syntax errors and continues parsing
- Concrete Syntax Trees — Full token access with positions for tooling
- RDF/JS Quads — Reader classes produce RDF/JS-compliant quads
- Blank Node Tracking — Pre-assigned IDs for IDE features like "go to definition"
Supported Languages
| Language | Parser | Reader | Specification | |----------|:------:|:------:|---------------| | N-Triples | ✓ | ✓ | RDF 1.2 N-Triples | | N-Quads | ✓ | ✓ | RDF 1.2 N-Quads | | Turtle | ✓ | ✓ | RDF 1.2 Turtle | | TriG | ✓ | ✓ | RDF 1.2 TriG | | N3 | ✓ | ✓ | W3C N3 | | SPARQL 1.2 | ✓ | — | SPARQL 1.2 Query |
Installation
npm install @faubulous/mentor-rdf-parsersQuick Start
import { TurtleLexer, TurtleParser, TurtleReader } from '@faubulous/mentor-rdf-parsers';
const input = `
@prefix ex: <http://example.org/> .
ex:Alice ex:knows ex:Bob .
`;
// Tokenize → Parse → Read
const lexResult = new TurtleLexer().tokenize(input);
const cst = new TurtleParser().parse(lexResult.tokens);
const quads = new TurtleReader().visit(cst);
console.log(quads[0].subject.value); // "http://example.org/Alice"Architecture
flowchart LR
A[Input String] --> B[Lexer]
B -->|tokens| C[Parser]
C -->|CST| D[Reader]
D -->|quads| E[RDF/JS Quads]
B -.->|lexing errors| F[errors]
C -.->|parsing errors| F| Component | Purpose | |-----------|---------| | Lexer | Tokenizes input string into a token stream | | Parser | Produces a concrete syntax tree (CST) from tokens | | Reader | Walks the CST and produces RDF/JS Quad objects |
Documentation
Examples by Language
| Language | Guide | |----------|-------| | Turtle | docs/examples/turtle.md | | N-Triples | docs/examples/n-triples.md | | N-Quads | docs/examples/n-quads.md | | TriG | docs/examples/trig.md | | N3 | docs/examples/n3.md | | SPARQL | docs/examples/sparql.md |
Advanced Topics
- Error Handling — Fault-tolerant parsing and error collection
- Blank Node IDs — Pre-assigned identifiers for IDE integration
Error Handling
Parsers collect errors instead of throwing, allowing partial results:
const parser = new TurtleParser();
const cst = parser.parse(tokens, false); // Pass false to collect errors
// Check all error sources
console.log(lexResult.errors); // Lexer errors
console.log(parser.errors); // Parser errors
console.log(parser.semanticErrors); // Semantic errors (e.g., undefined prefixes)See Error Handling Guide for details.
Token Access
Each language exports its token array for use in formatters and syntax highlighters:
import { TurtleTokens, SparqlTokens } from '@faubulous/mentor-rdf-parsers';
console.log(TurtleTokens.map(t => t.name));Testing
npm testThe test suite includes over 1,500 tests covering all supported languages and the official W3C conformance test suites.
