turtle_syntax_next/
ast.rs

1//! Syntax elements of Turtle.
2use std::fmt;
3
4use iref::IriRefBuf;
5pub use rdf_types::{BlankId, BlankIdBuf, LiteralType};
6pub use xsd_types::lexical::{DecimalBuf, DoubleBuf, IntegerBuf};
7
8/// An IRI or compact IRI.
9#[derive(Clone, Debug)]
10pub enum Iri<M> {
11	IriRef(IriRefBuf),
12	PrefixedName((String, M), (String, M)),
13}
14
15/// A Turtle document.
16#[derive(Clone, Debug)]
17pub struct Document<M> {
18	pub statements: Vec<(Statement<M>, M)>,
19}
20
21impl<M> Default for Document<M> {
22	fn default() -> Self {
23		Self {
24			statements: Vec::new(),
25		}
26	}
27}
28
29impl<M> Document<M> {
30	pub fn new() -> Document<M> {
31		Self::default()
32	}
33
34	pub fn insert(&mut self, statement: (Statement<M>, M)) {
35		self.statements.push(statement)
36	}
37}
38
39/// A statement (directive of triples declaration).
40#[derive(Clone, Debug)]
41pub enum Statement<M> {
42	/// Directive.
43	Directive(Directive<M>),
44
45	/// Triples declaration.
46	Triples(Triples<M>),
47}
48
49#[derive(Clone, Debug)]
50pub struct Triples<M> {
51	pub subject: (Subject<M>, M),
52	pub predicate_objects_list: (PredicateObjectsList<M>, M),
53}
54
55pub type PredicateObjectsList<M> = Vec<(PredicateObjects<M>, M)>;
56
57/// A directive.
58#[derive(Clone, Debug)]
59pub enum Directive<M> {
60	/// `@prefix` directive.
61	Prefix((String, M), (IriRefBuf, M)),
62
63	/// `@base` directive.
64	Base((IriRefBuf, M)),
65
66	/// SPARQL `PREFIX` directive.
67	SparqlPrefix((String, M), (IriRefBuf, M)),
68
69	/// SPARQL `BASE` directive.
70	SparqlBase((IriRefBuf, M)),
71}
72
73/// Verb (either `a` or a predicate).
74#[derive(Clone, Debug)]
75pub enum Verb<M> {
76	/// `a` keyword.
77	A,
78
79	/// Predicate.
80	Predicate(Iri<M>),
81}
82
83/// Subject of a triples declaration.
84#[derive(Clone, Debug)]
85pub enum Subject<M> {
86	/// IRI or compact IRI.
87	Iri(Iri<M>),
88
89	/// Blank node.
90	BlankNode(BlankNode<M>),
91
92	/// Collection of subjects.
93	Collection(Collection<M>),
94}
95
96/// Collection of objects.
97#[derive(Clone, Debug)]
98pub struct Collection<M>(pub Vec<(Object<M>, M)>);
99
100#[derive(Clone, Debug)]
101pub enum BlankNode<M> {
102	Label(BlankIdBuf),
103	Anonymous((BlankNodePropertyList<M>, M)),
104}
105
106pub type BlankNodePropertyList<M> = PredicateObjectsList<M>;
107
108/// Object of a triples declaration.
109#[derive(Clone, Debug)]
110pub enum Object<M> {
111	/// IRI or compact IRI.
112	Iri(Iri<M>),
113
114	/// Blank node.
115	BlankNode(BlankNode<M>),
116
117	/// Collection of objects.
118	Collection(Collection<M>),
119
120	/// Literal value.
121	Literal(Literal<M>),
122}
123
124#[derive(Clone, Debug)]
125pub struct PredicateObjects<M> {
126	pub verb: (Verb<M>, M),
127	pub objects: (Objects<M>, M),
128}
129
130/// Non empty list of objects.
131#[derive(Clone, Debug)]
132pub struct Objects<M>(pub Vec<(Object<M>, M)>);
133
134/// Literal value.
135#[derive(Clone, Debug)]
136pub enum Literal<M> {
137	/// RDF literal.
138	Rdf(RdfLiteral<M>),
139
140	/// Numeric literal.
141	Numeric(NumericLiteral),
142
143	/// Boolean literal.
144	Boolean(bool),
145}
146
147/// Replacment for [rdf_types::Literal]
148#[derive(Clone, Debug)]
149pub struct RdfLiteral<M, T = Iri<M>> {
150	pub value: (String, M),
151
152	pub type_: (LiteralType<T>, M),
153}
154
155impl<M, T> RdfLiteral<M, T> {
156	pub fn new(value: (String, M), type_: (LiteralType<T>, M)) -> Self {
157		Self { value, type_ }
158	}
159}
160
161impl<M, T> From<RdfLiteral<M, T>> for rdf_types::Literal<T> {
162	fn from(value: RdfLiteral<M, T>) -> Self {
163		rdf_types::Literal {
164			value: value.value.0,
165			type_: value.type_.0,
166		}
167	}
168}
169
170/// Numeric literal value.
171#[derive(Clone, Debug)]
172pub enum NumericLiteral {
173	Integer(IntegerBuf),
174	Decimal(DecimalBuf),
175	Double(DoubleBuf),
176}
177
178impl fmt::Display for NumericLiteral {
179	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180		match self {
181			Self::Integer(i) => i.fmt(f),
182			Self::Decimal(d) => d.fmt(f),
183			Self::Double(d) => d.fmt(f),
184		}
185	}
186}