Commit a9e5ab60 authored by Daniel Honsel's avatar Daniel Honsel
Browse files

added some naming convention validation rules

parent 5730e61f
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -959,10 +959,10 @@ TemplateInstanceActualPar:
	InLineTemplate | {TemplateInstanceActualPar} MINUS;

TemplateRestriction:
	LPAREN (OMITKEYWORD | VALUEKEYWORD | PRESENTKEYWORD) RPAREN;
	LPAREN (omit=OMITKEYWORD | value=VALUEKEYWORD | present=PRESENTKEYWORD) RPAREN;

RestrictedTemplate:
	OMITKEYWORD | (TEMPLATEKEYWORD TemplateRestriction);
	{RestrictedTemplate}OMITKEYWORD | (TEMPLATEKEYWORD restriction=TemplateRestriction);

LogStatement:
	LOGKEYWORD LPAREN item+=LogItem (COMMA item+=LogItem)* RPAREN;
@@ -1146,7 +1146,7 @@ GroupRefList:
	groups+=[GroupDef|QualifiedIdentifier] (COMMA groups+=[GroupDef|QualifiedIdentifier])*;

TTCN3Reference:
	ReferencedType | RefValue | TestcaseDef | AltstepDef | SignatureDef;
	ReferencedType | RefValue | TestcaseDef | AltstepDef | SignatureDef | PortElement;

TTCN3ReferenceList:
	refs+=[TTCN3Reference|IDENTIFIER] (COMMA refs+=[TTCN3Reference|IDENTIFIER])*;
@@ -1244,7 +1244,7 @@ FormalPortPar:
	INOUTPARKEYWORD? name=IDENTIFIER id=IDENTIFIER;

FormalTemplatePar:
	(inOut=(INPARKEYWORD | OUTPARKEYWORD | INOUTPARKEYWORD))? (TEMPLATEKEYWORD | RestrictedTemplate) type=Type
	(inOut=(INPARKEYWORD | OUTPARKEYWORD | INOUTPARKEYWORD))? (TEMPLATEKEYWORD | restriction=RestrictedTemplate) type=Type
	name=IDENTIFIER (ASSIGNMENTCHAR (templ=InLineTemplate | MINUS))?;

RunsOnSpec:
+158 −9
Original line number Diff line number Diff line
package de.ugoe.cs.swe.validation

import de.ugoe.cs.swe.common.logging.LoggingInterface.MessageClass
import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel
import com.google.common.base.Strings
import de.ugoe.cs.swe.TTCN3Configuration.QualityCheckProfile
import de.ugoe.cs.swe.TTCN3Configuration.T3QConfig
import de.ugoe.cs.swe.common.ConfigTools
import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel
import de.ugoe.cs.swe.common.logging.LoggingInterface.MessageClass
import de.ugoe.cs.swe.tTCN3.AltstepDef
import de.ugoe.cs.swe.tTCN3.AltstepLocalDef
import de.ugoe.cs.swe.tTCN3.AltstepLocalDefList
@@ -18,7 +17,12 @@ import de.ugoe.cs.swe.tTCN3.EnumDef
import de.ugoe.cs.swe.tTCN3.EnumDefNamed
import de.ugoe.cs.swe.tTCN3.Enumeration
import de.ugoe.cs.swe.tTCN3.EnumerationList
import de.ugoe.cs.swe.tTCN3.ExtFunctionDef
import de.ugoe.cs.swe.tTCN3.ForStatement
import de.ugoe.cs.swe.tTCN3.FormalPortPar
import de.ugoe.cs.swe.tTCN3.FormalTemplatePar
import de.ugoe.cs.swe.tTCN3.FormalTimerPar
import de.ugoe.cs.swe.tTCN3.FormalValuePar
import de.ugoe.cs.swe.tTCN3.FunctionDef
import de.ugoe.cs.swe.tTCN3.FunctionDefList
import de.ugoe.cs.swe.tTCN3.FunctionFormalPar
@@ -61,14 +65,15 @@ import java.util.regex.Matcher
import java.util.regex.Pattern
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.validation.Check

