@ad2302/pattern-match 中文文档教程

发布于 3 年前 浏览 11 项目主页 更新于 2 年前

pattern-match

用于 JavaScript 的模式匹配 DSL。 该模块是一个功能 获取任意 JavaScript 值并针对 模式。 如果匹配成功,结果是一个子匹配对象, 它由匹配命名的值的子组件组成 子模式(使用 var 模式)。 如果匹配失败,一个 MatchError 被抛出。

Usage

这是一个使用模式匹配来分析 AST 的简单示例 对于一种假设的语言:

var match = require('pattern-match');

match(ast, function(when) {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, function(vars) {
        this.analyzeFunctionCall(vars.callee, vars.args);
    }, this);

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, function(vars) {
        this.analyzeAssignment(vars.lhs, vars.rhs);
    }, this);

    when({
        type: 'Return',
        arg: match.var('arg')
    }, function(vars) {
        this.analyzeReturn(vars.arg);
    }, this);
}, this);

这将在 ES6 中通过解构变得更甜美:

var match = require('pattern-match');

match(ast, function(when) {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, function({ callee, args }) {
        this.analyzeFunctionCall(callee, args);
    }, this);

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, function({ lhs, rhs }) {
        this.analyzeAssignment(lhs, rhs);
    }, this);

    when({
        type: 'Return',
        arg: match.var('arg')
    }, function({ arg }) {
        this.analyzeReturn(arg);
    }, this);
}, this);

使用 ES6 箭头函数更甜美:

var match = require('pattern-match');

match(ast, (when) => {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, ({ callee, args }) => {
        this.analyzeFunctionCall(callee, args);
    });

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, ({ lhs, rhs }) => {
        this.analyzeAssignment(lhs, rhs);
    });

    when({
        type: 'Return',
        arg: match.var('arg')
    }, ({ arg }) => {
        this.analyzeReturn(arg);
    });
});

API

Entry points

  • match(x, body[, thisArg])

x 与一系列模式匹配,返回结果 第一次成功匹配。 案例由 body 函数

  • body.call(thisArg, when)

提供:通过按案例应该出现的顺序调用 when 来提供案例 尝试过。 该库使用提供给 matchthisArg 调用 body 作为 this 的绑定。

  • when(pattern[, template[, thisArg]])

提供下一个案例,由一个可选的模式组成 模板。 如果模式匹配成功,则将结果传递给 template with thisArg 绑定到 this(默认为全局 目的)。 如果未提供 template,则这种情况会生成 子匹配对象。

  • match(x).when(pattern[, template[, thisArg]])

x 与单个模式匹配。 返回调用结果 template 在带有 thisArg 的子匹配对象上(或全局 默认情况下对象)作为 this 的绑定。 如果 template 不是 提供,返回子匹配对象。

Patterns

  • match.any - matches any value.
  • match.primitive - matches any primitive (non-object) value.
  • match.object - matches any non-null object.
  • match.array - matches anything Array.isArray matches.
  • match.function - assumes the pattern is a boolean-valued function and matches any value for which the function returns true.
  • match.null - matches the null value.
  • match.undefined - matches the undefined value.
  • match.boolean - matches any boolean value.
  • match.number - matches any number value.
  • match.int32 - matches any integral number value in the range [-2^31, 2^31).
  • match.uint32 - matches any integral number value in the range [0, 2^32).
  • match.integer - matches any integral number value, including -Infinity and Infinity.
  • match.finite - matches any number value other than NaN, -Infinity, and Infinity.
  • match.infinite - matches -Infinity and Infinity.
  • match.negative - matches any number less than 0.
  • match.positive - matches any number greater than 0.
  • match.nonnegative - matches any number greater than or equal to 0 (including -0, which most of the time should just be considered 0).
  • match.plusZero - matches only +0 (and not -0). If you don't know if you need this, don't use it.
  • match.minusZero - matches only -0 (and not +0). If you don't know if you need this, don't use it.
  • match.range(low, high) - matches any number value in the half-open range [low, high).
  • match.string - matches any string value.
  • match.var(name[, pattern]) - matches the pattern (defaults to any) and saves the value in the sub-match object with property name name.
  • match.all(pattern, …) - matches if every pattern matches.
  • match.some(pattern, …) - matches if one pattern matches.
  • pred(testValue) - matches any value for which pred returns a truthy value.
  • { x1: pattern1, …, xn: patternn } - matches any object with property names x1 to xn matching patterns pattern1 to patternn, respectively. Only the own properties of the pattern are used.
  • [ pattern0, …, patternn ] - matches any object with property names 0 to n matching patterns pattern0 to patternn, respectively.

Custom patterns

您可以通过扩展根模式原型来创建自定义模式。

  • match.pattern - the root pattern prototype.

Match errors

  • match.MatchError - an object extending Error that represents a failed pattern-match.
    • e.expected - the expected pattern.
    • e.actual - the actual value tested.

