Skip to content
This repository was archived by the owner on Feb 17, 2026. It is now read-only.
This repository was archived by the owner on Feb 17, 2026. It is now read-only.

Parse partial syntax #190

Description

@tpluscode

Could it be possible to expose utils to parse portions of SPARQL? This would help working with sparqljs where I need to generate some JS representation programmatically but the code becomes quite verbose.

For example, I want to create a BindExpression for this snippet

BIND(IF(REGEX(str(?res), ?resourcePattern), ?pagePattern, 1/0) as ?page)

is create like

const expression = {
                        type: 'bind',
                        variable: { termType: 'Variable', value: 'page' },
                        expression: {
                            type: 'operation',
                            operator: 'if',
                            args: [
                                {
                                    type: 'operation',
                                    operator: 'regex',
                                    args: [
                                        {
                                            type: 'operation',
                                            operator: 'str',
                                            args: [
                                                { termType: 'Variable', value: 'res' }
                                            ]
                                        },
                                        this.factory.variable('resourcePattern'),
                                    ]
                                },
                                this.factory.variable('pagePattern'),
                                {
                                    type: 'operation',
                                    operator: '/',
                                    args: [
                                        toRdf(1),
                                        toRdf(0),
                                    ]
                                }
                            ]
                        }
                    }

It would be much more efficient to write something like

let parser

parser.parseBindExpression('BIND(IF(REGEX(str(?res), ?resourcePattern), ?pagePattern, 1/0) as ?page)')

Combined with @tpluscode/sparql-builder, I could also interpolate RDF/JS terms

import { sparql } from '@tpluscode/sparql-builder'
import rdf from '@rdfjs/data-model'

let parser

let pageVar = rdf.variable('page')

parser.parseBindExpression(sparql`BIND(IF(REGEX(str(?res), ?resourcePattern), ?pagePattern, 1/0) as ${pageVar})`)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions