use std::cell::RefCell;
use crate::engine::compiler::model::scope::ScopeId;
use crate::engine::compiler::optional_chain::FinalizedOptionalChain;
use crate::engine::compiler::parser::lookahead::LookaheadCache;
#[derive(Debug)]
pub(in crate::engine::compiler) struct FunctionParseContext {
pub(in crate::engine::compiler) last_member_reference: Option<usize>,
pub(in crate::engine::compiler) last_identifier_reference: Option<usize>,
pub(in crate::engine::compiler) last_optional_chain: Option<FinalizedOptionalChain>,
pub(in crate::engine::compiler) break_controls: Vec<BreakControlContext>,
pub(in crate::engine::compiler) stack_depth: usize,
pub(in crate::engine::compiler) current_scope: ScopeId,
pub(in crate::engine::compiler) in_function_body: bool,
}
impl FunctionParseContext {
pub(super) fn new(body_scope: ScopeId) -> Self {
Self {
last_member_reference: None,
last_identifier_reference: None,
last_optional_chain: None,
break_controls: Vec::new(),
stack_depth: 0,
current_scope: body_scope,
in_function_body: false,
}
}
}
use crate::engine::api::error::Error;
use crate::engine::compiler::EvalCompileContext;
use crate::engine::compiler::lexer::Lexer;
use crate::engine::compiler::lexer::Span;
use crate::engine::compiler::lexer::Token;
use crate::engine::compiler::lexer::{LexContext, LexicalGoal};
use crate::engine::compiler::model::bindings::BindingId;
use crate::engine::compiler::model::ir::FunctionId;
use crate::engine::compiler::module;
use crate::engine::compiler::names::NameId;
use crate::engine::compiler::names::NameTable;
use crate::engine::compiler::parser::builder::FunctionBuilder;
use crate::source::SourceOffset;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum StatementCompletion {
Eval,
Discard,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum StatementPosition {
ProgramBody,
FunctionBody,
NestedList,
AnnexBIfArm,
AnnexBLabelBody,
Single,
}
impl StatementPosition {
pub(in crate::engine::compiler) const fn allows_other_declaration(self) -> bool {
matches!(
self,
Self::ProgramBody | Self::FunctionBody | Self::NestedList
)
}
pub(in crate::engine::compiler) const fn allows_labelled_annex_b(self) -> bool {
matches!(
self,
Self::ProgramBody | Self::FunctionBody | Self::NestedList | Self::AnnexBLabelBody
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum MemberReference {
Field {
key: u32,
site: SourceOffset,
},
Computed {
site: SourceOffset,
},
Super {
site: SourceOffset,
},
Private {
name: NameId,
span: Span,
scope: ScopeId,
site: SourceOffset,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) struct IdentifierReference {
pub(in crate::engine::compiler) name: NameId,
pub(in crate::engine::compiler) span: Span,
pub(in crate::engine::compiler) scope: ScopeId,
pub(in crate::engine::compiler) object_environment: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum LogicalAssignment {
And,
Or,
Nullish,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum PowerMode {
Allowed,
Forbidden,
None,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum InMode {
Allow,
Disallow,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum ForHeadDelimiter {
Parenthesis,
Bracket,
Brace,
Template,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum ForIterationKind {
In,
Of,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum ForAssignmentDeclaration {
Assignment,
Var,
Lexical,
}
#[derive(Clone, Debug)]
pub(in crate::engine::compiler) struct ForAssignmentTargetInfo {
pub(in crate::engine::compiler) declaration: ForAssignmentDeclaration,
pub(in crate::engine::compiler) var_initializer: Option<IdentifierReference>,
pub(in crate::engine::compiler) is_destructuring: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum BreakControlKind {
RegularStatement,
Loop,
DestructuringIterator,
ForOfAssignmentFragment,
ForOf,
ForIn,
Switch,
TryFinally,
FinallyBody,
}
#[derive(Debug)]
pub(in crate::engine::compiler) struct BreakControlContext {
pub(in crate::engine::compiler) kind: BreakControlKind,
pub(in crate::engine::compiler) label_name: Option<String>,
pub(in crate::engine::compiler) scope: ScopeId,
pub(in crate::engine::compiler) entry_depth: usize,
pub(in crate::engine::compiler) drop_count: usize,
pub(in crate::engine::compiler) break_jumps: Vec<usize>,
pub(in crate::engine::compiler) continue_jumps: Vec<usize>,
pub(in crate::engine::compiler) finally_gosubs: Vec<usize>,
}
#[derive(Clone, Copy, Debug)]
pub(in crate::engine::compiler) struct PreparedScopedFunction {
pub(in crate::engine::compiler) binding: BindingId,
pub(in crate::engine::compiler) create_annex_binding: bool,
}
#[derive(Clone, Copy, Debug)]
pub(in crate::engine::compiler) enum AnonymousFunctionDefinition {
Function,
Class {
owner: FunctionId,
static_initializer_start: Option<usize>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::engine::compiler) enum ModuleDeclarationExport {
None,
Named,
Default,
}
pub(in crate::engine::compiler) struct Parser<'source> {
pub(in crate::engine::compiler) lexer: Lexer<'source>,
pub(in crate::engine::compiler) names: NameTable,
pub(in crate::engine::compiler) token: Token<'source>,
pub(in crate::engine::compiler) token_context: (LexContext, LexicalGoal),
pub(in crate::engine::compiler) previous_end: Option<usize>,
pub(in crate::engine::compiler) current_function: FunctionId,
pub(in crate::engine::compiler) in_mode: InMode,
pub(in crate::engine::compiler) functions: Vec<FunctionBuilder>,
pub(in crate::engine::compiler) module: Option<module::IrModule>,
pub(in crate::engine::compiler) module_declaration_export: ModuleDeclarationExport,
pub(in crate::engine::compiler) module_declaration_export_target: Option<(FunctionId, ScopeId)>,
pub(in crate::engine::compiler) anonymous_function_definition:
Option<AnonymousFunctionDefinition>,
pub(in crate::engine::compiler) pending_unsupported: Option<Error>,
pub(in crate::engine::compiler) lookahead: RefCell<LookaheadCache<'source>>,
pub(in crate::engine::compiler) stack_guard:
crate::engine::compiler::stack_guard::ParserStackGuard,
}
pub(in crate::engine::compiler) enum RootCompileContext {
Script,
Module,
Eval(EvalCompileContext),
}