jcornaz / beancount-parser

A beancount file parser library for rust
The Unlicense
21 stars 7 forks source link

\" escapes in strings #64

Closed blais closed 10 months ago

blais commented 10 months ago

Salut Jonathan, I had a quick go at attempting to parse my large personal ledger with your Rust parser tonight -- gave it one hour to kick the tires -- and here's a quick patch of what I changed. I didn't manage to get it working, the string parser needs to get modified to allow escaping quotation marks (") with \".

Feel free to use or ignore. If you can figure out how to invoke the right combinator magic to parser \", I'll resume testing soon.

lumia [git|main]:~/p/.../other/beancount-parser$ git diff
diff --git a/src/amount.rs b/src/amount.rs
index 146cf70..2b23169 100644
--- a/src/amount.rs
+++ b/src/amount.rs
@@ -153,9 +153,10 @@ fn literal<D: Decimal>(input: Span<'_>) -> IResult<'_, D> {
     map_res(
         recognize(tuple((
             opt(char('-')),
-            take_while1(|c: char| c.is_numeric() || c == '.'),
+            /* TODO(blais): Do beter, split this, no commas after period. */
+            take_while1(|c: char| c.is_numeric() || c == '.' || c == ','),
         ))),
-        |s: Span<'_>| s.fragment().parse(),
+        |s: Span<'_>| s.fragment().replace(",", "").parse(),
     )(input)
 }

diff --git a/src/lib.rs b/src/lib.rs
old mode 100644
new mode 100755
index 7ff4863..5eff36f
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -461,6 +461,11 @@ fn empty_line(input: Span<'_>) -> IResult<'_, ()> {

 fn string(input: Span<'_>) -> IResult<'_, &str> {
     map(
+        // tuple((
+        // tag('"'),
+        // escaped_transform(none_of("\""), '\\', one_of('"')),
+        // tag('"'),
+        // )),
         delimited(char('"'), take_till(|c: char| c == '"'), char('"')),
         |s: Span<'_>| *s.fragment(),
     )(input)
diff --git a/src/transaction.rs b/src/transaction.rs
index 397191e..8375f13 100644
--- a/src/transaction.rs
+++ b/src/transaction.rs
@@ -280,7 +280,7 @@ pub(super) fn parse_link(input: Span<'_>) -> IResult<'_, Link> {
     map(
         preceded(
             char_tag('^'),
-            take_while(|c: char| c.is_alphanumeric() || c == '-' || c == '_'),
+            take_while(|c: char| c.is_alphanumeric() || c == '-' || c == '_' || c == '.'),
         ),
         |s: Span<'_>| Link((*s.fragment()).into()),
     )(input)
jcornaz commented 10 months ago

Yes, indeed, that's not yet supported. Thanks for the suggested patch. I'll try to have a look soon-ish.

jcornaz commented 10 months ago

This is fix in the version 2.0.2.

Thanks @blais for the bug report.

jcornaz commented 10 months ago

The other points addressed by your patch ( , in amounts, and . in links) have been addressed in version 2.0.3

blais commented 10 months ago

Amazing! Thx, will try again