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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(unused_must_use)]
#![deny(clippy::all)]
#![allow(clippy::needless_return)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::wrong_self_convention)]
use swc_common::{input::StringInput, SourceFile};
use swc_html_ast::{Document, DocumentFragment, Element};
use crate::{
error::Error,
lexer::Lexer,
parser::{PResult, Parser, ParserConfig},
};
#[macro_use]
mod macros;
pub mod error;
pub mod lexer;
pub mod parser;
pub trait Parse<T> {
fn parse(&mut self) -> PResult<T>;
}
impl<T, P> Parse<Box<T>> for P
where
Self: Parse<T>,
{
fn parse(&mut self) -> PResult<Box<T>> {
self.parse().map(Box::new)
}
}
pub fn parse_file_as<'a, T>(
fm: &'a SourceFile,
config: ParserConfig,
errors: &mut Vec<Error>,
) -> PResult<T>
where
Parser<Lexer<StringInput<'a>>>: Parse<T>,
{
let lexer = Lexer::new(StringInput::from(fm), config);
let mut parser = Parser::new(lexer, config);
let result = parser.parse();
errors.extend(parser.take_errors());
result
}
pub fn parse_file_as_document(
fm: &SourceFile,
config: ParserConfig,
errors: &mut Vec<Error>,
) -> PResult<Document> {
let lexer = Lexer::new(StringInput::from(fm), config);
let mut parser = Parser::new(lexer, config);
let result = parser.parse_document();
errors.extend(parser.take_errors());
result
}
pub fn parse_file_as_document_fragment(
fm: &SourceFile,
context_element: Element,
config: ParserConfig,
errors: &mut Vec<Error>,
) -> PResult<DocumentFragment> {
let lexer = Lexer::new(StringInput::from(fm), config);
let mut parser = Parser::new(lexer, config);
let result = parser.parse_document_fragment(context_element);
errors.extend(parser.take_errors());
result
}