Skip to content

[Query] Implement SPARQL negative syntax rejection and blank node scoping in ANTLR parser #571

Description

@remiceres

Summary

Enforce SPARQL syntax validation and blank node scoping in the ANTLR parsing and semantic analysis pipeline, focusing on blank node scope validation across graph patterns and proper rejection of negative syntax test cases (syn-bad-*, syntax-query/, syntax-update-1/).

Context and Specification Requirements

According to W3C SPARQL specifications (Section 12.3: Scope of Blank Node Labels):

  • Blank node labels (_:label) are scoped to a single Basic Graph Pattern (BGP).
  • Reusing the same blank node label across distinct BGPs, or across UNION, OPTIONAL, or GRAPH pattern blocks, is a syntax violation and must be rejected with QuerySyntaxException (or a semantic error diagnostic).
  • Negative syntax test cases must be rejected during query parsing or semantic analysis rather than producing an AST.

Implementation Details

1. Semantic Validation and AST Architecture

  • Rules package: fr.inria.corese.core.next.query.impl.sparql.parser.semantic.rule
  • Reference implementation: SelectProjectionScopeValidationRule.java implements SemanticValidationRule (or extends AbstractSemanticValidationRule).
  • New rule: Implement blank node scope validation (e.g., BlankNodeScopeValidationRule.java) to traverse patterns and detect blank node label reuse across disjoint BGPs or pattern blocks (UNION, OPTIONAL, GRAPH).
  • Register the new rule in SparqlQuerySemanticValidator.java.
  • AST / Parser builders: fr.inria.corese.core.next.query.impl.sparql.parser.SparqlAstBuilder and SparqlQueryAstBuilder.java maintain groupStack and unionStack. Ensure blank node occurrences retain scope context or are verified during AST construction.
  • Syntax errors: Handled by SparqlErrorListener.java and SparqlQueryAnalyzer.java.

2. Escapes and Lexical Rules

  • Ensure SPARQL escape sequences in IRIs and string literals adhere to standard rules (syntax-esc-04.rq, syntax-esc-05.rq).

Test Execution and Verification

Unit tests in corese-core

mvn test -Dtest=SparqlParserValidationTest,SparqlParserTest

Unit test location: corese-core/src/test/java/fr/inria/corese/core/next/query/impl/sparql/parser/SparqlParserValidationTest.java

W3C test suite in corese-w3c

mvn test -Dtest=Sparql10DynamicTest
mvn test -Dtest=Sparql11DynamicTest

Test classes: Sparql10DynamicTest.java and Sparql11DynamicTest.java in corese-w3c.

Targeted W3C Test Cases

SPARQL 1.0 Syntax

  • syn-bad-UNION-breaks-BGP
  • syn-bad-OPT-breaks-BGP
  • syn-bad-GRAPH-breaks-BGP
  • syn-blabel-cross-union-bad
  • syn-blabel-cross-optional-bad
  • syn-blabel-cross-graph-bad
  • syn-bad-34.rq through syn-bad-38.rq
  • syntax-esc-04.rq, syntax-esc-05.rq

SPARQL 1.1 Negative Syntax

  • syntax-query/ negative tests (e.g. syntaxselectscope2, syntaxbindscope6)
  • syntax-update-1/ negative update syntax tests

Quality Standards

  • 0 SonarLint issues.
  • Cognitive complexity strictly below 15 for all methods.
  • Braces on all control statements.

Acceptance Criteria

  • Blank node label reuse across UNION, OPTIONAL, GRAPH, or distinct BGPs raises a syntax or semantic error.
  • Targeted W3C SPARQL syntax tests pass in corese-w3c.
  • Existing positive query syntax tests in corese-core pass without regression.
  • Zero SonarLint warnings and compliance with project quality thresholds.

Activity

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requeststandard featureIssue related to an implementation based on W3C standards

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions