wgsl_parser/decl/
legacy.rs1use gramatika::{Parse, ParseStreamer, Span, Spanned, Substr, Token as _};
2
3use crate::{
4 token::{keyword, punct, Token, TokenKind},
5 ParseStream,
6};
7
8#[derive(Clone, DebugLisp)]
9pub struct ExtensionDecl {
10 pub keyword: Token,
11 pub name: Token,
12}
13
14#[derive(Clone, DebugLisp)]
15pub struct ModuleDecl {
16 pub import_kw: Token,
17 pub name: Token,
18 pub from_kw: Token,
19 pub path: Token,
20 pub semicolon: Token,
21}
22
23impl Parse for ExtensionDecl {
24 type Stream = ParseStream;
25
26 fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
27 let keyword = input.consume(keyword![enable])?;
28 let name = input.consume_as(TokenKind::Ident, Token::Module)?;
29 input.consume(punct![;])?;
30
31 Ok(Self { keyword, name })
32 }
33}
34
35impl Spanned for ExtensionDecl {
36 fn span(&self) -> Span {
37 self.keyword.span().through(self.name.span())
38 }
39}
40
41impl ModuleDecl {
42 pub fn path(&self) -> Substr {
43 let path = self.path.lexeme();
44
45 path.substr(1..path.len() - 1)
46 }
47}
48
49impl Parse for ModuleDecl {
50 type Stream = ParseStream;
51
52 fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
53 let import_kw = input.consume(keyword![import])?;
54 let name = input.consume_as(TokenKind::Ident, Token::Module)?;
55 let from_kw = input.consume(keyword![from])?;
56 let path = input.consume_kind(TokenKind::Path)?;
57 let semicolon = input.consume(punct![;])?;
58
59 Ok(Self {
60 import_kw,
61 name,
62 from_kw,
63 path,
64 semicolon,
65 })
66 }
67}
68
69impl Spanned for ModuleDecl {
70 fn span(&self) -> Span {
71 self.import_kw.span().through(self.semicolon.span())
72 }
73}