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 source file location into RDF output #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdml-core/src/model/definitions/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ impl TypeClassDef {
extend_variables
=> variables, TypeVariable
);

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually performed by the impl_has_source_span_for macro, which implements the crate::model::HasSourceSpan trait resulting in the methods with_source_span, has_source_span, source_span, set_source_span and unset_source_span.

}

// ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion sdml-core/src/model/definitions/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Validate for DatatypeDef {
loader
.report(&type_definition_not_found(
top.file_id().copied().unwrap_or_default(),
self.span.as_ref().map(|span| span.clone().into()),
self.span.as_ref().map(|span| span.into()),
self.base_type(),
))
.unwrap();
Expand Down Expand Up @@ -156,4 +156,6 @@ impl DatatypeDef {
get_and_set_bool!(pub opaque, is_opaque, set_opaque);

get_and_set!(pub base_type, set_base_type => IdentifierReference);

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
2 changes: 2 additions & 0 deletions sdml-core/src/model/definitions/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ impl EntityDef {
body: None,
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}

// ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions sdml-core/src/model/definitions/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ impl EnumDef {
body: None,
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -168,4 +170,6 @@ impl ValueVariant {
body: None,
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
1 change: 1 addition & 0 deletions sdml-core/src/model/definitions/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ impl EventDef {
// --------------------------------------------------------------------------------------------

get_and_set!(pub event_source, set_event_source => IdentifierReference);
get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
1 change: 1 addition & 0 deletions sdml-core/src/model/definitions/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ impl PropertyDef {
// --------------------------------------------------------------------------------------------

get_and_set!(pub member, member_def, set_member_def => MemberDef);
get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
2 changes: 2 additions & 0 deletions sdml-core/src/model/definitions/rdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ impl RdfDef {
pub fn individual(name: Identifier) -> Self {
Self::new(name)
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
2 changes: 2 additions & 0 deletions sdml-core/src/model/definitions/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl StructureDef {
body: None,
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
}

// ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions sdml-core/src/model/definitions/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl UnionDef {
..self
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -225,4 +227,6 @@ impl TypeVariant {
}
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}
2 changes: 1 addition & 1 deletion sdml-core/src/model/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Identifier {
loader
.report(&invalid_identifier(
top.file_id().copied().unwrap_or_default(),
self.span.clone().map(|s| s.into()),
self.span.map(|s| s.into()),
&self.value,
))
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions sdml-core/src/model/members/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ impl Member {
MemberKind::Definition(defn) => Some(defn.target_cardinality().clone()),
}
}

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.

}

// ------------------------------------------------------------------------------------------------
Expand Down
104 changes: 76 additions & 28 deletions sdml-core/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use std::{
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// ------------------------------------------------------------------------------------------------
/// Load the macros
/// ------------------------------------------------------------------------------------------------
#[macro_use]
mod macros;

// ------------------------------------------------------------------------------------------------
// Public Types ❱ Traits
// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -179,9 +185,20 @@ pub trait References {
/// The source location information from the tree-sitter `Node` type. The location is stored as
/// a start and end position, where the positions are byte indices.
///
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Span {
start: SpanPosition,
end: SpanPosition,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Span(Range<usize>);
pub struct SpanPosition {
byte: usize,
line: usize,
column: usize,
}

// ------------------------------------------------------------------------------------------------
// Implementations
Expand All @@ -190,7 +207,10 @@ pub struct Span(Range<usize>);
#[cfg(feature = "tree-sitter")]
impl From<&tree_sitter::Node<'_>> for Span {
fn from(node: &tree_sitter::Node<'_>) -> Self {
Self(node.byte_range())
Self {
start: SpanPosition::from(node.start_position(), node.start_byte()),
end: SpanPosition::from(node.end_position(), node.end_byte()),
}
}
}

Expand All @@ -201,30 +221,21 @@ impl From<tree_sitter::Node<'_>> for Span {
}
}

impl From<Span> for sdml_errors::Span {
fn from(value: Span) -> Self {
value.byte_range()
}
}

impl From<&Span> for sdml_errors::Span {
fn from(value: &Span) -> Self {
sdml_errors::Span::from(value.clone())
value.byte_range()
}
}

impl Debug for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Span")
.field("start", &self.0.start)
.field("end", &self.0.end)
.finish()
impl From<Span> for sdml_errors::Span {
fn from(value: Span) -> Self {
value.byte_range()
}
}

impl Display for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}..{}", self.0.start, self.0.end)
write!(f, "{}..{}", self.start.byte, self.end.byte)
}
}

