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: usizebuf[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: boolImplementations§
Source§impl Parser
impl Parser
pub fn new(lexer: Lexer) -> Self
Sourcepub fn new_lazy(lexer: Lexer) -> Self
pub fn new_lazy(lexer: Lexer) -> Self
関数本体を遅延解析するパーサ(大きな本体は共有ソースと範囲だけ持ち、初回実行時に解析する)。 本体の中の構文エラーは、その関数を初めて実行するときに報告される。
fn build(lexer: Lexer, lazy: bool) -> Self
fn ctx_snapshot(&self) -> ParserCtx
fn new_sub(lexer: Lexer, ctx: ParserCtx) -> Self
pub(crate) fn current_eval_ctx(&self) -> EvalSyntaxCtx
pub(crate) fn apply_eval_ctx(&mut self, ctx: &EvalSyntaxCtx)
Sourcefn flush_lexer_errors(&mut self)
fn flush_lexer_errors(&mut self)
字句解析器で発生したエラーを(投機的先読み中でなければ)パーサのエラー記録へ引き取る。
Sourcefn ensure_pos(&mut self, target_pos: usize)
fn ensure_pos(&mut self, target_pos: usize)
論理トークン位置 target_pos まで buf に読み込む。
Sourcefn tok_at_abs(&self, abs: usize) -> Option<&Token>
fn tok_at_abs(&self, abs: usize) -> Option<&Token>
論理トークン位置 abs のトークンを参照する。
Sourcefn token_start_at(&self, abs: usize) -> u32
fn token_start_at(&self, abs: usize) -> u32
論理トークン位置 abs の開始バイト位置を返す。
Sourcefn token_start_opt(&self, abs: usize) -> Option<u32>
fn token_start_opt(&self, abs: usize) -> Option<u32>
論理トークン位置 abs の開始バイト位置(窓から外れていれば None)。
Sourcefn nl_before_at_abs(&self, abs: usize) -> bool
fn nl_before_at_abs(&self, abs: usize) -> bool
論理トークン位置 abs の直前に行終端文字があったか。
Sourcefn nl_before_cur(&self) -> bool
fn nl_before_cur(&self) -> bool
現在トークンの直前に行終端文字があったか(ASI 判定用)。
Sourcefn nl_before_at(&self, off: usize) -> bool
fn nl_before_at(&self, off: usize) -> bool
現在位置から off 個先のトークンの直前に行終端文字があったか。
Sourcefn record_fn_source(&mut self, body: &Rc<FnBody>, start: Option<u32>)
fn record_fn_source(&mut self, body: &Rc<FnBody>, start: Option<u32>)
直前に parse_fn_body が読んだ本体の閉じ } の直後のバイト位置を使い、start からの定義全体を
本体に記録する(Function.prototype.toString 用)。
Sourcefn fn_source_start(&self, is_async: bool) -> Option<u32>
fn fn_source_start(&self, is_async: bool) -> Option<u32>
function の定義の先頭のバイト位置(async function なら直前の async から)。
fn step(&mut self)
Sourcefn has_use_strict_directive(&mut self, at: usize) -> bool
fn has_use_strict_directive(&mut self, at: usize) -> bool
at から始まる文の並びの先頭(指令プロローグ)に "use strict" があるか。
Sourcefn lex_push(&mut self, barrier: bool)
fn lex_push(&mut self, barrier: bool)
関数本体({ ... })。遅延モードで本体が大きければ、対応する } までトークンを貯めずに
字句解析器を直接進めて共有ソースと範囲 (start..end) だけ持つ。
fn lex_pop(&mut self)
Sourcefn lex_is_const(&self, name: &str) -> bool
fn lex_is_const(&self, name: &str) -> bool
name が同じ関数の中で見える const か。
Sourcefn lex_declare(&mut self, name: &Rc<str>, is_const: bool)
fn lex_declare(&mut self, name: &Rc<str>, is_const: bool)
宣言を今の枠へ記録する(const 以外は、同名の const を隠すときだけ)。
fn lex_declare_pattern(&mut self, pat: &Pattern, is_const: bool)
Sourcefn lex_note_stmt(&mut self, s: &Statement)
fn lex_note_stmt(&mut self, s: &Statement)
文が宣言する名前を今の枠へ記録する。
Sourcefn const_thrower(&mut self) -> Expression
fn const_thrower(&mut self) -> Expression
Assignment to constant variable. の TypeError を投げる呼び出し式。
Sourcefn lex_guard_const_target(&mut self, e: Expression) -> Expression
fn lex_guard_const_target(&mut self, e: Expression) -> Expression
分割代入(や for (x of …) の頭)の代入先に含まれる const の識別子を、評価すると TypeError を投げる
メンバー参照((() => { throw … })().x)へ置き換える。代入の時点(参照の評価)で投げる。
Sourcefn lex_guard_const_pattern(&mut self, p: Pattern) -> Pattern
fn lex_guard_const_pattern(&mut self, p: Pattern) -> Pattern
lex_guard_const_target の Pattern 版(for ([c] of …) の頭など)。
Sourcefn lex_check_const_assign(&mut self, e: Expression) -> Expression
fn lex_check_const_assign(&mut self, e: Expression) -> Expression
const への代入(文)を、右辺を評価してから TypeError を投げる式に置き換える。
fn parse_fn_body_with(&mut self, has_non_simple_params: bool) -> Rc<FnBody>
fn parse_static_block_body(&mut self) -> Rc<FnBody>
fn parse_fn_body(&mut self) -> Rc<FnBody>
fn parse_fn_body_with_params(&mut self, params: &[Param]) -> Rc<FnBody>
Sourcefn record_top_level_only_error(&mut self, message: String)
fn record_top_level_only_error(&mut self, message: String)
関数や eval を除いたスクリプト頂点のみでエラーになる構文(direct eval との互換用)。
fn scope_push(&mut self, kind: ScopeKind)
Sourcefn note_const_pattern(&mut self, pat: &Pattern)
fn note_const_pattern(&mut self, pat: &Pattern)
const 宣言の名前を今のスコープへ記録する(関数の境界を越えた const への代入検出用)。
Sourcefn scope_is_const(&self, name: &str) -> bool
fn scope_is_const(&self, name: &str) -> bool
name を最も内側で宣言しているスコープが const か(関数の境界を越えて探す)。
宣言より前に書かれた参照や、後ろで巻き上げられる var・関数は見えない(見逃すだけ)。
fn scope_pop(&mut self)
fn is_at_script_top_scope(&self) -> bool
fn declare_binding(&mut self, name: &Rc<str>, kind: BindingKind)
fn declare_pattern_bindings(&mut self, pat: &Pattern, kind: BindingKind)
fn collect_pattern_names<'a>(pat: &'a Pattern, out: &mut Vec<&'a Rc<str>>)
fn check_lexical_disallows_let(&mut self, pat: &Pattern)
fn check_params(&mut self, params: &[Param], require_unique: bool)
Sourcefn check_retro_strict(
&mut self,
name: Option<&str>,
params: &[Param],
body: &Rc<FnBody>,
was_strict: bool,
)
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" })。
fn check_ident_allowed(&mut self, name: &str, is_binding: bool)
fn consume_binding_ident( &mut self, ctx_desc: &str, advance_on_err: bool, ) -> Rc<str>
fn try_consume_opt_fn_ident(&mut self) -> Option<Rc<str>>
fn check_label_ident(&mut self, name: &str)
fn is_private_declared(&self, name: &str) -> bool
fn check_private_declared(&mut self, name: &str)
fn expect_asi_semicolon(&mut self)
fn check_accessor_params( &mut self, accessor_kind: Option<bool>, params: &[Param], )
fn parse_decorators(&mut self) -> bool
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, )
fn validate_array_destr(&mut self, items: &[Expression])
fn validate_object_destr(&mut self, props: &[Property])
Sourcepub fn errors(&self) -> &[ParseError]
pub fn errors(&self) -> &[ParseError]
記録済みの構文エラー(上限まで)。
Sourcepub fn error_count(&self) -> usize
pub fn error_count(&self) -> usize
検出した構文エラーの総数(記録上限で切り捨てた分も含む)。
Sourcepub fn take_errors(&mut self) -> Vec<ParseError>
pub fn take_errors(&mut self) -> Vec<ParseError>
記録済みの構文エラーを取り出す(呼び出し側へ渡してログ出力する用途)。
fn record_error(&mut self, message: String)
Sourcefn context_around(&self, at: usize) -> String
fn context_around(&self, at: usize) -> String
エラー位置の前後のトークンを短い文字列にする。
前 6 個・後 3 個。落ちた位置は »« で囲む。
Sourcefn absorb_errors(&mut self, sub: Parser)
fn absorb_errors(&mut self, sub: Parser)
別インスタンス(テンプレート補間式などの副パーサ)が検出したエラーを 自分の記録へ取り込む。位置は副パーサ内の索引なので、由来が分かるよう 印を付ける。
fn cur(&self) -> &Token
fn at(&self, off: usize) -> &Token
fn bump(&mut self) -> Token
fn is_eof(&self) -> bool
fn is_sym(&self, s: &str) -> bool
fn sym_at(&self, off: usize, s: &str) -> bool
fn ident_at(&self, off: usize, s: &str) -> bool
fn kw_at(&self, off: usize, k: &str) -> bool
Sourcefn is_property_key_token(&self, off: usize) -> bool
fn is_property_key_token(&self, off: usize) -> bool
off の位置がオブジェクト/クラスのプロパティ名になり得るトークン
(識別子/予約語/文字列/数値)かどうか(get/set アクセサ判定の先読み用)。
Sourcefn check_escaped_element_name(&mut self, name: &str)
fn check_escaped_element_name(&mut self, name: &str)
エスケープを含むクラス要素名を見直す(\u0023m・#\u0000・\u0000 は構文エラー)。
字句解析器は名前に使えない文字のエスケープも通すので、ここで ID_Start/ID_Continue を確かめる。
Sourcefn bigint_key(&mut self, s: &str) -> Rc<str>
fn bigint_key(&mut self, s: &str) -> Rc<str>
BigInt リテラルのプロパティ名(十進の文字列。{ 0x10n: 1 } は "16")。
fn eat_sym(&mut self, s: &str) -> bool
Sourcefn expect_sym(&mut self, s: &str)
fn expect_sym(&mut self, s: &str)
記号 s を消費する。無ければ回復して継続しつつ構文エラーとして記録する
(従来は let _ = で「無かった」事実ごと捨てていた)。
fn is_kw(&self, k: &str) -> bool
fn eat_kw(&mut self, k: &str) -> bool
fn ident_name(&mut self) -> Option<String>
fn save_err_state(&self) -> (usize, usize, usize, usize, usize, usize, usize)
fn restore_err_state( &mut self, s: (usize, usize, usize, usize, usize, usize, usize), )
pub fn parse_program(&mut self) -> Program
Sourcepub fn parse_program_reporting(&mut self) -> (Program, Vec<ParseError>)
pub fn parse_program_reporting(&mut self) -> (Program, Vec<ParseError>)
パースし、AST と構文エラーの両方を返す。
AST は従来どおり常に返る(ベストエフォート動作を変えない)ので、 呼び出し側は「実行はするが、壊れている事実は知っている」状態にできる。
fn parse_statement(&mut self) -> Option<Statement>
fn is_let_decl_start(&self) -> bool
fn is_using_decl_start(&self, off: usize) -> bool
fn parse_statement_in(&mut self, pos_kind: StmtPos) -> Option<Statement>
fn parse_block(&mut self) -> Statement
fn parse_brace_body(&mut self) -> Vec<Statement>
fn parse_brace_body_no_scope(&mut self) -> Vec<Statement>
fn parse_var_declaration(&mut self) -> Option<Statement>
Sourcefn parse_using_declaration(&mut self, is_await: bool) -> Option<Statement>
fn parse_using_declaration(&mut self, is_await: bool) -> Option<Statement>
using x = expr, y = expr2; / await using x = expr;。
仕様上バインディングは単純な識別子のみ(分割代入パターンは不可)。
fn parse_if(&mut self) -> Option<Statement>
fn parse_while(&mut self) -> Option<Statement>
fn parse_do_while(&mut self) -> Option<Statement>
fn parse_for(&mut self) -> Option<Statement>
fn parse_for_inner(&mut self) -> Option<Statement>
fn finish_c_for(&mut self, init: Option<Box<Statement>>) -> Option<Statement>
fn parse_function_declaration(&mut self, is_async: bool) -> Option<Statement>
fn parse_try(&mut self) -> Option<Statement>
Sourcefn parse_module_specifier(&mut self) -> String
fn parse_module_specifier(&mut self) -> String
文字列指定子(module specifier)を読む。from "mod" の “mod” 部分など。
Sourcefn eat_contextual(&mut self, word: &str) -> bool
fn eat_contextual(&mut self, word: &str) -> bool
文脈依存キーワード(from/as)を ident として消費する。一致したら true。
Sourcefn parse_named_specifiers(&mut self) -> Vec<(String, String)>
fn parse_named_specifiers(&mut self) -> Vec<(String, String)>
{ a, b as c } の中身を (元名, ローカル名) ペアの列として読む。
Sourcefn parse_import(&mut self) -> Option<Statement>
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"。
Sourcefn parse_export(&mut self) -> Option<Statement>
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 ...。
Sourcefn export_decl_names(&mut self) -> Vec<String>
fn export_decl_names(&mut self) -> Vec<String>
export に続く宣言から公開すべき名前を先読みで収集する(カーソルは進めない)。
fn parse_expression_statement(&mut self) -> Option<Statement>
fn parse_switch(&mut self) -> Option<Statement>
fn parse_param_list(&mut self) -> Vec<Param>
Sourcefn parse_pattern(&mut self) -> Pattern
fn parse_pattern(&mut self) -> Pattern
束縛パターンをパース(識別子 / {...} / [...])。
fn parse_object_pattern(&mut self) -> Pattern
fn parse_array_pattern(&mut self) -> Pattern
pub fn parse_expression(&mut self) -> Expression
Sourcefn parse_assignment_top(&mut self) -> Expression
fn parse_assignment_top(&mut self) -> Expression
括弧/配列/オブジェクトリテラルの外側の通常代入式境界。
カバー文法の { x = 1 } (CoverInitializedName) や __proto__ 重複が残っていれば構文エラーにする。
fn parse_assignment(&mut self) -> Expression
Sourcefn try_parse_single_ident_arrow(&mut self) -> Option<Expression>
fn try_parse_single_ident_arrow(&mut self) -> Option<Expression>
単一識別子アロー関数(x => ... または async x => ...)。
カッコ不要の形式であり、1〜2 トークン先読みのみで確定できる(バックトラックなし)。
Sourcefn parse_cover_paren_or_arrow(&mut self, is_async: bool) -> Expression
fn parse_cover_paren_or_arrow(&mut self, is_async: bool) -> Expression
カッコ付き式またはアロー関数引数リストのカバー文法(Cover Grammar)。
fn cover_expr_to_param(&mut self, expr: Expression) -> Result<Param, String>
fn cover_expr_to_pattern(&mut self, expr: Expression) -> Result<Pattern, String>
fn cover_object_to_pattern( &mut self, props: Vec<Property>, ) -> Result<Pattern, String>
fn cover_array_to_pattern( &mut self, exprs: Vec<Expression>, ) -> Result<Pattern, String>
fn parse_arrow_body_ctx( &mut self, src_start: Option<u32>, is_async: bool, params: &[Param], ) -> Rc<FnBody>
fn parse_conditional(&mut self) -> Expression
fn parse_binary(&mut self, min_prec: u8) -> Expression
Sourcefn current_binop_str(&self) -> Option<String>
fn current_binop_str(&self) -> Option<String>
現在のトークンが二項演算子なら、その文字列を返す(instanceof/in 含む)。
fn parse_unary(&mut self) -> Expression
fn parse_new(&mut self) -> Expression
fn parse_postfix(&mut self) -> Expression
Sourcefn parse_call_member(&mut self, allow_call: bool) -> Expression
fn parse_call_member(&mut self, allow_call: bool) -> Expression
メンバ/添字/呼出のチェーン。allow_call=false で呼出を抑止(new 用)。
Sourcefn parse_call_member_tail(
&mut self,
expr: Expression,
allow_call: bool,
) -> Expression
fn parse_call_member_tail( &mut self, expr: Expression, allow_call: bool, ) -> Expression
既存の式に続くメンバ/添字/呼出チェーンを消費する。
fn parse_arguments(&mut self) -> Vec<Expression>
fn parse_primary(&mut self) -> Expression
Sourcefn parse_import_phase_call(&mut self) -> Expression
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))。
fn parse_function_expression(&mut self, is_async: bool) -> Expression
Sourcefn parse_class(&mut self, require_name: bool) -> Expression
fn parse_class(&mut self, require_name: bool) -> Expression
クラス(本体は常に strict モード)。