moore_common/util.rs
1// Copyright (c) 2016-2021 Fabian Schuiki
2
3//! A collection of utility traits and functions specific to VHDL.
4
5#![deny(missing_docs)]
6
7use crate::source::{Span, Spanned};
8
9/// Provides span information for syntax nodes.
10pub trait HasSpan {
11 /// Obtain the full span of the input file that this node covers.
12 fn span(&self) -> Span;
13
14 /// Obtain a span which can be used to refer to this node in error messages
15 /// presented to humans. This will generally be the name for things like
16 /// entities, processes, and variables. Defaults to return whatever `span()`
17 /// returns.
18 fn human_span(&self) -> Span {
19 self.span()
20 }
21}
22
23impl<T> HasSpan for Spanned<T> {
24 fn span(&self) -> Span {
25 self.span
26 }
27}
28
29/// Describes syntax nodes.
30pub trait HasDesc {
31 /// Obtain a human-readable descriptive name for this node.
32 fn desc(&self) -> &'static str;
33
34 /// Obtain a human-readable description for this node, possibly containing
35 /// the node's name.
36 fn desc_full(&self) -> String {
37 self.desc().into()
38 }
39}
40
41impl<T> HasDesc for Spanned<T>
42where
43 T: HasDesc,
44{
45 fn desc(&self) -> &'static str {
46 self.value.desc()
47 }
48}