customasm/asm/parser/
symbol.rs

1use crate::*;
2
3
4#[derive(Clone, Debug)]
5pub struct AstSymbol
6{
7    pub decl_span: diagn::Span,
8    pub hierarchy_level: usize,
9    pub name: String,
10    pub kind: AstSymbolKind,
11    pub no_emit: bool,
12    
13    pub item_ref: Option<util::ItemRef::<asm::Symbol>>,
14}
15
16
17#[derive(Clone, Debug)]
18pub enum AstSymbolKind
19{
20    Constant(AstSymbolConstant),
21    Label,
22}
23
24
25#[derive(Clone, Debug)]
26pub struct AstSymbolConstant
27{
28    pub expr: expr::Expr,
29}
30
31
32pub fn parse(
33    report: &mut diagn::Report,
34    walker: &mut syntax::Walker)
35    -> Result<asm::AstAny, ()>
36{
37    let mut decl_span = diagn::Span::new_dummy();
38    let mut hierarchy_level = 0;
39    
40    while let Some(tk_dot) = walker.maybe_expect(syntax::TokenKind::Dot)
41    {
42        hierarchy_level += 1;
43        decl_span = decl_span.join(tk_dot.span);
44    }
45
46    let tk_name = walker.expect(report, syntax::TokenKind::Identifier)?;
47    let name = walker.get_span_excerpt(tk_name.span).to_string();
48    decl_span = decl_span.join(tk_name.span);
49
50
51    if walker.maybe_expect(syntax::TokenKind::Equal).is_some()
52    {
53        let expr = expr::parse(report, walker)?;
54        walker.expect_linebreak(report)?;
55        
56        Ok(asm::AstAny::Symbol(AstSymbol {
57            decl_span,
58            hierarchy_level,
59            name,
60            kind: AstSymbolKind::Constant(AstSymbolConstant {
61                expr,
62            }),
63            no_emit: false,
64
65            item_ref: None,
66        }))
67    }
68    else
69    {
70        let tk_colon = walker.expect(report, syntax::TokenKind::Colon)?;
71        decl_span = decl_span.join(tk_colon.span);
72        
73        Ok(asm::AstAny::Symbol(AstSymbol {
74            decl_span,
75            hierarchy_level,
76            name,
77            kind: AstSymbolKind::Label,
78            no_emit: false,
79
80            item_ref: None,
81        }))
82    }
83}