[swift] Log parser errors

* Eventually, this should throw, but the grammar
is very incomplete.
* Support opaque-types
This commit is contained in:
Andreas Dangel 2023-12-11 09:57:27 +01:00
parent dbfde44b92
commit 020e2da270
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 21 additions and 1 deletions

View File

@ -10,3 +10,5 @@ summary: "Swift-specific features and guidance"
> powerful for experts. It is fast, modern, safe, and a joy to write.
{% include language_info.html name='Swift' id='swift' implementation='swift::lang.swift.SwiftLanguageModule' supports_pmd=true supports_cpd=true since='5.3.7' %}
The grammar of the languages is documented in [The Swift Language Reference](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/aboutthelanguagereference/).

View File

@ -837,6 +837,7 @@ sType
| sType '?' // optional-type
| sType '!' // implicitly-unwrapped-optional-type
| protocolCompositionType
| 'some' sType // opaque-type
| sType '.' 'Type' | sType '.' 'Protocol' // metatype
| 'Any' | 'Self'
;
@ -969,7 +970,8 @@ contextSensitiveKeyword :
'lazy' | 'left' | 'mutating' | 'none' | 'nonmutating' | 'optional' | 'operator' | 'override' | 'postfix' | 'precedence' |
'prefix' | 'Protocol' | 'required' | 'right' | 'set' | 'Type' | 'unowned' | 'weak' | 'willSet' |
'iOS' | 'iOSApplicationExtension' | 'OSX' | 'OSXApplicationExtension­' | 'watchOS' | 'x86_64' |
'arm' | 'arm64' | 'i386' | 'os' | 'arch' | 'safe' | 'tvOS' | 'file' | 'line' | 'default' | 'Self' | 'var'
'arm' | 'arm64' | 'i386' | 'os' | 'arch' | 'safe' | 'tvOS' | 'file' | 'line' | 'default' | 'Self' | 'var' |
'some'
;
grammarString:

View File

@ -4,9 +4,14 @@
package net.sourceforge.pmd.lang.swift.ast;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser;
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwTopLevel;
@ -15,10 +20,21 @@ import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwTopLevel;
* Adapter for the SwiftParser.
*/
public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, SwTopLevel> {
private static final Logger LOGGER = LoggerFactory.getLogger(PmdSwiftParser.class);
@Override
protected SwTopLevel parse(final Lexer lexer, ParserTask task) {
SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
LOGGER.warn("Syntax error at {}:{}:{}: {}", task.getFileId().getOriginalPath(),
line, charPositionInLine, msg);
// TODO: eventually we should throw a parse exception
// throw new ParseException(msg).withLocation(FileLocation.caret(task.getFileId(), line, charPositionInLine));
}
});
return parser.topLevel().makeAstInfo(task);
}