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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::priv_prelude::*;

pub struct Module {
    pub kind: ModuleKind,
    pub semicolon_token: SemicolonToken,
    pub dependencies: Vec<Dependency>,
    pub items: Vec<Item>,
}

impl Spanned for Module {
    fn span(&self) -> Span {
        let start = self.kind.span();
        let end = match self.items.last() {
            Some(item) => item.span(),
            None => match self.dependencies.last() {
                Some(dependency) => dependency.span(),
                None => self.semicolon_token.span(),
            },
        };
        Span::join(start, end)
    }
}

pub enum ModuleKind {
    Script {
        script_token: ScriptToken,
    },
    Contract {
        contract_token: ContractToken,
    },
    Predicate {
        predicate_token: PredicateToken,
    },
    Library {
        library_token: LibraryToken,
        name: Ident,
    },
}

impl Spanned for ModuleKind {
    fn span(&self) -> Span {
        match self {
            Self::Script { script_token } => script_token.span(),
            Self::Contract { contract_token } => contract_token.span(),
            Self::Predicate { predicate_token } => predicate_token.span(),
            Self::Library {
                library_token,
                name,
            } => Span::join(library_token.span(), name.span().clone()),
        }
    }
}

impl Parse for ModuleKind {
    fn parse(parser: &mut Parser) -> ParseResult<Self> {
        if let Some(script_token) = parser.take() {
            Ok(Self::Script { script_token })
        } else if let Some(contract_token) = parser.take() {
            Ok(Self::Contract { contract_token })
        } else if let Some(predicate_token) = parser.take() {
            Ok(Self::Predicate { predicate_token })
        } else if let Some(library_token) = parser.take() {
            let name = parser.parse()?;
            Ok(Self::Library {
                library_token,
                name,
            })
        } else {
            Err(parser.emit_error(ParseErrorKind::ExpectedModuleKind))
        }
    }
}

impl ParseToEnd for Module {
    fn parse_to_end<'a, 'e>(mut parser: Parser<'a, 'e>) -> ParseResult<(Self, ParserConsumed<'a>)> {
        let kind = parser.parse()?;
        let semicolon_token = parser.parse()?;
        let mut dependencies = Vec::new();
        while let Some(..) = parser.peek::<DepToken>() {
            let dependency = parser.parse()?;
            dependencies.push(dependency);
        }
        let (items, consumed) = parser.parse_to_end()?;
        let module = Self {
            kind,
            semicolon_token,
            dependencies,
            items,
        };
        Ok((module, consumed))
    }
}