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

Custom operators can't end in < but should be able to #6

Closed
alex-pinkus opened this issue Aug 28, 2021 · 1 comment
Closed

Custom operators can't end in < but should be able to #6

alex-pinkus opened this issue Aug 28, 2021 · 1 comment
Labels
bug Something isn't working error-for-valid-code The grammar causes a parsing error for some code that should parse

Comments

@alex-pinkus
Copy link
Owner

alex-pinkus commented Aug 28, 2021

With the current setup, ending a custom operator in a < would create ambiguity with function type parameters. Take, for example, this case:

public func ??<V : Value>(optional: Expression<V?>, defaultValue: V) -> Expression<V> {
    return Expression(optional.inner() ?? defaultValue)
}

If my operator were allowed to end in <, this would be interpreted as implementing ??<, and would fail shortly thereafter since V: Value > is not expected here. Somehow the actual swiftc is able to look past this and the code compiles just fine.

I've hacked around this for now by excluding < from being a legal last character in custom operators. This is almost always fine, but is not correct at all. I can think of one way to fix this (there may be others):

  1. Move the custom operator rule to externals -- see issue 5.
  2. Add some marker rule in externals that flags that we're in a function declaration position, so that scanner.c can special case this.
  3. When detecting a custom_operator in scanner.c, if we're in a function declaration, stay one character behind with mark_end.
  4. When we reach the end, check the lookahead character. If it isn't (, then lop off the last character of the custom operator (i.e. return without calling mark_end again).

Is there a less weird and special-cased way to do it? Maybe!

@alex-pinkus alex-pinkus added bug Something isn't working error-for-valid-code The grammar causes a parsing error for some code that should parse labels Aug 28, 2021
@alex-pinkus alex-pinkus changed the title Custom operators can't end in < Custom operators can't end in < but should be able to Aug 28, 2021
@alex-pinkus
Copy link
Owner Author

I did this more simply in #41 by just redefining custom_operator as seq(CUSTOM_OPERATORS, optional("<")) and adding it to the conflict table. We're definitely going to need to move the custom operator to scanner.c, but not today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working error-for-valid-code The grammar causes a parsing error for some code that should parse
Projects
None yet
Development

No branches or pull requests

1 participant