import static extension de.ugoe.cs.swe.common.TTCN3ScopeHelper.*
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.nodemodel.INode
import de.ugoe.cs.swe.tTCN3.TimerInstance
import de.ugoe.cs.swe.tTCN3.ComponentElementDef

class TTCN3Validator extends AbstractTTCN3Validator {
	val private String configurationFilename = "config/test.t3q";
	val ConfigTools configTools = ConfigTools.getInstance;
	var QualityCheckProfile activeProfile = null;

@@ -80,16 +85,157 @@ class TTCN3Validator extends AbstractTTCN3Validator {
		}
	}

	/**
	 * BEGIN: NAMING CONVENTIONS -----------------------------------------------------------------------
	 */
	@Check
	def checkModuleName(TTCN3Module module) {
		val regExp = activeProfile.namingConventionsConfig.moduleRegExp;
		module.checkIdentifierForNamingConventionCompliance(regExp, "module")
		module.checkIdentifierForNamingConventionCompliance(regExp, "Module")
	}

	@Check
	def checkFunctionName(FunctionDef function) {
		val regExp = activeProfile.namingConventionsConfig.functionRegExp;
		function.checkIdentifierForNamingConventionCompliance(regExp, "Function")
	}

	@Check
	def checkExtFunctionName(ExtFunctionDef function) {
		val regExp = activeProfile.namingConventionsConfig.extFunctionRegExp;
		function.checkIdentifierForNamingConventionCompliance(regExp, "External Function")
	}

	@Check
	def checkConstName(SingleConstDef const) {
		var message = ""
		var regExp = ""
		val ConstDef cDef = const.findDesiredParent(ConstDef)
		if (cDef.eContainer instanceof ModuleDefinition) {
			message = " Constant"
			regExp = activeProfile.namingConventionsConfig.constantRegExp
		} else {
			message = " Local Constant"
			regExp = activeProfile.namingConventionsConfig.localConstantRegExp

			const.checkIdentifierForNamingConventionCompliance(regExp, message)
		}
	}

	//TODO: fix external constants
	//	@Check
	//	def checkExtConstantnName(ExtConstDef const) {
	//		val regExp = activeProfile.namingConventionsConfig.extFunctionRegExp;
	//		const.checkIdentifierForNamingConventionCompliance(regExp, "External Constant")
	//	}	
	@Check
	def checkAltstepName(AltstepDef altstep) {
		val regExp = activeProfile.namingConventionsConfig.altstepRegExp;
		altstep.checkIdentifierForNamingConventionCompliance(regExp, "Altstep")
	}

	@Check
	def checkGroupName(GroupDef group) {
		val regExp = activeProfile.namingConventionsConfig.groupRegExp;
		group.checkIdentifierForNamingConventionCompliance(regExp, "Group")
	}

	@Check
	def checkModuleParameterName(ModuleParameter moduleParameter) {
		val regExp = activeProfile.namingConventionsConfig.moduleParameterRegExp;
		moduleParameter.checkIdentifierForNamingConventionCompliance(regExp, "Module Parameter")
	}

	@Check
	def checkTestcaseName(TestcaseDef testcase) {
		val regExp = activeProfile.namingConventionsConfig.testcaseRegExp;
		testcase.checkIdentifierForNamingConventionCompliance(regExp, "Testcase")
	}

	@Check
	def checkTestcaseName(SignatureDef signature) {
		val regExp = activeProfile.namingConventionsConfig.signatureTemplateRegExp;
		signature.checkIdentifierForNamingConventionCompliance(regExp, "SignatureTemplate")
	}

	@Check
	def checkTestcaseName(FormalValuePar parameter) {
		val regExp = activeProfile.namingConventionsConfig.formalParameterRegExp;
		parameter.checkIdentifierForNamingConventionCompliance(regExp, "FormalParameter")
	}

	@Check
	def checkTestcaseName(FormalPortPar parameter) {
		val regExp = activeProfile.namingConventionsConfig.formalParameterRegExp;
		parameter.checkIdentifierForNamingConventionCompliance(regExp, "FormalParameter")
	}

	@Check
	def checkTestcaseName(FormalTemplatePar parameter) {
		val regExp = activeProfile.namingConventionsConfig.formalParameterRegExp;
		parameter.checkIdentifierForNamingConventionCompliance(regExp, "FormalParameter")
	}

	@Check
	def checkTestcaseName(FormalTimerPar parameter) {
		val regExp = activeProfile.namingConventionsConfig.formalParameterRegExp;
		parameter.checkIdentifierForNamingConventionCompliance(regExp, "FormalParameter")
	}

	@Check
	def checkEnumeratedValueName(Enumeration enumeratedValue) {
		val regExp = activeProfile.namingConventionsConfig.enumeratedValueRegExp;
		enumeratedValue.checkIdentifierForNamingConventionCompliance(regExp, "EnumeratedValue")
	}

	@Check
	def checkSubTypeValueName(SubTypeDefNamed dataType) {
		val regExp = activeProfile.namingConventionsConfig.dataTypeRegExp;
		dataType.checkIdentifierForNamingConventionCompliance(regExp, "DataType")
	}

	@Check
	def checkRecordOfTypeName(RecordOfDefNamed dataType) {
		val regExp = activeProfile.namingConventionsConfig.dataTypeRegExp;
		dataType.checkIdentifierForNamingConventionCompliance(regExp, "DataType")
	}

	@Check
	def checkSetOfTypeName(SetOfDefNamed dataType) {
		val regExp = activeProfile.namingConventionsConfig.dataTypeRegExp;
		dataType.checkIdentifierForNamingConventionCompliance(regExp, "DataType")
	}

	//TODO: check if something more is required (see implementation of old t3tools)
	// Timer instances are included here
	@Check
	def checkVariableName(SingleVarInstance variable) {
		var regExp = ""
		var message = ""
		val TimerInstance timer = variable.findDesiredParent(TimerInstance)
		val ComponentElementDef componend = variable.findDesiredParent(ComponentElementDef)

		if (timer != null && componend != null) {
			regExp = activeProfile.namingConventionsConfig.componentTimerRegExp
			message = "ComponentTimer"
		} else if (timer != null && componend == null) {
			regExp = activeProfile.namingConventionsConfig.timerRegExp
			message = "Timer"
		} else if (timer == null && componend != null) {
			regExp = activeProfile.namingConventionsConfig.componentVariableRegExp
			message = "ComponentVariable"
		} else {
			regExp = activeProfile.namingConventionsConfig.variableRegExp
			message = "Variable"
		}
		variable.checkIdentifierForNamingConventionCompliance(regExp, message)
	}

	//TODO: templates
	def private boolean regExpMatch(String regExp, String subject) {
		val pattern = Pattern.compile(regExp);
		val Matcher matcher = pattern.matcher(subject);
		return matcher.matches;
		return matcher.matches
	}

	def private checkIdentifierForNamingConventionCompliance(TTCN3Reference object, String regExp, String type) {
@@ -98,7 +244,7 @@ class TTCN3Validator extends AbstractTTCN3Validator {

		val INode node = NodeModelUtils.getNode(object)

		if (!object.name.regExpMatch(regExp)) {
		if (!regExp.regExpMatch(object.name)) {
			info(
				"Wrong " + type + " name!",
				TTCN3Package.eINSTANCE.TTCN3Reference_Name,
@@ -113,6 +259,9 @@ class TTCN3Validator extends AbstractTTCN3Validator {
		}
	}

	/**
	 * END: NAMING CONVENTIONS -----------------------------------------------------------------------
	 */
	@Check
	def checkUniqueNameNestedVariable(RefValue variable) {
		variable.isNameClashInHierarchy