Skip to main content

Parser

Struct Parser 

Source
pub struct Parser {
Show 36 fields lexer: Lexer, buf: VecDeque<(Token, u32, bool)>, base_pos: usize, pos: usize, pin_pos: Option<usize>, lazy: bool, errors: Vec<ParseError>, error_count: usize, pub(crate) strict: bool, last_body_end: Option<u32>, lex_frames: Vec<LexFrame>, in_function: u16, eval_in_function: bool, in_generator: bool, in_async: bool, in_param: bool, allow_super_call: bool, allow_super_prop: bool, in_field_init: bool, in_static_block: bool, arrow_body: bool, in_class_body: bool, loop_depth: u16, switch_depth: u16, labels: Vec<(Rc<str>, bool)>, scopes: Vec<ScopeFrame>, private_scopes: Vec<Vec<(Rc<str>, u8)>>, pending_cover_inits: usize, pending_dup_protos: usize, pending_spread_trailing_commas: usize, pending_paren_destr_targets: usize, last_expr_was_paren: bool, bin_top_logical: u8, has_concrete_super_scope: bool, top_level_script_errors: Vec<ParseError>, in_lazy_expanded_body: bool,
}

Fields§

§lexer: Lexer

逐次読み出し用の字句解析器(トークン列全体を事前にメモリへ展開しない)。

§buf: VecDeque<(Token, u32, bool)>

スライディングウィンドウ: 論理位置 base_pos 以降の (Token, start_byte, newline_before)。 通常時は直前 HISTORY_KEEP 個+先読み LOOKAHEAD_WINDOW 個(十数要素・数百バイト)のみを保持する。

§base_pos: usize

buf[0] に対応する論理トークン位置。

§pos: usize

現在の論理トークン位置 (pos >= base_pos)。

§pin_pos: Option<usize>

投機的先読み(for ([a,b] of ...) / for (o.p of ...))の巻き戻し起点。

§lazy: bool§errors: Vec<ParseError>

回復して解析を続けた構文エラーの記録(上限 MAX_RECORDED_ERRORS 件)。

§error_count: usize

上限で切り捨てた分も含めた実際の検出件数。

§strict: bool

いま解析している箇所が strict モードか(関数本体の FnBody::strict に記録する)。

§last_body_end: Option<u32>

直前に parse_fn_body が読んだ本体の閉じ } の直後のバイト位置(record_fn_source が使う)。

§lex_frames: Vec<LexFrame>

const への代入を見つけるための字句の枠(ブロック・関数ごと)。LexFrame。

§in_function: u16

静的早期エラー判定用の構文文脈・スコープ状態。

§eval_in_function: bool§in_generator: bool§in_async: bool§in_param: bool§allow_super_call: bool§allow_super_prop: bool§in_field_init: bool§in_static_block: bool§arrow_body: bool

これから読むのがアロー関数の本体か(アローは arguments を持たないので、フィールド初期化子・静的ブロックの制約を引き継ぐ)。

§in_class_body: bool§loop_depth: u16§switch_depth: u16§labels: Vec<(Rc<str>, bool)>§scopes: Vec<ScopeFrame>§private_scopes: Vec<Vec<(Rc<str>, u8)>>§pending_cover_inits: usize§pending_dup_protos: usize§pending_spread_trailing_commas: usize§pending_paren_destr_targets: usize§last_expr_was_paren: bool§bin_top_logical: u8

直前の parse_binary が最上位で最後に組んだ演算子の種類(0=なし・論理以外, 1=??, 2=||/&&)。 括弧なしで ?? と ||/&& を混ぜる式(a ?? b || c)を構文エラーにするために使う。

§has_concrete_super_scope: bool§top_level_script_errors: Vec<ParseError>§in_lazy_expanded_body: bool

Implementations§

Source§

impl Parser

Source

pub fn new(lexer: Lexer) -> Self

Source

pub fn new_lazy(lexer: Lexer) -> Self

関数本体を遅延解析するパーサ(大きな本体は共有ソースと範囲だけ持ち、初回実行時に解析する)。 本体の中の構文エラーは、その関数を初めて実行するときに報告される。

