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
lalrpop_mod!(wgsl_recognize);
lalrpop_mod!(wgsl_spanned);

use lalrpop_util::lalrpop_mod;

use crate::{error::SpannedError, lexer::Lexer, syntax::TranslationUnit};

pub struct Parser;

impl Parser {
    pub fn parse_str<'s>(source: &'s str) -> Result<TranslationUnit, SpannedError<'s>> {
        let lexer = Lexer::new(source);
        let parser = wgsl_spanned::TranslationUnitParser::new();
        let res = parser.parse(lexer);
        res.map_err(|e| SpannedError::new(e, source))
    }
    pub fn parse<'s>(mut lexer: &'s mut Lexer) -> Result<TranslationUnit, SpannedError<'s>> {
        let parser = wgsl_spanned::TranslationUnitParser::new();
        let res = parser.parse(&mut lexer);
        res.map_err(|e| SpannedError::new(e, lexer.source()))
    }
}

impl Parser {
    pub fn recognize_str<'s>(source: &'s str) -> Result<(), SpannedError<'s>> {
        let lexer = Lexer::new(source);
        let parser = wgsl_recognize::TranslationUnitParser::new();
        let res = parser.parse(lexer);
        res.map_err(|e| SpannedError::new(e, source))
    }
    pub fn recognize<'s>(mut lexer: &'s mut Lexer) -> Result<(), SpannedError<'s>> {
        let parser = wgsl_recognize::TranslationUnitParser::new();
        let res = parser.parse(&mut lexer);
        res.map_err(|e| SpannedError::new(e, lexer.source()))
    }
    pub fn recognize_template_list<'s>(mut lexer: &'s mut Lexer) -> Result<(), SpannedError<'s>> {
        let parser = wgsl_recognize::TryTemplateListParser::new();
        let res = parser.parse(&mut lexer);
        res.map_err(|e| SpannedError::new(e, lexer.source()))
    }
}