Commit 24a2e0db authored by Daniel Honsel's avatar Daniel Honsel
Browse files

check naming conventions of templates

parent cf1a6b27
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -835,7 +835,7 @@ PatternParticle:
	{PatternParticle} CSTRING | ReferencedValue;

TemplateDef:
	TEMPLATEKEYWORD res=TemplateRestriction? base=BaseTemplate derived=DerivedDef? ASSIGNMENTCHAR body=TemplateBody;
	TEMPLATEKEYWORD restriction=TemplateRestriction? base=BaseTemplate derived=DerivedDef? ASSIGNMENTCHAR body=TemplateBody;

DerivedDef:
	MODIFIESKEYWORD template=[BaseTemplate|QualifiedIdentifier];
@@ -1072,6 +1072,8 @@ ExtendedFieldReference:
	DOT (field=[FieldReference|IDENTIFIER] | type=PredefinedType) | array=ArrayOrBitRef | {ExtendedFieldReference}
	SQUAREOPEN MINUS SQUARECLOSE;

// BaseTemplate contained in FunctionRef
// SingleVarInstance contained in TimerVarInstance (according to EBNF rules)
RefValue:
	SingleConstDef | ModuleParameter | FieldReference | FormalTemplatePar | SingleTempVarInstance | Enumeration |
	FunctionRef | TimerVarInstance | FormalPortPar;
+104 −3
Original line number Diff line number Diff line
@@ -66,12 +66,16 @@ import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.validation.Check

import static extension de.ugoe.cs.swe.common.TTCN3ScopeHelper.*
import static extension org.eclipse.xtext.EcoreUtil2.*
import de.ugoe.cs.swe.tTCN3.TimerInstance
import de.ugoe.cs.swe.tTCN3.ComponentElementDef
import de.ugoe.cs.swe.common.ConfigTools
import de.ugoe.cs.swe.TTCN3Configuration.QualityCheckProfile
import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel
import de.ugoe.cs.swe.common.logging.LoggingInterface.MessageClass
import de.ugoe.cs.swe.tTCN3.BaseTemplate
import de.ugoe.cs.swe.common.MiscTools
import de.ugoe.cs.swe.tTCN3.MatchingSymbol

class TTCN3Validator extends AbstractTTCN3Validator {
	val ConfigTools configTools = ConfigTools.getInstance;
@@ -231,7 +235,101 @@ class TTCN3Validator extends AbstractTTCN3Validator {
		variable.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

	//TODO: templates
	@Check
	def checkMessageTemplate(BaseTemplate template) {
		var regExp = ""
		var message = ""
		val templateDef = template.findDesiredParent(TemplateDef)
		val isDerived = templateDef.derived != null
		val matchingSymbols = template.eAllOfType(MatchingSymbol)

		if (!isDerived) {
			if (matchingSymbols.empty) {
				regExp = activeProfile.namingConventionsConfig.messageTemplateRegExp
				message = "MessageTemplate";
			} else {
				regExp = activeProfile.namingConventionsConfig.messageTemplateWithWildcardsRegExp
				message = "MessageTemplateWithMatchingExpression";
			}
		} else {
			if (matchingSymbols.empty) {
				regExp = activeProfile.namingConventionsConfig.derivedMessageTemplateRegExp
				message = "DerivedMessageTemplate";
			} else {
				regExp = activeProfile.namingConventionsConfig.derivedMessageTemplateWithWildcardsRegExp
				message = "DerivedMessageTemplateWithMatchingExpression";
			}
		}

		// check meaage template conventions
		template.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

	@Check
	def checkSTF160Template(BaseTemplate template) {
		var regExp = ""
		var message = ""
		val templateDef = template.findDesiredParent(TemplateDef)
		val isDerived = templateDef.derived != null
		var isSendTemplate = false;
		var isAmbiguous = false;

		// check if it is not a present restriction, but a restriction
		if (templateDef.restriction != null && templateDef.restriction.present == null) {
			for (p : template.parList.params) {
				if (p instanceof FormalTemplatePar) {
					if (p.restriction == null || p.restriction.restriction == null ||
						p.restriction.restriction.present != null) {
						// problem occurred, inconsistent definition
						isAmbiguous = true
					}
				} else {
					// problem occurred, inconsistent definition
					isAmbiguous = true
				}
				if (isAmbiguous) {
					val INode node = NodeModelUtils.getNode(template)
					val infoMessage = "The template definition for \"" + template.name +
						"\" is ambiguous. It cannot be determined whether it is a send or a receive template according to STF160's conventions. It will not be analyzed further for naming conventions compliance."
					info(
						infoMessage,
						TTCN3Package.eINSTANCE.TTCN3Reference_Name,
						MessageClass.NAMING.toString,
						node.startLine.toString,
						node.endLine.toString,
						"2.1, " + MiscTools.getMethodName(),
						LogLevel.INFORMATION.toString
					);
					return;
				}
			}

			//if no problems occurred this far check name for send template
			isSendTemplate = true;
		} //(else) check name for receive template

		if (!isDerived) {
			if (isSendTemplate) {
				message = "STF160SendTemplate";
				regExp = activeProfile.namingConventionsConfig.stf160sendTemplateRegExp
			} else {
				message = "STF160ReceiveTemplate";
				regExp = activeProfile.namingConventionsConfig.stf160receiveTemplateRegExp
			}
		} else {
			if (isSendTemplate) {
				message = "DerivedSTF160SendTemplate";
				regExp = activeProfile.namingConventionsConfig.derivedStf160sendTemplateRegExp
			} else {
				message = "DerivedSTF160ReceiveTemplate";
				regExp = activeProfile.namingConventionsConfig.derivedStf160receiveTemplateRegExp
			}
		}

		// check STF160 template conventions
		template.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

	def private boolean regExpMatch(String regExp, String subject) {
		val pattern = Pattern.compile(regExp);
		val Matcher matcher = pattern.matcher(subject);
@@ -242,16 +340,19 @@ class TTCN3Validator extends AbstractTTCN3Validator {
		if (object == null || Strings.isNullOrEmpty(regExp))
			return;

		if (!activeProfile.checkNamingConventions)
			return;

		val INode node = NodeModelUtils.getNode(object)

		if (!regExp.regExpMatch(object.name)) {
			val message = "\"" + object.name + "\" does not comply to the naming conventions for \"" + type + "\"!"
			info(
				"Wrong " + type + " name!",
				message,
				TTCN3Package.eINSTANCE.TTCN3Reference_Name,
				MessageClass.NAMING.toString,
				node.startLine.toString,
				node.endLine.toString,
				"\"" + object.name + "\" does not comply to the naming conventions for \"" + type + "\"!",
				"2.1, " + regExp,
				LogLevel.INFORMATION.toString
			);