Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang] Error message for SARL keywords used as identifier.
see #413 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
f559beb
commit 3dbfb7c
Showing
8 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
eclipse-sarl/plugins/io.sarl.lang/src/io/sarl/lang/parser/Messages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2016 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.lang.parser; | ||
|
||
import org.eclipse.osgi.util.NLS; | ||
|
||
/** Messages for the SARL parser. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
*/ | ||
@SuppressWarnings("all") | ||
public class Messages extends NLS { | ||
private static final String BUNDLE_NAME = "io.sarl.lang.parser.messages"; //$NON-NLS-1$ | ||
public static String SARLSyntaxErrorMessageProvider_0; | ||
public static String SARLSyntaxErrorMessageProvider_1; | ||
static { | ||
// initialize resource bundle | ||
NLS.initializeMessages(BUNDLE_NAME, Messages.class); | ||
} | ||
|
||
private Messages() { | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...pse-sarl/plugins/io.sarl.lang/src/io/sarl/lang/parser/SARLSyntaxErrorMessageProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2016 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.lang.parser; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.ibm.icu.text.MessageFormat; | ||
import org.antlr.runtime.RecognitionException; | ||
import org.eclipse.xtext.nodemodel.SyntaxErrorMessage; | ||
import org.eclipse.xtext.util.Strings; | ||
import org.eclipse.xtext.xbase.conversion.XbaseValueConverterService; | ||
import org.eclipse.xtext.xtext.parser.CardinalityAwareSyntaxErrorMessageProvider; | ||
|
||
import io.sarl.lang.services.SARLGrammarKeywordAccess; | ||
|
||
/** Provider of messages for syntax errors. | ||
* | ||
* <p>This provider enhances the error messages when a keyword is misplaced. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
*/ | ||
public class SARLSyntaxErrorMessageProvider extends CardinalityAwareSyntaxErrorMessageProvider { | ||
|
||
@Inject | ||
private SARLGrammarKeywordAccess grammarAccess; | ||
|
||
@Inject | ||
private XbaseValueConverterService converter; | ||
|
||
@Override | ||
public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { | ||
if (context != null) { | ||
final RecognitionException recognitionException = context.getRecognitionException(); | ||
if (recognitionException != null && recognitionException.token != null) { | ||
final String text = recognitionException.token.getText(); | ||
if (!Strings.isEmpty(text)) { | ||
if (this.grammarAccess.isPureKeyword(text)) { | ||
final String protectedText = this.converter.getQualifiedNameValueConverter().toString(text); | ||
return new SyntaxErrorMessage( | ||
MessageFormat.format(Messages.SARLSyntaxErrorMessageProvider_0, text, protectedText), | ||
SyntaxIssueCodes.USED_RESERVED_KEYWORD, | ||
new String[] {text, protectedText}); | ||
} else if (this.grammarAccess.isKeyword(text)) { | ||
return new SyntaxErrorMessage( | ||
MessageFormat.format(Messages.SARLSyntaxErrorMessageProvider_1, text), | ||
SyntaxIssueCodes.USED_RESERVED_KEYWORD); | ||
} | ||
} | ||
} | ||
} | ||
return super.getSyntaxErrorMessage(context); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
eclipse-sarl/plugins/io.sarl.lang/src/io/sarl/lang/parser/SyntaxIssueCodes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2016 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.lang.parser; | ||
|
||
import org.eclipse.xtext.xtext.parser.CardinalityAwareSyntaxErrorMessageProvider; | ||
|
||
/** List of issue codes for the parser. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
*/ | ||
public final class SyntaxIssueCodes { | ||
|
||
/** Prefix related to SARL for the issue codes. | ||
*/ | ||
public static final String ISSUE_CODE_PREFIX = "io.sarl.lang.parser.SyntaxIssueCodes."; //$NON-NLS-1$ | ||
|
||
/** Identifier of the issue related to the invalid used of a reserved keyword. | ||
*/ | ||
public static final String USED_RESERVED_KEYWORD = ISSUE_CODE_PREFIX + "used_reserved_keyword"; //$NON-NLS-1$ | ||
|
||
/** A cardinality is overridden. | ||
*/ | ||
public static final String OVERRIDDEN_CARDINALITY = CardinalityAwareSyntaxErrorMessageProvider.CARDINALITY_ISSUE; | ||
|
||
private SyntaxIssueCodes() { | ||
// | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
eclipse-sarl/plugins/io.sarl.lang/src/io/sarl/lang/parser/messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SARLSyntaxErrorMessageProvider_0=''{0}'' is a reserved keyword which is not allowed as identifier. Please choose another word or alternatively confuse your co-workers by escaping it like this: "{1}". | ||
SARLSyntaxErrorMessageProvider_1=''{0}'' is a reserved keyword which is not allowed as identifier. Please choose another word. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters