Skip to content

语义分析

语义分析是检查源代码是否正确的过程。我们需要根据 ECMAScript 规范中的所有“早期错误”规则进行检查。

上下文

对于诸如 [Yield][Await] 等语法上下文,当语法禁止使用它们时,需要抛出错误,例如:

BindingIdentifier[Yield, Await] :
  Identifier
  yield
  await

13.1.1 静态语义:早期错误

BindingIdentifier[Yield, Await] : yield
* 如果此产生式具有 [Yield] 参数,则为语法错误。

* BindingIdentifier[Yield, Await] : await
如果此产生式具有 [Await] 参数,则为语法错误。

需要对以下代码抛出错误:

javascript
async function* foo() {
  var yield, await;
}

因为 AsyncGeneratorDeclaration 为其 AsyncGeneratorBody 设置了 [+Yield][+Await]

AsyncGeneratorBody :
  FunctionBody[+Yield, +Await]

Biome 检查 yield 关键字的一个示例:

rust
// https://github.com/rome/tools/blob/5a059c0413baf1d54436ac0c149a829f0dfd1f4d/crates/rome_js_parser/src/syntax/expr.rs#L1368-L1377

pub(super) fn parse_identifier(p: &mut Parser, kind: JsSyntaxKind) -> ParsedSyntax {
    if !is_at_identifier(p) {
        return Absent;
    }

    let error = match p.cur() {
        T![yield] if p.state.in_generator() => Some(
            p.err_builder("在生成器函数中非法使用 `yield` 作为标识符")
                .primary(p.cur_range(), ""),
        ),

范围

对于声明错误:

14.2.1 静态语义:早期错误

块 : { 语句列表 }
* 如果语句列表的词法声明名称(LexicallyDeclaredNames)包含任何重复项,则为语法错误。
* 如果语句列表的词法声明名称中的任意元素也出现在语句列表的变量声明名称(VarDeclaredNames)中,则为语法错误。

我们需要添加一个作用域树。作用域树包含其内部声明的所有 varlet。它也是一个父指针树,我们希望向上遍历该树,并在父作用域中查找绑定标识符。我们可以使用 indextree 数据结构。

rust
use indextree::{Arena, Node, NodeId};
use bitflags::bitflags;

pub type Scopes = Arena<Scope>;
pub type ScopeId = NodeId;

bitflags! {
    #[derive(Default)]
    pub struct ScopeFlags: u8 {
        const TOP = 1 << 0;
        const FUNCTION = 1 << 1;
        const ARROW = 1 << 2;
        const CLASS_STATIC_BLOCK = 1 << 4;
        const VAR = Self::TOP.bits | Self::FUNCTION.bits | Self::CLASS_STATIC_BLOCK.bits;
    }
}

#[derive(Debug, Clone)]
pub struct Scope {
    /// [严格模式代码](https://tc39.es/ecma262/#sec-strict-mode-code)
    /// [使用严格指令前言](https://tc39.es/ecma262/#sec-directive-prologues-and-the-use-strict-directive)
    pub strict_mode: bool,

    pub flags: ScopeFlags,

    /// [词法声明名称](https://tc39.es/ecma262/#sec-static-semantics-lexicallydeclarednames)
    pub lexical: IndexMap<Atom, SymbolId, FxBuildHasher>,

    /// [变量声明名称](https://tc39.es/ecma262/#sec-static-semantics-vardeclarednames)
    pub var: IndexMap<Atom, SymbolId, FxBuildHasher>,

    /// 函数声明
    pub function: IndexMap<Atom, SymbolId, FxBuildHasher>,
}

作用域树可以为了性能原因在解析器内部构建,也可以在另一个 AST 遍历过程中构建。

通常,需要一个 ScopeBuilder

rust
pub struct ScopeBuilder {
    scopes: Scopes,
    root_scope_id: ScopeId,
    current_scope_id: ScopeId,
}

impl ScopeBuilder {
    pub fn current_scope(&self) -> &Scope {
        self.scopes[self.current_scope_id].get()
    }

    pub fn enter_scope(&mut self, flags: ScopeFlags) {
        // 为函数继承严格模式
        // https://tc39.es/ecma262/#sec-strict-mode-code
        let mut strict_mode = self.scopes[self.root_scope_id].get().strict_mode;
        let parent_scope = self.current_scope();
        if !strict_mode
            && parent_scope.flags.intersects(ScopeFlags::FUNCTION)
            && parent_scope.strict_mode
        {
            strict_mode = true;
        }

        let scope = Scope::new(flags, strict_mode);
        let new_scope_id = self.scopes.new_node(scope);
        self.current_scope_id.append(new_scope_id, &mut self.scopes);
        self.current_scope_id = new_scope_id;
    }

    pub fn leave_scope(&mut self) {
      self.current_scope_id = self.scopes[self.current_scope_id].parent().unwrap();
    }
}

然后我们在解析函数中相应地调用 enter_scopeleave_scope,例如在 acorn 中:

javascript
https://github.com/acornjs/acorn/blob/11735729c4ebe590e406f952059813f250a4cbd1/acorn/src/statement.js#L425-L437

INFO

这种方案的一个缺点是,对于箭头函数,我们可能需要创建一个临时作用域,然后在后续丢弃它,特别是当它实际上不是箭头函数而是一个序列表达式时。这在 覆盖语法 中有详细说明。

访问者模式

如果我们决定为了简化而选择在另一轮遍历中构建作用域树,那么需要以深度优先前序方式遍历每个 AST 节点并构建作用域树。

我们可以使用 访问者模式 将遍历过程与每个对象上执行的操作分离开来。

在访问节点时,我们可以相应地调用 enter_scopeleave_scope 来构建作用域树。