Turtle Syntax
A RDF Turtle parser for Rust based on source-span
.
Basic usage
Elements of the Turtle abstract syntax tree (implementing Parsable
)
are parsed using a Token
iterator
(a.k.a a lexer). A Lexer
can be created from a char
iterator
(here using utf8_decode::UnsafeDecoder
).
We can then use the Document::parse
function to parse a Turtle document from
the lexer.
use ;
// Open the file.
let file = open?;
// Create a character stream out of it.
let chars = new;
// Create a Turtle token stream (lexer).
let mut lexer = new.peekable;
// Parse the Turtle document.
let doc = parse?;
Every node of the resulting abstract syntax tree is tagged with its position
(span) in the source text using source_span::Loc
.
This can be used to report eventual syntax error in a human readable way.
match parse
The above code will have the following kind of output when a syntax error is detected:
parse error: invalid iri `http://www.w3.org/TR/rdf-syntax- grammar`
. |
3 | @prefix ex: <http://example.org/stuff/1.0/> .
4 |
5 | <http://www.w3.org/TR/rdf-syntax- grammar>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.