swaroopch / edn_format

EDN reader and writer implementation in Python, using PLY (lex, yacc)
https://swaroopch.com/2012/12/24/edn-format-python/
Other
131 stars 31 forks source link

Handle fractions #49

Closed bfontaine closed 6 years ago

bfontaine commented 6 years ago

See https://github.com/edn-format/edn/issues/64.

This is the same PR as #23 but rebased on top of master.

swaroopch commented 6 years ago

@bfontaine My memory / context on this is fuzzy - this parses strings, right? What happens if there is a sentence and not a fraction, say "We can do X and/or Y" - will this change safely ignore such strings?

bfontaine commented 6 years ago

@swaroopch No, fractions are literals like other numbers:

[1 1/2 1/3 1/4 1/5]

The only thing the parser might confuse them with are namespaced symbols like foo/bar. I just did a couple tests: we correctly parse foo/bar vs. 1/2, but we accept foo/1 even if it’s forbidden in the spec: the part after / is subject to the same restrictions as normal symbols, i.e. it can’t start with a digit.

Also, we shouldn’t accept spaces around /: 1/2 should be parsed as Fraction(1,2) but 1 / 2 should be parsed as 1 Symbol(/) 2; a bit like 1.2 is parsed as a float while 1 . 2 is parsed as two integers and a symbol.

I’m going to add more tests to this PR to make sure we don’t break anything here.

bfontaine commented 6 years ago

I rewrote the implementation because I found a few issues when writing tests:

We know take care of the fractions at the lexing step rather than the parsing one. This makes the last two issues trivial to fix because we don’t have to deal with the DIVIDE symbol anymore.

swaroopch commented 6 years ago

We know take care of the fractions at the lexing step rather than the parsing one. This makes the last two issues trivial to fix because we don’t have to deal with the DIVIDE symbol anymore.

That's a great idea @bfontaine !