#算术 #表达式解析器 #脚本语言 #脚本 #语言 #解析器

no-std arithmetic-parser

具有灵活字面量和类型注解的算术表达式解析器

4 个版本 (2 个破坏性更新)

0.3.0 2021 年 5 月 24 日
0.2.0 2020 年 12 月 5 日
0.2.0-beta.12020 年 10 月 4 日
0.1.0 2020 年 6 月 1 日

#894 in 数学

Download history 25/week @ 2024-02-19 14/week @ 2024-02-26 7/week @ 2024-03-04 30/week @ 2024-03-11 10/week @ 2024-03-18 10/week @ 2024-03-25 29/week @ 2024-04-01 7/week @ 2024-04-08 23/week @ 2024-04-15

71 每月下载次数
用于 3 crates

MIT/Apache

195KB
4.5K SLoC

灵活算术解析器

Build Status License: MIT OR Apache-2.0 rust 1.44+ required

链接: Docs.rs crate docs (master) changelog

这是一种适用于算术表达式的多功能解析器,它允许自定义字面量定义、类型注解以及解析的多个其他方面。

使用方法

将以下内容添加到您的 Crate.toml

[dependencies]
arithmetic-parser = "0.3.0"

解析器在总体上与 Rust 类似。它支持变量、字面量、注释、算术和布尔运算、括号、函数调用、元组和元组解构、函数定义、块、方法和类型注解。换句话说,解析器构成了最小化脚本语言的基础,同时将某些方面留给用户(最重要的是字面量的指定)。

有关支持的特性的更多详细信息,请参阅 crate 文档。

代码示例

以下是一个使用具有实数值字面量和唯一支持的类型 Num 的文法进行解析的代码示例

// This is a comment.
x = 1 + 2.5 * 3 + sin(a^3 / b^2);

// Function declarations have syntax similar to Rust closures.
some_function = |x| {
    r = min(rand(), 0.5);
    r * x
};

// Objects are similar to JavaScript, except they require
// a preceding hash `#`, like in Rhai (https://rhai.rs/).
other_function = |a, b: Num| #{ sum: a + b, diff: a - b };

// Object destructuring is supported as well.
{ sum, diff: Num } = other_function(
    x,
    // Blocks are supported and have a similar syntax to Rust.
    some_function({ x = x - 0.5; x }),
);

// Tuples have syntax similar to Rust (besides spread syntax
// in destructuring, which is similar to one in JavaScript).
(x, ...tail) = (1, 2).map(some_function);

实现细节

解析器基于 nom crate。库的核心特质 Grammar 被设计得如此,以至于切换可选功能不应产生运行时开销;在编译期间应删除未使用的解析代码路径。

另请参阅

许可证

根据您的选择,在以下两种许可证下获得许可:[Apache License, Version 2.0](https://github.com/slowli/arithmetic-parser/blob/5026e45ca6de3c3ddbddf47ea95a2e4c5948117c/parser/LICENSE-APACHE) 或 [MIT许可证](https://github.com/slowli/arithmetic-parser/blob/5026e45ca6de3c3ddbddf47ea95a2e4c5948117c/parser/LICENSE-MIT)。

除非您明确声明,否则根据Apache-2.0许可证定义,您提交给 arithmetic-parser 的任何有意贡献都应按上述方式双重许可,不附加任何额外条款或条件。

依赖关系

~2.5MB
~54K SLoC