Reputation: 4419
Developing with CDT can become frustrating, because nobody answers your questions and there is hardly any documentation. That being said, let me try it with a simple one:
In my eclipse plugin the user wants to edit files with a language similar to C++. To have additional keywords highlighted, I created a class that extends GPPScannerExtensionConfiguration like this:
public class GRASPScannerExtensionConfiguration extends GPPScannerExtensionConfiguration {
private static GRASPScannerExtensionConfiguration instance;
public static synchronized GRASPScannerExtensionConfiguration getInstance() {
if (instance == null)
instance = new GRASPScannerExtensionConfiguration(version(8, 0));
return instance;
}
public GRASPScannerExtensionConfiguration(int version) {
super(version);
/*
* add GRASP specific configuration
*/
addKeyword("@func".toCharArray(), IToken.t_void);
addKeyword("@tag".toCharArray(), IToken.t_void);
}
}
With this class I have my keywords @func
or @tag
highlighted like void
in C++. But the editor underlines those keywords and says "Syntax error".
I know I can deactivate syntax error in Window -> Preferences -> General -> Editors -> Text Editors -> Annotations
but that would 1.) deactivate all syntax errors and 2.) require users to manually do this setting.
How can I deactivate the syntax error marking only for my own additional keywords?
Upvotes: 1
Views: 63
Reputation: 4419
I was successful in deactivating syntax errors and I want to briefly write down how I did it:
1.) Write a class that extends CPreprocessor
and overrides handleProblem
. In my case my keyword is "@func", but It's tokenized as '@' and 'func'. The '@' is put into a list of problems which is handled in handleProblem
.
import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor;
...
public class GRASPPreprocessor extends CPreprocessor {
public GRASPPreprocessor(FileContent fileContent, IScannerInfo info, ParserLanguage language, IParserLogService log,
IScannerExtensionConfiguration configuration, IncludeFileContentProvider readerFactory) {
super(fileContent, info, language, log, configuration,
}
@Override
public void handleProblem(int id, char[] arg, int offset, int endOffset) {
if (new String(arg).equals("@")) {
this.problem = new ProblemStorage(id, arg, offset, endOffset);
} else {
super.handleProblem(id, arg, offset, endOffset);
}
}
}
I created a short Storage class to store the arguments. But only call the superclass's handleProblem
method, if the "@" is not followed by "func". This is done in nextToken
:
@Override
public IToken nextToken() throws EndOfFileException {
IToken token = super.nextToken();
if(problem != null && token.getType() == IToken.tIDENTIFIER) {
if (new String(token.getCharImage()).equals("func")) {
problem = null;
return super.nextToken();
} else {
super.handleProblem(problem.id, problem.arg, problem.offset, problem.endOffset);
return token;
}
}
return token;
}
This basically ignores "@func". A stray '@' would still marked as a problem.
2.) Define your own language class, extending GPPLanguage and override createScanner
:
protected IScanner createScanner(FileContent content, IScannerInfo scanInfo, IncludeFileContentProvider fcp,
IParserLogService log) {
return new GRASPPreprocessor(content, scanInfo, getParserLanguage(), log,
getScannerExtensionConfiguration(scanInfo), fcp);
}
3.)
Add an extension to the plugin.xml with the extension point org.eclipse.cdt.core.language
and also tell the plugin which content type has to be used for this language:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
file-extensions="grasp"
id="de.blub.contenttype.grasp"
name="GRASP"
priority="high">
</content-type>
</extension>
<extension
point="org.eclipse.cdt.core.language">
<language
class="de.blub.language.GRASPLanguage"
id="de.blub.language.grasp"
name="Great Alternative Stupid Language">
<contentType
id="de.blub.contenttype.grasp">
</contentType>
</language>
4.) I don't know what's wrong with the content type binding, but it's not working. I asked the community here about it. However, you can define add your own association via Window->Preferences->General->Content Types.
Upvotes: 0
Reputation: 52819
How can I deactivate the syntax error marking only for my own additional keywords?
The short answer is you can't.
The parser doesn't have a way of distinguishing between "a syntax error caused by your additional keyword" and "a syntax error caused by something else" without being taught such a way. It also doesn't have a way to recover the parse after encounting such a keyword, such that subsequent parts of a declaration (or expression or whatever context the keyword appears in) are parsed correctly, again without being taught such a way.
To teach the parser these things, you'd have to modify / extend it in new ways. I'm not aware of current extension points that would allow you to do this, so you'd have to fork the plugin providing the parser, or upstream new extension points to it.
Upvotes: 1