icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Fix Miscomputed Spans #628

Closed InsertCreativityHere closed 1 year ago

InsertCreativityHere commented 1 year ago

This fixes #603 where the parser will sometimes return over-extendeds span.

What was the problem?

The problem occurs when we try to get the location in between two tokens that can match nothing (an empty string). For example, this is the rule for a struct:

<prelude: Prelude> <location: @L> <ck: compact_keyword?> struct_keyword

Prelude can match an empty string (you don't have a comment or attribute) and the compact_keyword is optional. So the location in between these two tokens is formally undefined, because they can be matched anywhere.

How was this fixed?

When the grammar is in these situations, it gets 2 locations now:

<prelude: Prelude> <l1: @L> <ck: compact_keyword?> <l2: @L> struct_keyword

If the compact keyword is missing it uses l2, and if it's present it uses l1. This ensures that we never try to query the location in between two non-existent tokens.

InsertCreativityHere commented 1 year ago

@externl, it looks like rustfmt got an update recently, because this PR had formatting errors in alot of files I didn't even touch: https://github.com/icerpc/slicec/actions/runs/5467763283/jobs/9954497252

So just an FYI, I added a configuration option just to get the actions working again so the build for slicec wouldn't fail, and so they could catch if I'd made any 'real' formatting mistakes: single_line_let_else_max_width = 120