Expand All @@ -235,9 +246,15 @@ impl Span {

/// Create a new span from the `start` byte and `end` byte indices.
#[inline(always)]
pub fn new(start: usize, end: usize) -> Self {
assert!(start <= end);
Self(start..end)
pub fn new(start: SpanPosition, end: SpanPosition) -> Self {
assert!(start.byte <= end.byte);
assert!(start.line <= end.line);
assert!(start.column <= end.column);

Self {
start,
end,
}
}

// --------------------------------------------------------------------------------------------
Expand All @@ -246,30 +263,61 @@ impl Span {

/// Return the starting byte index of this span.
#[inline(always)]
pub fn start(&self) -> usize {
self.0.start
pub fn start(&self) -> SpanPosition {
self.start
}

/// Return the ending byte index of this span.
#[inline(always)]
pub fn end(&self) -> usize {
self.0.end
pub fn end(&self) -> SpanPosition {
self.end
}

/// Return this span as a `start..end` range.
#[inline(always)]
pub fn byte_range(&self) -> Range<usize> {
self.0.clone()
self.start.byte..self.end.byte
}
}

impl SpanPosition {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------

/// Create a new span position from the `byte`, `line`, and `column` indices.
#[inline(always)]
pub fn new(byte: usize, line: usize, column: usize) -> Self {
Self {
byte,
line,
column,
}
}

/// Create a new span position from the `byte` and tree-sitter point.
#[cfg(feature = "tree-sitter")]
pub fn from(node_point: tree_sitter::Point, byte: usize) -> Self {
Self::new(byte, node_point.row + 1, node_point.column + 1)
}

pub const fn byte(&self) -> usize {
self.byte
}

pub const fn line(&self) -> usize {
self.line
}

pub const fn column(&self) -> usize {
self.column
}
}

// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------

#[macro_use]
mod macros;

pub mod annotations;

pub mod check;
Expand Down
2 changes: 2 additions & 0 deletions sdml-core/src/model/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ impl Module {

get_and_set!(pub source_file, set_source_file, unset_source_file => optional has_source_file, PathBuf);

get_and_set!(pub span, set_span, unset_span => optional has_span, Span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above re:impl_has_source_span_for.


get_and_set!(pub base_uri, set_base_uri, unset_base_uri => optional has_base_uri, HeaderValue<Url>);

get_and_set!(pub version_info, set_version_info, unset_version_info => optional has_version_info, HeaderValue<String>);
Expand Down
4 changes: 4 additions & 0 deletions sdml-core/src/stdlib/sdml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ pub const HAS_TYPE_VARIANT: &str = "hasTypeVariant";
pub const HAS_VALUE_VARIANT: &str = "hasValueVariant";
pub const LOCATION_END_BYTE: &str = "endByte";
pub const LOCATION_START_BYTE: &str = "startByte";
pub const LOCATION_START_LINE: &str = "startLine";
pub const LOCATION_END_LINE: &str = "endLine";
pub const LOCATION_START_COL: &str = "startColumn";
pub const LOCATION_END_COL: &str = "endColumn";
pub const MAX_OCCURS: &str = "maxOccurs";
pub const MIN_OCCURS: &str = "minOccurs";
pub const ORDERING: &str = "ordering";
Expand Down
2 changes: 1 addition & 1 deletion sdml-generate/src/actions/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn ctag_line_from(name: &Identifier, file_name: &str) -> (String, String) {
"{}\t{}go",
file_name,
name.source_span()
.map(|span| span.start() + 1)
.map(|span| span.start().byte() + 1)
.unwrap_or_default()
),
)
Expand Down
Loading