From 1756b7fc17fa111e977f8828568417f876a9470a Mon Sep 17 00:00:00 2001 From: Vitaly Bogdanov Date: Mon, 14 Oct 2024 15:52:16 +0300 Subject: [PATCH 1/2] Fix parsing \n inside string in double quotes --- lib/src/metta/runner/stdlib_minimal.rs | 6 +++++- python/hyperon/stdlib.py | 2 +- python/tests/test_stdlib.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/metta/runner/stdlib_minimal.rs b/lib/src/metta/runner/stdlib_minimal.rs index 4f61f7068..066fe7503 100644 --- a/lib/src/metta/runner/stdlib_minimal.rs +++ b/lib/src/metta/runner/stdlib_minimal.rs @@ -506,7 +506,7 @@ pub fn register_rust_stdlib_tokens(target: &mut Tokenizer) { |token| { Ok(Atom::gnd(Number::from_float_str(token)?)) }); tref.register_token(regex(r"True|False"), |token| { Atom::gnd(Bool::from_str(token)) }); - tref.register_token(regex(r#"^".*"$"#), + tref.register_token(regex(r#"(?s)^".*"$"#), |token| { let mut s = String::from(token); s.remove(0); s.pop(); Atom::gnd(Str::from_string(s)) }); let sum_op = Atom::gnd(SumOp{}); tref.register_token(regex(r"\+"), move |_| { sum_op.clone() }); @@ -1285,6 +1285,8 @@ mod tests { !(id "te st") !(id "te\"st") !(id "") + !(id "te\nst") + !("te\nst"test) "#); assert_eq_metta_results!(metta.run(parser), Ok(vec![ @@ -1292,6 +1294,8 @@ mod tests { vec![expr!({Str::from_str("te st")})], vec![expr!({Str::from_str("te\"st")})], vec![expr!({Str::from_str("")})], + vec![expr!({Str::from_str("te\nst")})], + vec![expr!({Str::from_str("te\nst")} "test")], ])); } } diff --git a/python/hyperon/stdlib.py b/python/hyperon/stdlib.py index b2e9999de..c5ed1d27b 100644 --- a/python/hyperon/stdlib.py +++ b/python/hyperon/stdlib.py @@ -119,7 +119,7 @@ def type_tokens(): r"[-+]?\d+" : lambda token: ValueAtom(int(token), 'Number'), r"[-+]?\d+\.\d+": lambda token: ValueAtom(float(token), 'Number'), r"[-+]?\d+(\.\d+)?[eE][-+]?\d+": lambda token: ValueAtom(float(token), 'Number'), - r"^\".*\"$": lambda token: ValueAtom(str(token[1:-1]), 'String'), + r"(?s)^\".*\"$": lambda token: ValueAtom(str(token[1:-1]), 'String'), "\'[^\']\'": lambda token: ValueAtom(Char(token[1]), 'Char'), r"True|False": lambda token: ValueAtom(token == 'True', 'Bool'), r'regex:"[^"]*"': lambda token: G(RegexMatchableObject(token), AtomType.UNDEFINED) diff --git a/python/tests/test_stdlib.py b/python/tests/test_stdlib.py index 21ee11a65..6bad14ccc 100644 --- a/python/tests/test_stdlib.py +++ b/python/tests/test_stdlib.py @@ -79,6 +79,7 @@ def test_string_parsing(self): self.assertEqualMettaRunnerResults(metta.run("!(id' \"te st\")"), [[ValueAtom("te st")]]) self.assertEqualMettaRunnerResults(metta.run("!(id' \"te\\\"st\")"), [[ValueAtom("te\"st")]]) self.assertEqualMettaRunnerResults(metta.run("!(id' \"\")"), [[ValueAtom("")]]) + self.assertEqualMettaRunnerResults(metta.run("!(id' \"te\\nst\")"), [[ValueAtom("te\nst")]]) def test_regex(self): metta = MeTTa(env_builder=Environment.test_env()) From 6bf24d308b8065bf99aefb1190157a815b3e0cf7 Mon Sep 17 00:00:00 2001 From: Vitaly Bogdanov Date: Mon, 14 Oct 2024 15:52:41 +0300 Subject: [PATCH 2/2] Upgrade regex library --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 58d30ccbe..aa24a27aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ edition = "2021" [workspace.dependencies] hyperon = { path = "./lib", version = "0.2.0" } -regex = "1.5.4" +regex = "1.11.0" log = "0.4.0" env_logger = "0.8.4"