aldrin_parser/ast/
ident.rs1use crate::error::InvalidIdent;
2use crate::grammar::Rule;
3use crate::validate::Validate;
4use crate::warning::ReservedIdent;
5use crate::Span;
6use pest::iterators::Pair;
7
8#[derive(Debug, Clone)]
9pub struct Ident {
10 span: Span,
11 value: String,
12}
13
14impl Ident {
15 pub(crate) fn parse(pair: Pair<Rule>) -> Self {
16 assert_eq!(pair.as_rule(), Rule::ident);
17
18 Self {
19 span: Span::from_pair(&pair),
20 value: pair.as_str().to_owned(),
21 }
22 }
23
24 pub(crate) fn validate(&self, is_def: bool, validate: &mut Validate) {
25 if is_def {
26 InvalidIdent::validate(self, validate);
27 ReservedIdent::validate(self, validate);
28 }
29 }
30
31 pub(crate) fn is_valid(ident: &str) -> bool {
32 !ident.is_empty()
33 && ident.chars().enumerate().all(|(i, c)| {
34 (c == '_')
35 || if i == 0 {
36 c.is_ascii_alphabetic()
37 } else {
38 c.is_ascii_alphanumeric()
39 }
40 })
41 }
42
43 pub fn span(&self) -> Span {
44 self.span
45 }
46
47 pub fn value(&self) -> &str {
48 &self.value
49 }
50}