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

Support for trailing whitespaces & newlines #34

Merged
merged 6 commits into from
May 28, 2022

Conversation

complected
Copy link
Contributor

@complected complected commented May 27, 2022

Trailing spaces/lines

""\n or "a " turn out they don't work the whole time. It doesn't work because we are not accepting trailing lines/spaces for standalone strings (bare or not).

After changing via the grammar, need to change empty string check because ""\n doesn't equal "".

Encoding

Following up last convo. I read around, I think the link below help and the comments added will help others.

What I gathered: as long as the string passed into jam is correctly decoded i.e. the string in the JS runtime is a well formed JSON string, our parsing code works. The encoding of the file is only relevant when converting between the raw bytes and JS in memory representation.

@complected complected changed the title Escape parse Follow up with file encoding May 27, 2022
@complected complected changed the title Follow up with file encoding Support for trailing whitespaces & newlines May 27, 2022
jams.js Outdated
@@ -14,6 +14,8 @@ ANY ::= (SAFE | WS | SYN | #x5C)
SAFE ::= #x21 | [#x23-#x5A] | [#x5E-#x7A] | #x7C | #x7E
`)

// Expect a well-formed JSON string, already decoded, not UTF-8/UTF-16 encoded bytes etc.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not clear, implies the function is accepting, well, a JSON string, probably different interpretation than you meant

Copy link
Contributor Author

@complected complected May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I meant jams expects a JAMS string, but the leave strings have to be JSON strings.

case 'bare':
case 'quote': {
const quoted = String.raw`"${ast.text}"`
const json = JSON.parse(quoted)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we verify json strings escape unicode and not utf16?

Copy link
Contributor Author

@complected complected May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we verify json strings escape unicode...?

Yep.

Longer answer is, I verified that a file of \n needs to transform to String.raw"\n"` (note the quotes) for JSON.parse to parse, otherwise it's a parsing exception. Did I answer your question?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and not utf16?

I think I am not understanding the question. Could you please break it down a bit?
JSON strings ----can transform to---> JS Objects ---> UTF-8 encoded files.

Could you please let me know if this and the comment in the diff test.js helps?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m talking about escape sequences, how you specify them like \uff vs U+FF etc, json spec encodes utf16 I’m 99% sure and I want it to be unicode instead
Will merge this but investigate some more later

@nmushegian nmushegian merged commit 51a8884 into nmushegian:master May 28, 2022
if (ast.children.length !== 1) throw new Error(`Invalid string`)
const utf8_encoded = String.raw`"${ast.children[0].text}"`
const json = JSON.parse(utf8_encoded)
return (ast.text.includes('""') && ast.children.length === 0) ? "" : _jams(ast.children[0])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability pls

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

Successfully merging this pull request may close these issues.

2 participants