Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
sparrowzz
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
sgool
sparrowzz
Commits
d797d415
Commit
d797d415
authored
Mar 03, 2025
by
dongshufeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: Bump
parent
c67d0a0d
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
422 行增加
和
137 行删除
+422
-137
Cargo.lock
+72
-89
eig-expr/Cargo.toml
+1
-1
eig-expr/src/expr_tensor.rs
+2
-2
eig-expr/src/lib.rs
+19
-21
eig-expr/src/tokenizer.rs
+328
-24
没有找到文件。
Cargo.lock
查看文件 @
d797d415
...
...
@@ -43,9 +43,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.9
6
"
version = "1.0.9
7
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4
"
checksum = "
dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f
"
[[package]]
name = "anymap2"
...
...
@@ -73,9 +73,9 @@ dependencies = [
[[package]]
name = "arrow-schema"
version = "54.2.
0
"
version = "54.2.
1
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
0f40f6be8f78af1ab610db7d9b236e21d587b7168e368a36275d2e5670096735
"
checksum = "
85934a9d0261e0fa5d4e2a5295107d743b543a6e0484a835d4b8db2da15306f9
"
dependencies = [
"serde",
]
...
...
@@ -112,9 +112,9 @@ dependencies = [
[[package]]
name = "bitflags"
version = "2.
8
.0"
version = "2.
9
.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36
"
checksum = "
5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd
"
[[package]]
name = "boolinator"
...
...
@@ -130,9 +130,9 @@ checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "bytemuck"
version = "1.2
1
.0"
version = "1.2
2
.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3
"
checksum = "
b6b1fc10dbac614ebc03540c9dbd60e83887fda27794998c6528f1782047d540
"
[[package]]
name = "byteorder"
...
...
@@ -163,9 +163,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.2.1
5
"
version = "1.2.1
6
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
c736e259eea577f443d5c86c304f9f4ae0295c43f3ba05c21f1d66b5f06001af
"
checksum = "
be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c
"
dependencies = [
"shlex",
]
...
...
@@ -178,16 +178,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.
39
"
version = "0.4.
40
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825
"
checksum = "
1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c
"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-
targets
",
"windows-
link
",
]
[[package]]
...
...
@@ -313,27 +313,27 @@ checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
name = "derive_more"
version = "
1.0.0
"
version = "
2.0.1
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05
"
checksum = "
093242cf7570c207c83073cf82f79706fe7b8317e98620a47d5be7c3d8497678
"
dependencies = [
"derive_more-impl",
]
[[package]]
name = "derive_more-impl"
version = "
1.0.0
"
version = "
2.0.1
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22
"
checksum = "
bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3
"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
"unicode-xid",
]
...
...
@@ -345,7 +345,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -427,7 +427,7 @@ dependencies = [
"ciborium",
"log",
"mems",
"petgraph
0.7.1
",
"petgraph",
]
[[package]]
...
...
@@ -451,7 +451,7 @@ dependencies = [
"eig-domain",
"eig-expr",
"log",
"petgraph
0.7.1
",
"petgraph",
"serde",
]
...
...
@@ -482,9 +482,9 @@ dependencies = [
[[package]]
name = "either"
version = "1.1
3
.0"
version = "1.1
4
.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0
"
checksum = "
b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d
"
[[package]]
name = "encoding_rs"
...
...
@@ -519,21 +519,15 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "fixedbitset"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
[[package]]
name = "flate2"
version = "1.
0.35
"
version = "1.
1.0
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07
c"
checksum = "
11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38d
c"
dependencies = [
"crc32fast",
"miniz_oxide",
...
...
@@ -598,7 +592,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1023,7 +1017,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1146,7 +1140,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9311685eb9a34808bbb0608ad2fcab9ae216266beca5848613e95553ac914e3b"
dependencies = [
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1187,9 +1181,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.1
69
"
version = "0.2.1
70
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a
"
checksum = "
875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828
"
[[package]]
name = "linux-raw-sys"
...
...
@@ -1236,12 +1230,6 @@ dependencies = [
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
...
...
@@ -1274,7 +1262,7 @@ checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1294,12 +1282,11 @@ dependencies = [
[[package]]
name = "nom"
version = "
7.1.3
"
version = "
8.0.0
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d
273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a
"
checksum = "d
f9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405
"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
...
...
@@ -1398,24 +1385,14 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "petgraph"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [
"fixedbitset 0.4.2",
"indexmap 2.7.1",
"serde",
"serde_derive",
]
[[package]]
name = "petgraph"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772"
dependencies = [
"fixedbitset
0.5.7
",
"fixedbitset",
"indexmap 2.7.1",
"serde",
"serde_derive",
]
[[package]]
...
...
@@ -1473,7 +1450,7 @@ checksum = "f6e859e6e5bd50440ab63c47e3ebabc90f26251f7c73c3d3e837b74a1cc3fa67"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1501,9 +1478,9 @@ dependencies = [
[[package]]
name = "portable-atomic"
version = "1.1
0
.0"
version = "1.1
1
.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6
"
checksum = "
350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e
"
[[package]]
name = "portable-atomic-util"
...
...
@@ -1531,7 +1508,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac"
dependencies = [
"proc-macro2",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -1570,9 +1547,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.9
3
"
version = "1.0.9
4
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99
"
checksum = "
a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84
"
dependencies = [
"unicode-ident",
]
...
...
@@ -1658,9 +1635,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.3
8
"
version = "1.0.3
9
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc
"
checksum = "
c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801
"
dependencies = [
"proc-macro2",
]
...
...
@@ -1804,14 +1781,14 @@ checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
name = "serde_json"
version = "1.0.1
39
"
version = "1.0.1
40
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6
"
checksum = "
20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373
"
dependencies = [
"itoa",
"memchr",
...
...
@@ -1884,9 +1861,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.9
8
"
version = "2.0.9
9
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1
"
checksum = "
e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2
"
dependencies = [
"proc-macro2",
"quote",
...
...
@@ -1918,11 +1895,11 @@ dependencies = [
[[package]]
name = "thiserror"
version = "2.0.1
1
"
version = "2.0.1
2
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc
"
checksum = "
567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708
"
dependencies = [
"thiserror-impl 2.0.1
1
",
"thiserror-impl 2.0.1
2
",
]
[[package]]
...
...
@@ -1933,18 +1910,18 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
name = "thiserror-impl"
version = "2.0.1
1
"
version = "2.0.1
2
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2
"
checksum = "
7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d
"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -2004,7 +1981,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
...
...
@@ -2077,7 +2054,7 @@ dependencies = [
"log",
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
"wasm-bindgen-shared",
]
...
...
@@ -2112,7 +2089,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
...
...
@@ -2168,6 +2145,12 @@ dependencies = [
]
[[package]]
name = "windows-link"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dccfd733ce2b1753b03b6d3c65edf020262ea35e20ccdf3e288043e6dd620e3"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
...
...
@@ -2321,7 +2304,7 @@ dependencies = [
[[package]]
name = "yew-bulma"
version = "0.1.0"
source = "git+https://github.com/shufengdong/yew-bulma.git#
e553f07bcd09f5fdc24a503adb5fa961993cdd18
"
source = "git+https://github.com/shufengdong/yew-bulma.git#
9d4e782152f1fe43408dba5087638e24cb483ceb
"
dependencies = [
"bytes",
"chrono",
...
...
@@ -2329,7 +2312,7 @@ dependencies = [
"gloo-utils 0.2.0",
"js-sys",
"log",
"petgraph
0.6.5
",
"petgraph",
"serde",
"serde_cbor",
"serde_json",
...
...
@@ -2367,14 +2350,14 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.9
8
",
"syn 2.0.9
9
",
]
[[package]]
name = "zip"
version = "2.2.
2
"
version = "2.2.
3
"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "
ae9c1ea7b3a5e1f4b922ff856a129881167511563dc219869afe3787fc0c1a45
"
checksum = "
b280484c454e74e5fff658bbf7df8fdbe7a07c6b2de4a53def232c15ef138f3a
"
dependencies = [
"arbitrary",
"crc32fast",
...
...
@@ -2383,7 +2366,7 @@ dependencies = [
"flate2",
"indexmap 2.7.1",
"memchr",
"thiserror 2.0.1
1
",
"thiserror 2.0.1
2
",
"zopfli",
]
...
...
eig-expr/Cargo.toml
查看文件 @
d797d415
...
...
@@ -7,7 +7,7 @@ rust-version.workspace = true
[dependencies]
fnv
=
"1.0"
nom
=
"
7.1
"
nom
=
"
8.0
"
serde
=
{
version
=
"1.0"
,
features
=
["derive"]
}
num-traits
=
"0.2"
num-complex
=
"0.4"
...
...
eig-expr/src/expr_tensor.rs
查看文件 @
d797d415
...
...
@@ -3,7 +3,7 @@ use fnv::FnvHashMap;
use
ndarray
::{
Array
,
Ix1
,
Ix2
,
IxDyn
};
use
num_complex
::
Complex64
;
use
crate
::{
Ctx
Provider
,
Expr
,
Operation
,
Token
::
*
};
use
crate
::{
Ctx
Maps
,
Expr
,
Operation
,
Token
::
*
};
use
crate
::{
ContextProvider
,
Error
,
factorial
,
FuncEvalError
,
MyCx
,
MyF
};
use
crate
::
expr
::
Context
;
use
crate
::
expr_complex
::
ContextCx
;
...
...
@@ -13,7 +13,7 @@ use crate::tsfn_basic::*;
thread_local!
(
static
DEFAULT_CONTEXT
:
Context
<
'static
>
=
Context
::
new
());
thread_local!
(
pub
static
DEFAULT_CONTEXT_TENSOR
:
ContextTensor
<
'static
>
=
ContextTensor
::
new
());
impl
ContextProvider
for
Ctx
Provider
{
impl
ContextProvider
for
Ctx
Maps
{
fn
get_var
(
&
self
,
name
:
&
str
)
->
Option
<
f64
>
{
self
.var_values
.get
(
name
)
.cloned
()
}
...
...
eig-expr/src/lib.rs
查看文件 @
d797d415
...
...
@@ -33,7 +33,7 @@ pub enum MyCx {
pub
enum
ParseError
{
/// A token that is not allowed at the given location (contains the location of the offending
/// character in the source string).
UnexpectedToken
(
usize
),
UnexpectedToken
(
usize
,
usize
),
/// Missing right parentheses at the end of the source string (contains the number of missing
/// parens).
MissingRParen
(
i32
),
...
...
@@ -44,13 +44,9 @@ pub enum ParseError {
impl
Display
for
ParseError
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
fmt
::
Result
{
match
*
self
{
ParseError
::
UnexpectedToken
(
i
)
=>
write!
(
f
,
"Unexpected token at byte {}."
,
i
),
ParseError
::
MissingRParen
(
i
)
=>
write!
(
f
,
"Missing {} right parenthes{}."
,
i
,
if
i
==
1
{
"is"
}
else
{
"es"
}
),
ParseError
::
UnexpectedToken
(
row
,
col
)
=>
write!
(
f
,
"Unexpected char at line: {row} column: {col}"
),
ParseError
::
MissingRParen
(
i
)
=>
write!
(
f
,
"Missing {i} right parenthes{}."
,
if
i
==
1
{
"is"
}
else
{
"es"
}),
ParseError
::
MissingArgument
=>
write!
(
f
,
"Missing argument at the end of expression."
),
}
}
...
...
@@ -59,7 +55,7 @@ impl Display for ParseError {
impl
std
::
error
::
Error
for
ParseError
{
fn
description
(
&
self
)
->
&
str
{
match
*
self
{
ParseError
::
UnexpectedToken
(
_
)
=>
"unexpected token"
,
ParseError
::
UnexpectedToken
(
_
,
_
)
=>
"unexpected token"
,
ParseError
::
MissingRParen
(
_
)
=>
"missing right parenthesis"
,
ParseError
::
MissingArgument
=>
"missing argument"
,
}
...
...
@@ -79,7 +75,7 @@ impl Display for FuncEvalError {
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
fmt
::
Result
{
match
*
self
{
FuncEvalError
::
UnknownFunction
=>
write!
(
f
,
"Unknown function"
),
FuncEvalError
::
NumberArgs
(
i
)
=>
write!
(
f
,
"Expected {
} arguments"
,
i
),
FuncEvalError
::
NumberArgs
(
i
)
=>
write!
(
f
,
"Expected {
i} arguments"
),
FuncEvalError
::
TooFewArguments
=>
write!
(
f
,
"Too few arguments"
),
FuncEvalError
::
TooManyArguments
=>
write!
(
f
,
"Too many arguments"
),
}
...
...
@@ -134,19 +130,19 @@ impl Display for RPNError {
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
fmt
::
Result
{
match
*
self
{
RPNError
::
MismatchedLParen
(
i
)
=>
{
write!
(
f
,
"Mismatched left parenthesis at token {
}."
,
i
)
write!
(
f
,
"Mismatched left parenthesis at token {
i}."
)
}
RPNError
::
MismatchedRParen
(
i
)
=>
{
write!
(
f
,
"Mismatched right parenthesis at token {
}."
,
i
)
write!
(
f
,
"Mismatched right parenthesis at token {
i}."
)
}
RPNError
::
MismatchedLBracket
(
i
)
=>
{
write!
(
f
,
"Mismatched left blackets at token {
}."
,
i
)
write!
(
f
,
"Mismatched left blackets at token {
i}."
)
}
RPNError
::
MismatchedRBracket
(
i
)
=>
{
write!
(
f
,
"Mismatched right blackets at token {
}."
,
i
)
write!
(
f
,
"Mismatched right blackets at token {
i}."
)
}
RPNError
::
UnexpectedComma
(
i
)
=>
write!
(
f
,
"Unexpected comma at token {
}"
,
i
),
RPNError
::
NotEnoughOperands
(
i
)
=>
write!
(
f
,
"Missing operands at token {
}"
,
i
),
RPNError
::
UnexpectedComma
(
i
)
=>
write!
(
f
,
"Unexpected comma at token {
i}"
),
RPNError
::
NotEnoughOperands
(
i
)
=>
write!
(
f
,
"Missing operands at token {
i}"
),
RPNError
::
TooManyOperands
=>
{
write!
(
f
,
"Too many operands left at the end of expression."
)
}
...
...
@@ -306,7 +302,7 @@ pub struct Expr {
}
impl
Display
for
Expr
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
fmt
::
Result
{
write!
(
f
,
"{:?}"
,
self
)
}
}
...
...
@@ -344,22 +340,22 @@ pub trait ContextProvider {
}
}
pub
struct
Ctx
Provider
{
pub
struct
Ctx
Maps
{
var_values
:
HashMap
<
String
,
f64
>
,
var_values_cx
:
HashMap
<
String
,
Complex64
>
,
var_values_tensor
:
HashMap
<
String
,
Array
<
f64
,
IxDyn
>>
,
var_values_tensor_cx
:
HashMap
<
String
,
Array
<
Complex64
,
IxDyn
>>
,
}
impl
Default
for
Ctx
Provider
{
impl
Default
for
Ctx
Maps
{
fn
default
()
->
Self
{
Self
::
new
()
}
}
impl
Ctx
Provider
{
impl
Ctx
Maps
{
pub
fn
new
()
->
Self
{
Ctx
Provider
{
Ctx
Maps
{
var_values
:
Default
::
default
(),
var_values_cx
:
Default
::
default
(),
var_values_tensor
:
Default
::
default
(),
...
...
@@ -427,3 +423,4 @@ pub fn parse_exprs(s: &str) -> Option<Vec<(String, Expr)>> {
}
Some
(
exprs
)
}
// above should as same as in sparrowzz
\ No newline at end of file
eig-expr/src/tokenizer.rs
查看文件 @
d797d415
//! Tokenizer that converts a mathematical expression in a string form into a series of `Token`s.
//!
//! The underlying parser is build using the [nom] parser combinator crate.
//!
//! The parser should tokenize only well-formed expressions.
//!
//! [nom]: https://crates.io/crates/nom
//!
use
std
;
use
std
::
fmt
;
use
std
::
fmt
::{
Display
,
Formatter
};
...
...
@@ -9,7 +17,7 @@ use nom::character::complete::{digit0, digit1, multispace0};
use
nom
::
combinator
::{
complete
,
map
,
map_res
,
opt
};
use
nom
::
error
::{
Error
,
ErrorKind
};
use
nom
::
sequence
::{
delimited
,
preceded
,
terminated
};
use
nom
::
IResult
;
use
nom
::
{
IResult
,
Parser
}
;
use
crate
::{
ParseError
,
Token
,
Operation
};
#[derive(Debug,
Clone,
Copy)]
...
...
@@ -59,29 +67,29 @@ fn binop(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
map
(
tag
(
">"
),
|
_
|
Token
::
Binary
(
Operation
::
GreatThan
)),
map
(
tag
(
"<"
),
|
_
|
Token
::
Binary
(
Operation
::
LessThan
)),
)),
))(
i
)
))
.parse
(
i
)
}
//
fn
lparen
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
"("
),
|
_
|
Token
::
LParen
)(
i
)
map
(
tag
(
"("
),
|
_
|
Token
::
LParen
)
.parse
(
i
)
}
fn
tensor
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
"["
),
|
_
|
Token
::
Tensor
(
None
))(
i
)
map
(
tag
(
"["
),
|
_
|
Token
::
Tensor
(
None
))
.parse
(
i
)
}
fn
rparen
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
")"
),
|
_
|
Token
::
RParen
)(
i
)
map
(
tag
(
")"
),
|
_
|
Token
::
RParen
)
.parse
(
i
)
}
fn
rbracket
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
"]"
),
|
_
|
Token
::
RBracket
)(
i
)
map
(
tag
(
"]"
),
|
_
|
Token
::
RBracket
)
.parse
(
i
)
}
fn
fact
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
if
!
i
.starts_with
(
&
b
"!="
[
..
])
{
map
(
tag
(
"!"
),
|
_
|
Token
::
Unary
(
Operation
::
Fact
))(
i
)
map
(
tag
(
"!"
),
|
_
|
Token
::
Unary
(
Operation
::
Fact
))
.parse
(
i
)
}
else
{
Err
(
nom
::
Err
::
Error
(
Error
{
input
:
i
,
...
...
@@ -91,29 +99,29 @@ fn fact(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
}
fn
comma
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
","
),
|
_
|
Token
::
Comma
)(
i
)
map
(
tag
(
","
),
|
_
|
Token
::
Comma
)
.parse
(
i
)
}
fn
negpos
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
alt
((
map
(
tag
(
"+"
),
|
_
|
Token
::
Unary
(
Operation
::
Plus
)),
map
(
tag
(
"-"
),
|
_
|
Token
::
Unary
(
Operation
::
Minus
)),
))(
i
)
))
.parse
(
i
)
}
fn
not
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
"~~"
),
|
_
|
Token
::
Unary
(
Operation
::
Not
))(
i
)
map
(
tag
(
"~~"
),
|
_
|
Token
::
Unary
(
Operation
::
Not
))
.parse
(
i
)
}
fn
bitnot
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
map
(
tag
(
"~"
),
|
_
|
Token
::
Unary
(
Operation
::
BitNot
))(
i
)
map
(
tag
(
"~"
),
|
_
|
Token
::
Unary
(
Operation
::
BitNot
))
.parse
(
i
)
}
fn
number
(
i
:
&
[
u8
])
->
IResult
<&
[
u8
],
Token
,
Error
<&
[
u8
]
>>
{
let
(
left
,
mut
len
)
=
map
(
digit1
,
|
s
:
&
[
u8
]|
s
.len
())(
i
)
?
;
let
(
left
,
s
)
=
opt
(
preceded
(
tag
(
"."
),
map
(
digit0
,
|
s
:
&
[
u8
]|
s
.len
()
+
1
)))(
left
)
?
;
let
(
left
,
mut
len
)
=
map
(
digit1
,
|
s
:
&
[
u8
]|
s
.len
())
.parse
(
i
)
?
;
let
(
left
,
s
)
=
opt
(
preceded
(
tag
(
"."
),
map
(
digit0
,
|
s
:
&
[
u8
]|
s
.len
()
+
1
)))
.parse
(
left
)
?
;
len
+=
s
.unwrap_or
(
0
);
let
(
mut
left
,
op
)
=
opt
(
alt
((
tag
(
"e"
),
tag
(
"E"
))))(
left
)
?
;
let
(
mut
left
,
op
)
=
opt
(
alt
((
tag
(
"e"
),
tag
(
"E"
))))
.parse
(
left
)
?
;
if
op
.is_some
()
{
let
(
l
,
s
)
=
alt
((
preceded
(
...
...
@@ -121,7 +129,7 @@ fn number(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
map
(
digit1
,
|
s
:
&
[
u8
]|
s
.len
()
+
2
),
),
map
(
digit1
,
|
s
:
&
[
u8
]|
s
.len
()
+
1
),
))(
left
)
?
;
))
.parse
(
left
)
?
;
len
+=
s
;
left
=
l
;
}
...
...
@@ -142,6 +150,7 @@ fn ident(input: &[u8]) -> IResult<&[u8], &[u8], Error<&[u8]>> {
let
(
parsed
,
rest
)
=
input
.split_at
(
n
+
1
);
Ok
((
rest
,
parsed
))
}
// support chinese variable name
Some
(
b
'\''
)
|
Some
(
b
'\
"')=> {
let start = *input.first().unwrap();
let n = input
...
...
@@ -167,7 +176,7 @@ fn ident(input: &[u8]) -> IResult<&[u8], &[u8], Error<&[u8]>> {
}
fn var(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
map(map_res(ident, from_utf8), |s: &str| Token::Var(s.into()))(i)
map(map_res(ident, from_utf8), |s: &str| Token::Var(s.into()))
.parse
(i)
}
fn func(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
...
...
@@ -177,7 +186,7 @@ fn func(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
from_utf8,
),
|s: &str| Token::Func(s.into(), None),
)(i)
)
.parse
(i)
}
fn lexpr(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
...
...
@@ -185,7 +194,7 @@ fn lexpr(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
multispace0,
alt((number, func, tensor, var, negpos, lparen, not, bitnot, fact)),
multispace0,
)(i)
)
.parse
(i)
}
fn after_rexpr(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
...
...
@@ -193,11 +202,11 @@ fn after_rexpr(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
multispace0,
alt((binop, rparen, rbracket)),
multispace0,
)(i)
)
.parse
(i)
}
fn after_rexpr_no_paren(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
delimited(multispace0, binop, multispace0)(i)
delimited(multispace0, binop, multispace0)
.parse
(i)
}
fn after_rexpr_comma(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
...
...
@@ -205,7 +214,7 @@ fn after_rexpr_comma(i: &[u8]) -> IResult<&[u8], Token, Error<&[u8]>> {
multispace0,
alt((binop, rparen, rbracket, comma)),
multispace0,
)(i)
)
.parse
(i)
}
pub fn tokenize<S: AsRef<str>>(input: S) -> Result<Vec<Token>, ParseError> {
...
...
@@ -235,7 +244,7 @@ pub fn tokenize<S: AsRef<str>>(input: S) -> Result<Vec<Token>, ParseError> {
}
Token::Tensor(_) => {
paren_stack.push(ParenState::Tensor);
if let Ok((rest2, _)) = delimited(multispace0, rbracket, multispace0)(rest) {
if let Ok((rest2, _)) = delimited(multispace0, rbracket, multispace0)
.parse
(rest) {
res.push(Token::Tensor(Some(0)));
s = rest2;
paren_stack.pop().expect("
The
paren_stack
is
empty!
");
...
...
@@ -245,7 +254,7 @@ pub fn tokenize<S: AsRef<str>>(input: S) -> Result<Vec<Token>, ParseError> {
}
Token::Func(name, _) => {
paren_stack.push(ParenState::Func);
if let Ok((rest2, _)) = delimited(multispace0, rparen, multispace0)(rest) {
if let Ok((rest2, _)) = delimited(multispace0, rparen, multispace0)
.parse
(rest) {
res.push(Token::Func(name.clone(), Some(0)));
s = rest2;
paren_stack.pop().expect("
The
paren_stack
is
empty!
");
...
...
@@ -277,7 +286,7 @@ pub fn tokenize<S: AsRef<str>>(input: S) -> Result<Vec<Token>, ParseError> {
String::from_utf8_lossy(s),
r
);
return Err(ParseError::UnexpectedToken(s.len()));
return Err(ParseError::UnexpectedToken(
1,
s.len()));
}
}
}
...
...
@@ -337,3 +346,298 @@ impl Display for Token {
}
}
}
#[cfg(test)]
mod tests {
use nom::error;
use nom::error::ErrorKind::{Alpha, Digit};
use nom::Err::Error;
use crate::ParseError;
use super::*;
#[test]
fn test_binop() {
assert_eq!(
binop(b"
+
"),
Ok((&b""[..], Token::Binary(Operation::Plus)))
);
assert_eq!(
binop(b"
-
"),
Ok((&b""[..], Token::Binary(Operation::Minus)))
);
assert_eq!(
binop(b"
*
"),
Ok((&b""[..], Token::Binary(Operation::Times)))
);
assert_eq!(
binop(b"
/
"),
Ok((&b""[..], Token::Binary(Operation::Div)))
);
}
#[test]
fn test_number() {
assert_eq!(
number(b"
32143
"),
Ok((&b""[..], Token::Number(32143f64)))
);
assert_eq!(
number(b"
2
.
"),
Ok((&b""[..], Token::Number(2.0f64)))
);
assert_eq!(
number(b"
32143.25
"),
Ok((&b""[..], Token::Number(32143.25f64)))
);
assert_eq!(
number(b"
0.125e9
"),
Ok((&b""[..], Token::Number(0.125e9f64)))
);
assert_eq!(
number(b"
20.5
E
-
3
"),
Ok((&b""[..], Token::Number(20.5E-3f64)))
);
assert_eq!(
number(b"
123423e+50
"),
Ok((&b""[..], Token::Number(123423e+50f64)))
);
assert_eq!(
number(b""),
Err(Error(error::Error {
input: &b""[..],
code: Digit
}))
);
assert_eq!(
number(b"
.
2
"),
Err(Error(error::Error {
input: &b"
.
2
"[..],
code: Digit
}))
);
assert_eq!(
number(b"
+
"),
Err(Error(error::Error {
input: &b"
+
"[..],
code: Digit
}))
);
assert_eq!(
number(b"
e
"),
Err(Error(error::Error {
input: &b"
e
"[..],
code: Digit
}))
);
assert_eq!(
number(b"
1
E
"),
Err(Error(error::Error {
input: &b""[..],
code: Digit
}))
);
assert_eq!(
number(b"
1
e
+
"),
Err(Error(error::Error {
input: &b"
+
"[..],
code: Digit
}))
);
}
#[test]
fn test_var() {
for &s in ["
abc
", "
U0
", "
_034
", "
a_be45EA
", "
aAzZ_
"].iter() {
assert_eq!(
var(s.as_bytes()),
Ok((&b""[..], Token::Var(s.into())))
);
}
for &s in ["
\
'a
\'
", "
\
"U0
\
"", "
\
"_034
\
"", "
'*'
", "
\
"+
\
""].iter() {
assert_eq!(
var(s.as_bytes()),
Ok((&b""[..], tokenize(s).unwrap()[0].clone()))
);
}
assert_eq!(
var(b""),
Err(Error(error::Error {
input: &b""[..],
code: Alpha
}))
);
assert_eq!(
var(b"
0
"),
Err(Error(error::Error {
input: &b"
0
"[..],
code: Alpha
}))
);
}
#[test]
fn test_func() {
for &s in ["
abc
(
", "
u0
(
", "
_034
(
", "
A_be45EA
(
"].iter() {
assert_eq!(
func(s.as_bytes()),
Ok((&b""[..], Token::Func(s[0..s.len() - 1].trim().into(), None)))
);
}
assert_eq!(
func(b""),
Err(Error(error::Error {
input: &b""[..],
code: Alpha
}))
);
assert_eq!(
func(b"
(
"),
Err(Error(error::Error {
input: &b"
(
"[..],
code: Alpha
}))
);
assert_eq!(
func(b"
0
(
"),
Err(Error(error::Error {
input: &b"
0
(
"[..],
code: Alpha
}))
);
}
#[test]
fn test_tokenize() {
use super::Operation::*;
use super::Token::*;
assert_eq!(tokenize("
a
"), Ok(vec![Var("
a
".into())]));
assert_eq!(
tokenize("
2
+
(
3
--
2
)
"),
Ok(vec![
Number(2f64),
Binary(Plus),
LParen,
Number(3f64),
Binary(Minus),
Unary(Minus),
Number(2f64),
RParen,
])
);
assert_eq!(
tokenize("
-
2
^
ab0
*
12
-
C_0
"),
Ok(vec![
Unary(Minus),
Number(2f64),
Binary(Pow),
Var("
ab0
".into()),
Binary(Times),
Number(12f64),
Binary(Minus),
Var("
C_0
".into()),
])
);
assert_eq!(
tokenize("
-
sin
(
pi
*
3
)
^
cos
(
2
)
/
Func2
(
x
,
f
(
y
),
z
)
*
_buildIN
(
y
)
"),
Ok(vec![
Unary(Minus),
Func("
sin
".into(), None),
Var("
pi
".into()),
Binary(Times),
Number(3f64),
RParen,
Binary(Pow),
Func("
cos
".into(), None),
Number(2f64),
RParen,
Binary(Div),
Func("
Func2
".into(), None),
Var("
x
".into()),
Comma,
Func("
f
".into(), None),
Var("
y
".into()),
RParen,
Comma,
Var("
z
".into()),
RParen,
Binary(Times),
Func("
_buildIN
".into(), None),
Var("
y
".into()),
RParen,
])
);
assert_eq!(
tokenize("
2
%
3
"),
Ok(vec![Number(2f64), Binary(Rem), Number(3f64)])
);
assert_eq!(
tokenize("
1
+
!
3
+
1
"),
Ok(vec![
Number(1f64),
Binary(Plus),
Unary(Fact),
Number(3f64),
Binary(Plus),
Number(1f64),
])
);
assert_eq!(tokenize("
3
!
"), Err(ParseError::UnexpectedToken(1, 1)));
assert_eq!(tokenize("
()
"), Err(ParseError::UnexpectedToken(1, 1)));
assert_eq!(tokenize(""), Err(ParseError::MissingArgument));
assert_eq!(tokenize("
2
)
"), Err(ParseError::UnexpectedToken(1, 1)));
assert_eq!(tokenize("
2
^
"), Err(ParseError::MissingArgument));
assert_eq!(tokenize("
(((
2
)
"), Err(ParseError::MissingRParen(2)));
assert_eq!(tokenize("
f
(
2
,)
"), Err(ParseError::UnexpectedToken(1, 1)));
assert_eq!(tokenize("
f
(,
2
)
"), Err(ParseError::UnexpectedToken(1, 3)));
}
#[test]
fn test_func_with_no_para() {
assert_eq!(
tokenize("
f
()
"),
Ok(vec![Token::Func("
f
".to_string(), Some(0))])
);
assert_eq!(
tokenize("
f
(
)
"),
Ok(vec![Token::Func("
f
".to_string(), Some(0))])
);
assert!(tokenize("
f
(
f2
(
1
),
f3
())
").is_ok());
assert!(tokenize("
f
(
f2
(
1
),
f3
(),
a
)
").is_ok());
assert!(tokenize("
f
(
a
,
b
,
f2
(),
f3
(),
c
)
").is_ok());
assert!(tokenize("
-
sin
(
pi
*
3
)
^
cos
(
2
)
/
Func2
(
x
,
f
(),
z
)
*
_buildIN
()
").is_ok());
}
#[test]
fn test_show_latex() {
//let test_token = tokenize("
x1
^
2
-
10
*
x1
+
x2
^
2
+
8
<=
5
*
2
").unwrap();
//let test_token = tokenize("
max
((
5
*
1
)
*
x1
+
3
*
x2
+
2
*
x3
+
(
10
-
3
)
*
x4
+
4
*
x5
)
").unwrap();
//let test_token = tokenize("
1
*
3
*
x2
+
sin
(
8
-
2
)
*
x3
-
cos
(
pi
)
<
7
").unwrap();
//let test_token = tokenize("
x1
%
5
+
3
/
3
*
x2
+
min
(
2
,
5
)
*
x3
*
2e19
&&
1
").unwrap();
//let test_token = tokenize("
2
!
").unwrap();
let test_token = tokenize("
~
x1
").unwrap();
println!("
{:
?
}
", test_token);
for x in test_token {
println!("
{}
", x);
}
}
#[test]
fn test_tensor() {
assert_eq!(
tokenize("
[
3
]
"),
Ok(vec![Token::Tensor(None), Token::Number(3.), Token::RBracket])
);
assert!(tokenize("
[[
1
,
2
],[
3
,
4
]]
").is_ok());
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论