Source

pub fn intern(&mut self, s: &str) -> Rc<str>

識別子・プロパティ名をインターンして共有 Rc<str> を返す。

Source

fn build(lexer: Lexer, lazy: bool) -> Self

Source

fn ctx_snapshot(&self) -> ParserCtx

Source

fn new_sub(lexer: Lexer, ctx: ParserCtx) -> Self

Source

pub(crate) fn current_eval_ctx(&self) -> EvalSyntaxCtx

Source

pub(crate) fn apply_eval_ctx(&mut self, ctx: &EvalSyntaxCtx)

Source

fn flush_lexer_errors(&mut self)

字句解析器で発生したエラーを(投機的先読み中でなければ)パーサのエラー記録へ引き取る。

Source

fn ensure_pos(&mut self, target_pos: usize)

論理トークン位置 target_pos まで buf に読み込む。

Source

fn tok_at_abs(&self, abs: usize) -> Option<&Token>

論理トークン位置 abs のトークンを参照する。

Source

fn token_start_at(&self, abs: usize) -> u32

論理トークン位置 abs の開始バイト位置を返す。

Source

fn token_start_opt(&self, abs: usize) -> Option<u32>

論理トークン位置 abs の開始バイト位置(窓から外れていれば None)。

Source

fn nl_before_at_abs(&self, abs: usize) -> bool

論理トークン位置 abs の直前に行終端文字があったか。

Source

fn nl_before_cur(&self) -> bool

現在トークンの直前に行終端文字があったか(ASI 判定用)。

Source

fn nl_before_at(&self, off: usize) -> bool

現在位置から off 個先のトークンの直前に行終端文字があったか。

Source

fn record_fn_source(&mut self, body: &Rc<FnBody>, start: Option<u32>)

直前に parse_fn_body が読んだ本体の閉じ } の直後のバイト位置を使い、start からの定義全体を 本体に記録する(Function.prototype.toString 用)。

Source

fn fn_source_start(&self, is_async: bool) -> Option<u32>

function の定義の先頭のバイト位置(async function なら直前の async から)。

Source

fn set_pos(&mut self, new_pos: usize)

現在位置を new_pos に進め(または戻し)、先読み窓の補充と古いトークンの破棄を行う。

Source

fn step(&mut self)

Source

fn has_use_strict_directive(&mut self, at: usize) -> bool

at から始まる文の並びの先頭(指令プロローグ)に "use strict" があるか。

Source

fn lex_push(&mut self, barrier: bool)

関数本体({ ... })。遅延モードで本体が大きければ、対応する } までトークンを貯めずに 字句解析器を直接進めて共有ソースと範囲 (start..end) だけ持つ。

Source

fn lex_pop(&mut self)

Source

fn lex_is_const(&self, name: &str) -> bool

name が同じ関数の中で見える const か。

Source

fn lex_declare(&mut self, name: &Rc<str>, is_const: bool)

宣言を今の枠へ記録する(const 以外は、同名の const を隠すときだけ)。

Source

fn lex_declare_pattern(&mut self, pat: &Pattern, is_const: bool)

Source

fn lex_note_stmt(&mut self, s: &Statement)

文が宣言する名前を今の枠へ記録する。

Source

fn const_thrower(&mut self) -> Expression

Assignment to constant variable. の TypeError を投げる呼び出し式。

Source

fn lex_guard_const_target(&mut self, e: Expression) -> Expression

分割代入(や for (x of …) の頭)の代入先に含まれる const の識別子を、評価すると TypeError を投げる メンバー参照((() => { throw … })().x)へ置き換える。代入の時点(参照の評価)で投げる。

Source

fn lex_guard_const_pattern(&mut self, p: Pattern) -> Pattern

lex_guard_const_target の Pattern 版(for ([c] of …) の頭など)。

Source

fn lex_check_const_assign(&mut self, e: Expression) -> Expression

const への代入(文)を、右辺を評価してから TypeError を投げる式に置き換える。

Source

fn parse_fn_body_with(&mut self, has_non_simple_params: bool) -> Rc<FnBody>

Source

