Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser and serializer for the OWL2 Functional-style syntax #68

Merged
merged 8 commits into from
Apr 19, 2024
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ log = {version="0.4.8"}
oxiri="0.2.2"
quick-xml={version="0.26.0"}
indexmap={workspace=true}
pest = "2.7.8"
pest_derive = "2.7.8"
pretty_rdf={workspace=true}
rio_api={workspace=true}
rio_xml={workspace=true}
Expand Down
32 changes: 32 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Errors for the Horned-OWL library
use std::fmt::{Display, Formatter};
use std::ops::Range;

use pest::RuleType;
use thiserror::Error;

#[derive(Debug)]
pub enum Location {
BytePosition(usize),
ByteSpan(Range<usize>),
Unknown
}

Expand All @@ -15,10 +18,32 @@ impl From<usize> for Location{
}
}

impl From<Range<usize>> for Location {
fn from(r: Range<usize>) -> Self {
Location::ByteSpan(r)
}
}

impl From<pest::error::InputLocation> for Location {
fn from(l: pest::error::InputLocation) -> Self {
match l {
pest::error::InputLocation::Pos(x) => Location::BytePosition(x),
pest::error::InputLocation::Span((x, y)) => Location::ByteSpan(x..y),
}
}
}

impl<'i> From<pest::Span<'i>> for Location {
fn from(span: pest::Span<'i>) -> Self {
Location::ByteSpan(span.start()..span.end())
}
}

impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BytePosition(u) => write!(f, "Byte Position: {}", u),
Self::ByteSpan(r) => write!(f, "Byte Span: {} to {}", r.start, r.end),
Self::Unknown => write!(f, "Unknown")
}
}
Expand Down Expand Up @@ -75,3 +100,10 @@ impl From<rio_xml::RdfXmlError> for HornedError {
Self::ParserError(e.into(), Location::Unknown)
}
}

impl<R: RuleType + 'static> From<pest::error::Error<R>> for HornedError {
fn from(e: pest::error::Error<R>) -> Self {
let location = e.location.clone().into();
Self::ParserError(e.into(), location)
}
}
54 changes: 54 additions & 0 deletions src/grammars/bcp47.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Annex III: Language Tag Grammar from BCP 47
// (source: https://www.rfc-editor.org/bcp/bcp47.txt)

BCP47_LanguageTag = ${ BCP47_LangTag | BCP47_PrivateUse | BCP47_GrandFathered }
BCP47_LangTag = ${
BCP47_Language ~ ("-" ~ BCP47_Script)? ~ ("-" ~ BCP47_Region)? ~ ("-" ~ BCP47_Variant)* ~ ("-" ~ BCP47_Extension)* ~ ("-" ~ BCP47_PrivateUse)?
}

BCP47_Language = ${
ASCII_ALPHA{2, 3} ~ ("-" ~ BCP47_ExtLang)?
| ASCII_ALPHA{4}
| ASCII_ALPHA{5}
}

BCP47_ExtLang = ${ ASCII_ALPHA{3} ~ ("-" ~ ASCII_ALPHA{3}){,2} }
BCP47_Script = ${ ASCII_ALPHA{4} }
BCP47_Region = ${ ASCII_ALPHA{2} | ASCII_DIGIT{3} }
BCP47_Variant = ${ ASCII_ALPHANUMERIC{5, 8} | ASCII_DIGIT ~ ASCII_ALPHANUMERIC{3} }
BCP47_Extension = ${ BCP47_Singleton ~ ("-" ~ ASCII_ALPHANUMERIC{2, 8})+ }
BCP47_Singleton = @{ ASCII_DIGIT | '\u{41}'..'\u{57}' | '\u{59}'..'\u{5A}' | '\u{61}'..'\u{77}' | '\u{79}'..'\u{7A}' }
BCP47_PrivateUse = ${ "x" ~ ("-" ~ ASCII_ALPHANUMERIC{1, 8})+ }
BCP47_GrandFathered = ${ BCP47_Irregular | BCP47_Regular }

BCP47_Irregular = ${
"en-GB-oed"
| "i-ami"
| "i-bnn"
| "i-default"
| "i-enochian"
| "i-hak"
| "i-klingon"
| "i-lux"
| "i-mingo"
| "i-navajo"
| "i-pwn"
| "i-tao"
| "i-tay"
| "i-tsu"
| "sgn-BE-FR"
| "sgn-BE-NL"
| "sgn-CH-DE"
}

BCP47_Regular = ${
"art-lojban"
| "cel-gaulish"
| "no-bok"
| "no-nyn"
| "zh-guoyu"
| "zh-hakka"
| "zh-min"
| "zh-min-nan"
| "zh-xiang"
}
Loading
Loading