swift-plugin icon indicating copy to clipboard operation
swift-plugin copied to clipboard

Errors with valid syntax

Open kuglee opened this issue 1 year ago • 7 comments

Thanks for developing this. I've tried it with one of my projects: SwiftFormatter. Unfortunately I've got some phantom errors reported in the following files (I'm using Xcode Version 15.2 (15C500b)):

  • Sources/AppExtension/AppExtension.swift
    • line 76: ',', ';', <extension member>, '=', '?', '{' or '}' expected, got '.'
    • line 83: '#if', ';', <infix expression>, <statement>, IDENTIFIER or KEYWORD_CONTEXT_SENSITIVE expected, got '}'
  • Sources/FormatterSettings/FormatterSettings.swift
    • line 404: ',', ':', <struct member>, '=' or '}' expected, got ':'
  • Sources/SettingsFeature/SettingsFeature.swift
    • line 244: ')', ',', '.', '...', <, '=' or '?' expected, got ','
  • Sources/StyleGuide/Buttons.swift
    • line 6: ')', ',', '...', '=', '?' or async expected, got '.'
    • line 6: '!', '#if', '(', '.', ';', <infix expression>, <statement>, '?' or '{' expected, got ')'
  • Sources/StyleGuide/Colors.swift
    • line 10: IDENTIFIER or KEYWORD_CONTEXT_SENSITIVE expected, got 'Self'
  • Sources/WelcomeFeature/EqualIconWidthDomain.swift
    • line 10: ',', '.', ':', < or where expected, got '='
    • line 12: ',', '.', ':', < or where expected, got '='
    • line 52: ')' expected, got '@'

kuglee avatar Feb 27 '24 19:02 kuglee

Thanks for the pointers! These are most likely parser errors in this plugin, Swift isn't the easiest language for parsing ;)

This project is a little quiet at the moment, I'll be able to commit time for it in about 4-6 weeks.

jansorg avatar Feb 27 '24 19:02 jansorg

No rush 🙂. Keep up the good work.

kuglee avatar Feb 27 '24 19:02 kuglee

First of all, amazing work! Especially when working in a Kotlin Multipaltform application, having some Swift support is great!

I came across some syntax that was not supported, so just dropping it here for reference:

let task = Task { @MainActor in
               ^ 
    // ...
}

where the error message says:

'#', '#if', '#keypath', '&', '(', '.', ';', <, , , , , , , , , , , , , Any, Self, '', _, await, defer, do, self, super, try, '{' or '}' expected, got '{'

If I remove the @MainActor, the error goes away.

eirikvaa avatar Feb 29 '24 21:02 eirikvaa

@kuglee I've fixed all your reported problems for the next update.

@eirikvaa Could you provide a working example for the problem, please? If that's a closure expression, then the official Swift grammar doesn't support this, "Grammar of a closure expression" of https://docs.swift.org/swift-book/documentation/the-swift-programming-language/summaryofthegrammar#Declarations A working example would help to fix it and to report it as change to the maintainers of the Grammar.

jansorg avatar Apr 19 '24 11:04 jansorg

I can look at it next week, but the project I linked is a good example.

kuglee avatar Apr 19 '24 12:04 kuglee

@kuglee All your reported problems are fixed for the next update. The other question was about the appended problem by @eirikvaa .

jansorg avatar Apr 19 '24 12:04 jansorg

@jansorg By looking at the Swift grammar, I think we can get to the syntax with the following rules and substitutions:

closure-expression → { attributes? closure-signature? statements? }
attributes → attribute attributes?
attribute → @ attribute-name attribute-argument-clause?
attribute-name → identifier

And then we are pretty much at Task { @MainActor }. A working example would be something like this:

func foo() {
    Task { @MainActor in 
        print("I'm running on the main actor")
    }
}

eirikvaa avatar Apr 19 '24 13:04 eirikvaa