wright/ast/identifier.rs
1//! [Identifier]s are used throughout wright as variable names, type names, function names, etc.
2//! Their modeling is pretty simple, and is defined here.
3//!
4//! [Identifier]: https://en.wikipedia.org/wiki/Identifier
5
6use super::AstNode;
7use crate::source_tracking::fragment::Fragment;
8
9/// Identifiers are used as names for variables, functions, modules, etc.
10/// These are defined using [Fragment]s of source code, which will contain the identifier itself.
11#[derive(Debug, Clone, PartialEq)]
12pub struct Identifier {
13 /// The fragment of source code containing the identifier.
14 ///
15 /// This is empty if the identifier is [missing](Identifier::is_missing).
16 pub fragment: Fragment,
17}
18
19impl Identifier {
20 /// Get the identifier as a string.
21 pub fn as_str(&self) -> &str {
22 self.fragment.as_str()
23 }
24
25 /// Was this identifier missing from the source code?
26 ///
27 /// The parser produces missing identifiers (with empty fragments, located where the identifier was expected)
28 /// when recovering from syntax errors.
29 pub fn is_missing(&self) -> bool {
30 self.fragment.is_empty()
31 }
32}
33
34impl AstNode for Identifier {
35 fn matching_source(&self) -> &Fragment {
36 &self.fragment
37 }
38}