Browse Source

Fix formatting of negative floating point literals (#138)

Fix formatting of floating point literals that use `−` (Unicode U+2212)
instead of the typically used `-` (ASCII/Unicode U+002D). The parser
treats them as synonyms but the former triggered an assertion error in
Runic while formatting. After this change Runic will normalize to `-`
when formatting. Fixes #137.
pull/139/head
Fredrik Ekre 9 months ago committed by GitHub
parent
commit
031348fe0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      CHANGELOG.md
  2. 9
      src/runestone.jl
  3. 5
      test/runtests.jl

10
CHANGELOG.md

@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Fix formatting of floating point literals that use `−` (Unicode U+2212) instead of the
typically used `-` (ASCII/Unicode U+002D). The parser treats them as synonyms but the
former triggered an assertion error in Runic while formatting. After this change Runic
will normalize to `-` when formatting ([#137], [#138]).
## [v1.4.2] - 2025-02-18 ## [v1.4.2] - 2025-02-18
### Fixed ### Fixed
- Add missing documentation of `--lines` to the `--help` output ([#136]). - Add missing documentation of `--lines` to the `--help` output ([#136]).
@ -122,6 +129,7 @@ First stable release of Runic.jl. See [README.md](README.md) for details and doc
[v1.3.0]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.3.0 [v1.3.0]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.3.0
[v1.4.0]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.4.0 [v1.4.0]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.4.0
[v1.4.1]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.4.1 [v1.4.1]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.4.1
[v1.4.2]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.4.2
[#97]: https://github.com/fredrikekre/Runic.jl/issues/97 [#97]: https://github.com/fredrikekre/Runic.jl/issues/97
[#108]: https://github.com/fredrikekre/Runic.jl/issues/108 [#108]: https://github.com/fredrikekre/Runic.jl/issues/108
[#109]: https://github.com/fredrikekre/Runic.jl/issues/109 [#109]: https://github.com/fredrikekre/Runic.jl/issues/109
@ -142,3 +150,5 @@ First stable release of Runic.jl. See [README.md](README.md) for details and doc
[#132]: https://github.com/fredrikekre/Runic.jl/issues/132 [#132]: https://github.com/fredrikekre/Runic.jl/issues/132
[#133]: https://github.com/fredrikekre/Runic.jl/issues/133 [#133]: https://github.com/fredrikekre/Runic.jl/issues/133
[#136]: https://github.com/fredrikekre/Runic.jl/issues/136 [#136]: https://github.com/fredrikekre/Runic.jl/issues/136
[#137]: https://github.com/fredrikekre/Runic.jl/issues/137
[#138]: https://github.com/fredrikekre/Runic.jl/issues/138

9
src/runestone.jl

@ -97,12 +97,15 @@ function format_float_literals(ctx::Context, node::Node)
return nothing return nothing
end end
# Split up the pieces # Split up the pieces
r = r"^(?<sgn>[+-])?(?<int>\d*)(?:\.?(?<frac>\d*))?(?:(?<epm>[eEf][+-]?)(?<exp>\d+))?$" # Note that Julia considers '−' (Unicode U+2212) to be a synonym to the normally used
# '-' (ASCII/Unicode U+002D) so we need to check for the former in this regex, and
# normalize it to '-' when writing it out.
r = r"^(?<sgn>[+\-−])?(?<int>\d*)(?:\.?(?<frac>\d*))?(?:(?<epm>[eEf][+\-−]?)(?<exp>\d+))?$"
m = match(r, str)::RegexMatch m = match(r, str)::RegexMatch
io = IOBuffer() # TODO: Could be reused? io = IOBuffer() # TODO: Could be reused?
# Write the sign part # Write the sign part
if (sgn = m[:sgn]; sgn !== nothing) if (sgn = m[:sgn]; sgn !== nothing)
write(io, sgn) write(io, replace(sgn, "" => "-")) # \u2212 => \u002D
end end
# Strip leading zeros from integral part # Strip leading zeros from integral part
int_part = isempty(m[:int]) ? "0" : m[:int] int_part = isempty(m[:int]) ? "0" : m[:int]
@ -116,7 +119,7 @@ function format_float_literals(ctx::Context, node::Node)
write(io, frac_part) write(io, frac_part)
# Write the exponent part # Write the exponent part
if m[:epm] !== nothing if m[:epm] !== nothing
write(io, replace(m[:epm], "E" => "e")) write(io, replace(m[:epm], "E" => "e", "" => "-")) # \u2212 => \u002D
@assert m[:exp] !== nothing @assert m[:exp] !== nothing
# Strip leading zeros from integral part # Strip leading zeros from integral part
exp_part = isempty(m[:exp]) ? "0" : m[:exp] exp_part = isempty(m[:exp]) ? "0" : m[:exp]

5
test/runtests.jl

@ -194,6 +194,11 @@ end
end end
end end
end end
# Issue #137: '−' (Unicode U+2212) is a synonym in the parser to the normally used
# '-' (ASCII/Unicode U+002D)
@test format_string("\u22121.0") == "-1.0"
@test format_string("1.0e\u22121") == "1.0e-1"
@test format_string("\u22121.0e\u22121") == "-1.0e-1"
end end
@testset "whitespace between operators" begin @testset "whitespace between operators" begin

Loading…
Cancel
Save