1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::lexer::token::LexToken;
use crate::parser::Parser;
use crate::syntax::{NonEmptySyntax, Syntax};

use lark_debug_derive::DebugWith;
use lark_error::ErrorReported;
use lark_intern::Intern;
use lark_span::{FileName, Spanned};
use lark_string::GlobalIdentifier;

#[derive(DebugWith)]
pub struct SpannedGlobalIdentifier;

impl Syntax<'parse> for SpannedGlobalIdentifier {
    type Data = Spanned<GlobalIdentifier, FileName>;

    fn test(&mut self, parser: &Parser<'parse>) -> bool {
        SpannedLocalIdentifier.test(parser)
    }

    fn expect(&mut self, parser: &mut Parser<'parse>) -> Result<Self::Data, ErrorReported> {
        let Spanned { span, value } = SpannedLocalIdentifier.expect(parser)?;
        Ok(Spanned {
            value: value.intern(parser),
            span: span,
        })
    }
}

impl NonEmptySyntax<'parse> for SpannedGlobalIdentifier {}

#[derive(DebugWith)]
pub struct SpannedLocalIdentifier;

impl Syntax<'parse> for SpannedLocalIdentifier {
    type Data = Spanned<&'parse str, FileName>;

    fn test(&mut self, parser: &Parser<'parse>) -> bool {
        parser.is(LexToken::Identifier)
    }

    fn expect(&mut self, parser: &mut Parser<'parse>) -> Result<Self::Data, ErrorReported> {
        if self.test(parser) {
            let Spanned { span, .. } = parser.shift();
            Ok(Spanned {
                value: &parser.input()[span],
                span: span,
            })
        } else {
            Err(parser.report_error("expected an identifier", parser.peek_span()))
        }
    }
}

impl NonEmptySyntax<'parse> for SpannedLocalIdentifier {}