Skip to content

Commit

Permalink
[lang] Error message for SARL keywords used as identifier.
Browse files Browse the repository at this point in the history
see #413

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 17, 2016
1 parent f559beb commit 3dbfb7c
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
10 changes: 10 additions & 0 deletions eclipse-sarl/plugins/io.sarl.lang.mwe2/GenerateSARL2.mwe2
Expand Up @@ -773,6 +773,16 @@ Workflow {
singleton = true
instance = true
}
runtime = {
bind = "org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider"
to = "io.sarl.lang.parser.SARLSyntaxErrorMessageProvider"
singleton = true
}
runtime = {
bind = "org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider"
to = "io.sarl.lang.parser.SARLSyntaxErrorMessageProvider"
singleton = true
}

ui = {
bind = "org.eclipse.xtext.ui.editor.autoedit.AbstractEditStrategyProvider"
Expand Down
1 change: 1 addition & 0 deletions eclipse-sarl/plugins/io.sarl.lang/META-INF/MANIFEST.MF
Expand Up @@ -32,6 +32,7 @@ Export-Package: io.sarl.lang,
io.sarl.lang.formatting2,
io.sarl.lang.generator,
io.sarl.lang.jvmmodel,
io.sarl.lang.parser,
io.sarl.lang.parser.antlr,
io.sarl.lang.parser.antlr.internal,
io.sarl.lang.sarl,
Expand Down
Expand Up @@ -80,6 +80,7 @@
import io.sarl.lang.generator.SarlOutputConfigurationProvider;
import io.sarl.lang.jvmmodel.SARLJvmModelInferrer;
import io.sarl.lang.jvmmodel.SarlJvmModelAssociations;
import io.sarl.lang.parser.SARLSyntaxErrorMessageProvider;
import io.sarl.lang.parser.antlr.SARLAntlrTokenFileProvider;
import io.sarl.lang.parser.antlr.SARLParser;
import io.sarl.lang.parser.antlr.internal.InternalSARLLexer;
Expand Down Expand Up @@ -153,11 +154,13 @@
import org.eclipse.xtext.parser.antlr.AntlrTokenToStringConverter;
import org.eclipse.xtext.parser.antlr.IAntlrTokenFileProvider;
import org.eclipse.xtext.parser.antlr.IPartialParsingHelper;
import org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider;
import org.eclipse.xtext.parser.antlr.ITokenDefProvider;
import org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper;
import org.eclipse.xtext.parser.antlr.Lexer;
import org.eclipse.xtext.parser.antlr.LexerBindings;
import org.eclipse.xtext.parser.antlr.LexerProvider;
import org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider;
import org.eclipse.xtext.parser.antlr.UnorderedGroupHelper;
import org.eclipse.xtext.preferences.IPreferenceValuesProvider;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
Expand Down Expand Up @@ -601,6 +604,12 @@ public Class<? extends EObjectAtOffsetHelper> bindEObjectAtOffsetHelper() {
return XtendEObjectAtOffsetHelper.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
@SingletonBinding
public Class<? extends ISyntaxErrorMessageProvider> bindISyntaxErrorMessageProvider() {
return SARLSyntaxErrorMessageProvider.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
public Class<? extends IResourceValidator> bindIResourceValidator() {
return CachingResourceValidatorImpl.class;
Expand Down Expand Up @@ -688,6 +697,12 @@ public Class<? extends TargetURICollector> bindTargetURICollector() {
return JvmModelTargetURICollector.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
@SingletonBinding
public Class<? extends SyntaxErrorMessageProvider> bindSyntaxErrorMessageProvider() {
return SARLSyntaxErrorMessageProvider.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
public Class<? extends IDefaultResourceDescriptionStrategy> bindIDefaultResourceDescriptionStrategy() {
return XtendResourceDescriptionStrategy.class;
Expand Down
@@ -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() {
}
}
@@ -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);
}

}
@@ -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() {
//
}

}
@@ -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.
Expand Up @@ -15,6 +15,8 @@
*/
package io.sarl.lang.tests.parsing.general;

import io.sarl.lang.parser.SyntaxIssueCodes;
import io.sarl.lang.sarl.SarlPackage;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.tests.api.AbstractSarlTest;

Expand Down Expand Up @@ -69,4 +71,28 @@ public void noParamNoReturnActionInInterface() throws Exception {
validate(mas).assertNoIssues();
}

@Test
public void sarlKeywordAsIdentifier() throws Exception {
SarlScript mas = file(multilineString(
"package io.sarl.lang.tests.behavior.mypackage",
""));
validate(mas).assertError(
SarlPackage.eINSTANCE.getSarlScript(),
SyntaxIssueCodes.USED_RESERVED_KEYWORD,
26, 1,
"'behavior' is a reserved keyword which is not allowed as identifier.");
}

@Test
public void javaKeywordAsIdentifier() throws Exception {
SarlScript mas = file(multilineString(
"package io.sarl.lang.tests.null.mypackage",
""));
validate(mas).assertError(
SarlPackage.eINSTANCE.getSarlScript(),
SyntaxIssueCodes.USED_RESERVED_KEYWORD,
26, 1,
"'null' is a reserved keyword which is not allowed as identifier.");
}

}

0 comments on commit 3dbfb7c

Please sign in to comment.