Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin to attempt to add trailing zeros to Float literals #66

Merged
merged 2 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,13 @@ end
function p_literal(x, s)
loc = cursor_loc(s)
if !is_str_or_cmd(x.kind)
val = x.val
if x.kind === Tokens.FLOAT && x.val[end] == '.'
# If a floating point ends in `.`, add trailing zero.
val *= '0'
end
s.offset += x.fullspan
return PTree(x, loc[1], loc[1], x.val)
return PTree(x, loc[1], loc[1], val)
end

# Strings are unescaped by CSTParser
Expand All @@ -395,7 +400,7 @@ function p_literal(x, s)
# So we'll just look at the source directly!
str_info = get(s.doc.lit_strings, s.offset - 1, nothing)

# Tokenize treats the `ix` part of r"^(=?[^=]+)=(.*)$"ix as an
# Tokenize treats the `ix` part of r"^(=?[^=]+)=(.*)$"ix as an
# IDENTIFIER where as CSTParser parses it as a LITERAL.
# An IDENTIFIER won't show up in the string literal lookup table.
if str_info === nothing && x.parent.typ === CSTParser.x_Str
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,17 @@ end
@test fmt(str_, 2, 67) == str
end

@testset "Trailing zeros" begin
@test fmt("1.") == "1.0"
@test fmt("a * 1. + b") == "a * 1.0 + b"
@test fmt("1. + 2. * im") == "1.0 + 2.0 * im"
@test fmt("[1., 2.]") == "[1.0, 2.0]"
@test fmt("""
1. +
2.
""") == "1.0 + 2.0\n"
end

# @testset "meta-format" begin
# str = String(read("./runtests.jl"))
# str = fmt(str)
Expand Down