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 Smithy IDL 2.0 syntax #18

Merged
merged 11 commits into from
Jan 13, 2022

Conversation

JordonPhillips
Copy link
Contributor

@JordonPhillips JordonPhillips commented Nov 8, 2021

This adds support for some of the new idl syntax for 2.0. Namely it adds
support for:

  • optional commas
  • inline structure definitions
  • the required syntax sugar (e.g. trailing !)
  • the proposed default syntax sugar (e.g. trailing = default)
  • mixins via the with statement

I do not think it is possible for a TextMate grammar to be able to distinguish
between a key and a value in every case when there are no pair separators,
though I would love to be proven wrong on this.

TM grammars are regex-based, but crucially regexes can never match across
lines. A regex with a newline in the middle, like foo\nbar will NEVER match.
This is to allow the highlighter to be able to re-start at any line if parsing
failed on a previous line. To work around this somewhat, they allow you to
change states with begin and end regexes. Since these state changes are
regexes and regexes can't span lines, you can't have a complex end state. So
you can't simply say "end after parsing a json-like value". Since we have no
other indication that a pair is ended, we can't end a pair state if we enter
one.

For some context, the way the JSON TM grammar works is by having an effective
"value" state which is started by : and ended by , or }. This is what we
used to do.

Now what we CAN do if we don't care about being 100% accurate in all cases, is
try to match the key if it shares a line with the colon. This is the approach
I've gone with since I'm willing to bet this will be the case most of the time.

Screenshot 2021-11-08 at 18 38 37

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

This adds support for some of the new idl syntax for 2.0. Namely it adds
support for optional commas, inline IO, and the required syntax sugar.

I do not think it is possible for a TextMate grammar to be able to distinguish
between a key and a value in every case when there are no pair separators,
though I would love to be proven wrong on this.

TM grammars are regex-based, but crucially regexes can never match across
lines. A regex with a newline in the middle, like `foo\nbar` will  NEVER match.
This is to allow the highlighter to be able to re-start at any line if parsing
failed on a previous line. To work around this somewhat, they allow you to
change states with `begin` and `end`  regexes. Since these state changes are
*regexes* and regexes can't span lines, you can't have a complex end state. So
you can't simply say "end after parsing a json-like value".  Since we have no
other indication that a pair is ended, we can't end a pair state if we enter
one.

For some context, the way the JSON TM grammar works is by having an effective
"value" state which is started by `:` and ended by `,` or `}`. This is what we
used to do.

Now what we CAN do if we don't care about being 100% accurate in all cases, is
try to match the key if it shares a line with the colon. This is the approach
I've gone with since I'm willing to bet this will be the case most of the time.
@JordonPhillips
Copy link
Contributor Author

I added support for the = default sugar proposed in smithy-lang/smithy#920

Screenshot 2021-11-09 at 15 05 05

@JordonPhillips
Copy link
Contributor Author

Whoops, forgot mixins! While I was at it I fixed traits within shape definitions and a handful of bad captures.

Screenshot 2021-11-09 at 18 14 43

This splits up the innards of shapes and objects since shapes
have additional syntax that isn't allowed for generic objects.
syntaxes/smithy.tmLanguage Show resolved Hide resolved
syntaxes/smithy.tmLanguage Outdated Show resolved Hide resolved
syntaxes/smithy.tmLanguage Outdated Show resolved Hide resolved
This updates the with statment to require brackets. As part of this
I had to rethink how shapes worked. Previously we had fairly rigid
detection requirements for shapes that did / didn't have bodies and
what was allowed in those bodies. Since you can't have two subsequent
multiline rules, this presented some extreme difficulties. Instead,
the declaration of the shape and the shape body are now split. In
practice this means you theoretically have disconnected shape bodies.
This is somewhat unfortunate, but ultimately it's fine. This is
a syntax highlighter, not a parser. Perfectly enforcing the syntax
is a secondary goal to perfectly highlighting it. This isn't uncommon
either, you can for instance write "def def def" in a python file and
it will be highlighed by vs code just fine even though it won't parse
properly.
@JordonPhillips JordonPhillips merged commit 2a9b9a7 into smithy-lang:main Jan 13, 2022
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