1use crate::{
2 lexing::{self, Delimiter, Keyword, Punct, Token, Tokens},
3 BlankNode, Collection, Directive, Document, Iri, Lexer, Literal, Object, Objects,
4 PredicateObjects, RdfLiteral, Statement, Subject, Triples, Verb,
5};
6use decoded_char::DecodedChar;
7use locspan::Span;
8
9#[derive(Debug, thiserror::Error)]
11pub enum Unexpected {
12 #[error("unexpected token `{0}`")]
13 Token(Token),
14
15 #[error("unexpected end of file")]
16 EndOfFile,
17}
18
19impl From<Option<Token>> for Unexpected {
20 fn from(value: Option<Token>) -> Self {
21 match value {
22 Some(token) => Unexpected::Token(token),
23 None => Unexpected::EndOfFile,
24 }
25 }
26}
27
28#[derive(Debug, thiserror::Error)]
29pub enum Error<E> {
30 #[error(transparent)]
31 Lexer(E),
32
33 #[error(transparent)]
34 Unexpected(Unexpected),
35}
36
37pub type ParseError<E, M> = (Box<Error<E>>, M);
38
39pub trait Parse<M>: Sized {
40 #[allow(clippy::type_complexity)]
41 fn parse_with<L, F>(parser: &mut Parser<L, F>) -> Result<(Self, M), ParseError<L::Error, M>>
42 where
43 L: Tokens,
44 F: FnMut(Span) -> M,
45 {
46 match parser.next()? {
47 (Some(token), span) => Self::parse_from(parser, (token, span)),
48 (None, span) => Self::parse_empty::<L>(parser.build_metadata(span)),
49 }
50 }
51
52 #[allow(clippy::type_complexity)]
53 fn parse_from<L, F>(
54 parser: &mut Parser<L, F>,
55 token: (Token, Span),
56 ) -> Result<(Self, M), ParseError<L::Error, M>>
57 where
58 L: Tokens,
59 F: FnMut(Span) -> M;
60
61 #[allow(clippy::type_complexity)]
62 fn parse_empty<L>(meta: M) -> Result<(Self, M), ParseError<L::Error, M>>
63 where
64 L: Tokens,
65 {
66 Err((Box::new(Error::Unexpected(Unexpected::EndOfFile)), meta))
67 }
68
69 #[inline(always)]
70 fn parse<C, F, E>(
71 chars: C,
72 metadata_builder: F,
73 ) -> Result<(Self, M), ParseError<lexing::Error<E>, M>>
74 where
75 C: Iterator<Item = Result<DecodedChar, E>>,
76 F: FnMut(Span) -> M,
77 {
78 let mut parser = Parser::new(Lexer::new(chars), metadata_builder);
79 Self::parse_with(&mut parser)
80 }
81
82 #[inline(always)]
83 fn parse_infallible<C, F>(
84 chars: C,
85 metadata_builder: F,
86 ) -> Result<(Self, M), ParseError<lexing::Error, M>>
87 where
88 C: Iterator<Item = DecodedChar>,
89 F: FnMut(Span) -> M,
90 {
91 Self::parse(chars.map(Ok), metadata_builder)
92 }
93
94 #[inline(always)]
95 fn parse_utf8<C, F, E>(
96 chars: C,
97 metadata_builder: F,
98 ) -> Result<(Self, M), ParseError<lexing::Error<E>, M>>
99 where
100 C: Iterator<Item = Result<char, E>>,
101 F: FnMut(Span) -> M,
102 {
103 Self::parse(
104 decoded_char::FallibleUtf8Decoded::new(chars),
105 metadata_builder,
106 )
107 }
108
109 #[inline(always)]
110 fn parse_utf8_infallible<C, F>(
111 chars: C,
112 metadata_builder: F,
113 ) -> Result<(Self, M), ParseError<lexing::Error, M>>
114 where
115 C: Iterator<Item = char>,
116 F: FnMut(Span) -> M,
117 {
118 Self::parse_infallible(decoded_char::Utf8Decoded::new(chars), metadata_builder)
119 }
120
121 #[inline(always)]
122 fn parse_utf16<C, F, E>(
123 chars: C,
124 metadata_builder: F,
125 ) -> Result<(Self, M), ParseError<lexing::Error<E>, M>>
126 where
127 C: Iterator<Item = Result<char, E>>,
128 F: FnMut(Span) -> M,
129 {
130 Self::parse(
131 decoded_char::FallibleUtf16Decoded::new(chars),
132 metadata_builder,
133 )
134 }
135
136 #[inline(always)]
137 fn parse_utf16_infallible<C, F>(
138 chars: C,
139 metadata_builder: F,
140 ) -> Result<(Self, M), ParseError<lexing::Error, M>>
141 where
142 C: Iterator<Item = char>,
143 F: FnMut(Span) -> M,
144 {
145 Self::parse_infallible(decoded_char::Utf16Decoded::new(chars), metadata_builder)
146 }
147
148 #[inline(always)]
149 fn parse_str<F>(
150 string: &str,
151 metadata_builder: F,
152 ) -> Result<(Self, M), ParseError<lexing::Error, M>>
153 where
154 F: FnMut(Span) -> M,
155 {
156 Self::parse_utf8_infallible(string.chars(), metadata_builder)
157 }
158}
159
160pub struct Parser<L, F> {
161 lexer: L,
162 metadata_builder: F,
163}
164
165impl<L, F> Parser<L, F> {
166 pub fn new(lexer: L, metadata_builder: F) -> Self {
167 Self {
168 lexer,
169 metadata_builder,
170 }
171 }
172}
173
174impl<L: Tokens, F: FnMut(Span) -> M, M> Parser<L, F> {
175 fn next(&mut self) -> Result<(Option<Token>, Span), ParseError<L::Error, M>> {
176 self.lexer
177 .next()
178 .map_err(|(e, span)| (Box::new(Error::Lexer(e)), (self.metadata_builder)(span)))
179 }
180
181 #[allow(clippy::type_complexity)]
182 fn peek(&mut self) -> Result<(Option<&Token>, Span), ParseError<L::Error, M>> {
183 self.lexer
184 .peek()
185 .map_err(|(e, span)| (Box::new(Error::Lexer(e)), (self.metadata_builder)(span)))
186 }
187
188 fn last_span(&self) -> Span {
189 self.lexer.last()
190 }
191
192 fn build_metadata(&mut self, span: Span) -> M {
193 (self.metadata_builder)(span)
194 }
195}
196
197impl<M> Parse<M> for Document<M> {
198 fn parse_from<L, F>(
199 parser: &mut Parser<L, F>,
200 (token, mut span): (Token, Span),
201 ) -> Result<(Self, M), ParseError<L::Error, M>>
202 where
203 L: Tokens,
204 F: FnMut(Span) -> M,
205 {
206 let mut result = Document::default();
207 result.insert(Statement::parse_from(parser, (token, span))?);
208
209 while let (Some(token), span) = parser.next()? {
210 result.insert(Statement::parse_from(parser, (token, span))?)
211 }
212
213 span.append(parser.last_span());
214 Ok((result, parser.build_metadata(span)))
215 }
216
217 fn parse_empty<L>(meta: M) -> Result<(Self, M), ParseError<L::Error, M>>
218 where
219 L: Tokens,
220 {
221 Ok((Self::new(), meta))
222 }
223}
224
225impl<M> Parse<M> for Directive<M> {
226 fn parse_from<L, F>(
227 parser: &mut Parser<L, F>,
228 (token, mut span): (Token, Span),
229 ) -> Result<(Self, M), ParseError<L::Error, M>>
230 where
231 L: Tokens,
232 F: FnMut(Span) -> M,
233 {
234 match token {
235 Token::Keyword(Keyword::Prefix) => match parser.next()? {
236 (Some(Token::CompactIri((namespace, ns_span), (suffix, suffix_span))), _) => {
237 if suffix.is_empty() {
238 match parser.next()? {
239 (Some(Token::IriRef(iri_ref)), iri_ref_span) => match parser.next()? {
240 (Some(Token::Punct(Punct::Period)), dot_span) => {
241 span.append(dot_span);
242 Ok((
243 Directive::Prefix(
244 (namespace, parser.build_metadata(ns_span)),
245 (iri_ref, parser.build_metadata(iri_ref_span)),
246 ),
247 parser.build_metadata(span),
248 ))
249 }
250 (unexpected, span) => Err((
251 Box::new(Error::Unexpected(unexpected.into())),
252 parser.build_metadata(span),
253 )),
254 },
255 (unexpected, span) => Err((
256 Box::new(Error::Unexpected(unexpected.into())),
257 parser.build_metadata(span),
258 )),
259 }
260 } else {
261 Err((
262 Box::new(Error::Unexpected(
263 Some(Token::CompactIri(
264 (namespace, ns_span),
265 (suffix, suffix_span),
266 ))
267 .into(),
268 )),
269 parser.build_metadata(span),
270 ))
271 }
272 }
273 (unexpected, span) => Err((
274 Box::new(Error::Unexpected(unexpected.into())),
275 parser.build_metadata(span),
276 )),
277 },
278 Token::Keyword(Keyword::Base) => match parser.next()? {
279 (Some(Token::IriRef(iri_ref)), iri_ref_span) => match parser.next()? {
280 (Some(Token::Punct(Punct::Period)), dot_span) => {
281 span.append(dot_span);
282 Ok((
283 Directive::Base((iri_ref, parser.build_metadata(iri_ref_span))),
284 parser.build_metadata(span),
285 ))
286 }
287 (unexpected, span) => Err((
288 Box::new(Error::Unexpected(unexpected.into())),
289 parser.build_metadata(span),
290 )),
291 },
292 (unexpected, span) => Err((
293 Box::new(Error::Unexpected(unexpected.into())),
294 parser.build_metadata(span),
295 )),
296 },
297 Token::Keyword(Keyword::SparqlPrefix) => match parser.next()? {
298 (Some(Token::CompactIri((namespace, ns_span), (suffix, suffix_span))), _) => {
299 if suffix.is_empty() {
300 match parser.next()? {
301 (Some(Token::IriRef(iri_ref)), iri_ref_span) => {
302 span.append(iri_ref_span);
303 Ok((
304 Directive::SparqlPrefix(
305 (namespace, parser.build_metadata(ns_span)),
306 (iri_ref, parser.build_metadata(iri_ref_span)),
307 ),
308 parser.build_metadata(span),
309 ))
310 }
311 (unexpected, span) => Err((
312 Box::new(Error::Unexpected(unexpected.into())),
313 parser.build_metadata(span),
314 )),
315 }
316 } else {
317 Err((
318 Box::new(Error::Unexpected(
319 Some(Token::CompactIri(
320 (namespace, ns_span),
321 (suffix, suffix_span),
322 ))
323 .into(),
324 )),
325 parser.build_metadata(span),
326 ))
327 }
328 }
329 (unexpected, span) => Err((
330 Box::new(Error::Unexpected(unexpected.into())),
331 parser.build_metadata(span),
332 )),
333 },
334 Token::Keyword(Keyword::SparqlBase) => match parser.next()? {
335 (Some(Token::IriRef(iri_ref)), iri_ref_span) => {
336 span.append(iri_ref_span);
337 Ok((
338 Directive::SparqlBase((iri_ref, parser.build_metadata(iri_ref_span))),
339 parser.build_metadata(span),
340 ))
341 }
342 (unexpected, span) => Err((
343 Box::new(Error::Unexpected(unexpected.into())),
344 parser.build_metadata(span),
345 )),
346 },
347 unexpected => Err((
348 Box::new(Error::Unexpected(Unexpected::Token(unexpected))),
349 parser.build_metadata(span),
350 )),
351 }
352 }
353}
354
355impl<M> Parse<M> for Statement<M> {
356 fn parse_from<L, F>(
357 parser: &mut Parser<L, F>,
358 (token, span): (Token, Span),
359 ) -> Result<(Self, M), ParseError<L::Error, M>>
360 where
361 L: Tokens,
362 F: FnMut(Span) -> M,
363 {
364 match token {
365 token @ Token::Keyword(
366 Keyword::Prefix | Keyword::Base | Keyword::SparqlPrefix | Keyword::SparqlBase,
367 ) => {
368 let (directive, meta) = Directive::parse_from(parser, (token, span))?;
369 Ok((Self::Directive(directive), meta))
370 }
371 token => {
372 let (triples, meta) = Triples::parse_from(parser, (token, span))?;
373 Ok((Self::Triples(triples), meta))
374 }
375 }
376 }
377}
378
379impl<M> Parse<M> for Triples<M> {
380 fn parse_from<L, F>(
381 parser: &mut Parser<L, F>,
382 (token, mut span): (Token, Span),
383 ) -> Result<(Self, M), ParseError<L::Error, M>>
384 where
385 L: Tokens,
386 F: FnMut(Span) -> M,
387 {
388 let subject = Subject::parse_from(parser, (token, span))?;
389
390 let po_list = match parser.peek()? {
391 (Some(Token::Punct(Punct::Period)), p_span) => {
392 if !matches!(&subject, (Subject::BlankNode(BlankNode::Anonymous(l)), _) if !l.0.is_empty())
393 {
394 return Err((
395 Box::new(Error::Unexpected(Unexpected::Token(Token::Punct(
396 Punct::Period,
397 )))),
398 parser.build_metadata(p_span),
399 ));
400 }
401
402 let span = parser.last_span().next();
403 (Vec::new(), parser.build_metadata(span))
404 }
405 _ => Vec::parse_with(parser)?,
406 };
407
408 span.append(parser.last_span());
409
410 match parser.next()? {
411 (Some(Token::Punct(Punct::Period)), _) => (),
412 (unexpected, span) => {
413 return Err((
414 Box::new(Error::Unexpected(unexpected.into())),
415 parser.build_metadata(span),
416 ));
417 }
418 }
419
420 Ok((
421 Triples {
422 subject,
423 predicate_objects_list: po_list,
424 },
425 parser.build_metadata(span),
426 ))
427 }
428}
429
430impl<M> Parse<M> for Vec<(PredicateObjects<M>, M)> {
431 fn parse_from<L, F>(
432 parser: &mut Parser<L, F>,
433 (token, span): (Token, Span),
434 ) -> Result<(Self, M), ParseError<L::Error, M>>
435 where
436 L: Tokens,
437 F: FnMut(Span) -> M,
438 {
439 let mut result = vec![PredicateObjects::parse_from(parser, (token, span))?];
440
441 loop {
442 match parser.peek()? {
443 (Some(Token::Punct(Punct::Semicolon)), _) => {
444 parser.next()?;
445
446 match parser.peek()? {
447 (Some(Token::Punct(Punct::Period) | Token::End(Delimiter::Bracket)), _) => {
448 break
449 }
450 _ => result.push(PredicateObjects::parse_with(parser)?),
451 }
452 }
453 (Some(Token::Punct(Punct::Period) | Token::End(Delimiter::Bracket)), _) => break,
454 _ => {
455 let (unexpected, span) = parser.next()?;
456 return Err((
457 Box::new(Error::Unexpected(unexpected.into())),
458 parser.build_metadata(span),
459 ));
460 }
461 }
462 }
463
464 Ok((result, parser.build_metadata(span)))
465 }
466}
467
468impl<M> Parse<M> for PredicateObjects<M> {
469 fn parse_from<L, F>(
470 parser: &mut Parser<L, F>,
471 (token, mut span): (Token, Span),
472 ) -> Result<(Self, M), ParseError<L::Error, M>>
473 where
474 L: Tokens,
475 F: FnMut(Span) -> M,
476 {
477 let verb = Verb::parse_from(parser, (token, span))?;
478 let objects = Objects::parse_with(parser)?;
479 span.append(parser.last_span());
480 Ok((Self { verb, objects }, parser.build_metadata(span)))
481 }
482}
483
484impl<M> Parse<M> for Objects<M> {
485 fn parse_from<L, F>(
486 parser: &mut Parser<L, F>,
487 (token, span): (Token, Span),
488 ) -> Result<(Self, M), ParseError<L::Error, M>>
489 where
490 L: Tokens,
491 F: FnMut(Span) -> M,
492 {
493 let mut result = vec![Object::parse_from(parser, (token, span))?];
494
495 loop {
496 match parser.peek()? {
497 (Some(Token::Punct(Punct::Comma)), _) => {
498 parser.next()?;
499 result.push(Object::parse_with(parser)?);
500 }
501 (
502 Some(
503 Token::Punct(Punct::Period | Punct::Semicolon)
504 | Token::End(Delimiter::Bracket),
505 ),
506 _,
507 ) => break,
508 _ => {
509 let (unexpected, span) = parser.next()?;
510 return Err((
511 Box::new(Error::Unexpected(unexpected.into())),
512 parser.build_metadata(span),
513 ));
514 }
515 }
516 }
517
518 Ok((Self(result), parser.build_metadata(span)))
519 }
520}
521
522fn compact_iri<M, L, F>(
523 parser: &mut Parser<L, F>,
524 (prefix, prefix_span): (String, Span),
525 (suffix, suffix_span): (String, Span),
526) -> Iri<M>
527where
528 L: Tokens,
529 F: FnMut(Span) -> M,
530{
531 Iri::PrefixedName(
532 (prefix, parser.build_metadata(prefix_span)),
533 (suffix, parser.build_metadata(suffix_span)),
534 )
535}
536
537impl<M> Parse<M> for Subject<M> {
538 fn parse_from<L, F>(
539 parser: &mut Parser<L, F>,
540 (token, mut span): (Token, Span),
541 ) -> Result<(Self, M), ParseError<L::Error, M>>
542 where
543 L: Tokens,
544 F: FnMut(Span) -> M,
545 {
546 match token {
547 Token::IriRef(iri_ref) => Ok((
548 Subject::Iri(Iri::IriRef(iri_ref)),
549 parser.build_metadata(span),
550 )),
551 Token::CompactIri(prefix, suffix) => Ok((
552 Subject::Iri(compact_iri(parser, prefix, suffix)),
553 parser.build_metadata(span),
554 )),
555 Token::BlankNodeLabel(label) => Ok((
556 Subject::BlankNode(BlankNode::Label(label)),
557 parser.build_metadata(span),
558 )),
559 Token::Begin(Delimiter::Bracket) => {
560 let po_list = match parser.peek()? {
561 (Some(Token::End(Delimiter::Bracket)), _) => {
562 let span = parser.last_span().next();
563 (Vec::new(), parser.build_metadata(span))
564 }
565 _ => Vec::parse_with(parser)?,
566 };
567
568 match parser.next()? {
569 (Some(Token::End(Delimiter::Bracket)), _) => (),
570 (unexpected, span) => {
571 return Err((
572 Box::new(Error::Unexpected(unexpected.into())),
573 parser.build_metadata(span),
574 ));
575 }
576 }
577
578 span.append(parser.last_span());
579 Ok((
580 Subject::BlankNode(BlankNode::Anonymous(po_list)),
581 parser.build_metadata(span),
582 ))
583 }
584 Token::Begin(Delimiter::Parenthesis) => {
585 let (objects, meta) = Collection::parse_from(parser, (token, span))?;
586 Ok((Subject::Collection(objects), meta))
587 }
588 unexpected => Err((
589 Box::new(Error::Unexpected(Unexpected::Token(unexpected))),
590 parser.build_metadata(span),
591 )),
592 }
593 }
594}
595
596impl<M> Parse<M> for Collection<M> {
597 fn parse_from<L, F>(
598 parser: &mut Parser<L, F>,
599 (token, mut span): (Token, Span),
600 ) -> Result<(Self, M), ParseError<L::Error, M>>
601 where
602 L: Tokens,
603 F: FnMut(Span) -> M,
604 {
605 match token {
606 Token::Begin(Delimiter::Parenthesis) => {
607 let mut objects = Vec::new();
608
609 loop {
610 match parser.next()? {
611 (Some(Token::End(Delimiter::Parenthesis)), end_span) => {
612 span.append(end_span);
613 break;
614 }
615 (Some(token), span) => {
616 let object = Object::parse_from(parser, (token, span))?;
617 objects.push(object)
618 }
619 (unexpected, span) => {
620 return Err((
621 Box::new(Error::Unexpected(unexpected.into())),
622 parser.build_metadata(span),
623 ))
624 }
625 }
626 }
627
628 Ok((Collection(objects), parser.build_metadata(span)))
629 }
630 unexpected => Err((
631 Box::new(Error::Unexpected(Unexpected::Token(unexpected))),
632 parser.build_metadata(span),
633 )),
634 }
635 }
636}
637
638impl<M> Parse<M> for Object<M> {
639 fn parse_from<L, F>(
640 parser: &mut Parser<L, F>,
641 (token, mut span): (Token, Span),
642 ) -> Result<(Self, M), ParseError<L::Error, M>>
643 where
644 L: Tokens,
645 F: FnMut(Span) -> M,
646 {
647 match token {
648 Token::IriRef(iri_ref) => Ok((
649 Object::Iri(Iri::IriRef(iri_ref)),
650 parser.build_metadata(span),
651 )),
652 Token::CompactIri(prefix, suffix) => Ok((
653 Object::Iri(compact_iri(parser, prefix, suffix)),
654 parser.build_metadata(span),
655 )),
656 Token::BlankNodeLabel(label) => Ok((
657 Object::BlankNode(BlankNode::Label(label)),
658 parser.build_metadata(span),
659 )),
660 Token::Begin(Delimiter::Bracket) => {
661 let po_list = match parser.peek()? {
662 (Some(Token::End(Delimiter::Bracket)), _) => {
663 let span = parser.last_span().next();
664 (Vec::new(), parser.build_metadata(span))
665 }
666 _ => Vec::parse_with(parser)?,
667 };
668
669 match parser.next()? {
670 (Some(Token::End(Delimiter::Bracket)), _) => (),
671 (unexpected, span) => {
672 return Err((
673 Box::new(Error::Unexpected(unexpected.into())),
674 parser.build_metadata(span),
675 ));
676 }
677 }
678
679 span.append(parser.last_span());
680 Ok((
681 Object::BlankNode(BlankNode::Anonymous(po_list)),
682 parser.build_metadata(span),
683 ))
684 }
685 Token::Begin(Delimiter::Parenthesis) => {
686 let (objects, meta) = Collection::parse_from(parser, (token, span))?;
687 Ok((Object::Collection(objects), meta))
688 }
689 token => {
690 let (literal, meta) = Literal::parse_from(parser, (token, span))?;
691 Ok((Object::Literal(literal), meta))
692 }
693 }
694 }
695}
696
697const XSD_STRING: &iref::Iri = static_iref::iri!("http://www.w3.org/2001/XMLSchema#string");
698
699#[allow(clippy::type_complexity)]
700fn parse_rdf_literal<M, L, F>(
701 parser: &mut Parser<L, F>,
702 (string, string_span): (String, Span),
703) -> Result<(RdfLiteral<M>, M), ParseError<L::Error, M>>
704where
705 L: Tokens,
706 F: FnMut(Span) -> M,
707{
708 match parser.peek()? {
709 (Some(Token::LangTag(_)), tag_span) => {
710 let tag = match parser.next()? {
711 (Some(Token::LangTag(tag)), _) => tag,
712 _ => panic!("expected lang tag"),
713 };
714
715 let span = string_span.union(tag_span);
716 Ok((
717 RdfLiteral::new(
718 (string, parser.build_metadata(string_span)),
719 (
720 rdf_types::LiteralType::LangString(tag),
721 parser.build_metadata(tag_span),
722 ),
723 ),
724 parser.build_metadata(span),
725 ))
726 }
727 (Some(Token::Punct(Punct::Carets)), _) => {
728 parser.next()?;
729 let (type_iri, metadata) = Iri::parse_with(parser)?;
730 let literal_type = (rdf_types::LiteralType::Any(type_iri), metadata);
731 let span = string_span.union(parser.last_span());
732 Ok((
733 RdfLiteral::new((string, parser.build_metadata(string_span)), literal_type),
734 parser.build_metadata(span),
735 ))
736 }
737 _ => {
738 let ty = (
739 rdf_types::LiteralType::Any(Iri::IriRef(XSD_STRING.to_owned().into())),
740 parser.build_metadata(string_span),
741 );
742 Ok((
743 RdfLiteral::new((string, parser.build_metadata(string_span)), ty),
744 parser.build_metadata(string_span),
745 ))
746 }
747 }
748}
749
750impl<M> Parse<M> for Literal<M> {
751 fn parse_from<L, F>(
752 parser: &mut Parser<L, F>,
753 (token, span): (Token, Span),
754 ) -> Result<(Self, M), ParseError<L::Error, M>>
755 where
756 L: Tokens,
757 F: FnMut(Span) -> M,
758 {
759 match token {
760 Token::StringLiteral(string) => {
761 let (lit, loc) = parse_rdf_literal(parser, (string, span))?;
762 Ok((Literal::Rdf(lit), loc))
763 }
764 Token::Numeric(n) => Ok((Literal::Numeric(n), parser.build_metadata(span))),
765 Token::Keyword(Keyword::True) => {
766 Ok((Literal::Boolean(true), parser.build_metadata(span)))
767 }
768 Token::Keyword(Keyword::False) => {
769 Ok((Literal::Boolean(false), parser.build_metadata(span)))
770 }
771 unexpected => Err((
772 Box::new(Error::Unexpected(Unexpected::Token(unexpected))),
773 parser.build_metadata(span),
774 )),
775 }
776 }
777}
778
779impl<M> Parse<M> for Verb<M> {
780 fn parse_from<L, F>(
781 parser: &mut Parser<L, F>,
782 (token, span): (Token, Span),
783 ) -> Result<(Self, M), ParseError<L::Error, M>>
784 where
785 L: Tokens,
786 F: FnMut(Span) -> M,
787 {
788 match token {
789 Token::Keyword(Keyword::A) => Ok((Verb::A, parser.build_metadata(span))),
790 token => {
791 let (iri, meta) = Iri::parse_from(parser, (token, span))?;
792 Ok((Verb::Predicate(iri), meta))
793 }
794 }
795 }
796}
797
798impl<M> Parse<M> for Iri<M> {
799 fn parse_from<L, F>(
800 parser: &mut Parser<L, F>,
801 (token, span): (Token, Span),
802 ) -> Result<(Self, M), ParseError<L::Error, M>>
803 where
804 L: Tokens,
805 F: FnMut(Span) -> M,
806 {
807 match token {
808 Token::IriRef(iri_ref) => Ok((Iri::IriRef(iri_ref), parser.build_metadata(span))),
809 Token::CompactIri(prefix, suffix) => Ok((
810 compact_iri(parser, prefix, suffix),
811 parser.build_metadata(span),
812 )),
813 unexpected => Err((
814 Box::new(Error::Unexpected(Unexpected::Token(unexpected))),
815 parser.build_metadata(span),
816 )),
817 }
818 }
819}