fn parse_static_block_body(&mut self) -> Rc<FnBody>

Source

fn parse_fn_body(&mut self) -> Rc<FnBody>

Source

fn parse_fn_body_with_params(&mut self, params: &[Param]) -> Rc<FnBody>

Source

fn record_top_level_only_error(&mut self, message: String)

関数や eval を除いたスクリプト頂点のみでエラーになる構文(direct eval との互換用)。

Source

fn scope_push(&mut self, kind: ScopeKind)

Source

fn note_const_pattern(&mut self, pat: &Pattern)

const 宣言の名前を今のスコープへ記録する(関数の境界を越えた const への代入検出用)。

Source

fn scope_is_const(&self, name: &str) -> bool

name を最も内側で宣言しているスコープが const か(関数の境界を越えて探す)。 宣言より前に書かれた参照や、後ろで巻き上げられる var・関数は見えない(見逃すだけ)。

Source

fn scope_pop(&mut self)

Source

fn is_at_script_top_scope(&self) -> bool

Source

fn declare_binding(&mut self, name: &Rc<str>, kind: BindingKind)

Source

fn declare_pattern_bindings(&mut self, pat: &Pattern, kind: BindingKind)

Source

fn collect_pattern_names<'a>(pat: &'a Pattern, out: &mut Vec<&'a Rc<str>>)

Source

fn check_lexical_disallows_let(&mut self, pat: &Pattern)

Source

fn check_params(&mut self, params: &[Param], require_unique: bool)

Source

fn check_retro_strict( &mut self, name: Option<&str>, params: &[Param], body: &Rc<FnBody>, was_strict: bool, )

本体の "use strict" で初めて strict になった関数の名前・引数を、strict の規則で見直す (function eval() { "use strict" }・function f(a, a) { "use strict" })。

Source

fn check_ident_allowed(&mut self, name: &str, is_binding: bool)

Source

fn consume_binding_ident( &mut self, ctx_desc: &str, advance_on_err: bool, ) -> Rc<str>

Source

fn try_consume_opt_fn_ident(&mut self) -> Option<Rc<str>>

Source

fn check_label_ident(&mut self, name: &str)

Source

fn is_private_declared(&self, name: &str) -> bool

Source

fn check_private_declared(&mut self, name: &str)

Source

fn expect_asi_semicolon(&mut self)

Source

fn check_accessor_params( &mut self, accessor_kind: Option<bool>, params: &[Param], )

Source

fn parse_decorators(&mut self) -> bool

Source

fn validate_assignment_target( &mut self, expr: &Expression, allow_destructuring: bool, is_logical_assign: bool, was_paren: bool, spread_comma_increased: bool, paren_destr_increased: bool, )

Source

fn validate_array_destr(&mut self, items: &[Expression])

Source

fn validate_object_destr(&mut self, props: &[Property])

Source

pub fn errors(&self) -> &[ParseError]

記録済みの構文エラー(上限まで)。

Source

pub fn error_count(&self) -> usize

検出した構文エラーの総数(記録上限で切り捨てた分も含む)。

Source

pub fn take_errors(&mut self) -> Vec<ParseError>

記録済みの構文エラーを取り出す(呼び出し側へ渡してログ出力する用途)。

Source

fn record_error(&mut self, message: String)

Source

fn context_around(&self, at: usize) -> String

エラー位置の前後のトークンを短い文字列にする。

前 6 個・後 3 個。落ちた位置は »« で囲む。

Source

fn absorb_errors(&mut self, sub: Parser)

別インスタンス(テンプレート補間式などの副パーサ)が検出したエラーを 自分の記録へ取り込む。位置は副パーサ内の索引なので、由来が分かるよう 印を付ける。

Source

fn cur(&self) -> &Token

Source

fn at(&self, off: usize) -> &Token

Source

fn bump(&mut self) -> Token

Source

fn is_eof(&self) -> bool

Source

fn is_sym(&self, s: &str) -> bool

Source

fn sym_at(&self, off: usize, s: &str) -> bool

Source

fn ident_at(&self, off: usize, s: &str) -> bool

Source

fn kw_at(&self, off: usize, k: &str) -> bool

