// Type definitions for sparqljs 1.5 // Project: https://github.com/RubenVerborgh/SPARQL.js // Definitions by: Alexey Morozov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 export const Parser: { new ( prefixes?: { [prefix: string]: string }, baseIRI?: string, options?: ParserOptions, ): SparqlParser; }; export const Generator: { new (options?: GeneratorOptions): SparqlGenerator; }; export interface ParserOptions { /** @default true */ collapseGroups?: boolean; } export interface GeneratorOptions { allPrefixes?: boolean; } export interface SparqlParser { parse(query: string): SparqlQuery; } export interface SparqlGenerator { stringify(query: SparqlQuery): string; } export type SparqlQuery = Query | Update; export type Query = SelectQuery | ConstructQuery | AskQuery | DescribeQuery; export interface BaseQuery { type: 'query'; base?: string; prefixes: { [prefix: string]: string; }; where?: Pattern[]; values?: ValuePatternRow[]; } export interface SelectQuery extends BaseQuery { queryType: 'SELECT'; variables: Variable[] | ['*']; distinct?: boolean; from?: { default: string[]; named: string[]; }; reduced?: boolean; group?: Grouping[]; having?: Expression[]; order?: Ordering[]; limit?: number; offset?: number; } export interface Grouping { expression: Expression; } export interface Ordering { expression: Expression; descending?: boolean; } export interface ConstructQuery extends BaseQuery { queryType: 'CONSTRUCT'; template?: Triple[]; } export interface AskQuery extends BaseQuery { queryType: 'ASK'; } export interface DescribeQuery extends BaseQuery { queryType: 'DESCRIBE'; variables: Variable[] | ['*']; } export interface Update { type: 'update'; prefixes: { [prefix: string]: string; }; updates: UpdateOperation[]; } export type UpdateOperation = InsertDeleteOperation | ManagementOperation; export interface InsertDeleteOperation { updateType: 'insert' | 'delete' | 'deletewhere' | 'insertdelete'; insert?: Quads[]; delete?: Quads[]; where?: Pattern[]; } export type Quads = BgpPattern | GraphQuads; export interface ManagementOperation { type: 'load' | 'copy' | 'move' | 'add'; silent: boolean; source: string | { type: 'graph'; default: boolean; }; destination?: string | { type: 'graph'; name: string; }; } /** * Examples: '?var', '*', * SELECT (?a as ?b) ... ==> { expression: '?a', variable: '?b' } */ export type Variable = VariableExpression | Term; export interface VariableExpression { expression: Expression; variable: Term; } export type Pattern = | BgpPattern | BlockPattern | GraphPattern | ServicePattern | FilterPattern | BindPattern | ValuesPattern | SelectQuery; /** * Basic Graph Pattern */ export interface BgpPattern { type: 'bgp'; triples: Triple[]; } export interface GraphQuads { type: 'graph'; name: Term; triples: Triple[]; } export interface BlockPattern { type: 'optional' | 'union' | 'group' | 'minus' | 'graph' | 'service'; patterns: Pattern[]; } export interface GroupPattern extends BlockPattern { type: 'group'; } export interface GraphPattern extends BlockPattern { type: 'graph'; name: Term; } export interface ServicePattern extends BlockPattern { type: 'service'; name: Term; silent: boolean; } export interface FilterPattern { type: 'filter'; expression: Expression; } export interface BindPattern { type: 'bind'; expression: Expression; variable: Term; } export interface ValuesPattern { type: 'values'; values: ValuePatternRow[]; } export interface ValuePatternRow { [variable: string]: Term; } /** * Either '?var', 'schema:iri', '_:blankNode', * '"literal"^^' or '{undefined}'. * * Term is a nominal type based on string. */ export type Term = string & { __termBrand: string; }; export interface Triple { subject: Term; predicate: PropertyPath | Term; object: Term; } export interface PropertyPath { type: 'path'; pathType: '|' | '/' | '^' | '+' | '*' | '!'; items: Array; } export type Expression = | OperationExpression | FunctionCallExpression | AggregateExpression | BgpPattern | GroupPattern | Tuple | Term; // allow Expression circularly reference itself // tslint:disable-next-line no-empty-interface export interface Tuple extends Array {} export interface BaseExpression { type: string; distinct?: boolean; } export interface OperationExpression extends BaseExpression { type: 'operation'; operator: string; args: Expression[]; } export interface FunctionCallExpression extends BaseExpression { type: 'functionCall'; function: string; args: Expression[]; } export interface AggregateExpression extends BaseExpression { type: 'aggregate'; expression: Expression; aggregation: string; separator?: string; }