pattern-match

A pattern matching DSL for JavaScript. The module is a function that takes an arbitrary JavaScript value and tests it against a pattern. If the match succeeds, the result is a sub-match object, which consists of the sub-components of the value that matched named sub-patterns (using the var pattern). If the match fails, a MatchError is thrown.

Usage

Here's a simple example of using pattern matching to analyze an AST for a hypothetical language:

var match = require('pattern-match');

match(ast, function(when) {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, function(vars) {
        this.analyzeFunctionCall(vars.callee, vars.args);
    }, this);

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, function(vars) {
        this.analyzeAssignment(vars.lhs, vars.rhs);
    }, this);

    when({
        type: 'Return',
        arg: match.var('arg')
    }, function(vars) {
        this.analyzeReturn(vars.arg);
    }, this);
}, this);

This will get sweeter in ES6 with destructuring:

var match = require('pattern-match');

match(ast, function(when) {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, function({ callee, args }) {
        this.analyzeFunctionCall(callee, args);
    }, this);

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, function({ lhs, rhs }) {
        this.analyzeAssignment(lhs, rhs);
    }, this);

    when({
        type: 'Return',
        arg: match.var('arg')
    }, function({ arg }) {
        this.analyzeReturn(arg);
    }, this);
}, this);

And sweeter still with ES6 arrow-functions:

var match = require('pattern-match');

match(ast, (when) => {
    when({
        type: 'FunctionCall',
        callee: match.var('callee'),
        args: match.var('args')
    }, ({ callee, args }) => {
        this.analyzeFunctionCall(callee, args);
    });

    when({
        type: 'Assignment',
        lhs: match.var('lhs'),
        rhs: match.var('rhs')
    }, ({ lhs, rhs }) => {
        this.analyzeAssignment(lhs, rhs);
    });

    when({
        type: 'Return',
        arg: match.var('arg')
    }, ({ arg }) => {
        this.analyzeReturn(arg);
    });
});

API

Entry points

  • match(x, body[, thisArg])

Match x against a sequence of patterns, returning the result of the first successful match. The cases are provided by the body function:

  • body.call(thisArg, when)

Provides the cases by calling when in the order the cases should be tried. The library calls body with the thisArg provided to match as the binding of this.

  • when(pattern[, template[, thisArg]])

Provides the next case, consisting of a pattern an optional template. If matching the pattern succeeds, the result is passed to template with thisArg bound to this (defaults to the global object). If template is not provided, this case produces the sub-match object.

  • match(x).when(pattern[, template[, thisArg]])

Match x against a single pattern. Returns the result of calling template on the sub-match object with thisArg (or the global object by default) as the binding of this. If template is not provided, returns the sub-match object.

Patterns

  • match.any - matches any value.
  • match.primitive - matches any primitive (non-object) value.
  • match.object - matches any non-null object.
  • match.array - matches anything Array.isArray matches.
  • match.function - assumes the pattern is a boolean-valued function and matches any value for which the function returns true.
  • match.null - matches the null value.
  • match.undefined - matches the undefined value.
  • match.boolean - matches any boolean value.
  • match.number - matches any number value.
  • match.int32 - matches any integral number value in the range [-2^31, 2^31).
  • match.uint32 - matches any integral number value in the range [0, 2^32).
  • match.integer - matches any integral number value, including -Infinity and Infinity.
  • match.finite - matches any number value other than NaN, -Infinity, and Infinity.
  • match.infinite - matches -Infinity and Infinity.
  • match.negative - matches any number less than 0.
  • match.positive - matches any number greater than 0.
  • match.nonnegative - matches any number greater than or equal to 0 (including -0, which most of the time should just be considered 0).
  • match.plusZero - matches only +0 (and not -0). If you don't know if you need this, don't use it.
  • match.minusZero - matches only -0 (and not +0). If you don't know if you need this, don't use it.
  • match.range(low, high) - matches any number value in the half-open range [low, high).
  • match.string - matches any string value.
  • match.var(name[, pattern]) - matches the pattern (defaults to any) and saves the value in the sub-match object with property name name.
  • match.all(pattern, …) - matches if every pattern matches.
  • match.some(pattern, …) - matches if one pattern matches.
  • pred(testValue) - matches any value for which pred returns a truthy value.
  • { x1: pattern1, …, xn: patternn } - matches any object with property names x1 to xn matching patterns pattern1 to patternn, respectively. Only the own properties of the pattern are used.
  • [ pattern0, …, patternn ] - matches any object with property names 0 to n matching patterns pattern0 to patternn, respectively.

Custom patterns

You can create custom patterns by extending the root pattern prototype.

  • match.pattern - the root pattern prototype.

Match errors

  • match.MatchError - an object extending Error that represents a failed pattern-match.
    • e.expected - the expected pattern.
    • e.actual - the actual value tested.
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文