mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(ast,parser): move parsing context from ast to parser
This commit is contained in:
parent
fdbe365ac7
commit
d917348f9b
12 changed files with 41 additions and 43 deletions
|
|
@ -11,7 +11,6 @@ mod serialize;
|
|||
pub mod ast;
|
||||
mod ast_builder;
|
||||
mod ast_kind;
|
||||
pub mod context;
|
||||
pub mod module_record;
|
||||
mod source_type;
|
||||
mod span;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ use std::path::Path;
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::context::Context;
|
||||
|
||||
/// Source Type for JavaScript vs TypeScript / Script vs Module / JSX
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct SourceType {
|
||||
|
|
@ -62,16 +60,6 @@ impl Default for SourceType {
|
|||
pub const VALID_EXTENSIONS: [&str; 8] = ["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"];
|
||||
|
||||
impl SourceType {
|
||||
#[must_use]
|
||||
pub fn default_context(&self) -> Context {
|
||||
let ctx = Context::default().and_ambient(self.is_typescript_definition());
|
||||
match self.module_kind {
|
||||
ModuleKind::Script => ctx,
|
||||
// for [top-level-await](https://tc39.es/proposal-top-level-await/)
|
||||
ModuleKind::Module => ctx.and_await(true),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_script(self) -> bool {
|
||||
self.module_kind == ModuleKind::Script
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
//! Code related to navigating `Token`s from the lexer
|
||||
|
||||
use oxc_ast::{context::Context, Span};
|
||||
use oxc_ast::Span;
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use crate::lexer::{Kind, LexerCheckpoint, LexerContext, Token};
|
||||
use crate::{diagnostics, Parser};
|
||||
use crate::{diagnostics, Context, Parser};
|
||||
|
||||
pub struct ParserCheckpoint<'a> {
|
||||
lexer: LexerCheckpoint<'a>,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_ast::{ast::*, context::StatementContext, syntax_directed_operations::PropName, Span};
|
||||
use oxc_ast::{ast::*, syntax_directed_operations::PropName, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::list::ClassElements;
|
||||
use crate::{diagnostics, lexer::Kind, list::NormalList, Parser};
|
||||
use crate::{diagnostics, lexer::Kind, list::NormalList, Parser, StatementContext};
|
||||
|
||||
type Extends<'a> =
|
||||
Vec<'a, (Expression<'a>, Option<Box<'a, TSTypeParameterInstantiation<'a>>>, Span)>;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use oxc_allocator::Box;
|
||||
use oxc_ast::{ast::*, context::StatementContext, GetSpan, Span};
|
||||
use oxc_ast::{ast::*, GetSpan, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use crate::{diagnostics, lexer::Kind, Parser};
|
||||
use crate::{diagnostics, lexer::Kind, Parser, StatementContext};
|
||||
|
||||
#[derive(Clone, Debug, Copy, Eq, PartialEq)]
|
||||
pub enum VariableDeclarationParent {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
use oxc_allocator::Box;
|
||||
use oxc_ast::{
|
||||
ast::*,
|
||||
context::{Context, StatementContext},
|
||||
AstBuilder, GetSpan, Span,
|
||||
};
|
||||
use oxc_ast::{ast::*, AstBuilder, GetSpan, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::list::FormalParameterList;
|
||||
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser};
|
||||
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, Parser, StatementContext};
|
||||
|
||||
type ArrowFunctionHead<'a> = (
|
||||
Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_ast::{ast::*, context::Context, Span};
|
||||
use oxc_ast::{ast::*, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::function::FunctionKind;
|
||||
use super::list::{AssertEntries, ExportNamedSpecifiers, ImportSpecifierList};
|
||||
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser};
|
||||
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, Parser};
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
/// [Import Call](https://tc39.es/ecma262/#sec-import-calls)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_ast::{
|
||||
ast::*,
|
||||
context::{Context, StatementContext},
|
||||
Span,
|
||||
};
|
||||
use oxc_ast::{ast::*, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::{
|
||||
|
|
@ -11,7 +7,12 @@ use super::{
|
|||
grammar::CoverGrammar,
|
||||
list::SwitchCases,
|
||||
};
|
||||
use crate::{diagnostics, lexer::Kind, list::NormalList, Parser};
|
||||
use crate::{
|
||||
diagnostics,
|
||||
lexer::Kind,
|
||||
list::NormalList,
|
||||
Parser, {Context, StatementContext},
|
||||
};
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
/// `https://tc39.es/ecma262/#prod-StatementList`
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#![feature(portable_simd)]
|
||||
#![feature(slice_as_chunks)]
|
||||
|
||||
mod context;
|
||||
mod cursor;
|
||||
mod list;
|
||||
mod state;
|
||||
|
|
@ -17,8 +18,9 @@ mod lexer;
|
|||
|
||||
use std::rc::Rc;
|
||||
|
||||
use context::{Context, StatementContext};
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{ast::Program, context::Context, AstBuilder, SourceType, Span, Trivias};
|
||||
use oxc_ast::{ast::Program, AstBuilder, ModuleKind, SourceType, Span, Trivias};
|
||||
use oxc_diagnostics::{Error, Result};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -77,7 +79,7 @@ impl<'a> Parser<'a> {
|
|||
token: Token::default(),
|
||||
prev_token_end: 0,
|
||||
state: ParserState::new(allocator),
|
||||
ctx: source_type.default_context(),
|
||||
ctx: Self::default_context(source_type),
|
||||
ast: AstBuilder::new(allocator),
|
||||
}
|
||||
}
|
||||
|
|
@ -123,6 +125,16 @@ impl<'a> Parser<'a> {
|
|||
Ok(self.ast.program(span, directives, statements, self.source_type))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn default_context(source_type: SourceType) -> Context {
|
||||
let ctx = Context::default().and_ambient(source_type.is_typescript_definition());
|
||||
match source_type.module_kind() {
|
||||
ModuleKind::Script => ctx,
|
||||
// for [top-level-await](https://tc39.es/proposal-top-level-await/)
|
||||
ModuleKind::Module => ctx.and_await(true),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check for Flow declaration if the file cannot be parsed.
|
||||
/// The declaration must be [on the first line before any code](https://flow.org/en/docs/usage/#toc-prepare-your-code-for-flow)
|
||||
fn flow_error(&self) -> Option<Error> {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,18 @@
|
|||
use oxc_allocator::Box;
|
||||
use oxc_ast::{ast::*, context::StatementContext, Span};
|
||||
use oxc_ast::{ast::*, Span};
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::{
|
||||
list::{TSEnumMemberList, TSInterfaceOrObjectBodyList},
|
||||
types::ModifierFlags,
|
||||
};
|
||||
use crate::js::declaration::{VariableDeclarationContext, VariableDeclarationParent};
|
||||
use crate::js::function::FunctionKind;
|
||||
use crate::lexer::Kind;
|
||||
use crate::list::{NormalList, SeparatedList};
|
||||
use crate::Parser;
|
||||
use crate::{
|
||||
js::declaration::{VariableDeclarationContext, VariableDeclarationParent},
|
||||
js::function::FunctionKind,
|
||||
lexer::Kind,
|
||||
list::{NormalList, SeparatedList},
|
||||
Parser, StatementContext,
|
||||
};
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
/** ------------------- Enum ------------------ */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use bitflags::bitflags;
|
||||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_ast::{ast::*, context::Context};
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
|
||||
use super::list::{
|
||||
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
js::list::{ArrayPatternList, ObjectPatternProperties},
|
||||
lexer::Kind,
|
||||
list::{NormalList, SeparatedList},
|
||||
Parser,
|
||||
Context, Parser,
|
||||
};
|
||||
|
||||
bitflags! {
|
||||
|
|
|
|||
Loading…
Reference in a new issue