npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@traqula/parser-sparql-1-1

v1.0.5

Published

SPARQL 1.1 parser

Readme

Traqula parser engine for SPARQL 1.1

npm version

Traqula Sparql 1.1 is a SPARQL 1.1 query parser for TypeScript.

Installation

npm install @traqula/parser-sparql-1-1

or

yarn add @traqula/parser-sparql-1-1

Import

Either through ESM import:

import { Parser } from '@traqula/parser-sparql-1-1';

or CJS require:

const Parser = require('@traqula/parser-sparql-1-1').Parser;

Usage

This package contains a Parser that is able to parse SPARQL 1.1 queries:

const parser = new Parser();
const abstractSyntaxTree = parser.parse('SELECT * { ?s ?p ?o }');

Note that a single parser cannot parse multiple queries in parallel.

The package also contains multiple parserBuilders. These builders can be used either to consume to a parser, or to usage as a starting point for your own grammar.

Note: it is essential that you reuse created parser to the full extent. Traqula builds on top of Chevrotain, an amazing project that allows for the definition of parsers within JavaScript, since the definition of the parser is part of the program, so is the optimization of our parser. Everytime you create a parser, the grammar optimizations need to be computed again.

Configuration

Optionally, the following parameters can be set in the Parsers defaultContext:

  • dataFactory: A custom RDFJS DataFactory to construct terms and triples. (Default: require('@rdfjs/data-model'))
  • skipValidation: Can be used to disable the validation that used variables in a select clause are in scope. (Default: false)

AstFactory and AstTransformer

Traqula provides two tools to help you manipulate an AST. The AstFactory helps you create AST nodes and validate the type of node using type predicates. When creating nodes, the first argument will always be a SourceLocation, in case you are not using round tripping, you can just provide F.gen().

The transformer is a strongly typed generic yet optimized tree transformer. The AstTransformer is a type specified TransformerSubTyped and can be used as is on the Ast Nodes.

import { Parser } from '@traqula/parser-sparql-1-1';
import { AstTransformer, AstFactory } from '@traqula/rules-sparql-1-1';
const query = `SELECT * { ?s ?p ?o }`;
const parser = new Parser();
const F = new AstFactory();
const transformer = new AstTransformer();

const ast = parser.parse(query);
// expand variables s p and o to their long names:
// SELECT * { ?subject ?predicate ?object }
transformer.transformNodeSpecific<'unsafe', typeof ast>(ast, {}, {
  // Something of type 'term'
  term: {
    // With subtype 'variable'
    variable: {
      transform: (variable) => {
        const newName = {
          's': 'subject',
          'p': 'predicate',
          'o': 'object'
        }[variable.value];
        return F.termVariable(newName ?? variable.value, F.gen());
      }
    }
  }
});

[!note] The transformer will return a safe version of the node by default, meaning it will return the ast node, but say the values for all keys are unknown since they could have been replaced by any new value in the course of performing the transformation. To just get the type of the node you provide the generic 'unsafe'. Likewise the returned value of the transform function is unknown unless you call unsafe and provide the expected type.

Collecting round tripping information

The generated AST is constructed such that it allows for round tripping. This means that a parsed query string produces an AST that, when generated from provides exactly the same query string. By default, though, the configured lexer does not collect enough information to enable this because it comes at a small slowdown (see our report). Simply provide the following configuration to the parser, a astFactory that handles source information, and a lexerConfig that states location information should be collected:

import { AstFactory } from '@traqula/rules-sparql-1-1';
import { adjustParserBuilder, adjustLexerBuilder, Parser } from '@traqula/parser-sparql-1-1';
const sourceTrackingAstFactory = new AstFactory();
const sourceTrackingParser = new Parser({
  defaultContext: { astFactory: sourceTrackingAstFactory },
  lexerConfig: { positionTracking: 'full' },
});