swc_ecma_lexer/common/
input.rs

1use swc_common::BytePos;
2use swc_ecma_ast::EsVersion;
3
4use super::context::Context;
5use crate::{common::syntax::SyntaxFlags, error::Error, lexer};
6
7/// Clone should be cheap if you are parsing typescript because typescript
8/// syntax requires backtracking.
9pub trait Tokens<TokenAndSpan>: Clone + Iterator<Item = TokenAndSpan> {
10    type Checkpoint;
11
12    fn set_ctx(&mut self, ctx: Context);
13    fn ctx(&self) -> Context;
14    fn ctx_mut(&mut self) -> &mut Context;
15    fn syntax(&self) -> SyntaxFlags;
16    fn target(&self) -> EsVersion;
17
18    fn checkpoint_save(&self) -> Self::Checkpoint;
19    fn checkpoint_load(&mut self, checkpoint: Self::Checkpoint);
20
21    fn start_pos(&self) -> BytePos {
22        BytePos(0)
23    }
24
25    fn set_expr_allowed(&mut self, allow: bool);
26    fn set_next_regexp(&mut self, start: Option<BytePos>);
27
28    fn token_context(&self) -> &lexer::TokenContexts;
29    fn token_context_mut(&mut self) -> &mut lexer::TokenContexts;
30    fn set_token_context(&mut self, c: lexer::TokenContexts);
31
32    /// Implementors should use Rc<RefCell<Vec<Error>>>.
33    ///
34    /// It is required because parser should backtrack while parsing typescript
35    /// code.
36    fn add_error(&mut self, error: Error);
37
38    /// Add an error which is valid syntax in script mode.
39    ///
40    /// This errors should be dropped if it's not a module.
41    ///
42    /// Implementor should check for if [Context].module, and buffer errors if
43    /// module is false. Also, implementors should move errors to the error
44    /// buffer on set_ctx if the parser mode become module mode.
45    fn add_module_mode_error(&mut self, error: Error);
46
47    fn end_pos(&self) -> BytePos;
48
49    fn take_errors(&mut self) -> Vec<Error>;
50
51    /// If the program was parsed as a script, this contains the module
52    /// errors should the program be identified as a module in the future.
53    fn take_script_module_errors(&mut self) -> Vec<Error>;
54    fn update_token_flags(&mut self, f: impl FnOnce(&mut lexer::TokenFlags));
55    fn token_flags(&self) -> lexer::TokenFlags;
56}