Source

fn is_property_key_token(&self, off: usize) -> bool

off の位置がオブジェクト/クラスのプロパティ名になり得るトークン (識別子/予約語/文字列/数値)かどうか(get/set アクセサ判定の先読み用)。

Source

fn check_escaped_element_name(&mut self, name: &str)

エスケープを含むクラス要素名を見直す(\u0023m・#\u0000・\u0000 は構文エラー)。 字句解析器は名前に使えない文字のエスケープも通すので、ここで ID_Start/ID_Continue を確かめる。

Source

fn bigint_key(&mut self, s: &str) -> Rc<str>

BigInt リテラルのプロパティ名(十進の文字列。{ 0x10n: 1 } は "16")。

Source

fn eat_sym(&mut self, s: &str) -> bool

Source

fn expect_sym(&mut self, s: &str)

記号 s を消費する。無ければ回復して継続しつつ構文エラーとして記録する (従来は let _ = で「無かった」事実ごと捨てていた)。

Source

fn is_kw(&self, k: &str) -> bool

Source

fn eat_kw(&mut self, k: &str) -> bool

Source

fn ident_name(&mut self) -> Option<String>

Source

fn save_err_state(&self) -> (usize, usize, usize, usize, usize, usize, usize)

Source

fn restore_err_state( &mut self, s: (usize, usize, usize, usize, usize, usize, usize), )

Source

pub fn parse_program(&mut self) -> Program

Source

pub fn parse_program_reporting(&mut self) -> (Program, Vec<ParseError>)

パースし、AST と構文エラーの両方を返す。

AST は従来どおり常に返る(ベストエフォート動作を変えない)ので、 呼び出し側は「実行はするが、壊れている事実は知っている」状態にできる。

Source

fn parse_statement(&mut self) -> Option<Statement>

Source

fn is_let_decl_start(&self) -> bool

Source

fn is_using_decl_start(&self, off: usize) -> bool

Source

fn parse_statement_in(&mut self, pos_kind: StmtPos) -> Option<Statement>

Source

fn parse_block(&mut self) -> Statement

Source

fn parse_brace_body(&mut self) -> Vec<Statement>

Source

fn parse_brace_body_no_scope(&mut self) -> Vec<Statement>

Source

fn parse_var_declaration(&mut self) -> Option<Statement>

Source

fn parse_using_declaration(&mut self, is_await: bool) -> Option<Statement>

using x = expr, y = expr2; / await using x = expr;。 仕様上バインディングは単純な識別子のみ(分割代入パターンは不可)。

Source

fn parse_if(&mut self) -> Option<Statement>

Source

fn parse_while(&mut self) -> Option<Statement>

Source

fn parse_do_while(&mut self) -> Option<Statement>

Source

fn parse_for(&mut self) -> Option<Statement>

Source

fn parse_for_inner(&mut self) -> Option<Statement>

Source

fn finish_c_for(&mut self, init: Option<Box<Statement>>) -> Option<Statement>

Source

fn parse_function_declaration(&mut self, is_async: bool) -> Option<Statement>

Source

fn parse_try(&mut self) -> Option<Statement>

Source

fn parse_module_specifier(&mut self) -> String

文字列指定子(module specifier)を読む。from "mod" の “mod” 部分など。

Source

fn eat_contextual(&mut self, word: &str) -> bool

文脈依存キーワード(from/as)を ident として消費する。一致したら true。

Source

fn parse_named_specifiers(&mut self) -> Vec<(String, String)>

{ a, b as c } の中身を (元名, ローカル名) ペアの列として読む。

Source

fn parse_import(&mut self) -> Option<Statement>

import 宣言。 形態: import "mod" / import def from "mod" / import * as ns from "mod" / import { a, b as c } from "mod" / import def, { a } from "mod" / import def, * as ns from "mod"。

Source

fn parse_export(&mut self) -> Option<Statement>

export 宣言。 形態: export default <expr> / export { a, b as c } / export { a } from "mod" / export * from "mod" / export var/let/const/function/class ...。

Source

fn export_decl_names(&mut self) -> Vec<String>

export に続く宣言から公開すべき名前を先読みで収集する(カーソルは進めない)。

Source

fn parse_expression_statement(&mut self) -> Option<Statement>

Source

fn parse_switch(&mut self) -> Option<Statement>

Source

fn parse_param_list(&mut self) -> Vec<Param>

Source

fn parse_pattern(&mut self) -> Pattern

束縛パターンをパース(識別子 / {...} / [...])。

Source

fn parse_object_pattern(&mut self) -> Pattern

Source

fn parse_array_pattern(&mut self) -> Pattern

Source

pub fn parse_expression(&mut self) -> Expression

Source

fn parse_assignment_top(&mut self) -> Expression

括弧/配列/オブジェクトリテラルの外側の通常代入式境界。 カバー文法の { x = 1 } (CoverInitializedName) や __proto__ 重複が残っていれば構文エラーにする。

Source

fn parse_assignment(&mut self) -> Expression

Source

fn try_parse_single_ident_arrow(&mut self) -> Option<Expression>

単一識別子アロー関数(x => ... または async x => ...)。 カッコ不要の形式であり、1〜2 トークン先読みのみで確定できる(バックトラックなし)。

Source

fn parse_cover_paren_or_arrow(&mut self, is_async: bool) -> Expression

カッコ付き式またはアロー関数引数リストのカバー文法(Cover Grammar)。

Source

fn cover_expr_to_param(&mut self, expr: Expression) -> Result<Param, String>

Source

fn cover_expr_to_pattern(&mut self, expr: Expression) -> Result<Pattern, String>

Source

fn cover_object_to_pattern( &mut self, props: Vec<Property>, ) -> Result<Pattern, String>

Source

fn cover_array_to_pattern( &mut self, exprs: Vec<Expression>, ) -> Result<Pattern, String>

Source

fn parse_arrow_body_ctx( &mut self, src_start: Option<u32>, is_async: bool, params: &[Param], ) -> Rc<FnBody>

Source

fn parse_conditional(&mut self) -> Expression

Source

fn parse_binary(&mut self, min_prec: u8) -> Expression

Source

fn current_binop_str(&self) -> Option<String>

現在のトークンが二項演算子なら、その文字列を返す(instanceof/in 含む)。

Source

fn parse_unary(&mut self) -> Expression

Source

fn parse_new(&mut self) -> Expression

Source

fn parse_postfix(&mut self) -> Expression

Source

fn parse_call_member(&mut self, allow_call: bool) -> Expression

メンバ/添字/呼出のチェーン。allow_call=false で呼出を抑止(new 用)。

Source

fn parse_call_member_tail( &mut self, expr: Expression, allow_call: bool, ) -> Expression

既存の式に続くメンバ/添字/呼出チェーンを消費する。

Source

fn parse_arguments(&mut self) -> Vec<Expression>

Source

fn parse_primary(&mut self) -> Expression

Source

fn parse_import_phase_call(&mut self) -> Expression

import.defer(x)・import.source(x)(段階つきの動的 import。引数はちょうど 1 つ)。 defer は通常の import(x) と同じ組込みを呼ぶ(読み込みを遅らせない)。 source は ECMAScript のソーステキストのモジュールに source の表現が無いので、 指定子を ToString したうえで SyntaxError で拒否する Promise を返す((async (s) => { ${s}; throw … })(x))。

Source

fn parse_function_expression(&mut self, is_async: bool) -> Expression

Source

fn parse_class(&mut self, require_name: bool) -> Expression

クラス(本体は常に strict モード)。

Source

fn scan_class_private_names(&mut self) -> Vec<(Rc<str>, u8)>

Source

fn parse_class_inner(&mut self, require_name: bool) -> Expression

Source

fn parse_array_literal(&mut self) -> Expression

Source

fn parse_object_literal(&mut self) -> Expression

Auto Trait Implementations§

§

impl !RefUnwindSafe for Parser

§

impl !Send for Parser

§

impl !Sync for Parser

§

impl !UnwindSafe for Parser

§

impl Freeze for Parser

§

impl Unpin for Parser

§

impl UnsafeUnpin for Parser

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.