sway_parse/
lib.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
mod attribute;
mod brackets;
mod expr;
mod generics;
mod item;
mod keywords;
mod literal;
mod module;
mod parse;
mod parser;
mod path;
mod pattern;
mod priv_prelude;
mod punctuated;
mod submodule;
#[cfg(test)]
mod test_utils;
mod token;
mod ty;
mod where_clause;

use crate::priv_prelude::*;
pub use crate::{
    keywords::RESERVED_KEYWORDS,
    parse::Parse,
    parser::Parser,
    token::{lex, lex_commented, parse_int_suffix},
};

use sway_ast::{
    attribute::Annotated,
    token::{DocComment, DocStyle},
    Module, ModuleKind,
};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::SourceId;

use std::sync::Arc;

pub fn parse_file(
    handler: &Handler,
    src: Arc<str>,
    source_id: Option<SourceId>,
) -> Result<Annotated<Module>, ErrorEmitted> {
    let ts = lex(handler, &src, 0, src.len(), source_id)?;
    let (m, _) = Parser::new(handler, &ts).parse_to_end()?;
    Ok(m)
}

pub fn parse_module_kind(
    handler: &Handler,
    src: Arc<str>,
    source_id: Option<SourceId>,
) -> Result<ModuleKind, ErrorEmitted> {
    let ts = lex(handler, &src, 0, src.len(), source_id)?;
    let mut parser = Parser::new(handler, &ts);
    while let Some(DocComment {
        doc_style: DocStyle::Inner,
        ..
    }) = parser.peek()
    {
        parser.parse::<DocComment>()?;
    }
    parser.parse()
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn parse_invalid() {
        // just make sure these do not panic
        let _res = parse_file(&Handler::default(), Arc::from("script; fn main(256߄"), None);
        let _res = parse_file(
            &Handler::default(),
            Arc::from(
                "script;
            fn karr() {
                let c: f828 =  0x00000000000000000000000vncifxp;
            abi Zezybt {
                #[mfzbezc, storage(r#
            true }
            }
            cug",
            ),
            None,
        );
        let _res = parse_file(
            &Handler::default(),
            Arc::from(
                "script;

            corefn main() {
                let a: b256 =  0x000>0000000scri s = \"flibrary I24;

            use core::primitives::*;
            use std::assert::assert;

            ///\u{7eb}",
            ),
            None,
        );
        let _res = parse_file(
            &Handler::default(),
            Arc::from("script; \"\u{7eb}\u{7eb}"),
            None,
        );
    }
}