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

@fluent/syntax: Variant values with [] don't roundtrip through the parser/serializer #512

Closed
Pike opened this issue Aug 7, 2020 · 3 comments · Fixed by #513
Closed

@fluent/syntax: Variant values with [] don't roundtrip through the parser/serializer #512

Pike opened this issue Aug 7, 2020 · 3 comments · Fixed by #513

Comments

@Pike
Copy link
Contributor

Pike commented Aug 7, 2020

The following Fluent snippet doesn't round-trip right:

shared-photos =
    { $userName } { $photoCount ->
        [one][x]added a new photo { $item ->
                [bucket] to the bucket
               *[folder] to the folder
            }
       *[other] added { $photoCount } new photos
    }

The result is

shared-photos =
    { $userName } { $photoCount ->
        [one]
            [x]added a new photo { $item ->
                [bucket] to the bucket
               *[folder] to the folder
            }
       *[other] added { $photoCount } new photos
    }

Filing here, but it affects all of js, python, and kotlin.

@Pike
Copy link
Contributor Author

Pike commented Aug 7, 2020

This code here

const startOnNewLine =
pattern.elements.some(isSelectExpr) ||
pattern.elements.some(includesNewLine);

doesn't take into account that block_text starts with indented_char,

https://github.com/projectfluent/fluent/blob/7282b865958ea0602ddc6b3d583ab7477abb49e7/spec/fluent.ebnf#L110

aka, - "[" - "*" - ".".

Easy to fix. @stasm, I'm not sure where to test this, though. Should we go as far as have tests in the spec to ensure those are all parse errors?
And then, yes, serializer tests, too.

@Michael-F-Bryan
Copy link

Michael-F-Bryan commented Aug 9, 2020

As far as I can tell, the issue is that [one][x] is serialized to [one]\n[x] (i.e the literal [x] starts on a newline, maybe with some indents)... Is it guaranteed that a serializer should preserve all whitespace? I thought the whitespace before a variant's first pattern element wasn't significant.

When I was implementing projectfluent/fluent-rs#184 I noticed that test inputs were constructed so things round-trip exactly, but just thought that was to make testing easier and not because it's required by the spec.

EDIT: Never mind. I updated my code to re-parse the serialized text and make sure it's identical to the original, and we run into a parse error. Seems like putting the variant's text element on a new line isn't valid syntax (or at least, as implemented by the Rust parser).

@Pike
Copy link
Contributor Author

Pike commented Aug 9, 2020

Usually white-space around syntax elements is not significant, but in this case, newlines are. That's in the grammar snippet I linked to.

Generally speaking, handling white-space with proper round-tripping is a pro for tooling applications. It's easier that way to not have different tools battle over how to serialize strings. There are tricks to lessen the impact (compare ASTs when deciding to write messages, for example), but generally, full round-trip is good for tooling.

Not that I know of an implementations that does that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants