Skip to content

Commit

Permalink
Consolidate N-Triples and N-Triples Star grammars. Use LANGDIR termin…
Browse files Browse the repository at this point in the history
…al instead of LANGTAG.
  • Loading branch information
gkellogg committed Jul 15, 2023
1 parent 16c4086 commit 31a02d8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
50 changes: 22 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ This is a pure-Ruby library for working with [Resource Description Framework

1. [Features](#features)
2. [Differences between RDF 1.0 and RDF 1.1](#differences-between-rdf-1-0-and-rdf-1-1)
3. [Tutorials](#tutorials)
4. [Command Line](#command-line)
5. [Examples](#examples)
6. [Reader/Writer convenience methods](#reader/writer-convenience-methods)
7. [RDF* (RDFStar)](#rdf*-(rdfstar))
8. [Documentation](#documentation)
9. [Dependencies](#dependencies)
10. [Installation](#installation)
11. [Download](#download)
12. [Resources](#resources)
13. [Mailing List](#mailing-list)
14. [Authors](#authors)
15. [Contributors](#contributors)
16. [Contributing](#contributing)
17. [License](#license)
3. [Differences between RDF 1.1 and RDF 1.2](#differences-between-rdf-1-1-and-rdf-1-2)
4. [Tutorials](#tutorials)
5. [Command Line](#command-line)
6. [Examples](#examples)
7. [Reader/Writer convenience methods](#reader/writer-convenience-methods)
8. [RDF 1.2](#rdf\_12)
9. [Documentation](#documentation)
10. [Dependencies](#dependencies)
11. [Installation](#installation)
12. [Download](#download)
13. [Resources](#resources)
14. [Mailing List](#mailing-list)
15. [Authors](#authors)
16. [Contributors](#contributors)
17. [Contributing](#contributing)
18. [License](#license)

## Features

* 100% pure Ruby with minimal dependencies and no bloat.
* Fully compatible with [RDF 1.1][] specifications.
* Provisional support for [RDF 1.2][] specifications.
* 100% free and unencumbered [public domain](https://unlicense.org/) software.
* Provides a clean, well-designed RDF object model and related APIs.
* Supports parsing and serializing [N-Triples][] and [N-Quads][] out of the box, with more
Expand All @@ -49,7 +51,6 @@ This is a pure-Ruby library for working with [Resource Description Framework
* Note, changes in mapping hashes to keyword arguments for Ruby 2.7+ may require that arguments be passed more explicitly, especially when the first argument is a Hash and there are optional keyword arguments. In this case, Hash argument may need to be explicitly included within `{}` and the optional keyword arguments may need to be specified using `**{}` if there are no keyword arguments.
* Performs auto-detection of input to select appropriate Reader class if one
cannot be determined from file characteristics.
* Provisional support for [RDF*][].

### HTTP requests

Expand Down Expand Up @@ -263,23 +264,24 @@ A separate [SPARQL][SPARQL doc] gem builds on basic BGP support to provide full
foaf[:name] #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
foaf['mbox'] #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")

## RDF* (RDFStar)
## RDF 1.2

[RDF.rb][] includes provisional support for [RDF*][] with an N-Triples/N-Quads syntax extension that uses inline statements in the _subject_ or _object_ position.
[RDF.rb][] includes provisional support for [RDF 1.2][] with an N-Triples/N-Quads syntax for quoted triples in the _subject_ or _object_ position.
[RDF.rb][] includes provisional support for [RDF 1.2][] directional language-tagged strings, which are literals of type `rdf:dirLangString` having both a `language` and `direction`.

Internally, an `RDF::Statement` is treated as another resource, along with `RDF::URI` and `RDF::Node`, which allows an `RDF::Statement` to have a `#subject` or `#object` which is also an `RDF::Statement`.

**Note: This feature is subject to change or elimination as the standards process progresses.**

### Serializing a Graph containing embedded statements
### Serializing a Graph containing quoted triples

require 'rdf/ntriples'
statement = RDF::Statement(RDF::URI('bob'), RDF::Vocab::FOAF.age, RDF::Literal(23))
graph = RDF::Graph.new << [statement, RDF::URI("ex:certainty"), RDF::Literal(0.9)]
graph.dump(:ntriples, validate: false)
# => '<<<bob> <http://xmlns.com/foaf/0.1/age> "23"^^<http://www.w3.org/2001/XMLSchema#integer>>> <ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#double> .'

### Reading a Graph containing embedded statements
### Reading a Graph containing quoted triples

By default, the N-Triples reader will reject a document containing a subject resource.

Expand All @@ -289,13 +291,6 @@ By default, the N-Triples reader will reject a document containing a subject res
end
# => RDF::ReaderError

Readers support a boolean valued `rdfstar` option.

graph = RDF::Graph.new do |graph|
RDF::NTriples::Reader.new(nt, rdfstar: true) {|reader| graph << reader}
end
graph.count #=> 1

## Documentation

<https://ruby-rdf.github.io/rdf>
Expand Down Expand Up @@ -517,7 +512,6 @@ see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
[RDF::TriX]: https://ruby-rdf.github.io/rdf-trix
[RDF::Turtle]: https://ruby-rdf.github.io/rdf-turtle
[RDF::Raptor]: https://ruby-rdf.github.io/rdf-raptor
[RDF*]: https://w3c.github.io/rdf-star/rdf-star-cg-spec.html
[LinkedData]: https://ruby-rdf.github.io/linkeddata
[JSON::LD]: https://ruby-rdf.github.io/json-ld
[RestClient]: https://rubygems.org/gems/rest-client
Expand Down
7 changes: 0 additions & 7 deletions etc/n-triples-star.ebnf

This file was deleted.

4 changes: 2 additions & 2 deletions etc/n-triples.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ triple ::= subject predicate object '.'
subject ::= IRIREF | BLANK_NODE_LABEL | quotedTriple
predicate ::= IRIREF
object ::= IRIREF | BLANK_NODE_LABEL | literal | quotedTriple
literal ::= STRING_LITERAL_QUOTE ('^^' IRIREF | LANGTAG )?
literal ::= STRING_LITERAL_QUOTE ('^^' IRIREF | LANGDIR )?
quotedTriple ::= '<<' subject predicate object '>>'

@terminals

IRIREF ::= '<' ([^#x00-#x20<>"{}|^`\] | UCHAR)* '>'
BLANK_NODE_LABEL ::= '_:' ( PN_CHARS_U | [0-9] ) ((PN_CHARS|'.')* PN_CHARS)?
LANGTAG ::= "@" [a-zA-Z]+ ( "-" [a-zA-Z0-9]+ )* ('--' ('ltr'|'rtl'))?`
LANGDIR ::= "@" [a-zA-Z]+ ( "-" [a-zA-Z0-9]+ )* ('--' ('ltr'|'rtl'))?`
STRING_LITERAL_QUOTE ::= '"' ( [^#x22#x5C#xA#xD] | ECHAR | UCHAR )* '"'
UCHAR ::= ( "\u" HEX HEX HEX HEX )
| ( "\U" HEX HEX HEX HEX HEX HEX HEX HEX )
Expand Down
9 changes: 6 additions & 3 deletions lib/rdf/ntriples/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Reader < RDF::Reader

IRIREF = /<((?:#{IRI_RANGE}|#{UCHAR})*)>/.freeze
BLANK_NODE_LABEL = /_:((?:[0-9]|#{PN_CHARS_U})(?:(?:#{PN_CHARS}|\.)*#{PN_CHARS})?)/.freeze
LANGTAG = /@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*(?:--[a-zA-Z0-9]+)?)/.freeze
LANGDIR = /@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*(?:--[a-zA-Z0-9]+)?)/.freeze
STRING_LITERAL_QUOTE = /"((?:[^\"\\\n\r]|#{ECHAR}|#{UCHAR})*)"/.freeze

ST_START = /^<</.freeze
Expand All @@ -78,7 +78,7 @@ class Reader < RDF::Reader
NODEID = /^#{BLANK_NODE_LABEL}/.freeze
URIREF = /^#{IRIREF}/.freeze
LITERAL_PLAIN = /^#{STRING_LITERAL_QUOTE}/.freeze
LITERAL_WITH_LANGUAGE = /^#{STRING_LITERAL_QUOTE}#{LANGTAG}/.freeze
LITERAL_WITH_LANGUAGE = /^#{STRING_LITERAL_QUOTE}#{LANGDIR}/.freeze
LITERAL_WITH_DATATYPE = /^#{STRING_LITERAL_QUOTE}\^\^#{IRIREF}/.freeze
DATATYPE_URI = /^\^\^#{IRIREF}/.freeze
LITERAL = Regexp.union(LITERAL_WITH_LANGUAGE, LITERAL_WITH_DATATYPE, LITERAL_PLAIN).freeze
Expand All @@ -87,6 +87,9 @@ class Reader < RDF::Reader
OBJECT = Regexp.union(URIREF, NODEID, LITERAL).freeze
END_OF_STATEMENT = /^\s*\.\s*(?:#.*)?$/.freeze

# LANGTAG is deprecated
LANGTAG = LANGDIR

##
# Reconstructs an RDF value from its serialized N-Triples
# representation.
Expand Down Expand Up @@ -291,7 +294,7 @@ def read_literal
if literal_str = match(LITERAL_PLAIN)
literal_str = self.class.unescape(literal_str)
literal = case
when lang_dir = match(LANGTAG)
when lang_dir = match(LANGDIR)
language, direction = lang_dir.split('--')
RDF::Literal.new(literal_str, language: language, direction: direction)
when datatype = match(/^(\^\^)/) # FIXME
Expand Down

0 comments on commit 31a02d8

Please sign in to comment.