public void TestAddUnknownArgument() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); TypeNode arg = new TypeNode("int", true); mdn.AddUnknownArgument(arg); Assert.IsNotNull(mdn.UnknownArguments); Assert.AreEqual(1, mdn.UnknownArguments.Count); Assert.AreEqual(arg, mdn.UnknownArguments[0]); VariableDeclarationNode vdn = new VariableDeclarationNode("foo"); mdn.AddUnknownArgument(vdn); Assert.AreEqual(2, mdn.UnknownArguments.Count); Assert.AreEqual(arg, mdn.UnknownArguments[0]); Assert.AreEqual(vdn, mdn.UnknownArguments[1]); }
public void TestConstructor_Name() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Assert.AreEqual("MyMethod", mdn.Name); Assert.IsNull(mdn.Context); Assert.AreEqual(MethodRole.Unknown, mdn.Role); Assert.IsNull(mdn.FormalParameters); Assert.IsNull(mdn.DeclaringClass); Assert.IsNull(mdn.ReturnType); Assert.IsNull(mdn.Action); Assert.IsNull(mdn.Theme); Assert.IsNull(mdn.SecondaryArguments); Assert.IsNull(mdn.UnknownArguments); Assert.IsFalse(mdn.IsReactive); Assert.IsFalse(mdn.IsConstructor); Assert.IsFalse(mdn.IsDestructor); }
public void TestConstructor_Context() { MethodContext mc = new MethodContext("STATUS", false); MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod", mc); Assert.AreEqual("MyMethod", mdn.Name); Assert.AreEqual(mc, mdn.Context); Assert.AreEqual(MethodRole.Unknown, mdn.Role); Assert.IsNull(mdn.FormalParameters); Assert.IsNull(mdn.DeclaringClass); Assert.IsNull(mdn.ReturnType); Assert.IsNull(mdn.Action); Assert.IsNull(mdn.Theme); Assert.IsNull(mdn.SecondaryArguments); Assert.IsNull(mdn.UnknownArguments); Assert.IsFalse(mdn.IsReactive); Assert.IsFalse(mdn.IsConstructor); Assert.IsFalse(mdn.IsDestructor); }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { string firstWord = node.ParsedName[0].Text; if (IsChecker(firstWord) || IsSpecialCase(firstWord) || IsEventHandler(node.ParsedName) //|| IsEventHandler(node.FormalParameters) //pretty sure the parameters haven't been set yet || StartsNounPhrase(firstWord) || IsPrepositionalPhrase(node.ParsedName) || IsNonBaseVerb(firstWord)) { return(false); } else { return(true); } }
public override void Execute() { Console.WriteLine("Using srcML file {0}", this.File); UnigramSwumBuilder builder = new UnigramSwumBuilder(); if (CountFile != null) { Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile); builder.Splitter = new SamuraiIdSplitter(CountFile); } Console.WriteLine("SwumBuilder initialized"); if (this.SamplePercent <= 0) { this.SamplePercent = 100; } Random rand = new Random(); SrcMLFile testFile = new SrcMLFile(this.File); int methodCount = 0; var functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor }; foreach (XElement file in testFile.FileUnits) { Console.WriteLine("File {0}:", file.Attribute("filename").Value); //print all the function names foreach (var func in (from func in file.Descendants() where functionTypes.Any(c => c == func.Name) && !func.Ancestors(SRC.Declaration).Any() && (rand.Next(100) < this.SamplePercent) select func)) { string funcName = SrcMLElement.GetNameForMethod(func).Value; Console.WriteLine("<{0}> {1}", func.Name.LocalName, GetMethodSignature(func)); MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func)); builder.ApplyRules(mdn); Console.WriteLine(mdn.ToString() + Environment.NewLine); methodCount++; } } Console.WriteLine("{0} functions analyzed", methodCount); }
/// <summary> /// Constructs SWUMs for each of the methods defined in <paramref name="unitElement"/> and adds them to the cache. /// </summary> /// <param name="unitElement">The root element for the file unit to be processed.</param> /// <param name="filePath">The path for the file represented by <paramref name="unitElement"/>.</param> /// <exception cref="System.ArgumentNullException"><paramref name="unitElement"/> is null.</exception> protected void AddSwumForMethodDefinitions(XElement unitElement, string filePath) { if (unitElement == null) { throw new ArgumentNullException("unitElement"); } if (unitElement.Name != SRC.Unit) { throw new ArgumentException("Must be a SRC.Unit element", "unitElement"); } //iterate over each method definition in the SrcML file var fileAttribute = unitElement.Attribute("filename"); if (fileAttribute != null) { filePath = fileAttribute.Value; } var functions = from func in unitElement.Descendants() where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any() select func; foreach (XElement func in functions) { //construct SWUM on the function (if necessary) string sig = SrcMLElement.GetMethodSignature(func); lock (signaturesToSwum) { if (signaturesToSwum.ContainsKey(sig)) { //update the SwumDataRecord with the filename of the duplicate method signaturesToSwum[sig].FileNames.Add(filePath); } else { MethodDeclarationNode mdn = ConstructSwumFromMethodElement(func); var swumData = ProcessSwumNode(mdn); swumData.FileNames.Add(filePath); signaturesToSwum[sig] = swumData; } } } }
} //end InClassChecker Dictionary <SwumRule, MethodDeclarationNode> BuildValidSwums(String name, MethodContext mc) { Dictionary <SwumRule, bool> inClasses = InClassChecker(name, mc); MethodDeclarationNode mdn = null; Dictionary <SwumRule, MethodDeclarationNode> validSwums = new Dictionary <SwumRule, MethodDeclarationNode>(); foreach (KeyValuePair <SwumRule, bool> entry in inClasses) { if (entry.Value) { mdn = new MethodDeclarationNode(name, mc); entry.Key.InClass(mdn); entry.Key.ConstructSwum(mdn); validSwums.Add(entry.Key, mdn); } } return(validSwums); }
/// <summary> /// Construct the method summarizer /// </summary> /// <param name="md"></param> public SwumSummary(MethodDefinition md) { this.Method = md; var classBelong = md.GetAncestors <TypeDefinition>().FirstOrDefault(); //class name if (classBelong != null) { ClassName = classBelong.Name; } else { ClassName = ""; } //return type string returnType = "void"; if (md.ReturnType != null) { returnType = md.ReturnType.ToString(); } //Check if md returns primitive bool IsPrimitive = IsPrimitiveType(md.ReturnType); HashSet <FormalParameterRecord> paras = new HashSet <FormalParameterRecord>(); foreach (var para in md.Parameters) { var vType = para.VariableType; var tempPara = new FormalParameterRecord(vType.ToString(), BuiltInTypeFactory.IsBuiltIn(vType), para.Name); paras.Add(tempPara); } MethodContext mc = new MethodContext(returnType, IsPrimitive, ClassName, paras, false, md.IsConstructor, md.IsDestructor); this._mDeclarationNode = new MethodDeclarationNode(md.Name, mc); this._builder = new UnigramSwumBuilder(); this.IsSummarized = false; }
public void TestAddUnknownArguments() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.IsNotNull(mdn.UnknownArguments); Assert.AreEqual(args.Length, mdn.UnknownArguments.Count); for(int i = 0; i < args.Length; i++) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } List<Node> args2 = new List<Node>() { new VariableDeclarationNode("xyzzy"), new VariableDeclarationNode("myParam") }; mdn.AddUnknownArguments(args2); Assert.AreEqual(args.Length + args2.Count, mdn.UnknownArguments.Count); for(int i = 0; i < mdn.UnknownArguments.Count; i++) { if(i < args.Length) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } else { Assert.AreEqual(args2[i - args.Length], mdn.UnknownArguments[i]); } } }
/// <summary> /// Constructs the Software Word Use Model for the given node. /// This method assumes that the node has already been tested to satisfy this Rule, using the InClass method. /// </summary> /// <param name="node">The node to construct SWUM for.</param> public override void ConstructSwum(ProgramElementNode node) { if (node is MethodDeclarationNode) { MethodDeclarationNode mdn = (MethodDeclarationNode)node; mdn.Parse(this.Splitter); this.PosTagger.TagNounPhrase(mdn.ParsedName); mdn.AssignStructuralInformation(this.Splitter, this.PosTagger); mdn.Theme = mdn.ParsedName; mdn.AddUnknownArguments(mdn.FormalParameters); //TODO: from Emily, how to fill in Action? mdn.IsDestructor = true; mdn.SwumRuleUsed = this; } else { //TODO: return error? } }
public override void VisitMethodDeclarationNode(MethodDeclarationNode node) { if (_snapshot == null) return; base.VisitMethodDeclarationNode(node); var key = GenerateTag(node, _snapshot); if (key == null) { //We can't get a proper tag from this (likely ill-formed) method, so we'll skip it for now. var name = node.GetName(_snapshot); VSServiceProvider.Current.Logger.WriteToLog("Can't form a proper tag (likely ill-formed), ignoring member '" + name == null ? "" : name + "' for now..."); return; } if (_methods.ContainsKey(key)) { //For some reason, we have two methods with the same signature. There is //nothing we can do in this case so we just throw out the second method. VSServiceProvider.Current.Logger.WriteToLog("Two methods where found to have the exact same signature, ignoring second method for now..."); return; } _methods.Add(key, node); }
public void TestCreateEquivalenceFromUnknownArguments() { var arg1 = new VariableDeclarationNode("arg1"); var arg2 = new VariableDeclarationNode("arg2"); var arg3 = new VariableDeclarationNode("arg3"); var arg4 = new VariableDeclarationNode("arg4"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1, arg2, arg3, arg4 }); Assert.AreEqual(4, mdn.UnknownArguments.Count); var themeNode = new PhraseNode(new string[] { "Relevent", "Args" }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(themeNode, new bool[] { true, false, true, false }); Assert.AreEqual(3, equiv.EquivalentNodes.Count); Assert.IsTrue(equiv.EquivalentNodes.Contains(themeNode)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg1)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg3)); Assert.AreEqual(2, mdn.UnknownArguments.Count); Assert.IsTrue(mdn.UnknownArguments.Contains(arg2)); Assert.IsTrue(mdn.UnknownArguments.Contains(arg4)); }
/// <summary> /// Constructs SWUM on the given srcML method element. /// </summary> /// <param name="methodElement">The srcML element to use. This can be either a Function, Constructor or Destructor.</param> /// <param name="className">The class on which this method is declared.</param> /// <returns>A MethodDeclarationNode with SWUM rules applied to it.</returns> protected MethodDeclarationNode ConstructSwumFromMethodElement(XElement methodElement, string className) { if (!functionTypes.Contains(methodElement.Name)) { throw new ArgumentException("Element not a valid method type.", "methodElement"); } string funcName = SrcMLElement.GetNameForMethod(methodElement).Value; MethodContext mc = ContextBuilder.BuildMethodContext(methodElement); //set the declaring class name, if it's been passed in //this is necessary because the xml from the database for inline methods won't have the surrounding class xml if (string.IsNullOrEmpty(mc.DeclaringClass) && !string.IsNullOrEmpty(className)) { mc.DeclaringClass = className; } MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, mc); builder.ApplyRules(mdn); return(mdn); }
} // End Same-Action main method /// <summary> /// Checks if each SwumRule (13) returns (InClass = true) for the given method name. returns a dictionary of the SwumRule and boolean. /// Note that to build the swum you still have to run InClass for that method name. /// </summary> /// <param name="name">The name of the method</param> /// <param name="mc">The MethodContext object generated from the top-level method. This is required to generate a swum for any method</param> /// <returns>Dictionary of SwumRules and boolean thats true if InClass = true</returns> Dictionary <SwumRule, bool> InClassChecker(String name, MethodContext mc) { MethodDeclarationNode mdn = new MethodDeclarationNode(name, mc); Dictionary <SwumRule, bool> rules = new Dictionary <SwumRule, bool> { { new BaseVerbRule(posData, tagger, splitter), false }, { new CheckerRule(posData, tagger, splitter), false }, { new ConstructorRule(posData, tagger, splitter), false }, { new DefaultBaseVerbRule(posData, tagger, splitter), false }, { new DestructorRule(posData, tagger, splitter), false }, { new EmptyNameRule(posData, tagger, splitter), false }, { new EventHandlerRule(posData, tagger, splitter), false }, { new FieldRule(posData, tagger, splitter), false }, { new LeadingPrepositionRule(posData, tagger, splitter), false }, { new NonBaseVerbRule(posData, tagger, splitter), false }, { new NounPhraseRule(posData, tagger, splitter), false }, { new ReactiveRule(posData, tagger, splitter), false }, { new SpecialCaseRule(posData, tagger, splitter), false } //{new SwumRule(posData, tagger, splitter),""}, these are abstract //{new UnigramMethodRule(posData, tagger, splitter),""}, //{new UnigramRule(posData, tagger, splitter),""} }; var listing = rules.Keys.ToList(); foreach (SwumRule ent in listing) { // Console.WriteLine(ent.GetType() + " InClass = " + ent.InClass(mdn)); if (ent.InClass(mdn)) { rules[ent] = true; } } return(rules); } //end InClassChecker
public void TestLeadingPrepositionRule_OnGetAccObject() { var xml = @"<function><type><name>LRESULT</name></type> <name><name>CMenuContainer</name><op:operator>::</op:operator><name>OnGetAccObject</name></name><parameter_list>( <param><decl><type><name>UINT</name></type> <name>uMsg</name></decl></param>, <param><decl><type><name>WPARAM</name></type> <name>wParam</name></decl></param>, <param><decl><type><name>LPARAM</name></type> <name>lParam</name></decl></param>, <param><decl><type><name>BOOL</name><type:modifier>&</type:modifier></type> <name>bHandled</name></decl></param> )</parameter_list> <block>{ <if>if <condition>(<expr><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>lParam</name><op:operator>==</op:operator><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>OBJID_CLIENT</name> <op:operator>&&</op:operator> <name>m_pAccessible</name></expr>)</condition><then> <block>{ <return>return <expr><call><name>LresultFromObject</name><argument_list>(<argument><expr><name>IID_IAccessible</name></expr></argument>,<argument><expr><name>wParam</name></expr></argument>,<argument><expr><name>m_pAccessible</name></expr></argument>)</argument_list></call></expr>;</return> }</block></then> <else>else <block>{ <expr_stmt><expr><name>bHandled</name><op:operator>=</op:operator><name>FALSE</name></expr>;</expr_stmt> <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return> }</block></else></if> }</block></function>"; var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp"); var func = unit.Descendants(SRC.Function).First(); var mdn = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func)); builder.ApplyRules(mdn); Assert.AreEqual(typeof(LeadingPrepositionRule), mdn.SwumRuleUsed.GetType()); var expected = @"handle(Verb) | On(NounModifier) Get(NounModifier) Acc(NounModifier) Object(NounIgnorable) ++ [UINT(Noun) - u(Unknown) Msg(Unknown)] ++ [WPARAM(Noun) - w(Unknown) Param(Unknown)] ++ [LPARAM(Noun) - l(Unknown) Param(Unknown)] ++ [BOOL(Noun) - b(Unknown) Handled(Unknown)] ++ C(NounModifier) Menu(NounModifier) Container(NounIgnorable) ++ LRESULT(Noun)" ; Assert.AreEqual(expected, mdn.ToString()); }
private void AddRecommendation(Dictionary <string, int> recommendations, string p, int NormalWeight, MethodDeclarationNode methodDeclarationNode = null) { AddRecommendation(p.Trim(), NormalWeight, recommendations); if (methodDeclarationNode != null) { AddRecommendation(methodDeclarationNode.Name.Trim(), NormalWeight, recommendations); } }
public void TestAddSecondaryArgument() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); VariableDeclarationNode vdn = new VariableDeclarationNode("foo"); Assert.IsNull(mdn.SecondaryArguments); mdn.AddSecondaryArgument(vdn, new WordNode("to", PartOfSpeechTag.Preposition)); Assert.AreEqual(1, mdn.SecondaryArguments.Count); var sec = mdn.SecondaryArguments[0]; Assert.AreEqual(vdn, sec.Argument); Assert.AreEqual("to", sec.Preposition.Text); Assert.AreEqual(PartOfSpeechTag.Preposition, sec.Preposition.Tag); VariableDeclarationNode vdn2 = new VariableDeclarationNode("myParam"); mdn.AddSecondaryArgument(vdn2, new WordNode("from", PartOfSpeechTag.Preposition)); Assert.AreEqual(2, mdn.SecondaryArguments.Count); sec = mdn.SecondaryArguments[0]; Assert.AreEqual(vdn, sec.Argument); Assert.AreEqual("to", sec.Preposition.Text); Assert.AreEqual(PartOfSpeechTag.Preposition, sec.Preposition.Tag); sec = mdn.SecondaryArguments[1]; Assert.AreEqual(vdn2, sec.Argument); Assert.AreEqual("from", sec.Preposition.Text); Assert.AreEqual(PartOfSpeechTag.Preposition, sec.Preposition.Tag); }
static bool CheckCall(CallNode node, List <string> validVariables, List <string> validMethods, List <string> variableTypes, ClassDeclarationNode currentClass, string targetReturnType) { ClassDeclarationNode returnTypeClass = null; int argCount = 0; if (node.CallerName == "this") //nothing { returnTypeClass = currentClass; } else //variable { if (node.CallerName != null) // variable name check { if (validVariables.Contains(node.CallerName)) { returnTypeClass = findClass(variableTypes[validVariables.IndexOf(node.CallerName)]); } else { Error(node); return(false); } } } if (returnTypeClass == null && node.CalleeNames.Count == 0) { Error(node); return(false); } foreach (string calleeName in node.CalleeNames) //check Callee Names { MethodDeclarationNode method = null; if (returnTypeClass == null) { if (calleeName != node.CalleeNames[0]) { Error(node); return(false); } if (validMethods.Contains(calleeName)) //method { method = GetMethod(currentClass, calleeName); } else if (validVariables.Contains(calleeName)) //variable { method = null; returnTypeClass = findClass(variableTypes[validVariables.IndexOf(calleeName)]); } else if (findClass(calleeName) != null) //constructor { method = null; ClassDeclarationNode constructorClass = findClass(calleeName); bool isArgumentsOK = false; string argumentSignature = ""; foreach (ExpressionNode argument in node.arguments[argCount]) { argumentSignature += GetReturnType(argument, validVariables, validMethods, variableTypes, currentClass).name; } foreach (ConstructorDeclarationNode constructor in constructorClass.ConstructorDeclarations) { if (constructor.ParameterTypes.Count == node.arguments[argCount].Count) { string parameterSignature = ""; foreach (string parameterType in constructor.ParameterTypes) { parameterSignature += parameterType; } if (parameterSignature == argumentSignature) { isArgumentsOK = true; break; } } } if (!isArgumentsOK) { Error(node); return(false); } returnTypeClass = constructorClass; } else { Error(node); return(false); } } else { if (GetMethods(returnTypeClass)[0].Contains(calleeName)) { method = GetMethod(returnTypeClass, calleeName); } else if (GetVariables(returnTypeClass)[0].Contains(calleeName)) { returnTypeClass = findClass(GetVariables(returnTypeClass)[1][GetVariables(returnTypeClass)[0].IndexOf(calleeName)]); } else { Error(node); return(false); } } if (method != null) //if method, check arguments { if (method.ParameterTypes.Count > 0) { if (method.ParameterTypes.Count != node.arguments[argCount].Count) { Error(node); return(false); } for (int i = 0; i < node.arguments[argCount].Count; i++) { if (!CheckExpression(node.arguments[argCount][i], validVariables, validMethods, variableTypes, currentClass, method.ParameterTypes[i])) { Error(node.arguments[argCount][i]); return(false); } } } returnTypeClass = method.ReturnType == null ? null : findClass(method.ReturnType); } argCount++; } if (targetReturnType != "-1" && returnTypeClass == null && targetReturnType != null) //check maybe we shouldn't return anything { Error(node); return(false); } if (returnTypeClass == null && targetReturnType == null) { return(true); } if (targetReturnType != "-1" && returnTypeClass.name != targetReturnType && !GetSuperClasses(returnTypeClass).Contains(targetReturnType)) { Error(node); return(false); } return(true); }
private MethodSymbol BuildMethod(MethodDeclarationNode methodNode, TypeSymbol typeSymbol) { MethodSymbol method = null; if (methodNode.NodeType == ParseNodeType.ConstructorDeclaration) { method = new ConstructorSymbol(typeSymbol, (methodNode.Modifiers & Modifiers.Static) != 0); } else { TypeSymbol returnType = typeSymbol.SymbolSet.ResolveType(methodNode.Type, _symbolTable, typeSymbol); Debug.Assert(returnType != null); if (returnType != null) { method = new MethodSymbol(methodNode.Name, typeSymbol, returnType); BuildMemberDetails(method, typeSymbol, methodNode, methodNode.Attributes); ICollection <string> conditions = null; foreach (AttributeNode attrNode in methodNode.Attributes) { if (attrNode.TypeName.Equals("Conditional", StringComparison.Ordinal)) { if (conditions == null) { conditions = new List <string>(); } Debug.Assert(attrNode.Arguments[0] is LiteralNode); Debug.Assert(((LiteralNode)attrNode.Arguments[0]).Value is string); conditions.Add((string)((LiteralNode)attrNode.Arguments[0]).Value); } } if (conditions != null) { method.SetConditions(conditions); } } } if (method != null) { if ((methodNode.Modifiers & Modifiers.Abstract) != 0) { method.SetImplementationState(SymbolImplementationFlags.Abstract); } else if ((methodNode.Modifiers & Modifiers.Override) != 0) { method.SetImplementationState(SymbolImplementationFlags.Override); } if ((methodNode.Parameters != null) && (methodNode.Parameters.Count != 0)) { foreach (ParameterNode parameterNode in methodNode.Parameters) { ParameterSymbol paramSymbol = BuildParameter(parameterNode, method); if (paramSymbol != null) { paramSymbol.SetParseContext(parameterNode); method.AddParameter(paramSymbol); } } } if ((method.Visibility & MemberVisibility.Static) != 0) { string scriptAlias = GetAttributeValue(methodNode.Attributes, "ScriptAlias"); if (scriptAlias != null) { method.SetAlias(scriptAlias); } } } return(method); }
bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler) { MethodDeclarationNode methodNode = (MethodDeclarationNode)node; if ((methodNode.Modifiers & Modifiers.Static) == 0 && (methodNode.Modifiers & Modifiers.New) != 0) { errorHandler.ReportNodeValidationError(DSharpStringResources.STATIC_NEW_KEYWORD_UNSUPPORTED, methodNode); return(false); } if ((methodNode.Modifiers & Modifiers.Extern) != 0) { CustomTypeNode typeNode = (CustomTypeNode)methodNode.Parent; MethodDeclarationNode implMethodNode = null; if (methodNode.NodeType == ParseNodeType.MethodDeclaration) { foreach (MemberNode memberNode in typeNode.Members) { if (memberNode.NodeType == ParseNodeType.MethodDeclaration && (memberNode.Modifiers & Modifiers.Extern) == 0 && memberNode.Name.Equals(methodNode.Name, StringComparison.Ordinal)) { implMethodNode = (MethodDeclarationNode)memberNode; break; } } } else if (methodNode.NodeType == ParseNodeType.ConstructorDeclaration) { foreach (MemberNode memberNode in typeNode.Members) { if (memberNode.NodeType == ParseNodeType.ConstructorDeclaration && (memberNode.Modifiers & Modifiers.Extern) == 0) { implMethodNode = (MethodDeclarationNode)memberNode; break; } } } if (implMethodNode == null) { errorHandler.ReportNodeValidationError(DSharpStringResources.EXTERN_IMPLEMENTATION_FOUND_ERROR, methodNode); return(false); } if ((methodNode.Modifiers & (Modifiers.Static | Modifiers.AccessMask)) != (implMethodNode.Modifiers & (Modifiers.Static | Modifiers.AccessMask))) { errorHandler.ReportNodeValidationError(DSharpStringResources.EXTERN_STATIC_MEMBER_MISMATCH_ERROR, methodNode); } } return(true); }
public void TestToPlainString_Context() { var formals = new FormalParameterRecord[] { new FormalParameterRecord("SGVData*", false, "p"), new FormalParameterRecord("ASSchedule*", false, "p2") }; MethodContext mc = new MethodContext("int", true, "MyClass", formals, true, false, false); MethodDeclarationNode mdn = new MethodDeclarationNode("CalcNewValue", mc); var splitter = new ConservativeIdSplitter(); mdn.Parse(splitter); mdn.AssignStructuralInformation(splitter, new UnigramTagger()); Assert.AreEqual("Calc New Value", mdn.ToPlainString()); }
public static object GenerateTag(MethodDeclarationNode method, ITextSnapshot snapshot) { Contract.Requires(method != null); Contract.Requires(snapshot != null); var sb = new StringBuilder(); //Append our name var name = method.GetName(snapshot); if (name == null) { return(null); } sb.Append(name); sb.Append('!'); //Append our return type if (method.ReturnType != null) { var rn = method.ReturnType.GetName(snapshot); if (rn == null) { rn = "NONAME"; } sb.Append(rn); sb.Append('!'); } //Append parameters if (method.FormalParameterList != null) { foreach (var param in method.FormalParameterList) { //AppendParamter nameS if (param.Identifier != null && param.Identifier.Name != null) { sb.Append(param.Identifier.Name.Text); sb.Append('!'); } //Append parameter type if (param.Type != null) { var tn = param.Type.GetName(snapshot); if (tn != null) { sb.Append(tn); sb.Append('!'); } } //Append attributes? if (param.Attributes != null && param.Attributes.Count > 0) { } //Append flags if (param.Flags != default(NodeFlags)) { sb.Append(param.Flags); sb.Append('!'); } } } //var snapshotSpan = method.GetSpan().Convert(snapshot); //var methodText = snapshotSpan.GetText(); //string methodHeader; //if (methodText.Contains('{')) { // methodHeader = methodText.Substring(0, methodText.IndexOf('{')).Trim(); //} else if (methodText.Contains(';')) { // methodHeader = methodText.Substring(0, methodText.IndexOf(';')).Trim(); //} else // return null; //sb.Append(methodHeader); //sb.Append('!'); //Apend parent information var containingType = method.Parent; while (containingType != null) { var asClass = containingType.AsClass(); if (asClass != null) { sb.Append(asClass.Identifier.Name.Text); sb.Append('!'); goto EndOfLoop; } var asInterface = containingType.AsInterface(); if (asInterface != null) { sb.Append(asInterface.Identifier.Name.Text); sb.Append('!'); goto EndOfLoop; } /*Note: there is nothing we can do about the namespace, we can't seem to get namespace name info from the syntactic model. */ EndOfLoop: containingType = containingType.Parent; } //Append flags if (method.Flags != default(NodeFlags)) { sb.Append(method.Flags); sb.Append('!'); } //Append what kind of node we are sb.Append(MethodTagSuffix); return(sb.ToString()); }
public void TestAssignStructuralInformation_NullTagger() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); mdn.AssignStructuralInformation(new NullSplitter(), null); }
/// <summary> /// Sets the Action and Theme properties of the given MethodDeclarationNode using the default algorithms. /// This also sets the node's SecondaryArguments and UnknownArguments properties. /// </summary> /// <param name="node">The MethodDeclarationNode to set the Action and Theme on.</param> protected void SetDefaultActionAndTheme(MethodDeclarationNode node) { node.Action = GetVerbPhrase(node.ParsedName, node.Preamble); SetPrepositionThemeAndArguments(node); }
public void TestCreateEquivalenceFromUnknownArguments_NullNode() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(null, new bool[] { true }); }
public void TestCreateEquivalenceFromUnknownArguments() { var arg1 = new VariableDeclarationNode("arg1"); var arg2 = new VariableDeclarationNode("arg2"); var arg3 = new VariableDeclarationNode("arg3"); var arg4 = new VariableDeclarationNode("arg4"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1, arg2, arg3, arg4 }); Assert.AreEqual(4, mdn.UnknownArguments.Count); var themeNode = new PhraseNode(new string[] { "Relevent", "Args" }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(themeNode, new bool[] { true, false, true, false }); Assert.AreEqual(3, equiv.EquivalentNodes.Count); Assert.IsTrue(equiv.EquivalentNodes.Contains(themeNode)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg1)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg3)); Assert.AreEqual(2, mdn.UnknownArguments.Count); Assert.IsTrue(mdn.UnknownArguments.Contains(arg2)); Assert.IsTrue(mdn.UnknownArguments.Contains(arg4)); }
public void TestCreateEquivalenceFromUnknownArguments_MissizedArray() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(new PhraseNode(), new bool[] { true, false }); }
public void TestGetParse() { MethodContext mc = new MethodContext("int", true, "MyClass", null, true, false, false); MethodDeclarationNode mdn = new MethodDeclarationNode("CalcNewValue", mc); UnigramSwumBuilder builder = new UnigramSwumBuilder(); builder.ApplyRules(mdn); var parsedName = mdn.GetParse(); Assert.AreEqual(3, parsedName.Size()); Assert.AreEqual("Calc", parsedName[0].Text); Assert.AreEqual(PartOfSpeechTag.Verb, parsedName[0].Tag); Assert.AreEqual("New", parsedName[1].Text); Assert.AreEqual(PartOfSpeechTag.NounModifier, parsedName[1].Tag); Assert.AreEqual("Value", parsedName[2].Text); Assert.AreEqual(PartOfSpeechTag.NounIgnorable, parsedName[2].Tag); }
public void TestToPlainString_NotParsed() { var formals = new FormalParameterRecord[] { new FormalParameterRecord("SGVData*", false, "p"), new FormalParameterRecord("ASSchedule*", false, "p2") }; MethodContext mc = new MethodContext("int", true, "MyClass", formals, true, false, false); MethodDeclarationNode mdn = new MethodDeclarationNode("CalcNewValue", mc); Assert.AreEqual("CalcNewValue", mdn.ToPlainString()); }
public void GenerateVoidReturnSUnit() { var dataProject = new DataProject <CompleteWorkingSet>(folderName, Path.GetFullPath(fullFilePath), "..//..//..//SrcML"); dataProject.UpdateAsync().Wait(); //get srcml stuff in order NamespaceDefinition globalNamespace; Assert.That(dataProject.WorkingSet.TryObtainReadLock(5000, out globalNamespace)); //initialize swum stuff splitter = new ConservativeIdSplitter(); tagger = new UnigramTagger(); posData = new PCKimmoPartOfSpeechData(); //find an example method var guiMethod = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName).First(); var guiMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, guiMethod.PrimaryLocation); // forget that, find ALL the methods var methods = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName); foreach (MethodDefinition method in methods) { //Console.WriteLine(method.ToString()); var statements = method.ChildStatements; foreach (Statement statement in statements) { var expressions = statement.GetExpressions(); foreach (Expression expression in expressions) { // Skip any expression that contains an assignment if (expression.ToString().Contains(" =") || expression.ToString().Contains(" ->")) { continue; } // Print whatever's left. It should be a void return. Console.WriteLine(expression.ToString()); // *** PoS tag it *** // convert the string to 'PhraseNode' objects so we can feed them to SWUM var pn = PhraseNode.Parse(new WordNode(expression.ToString()).ToString()); Console.WriteLine(pn.ToString()); // construct the "rule" to break up method names into sentences BaseVerbRule thisrule = new BaseVerbRule(posData, tagger, splitter); var methodNode = new MethodDeclarationNode(expression.ToString()); thisrule.ConstructSwum(methodNode); Console.WriteLine(methodNode.ToString()); } } } }
/// <summary> /// Sets the Theme, SecondaryArguments and UnknownArguments properties of the given MethodDeclarationNode. /// </summary> /// <param name="node">The node to set the properties for.</param> private void SetPrepositionThemeAndArguments(MethodDeclarationNode node) { List <VariableDeclarationNode> unusedArgs = new List <VariableDeclarationNode>(); //populate UnknownArgs if (node.ParsedName.Size() > 0 && BooleanArgumentVerbs.Contains(node.ParsedName[0].Text)) { node.AddUnknownArguments(node.FormalParameters); } else { //only add non-boolean arguments foreach (VariableDeclarationNode arg in node.FormalParameters) { if (arg.Type.Name.ToLower().Contains("bool")) { unusedArgs.Add(arg); } else { node.AddUnknownArgument(arg); } } } int prepIndex = FindFirstPreposition(node.ParsedName, 0); PhraseNode nameTheme = GetNounPhrase(node.ParsedName); bool checkDO = false; //check Direct Object in name for overlap bool checkIO = false; //check Indirect Object in name for overlap //Assign args if (prepIndex > -1) { //There's a preposition in the name WordNode prep = node.ParsedName[prepIndex]; PhraseNode indirectObject = GetNounPhrase(node.ParsedName, prepIndex + 1); //set IO or P->NM if (!indirectObject.IsEmpty()) //IO in name { node.AddSecondaryArgument(indirectObject, prep); checkIO = true; } else if (node.UnknownArguments != null && node.UnknownArguments.Count() > 0) //or IO = f { node.AddSecondaryArgument(node.UnknownArguments[0], prep); } else { //The preposition doesn't seem to have an object, so change it to a NounModifier prep.Tag = PartOfSpeechTag.NounModifier; nameTheme = GetNounPhrase(node.ParsedName); //reset name theme after changing prep POS tag } //set Theme if (!nameTheme.IsEmpty()) { //theme is in the name nameTheme.SetLocation(Location.Name); node.Theme = nameTheme; checkDO = true; } else //use class as theme { node.Theme = node.DeclaringClass; } } else { //no prep in name, so set Theme only if (!nameTheme.IsEmpty()) { //theme is in the name nameTheme.SetLocation(Location.Name); node.Theme = nameTheme; checkDO = true; } else { //theme is first UnknownArg, or class name //also, potentially leaves class on list of unknown args, which is intentional if (node.DeclaringClass != null) { node.AddUnknownArgument(node.DeclaringClass); } if (node.UnknownArguments != null && node.UnknownArguments.Count > 0) { node.Theme = node.UnknownArguments[0]; node.UnknownArguments.RemoveAt(0); } } } //find equivalences if ((checkDO || checkIO) && node.UnknownArguments != null && node.UnknownArguments.Count > 0) { CheckOverlap(node, checkDO, checkIO); } //do cleanup node.AddUnknownArguments(unusedArgs); if (node.ReturnType != null && node.ReturnType.Name.ToLower() != "void") { //TODO: should this be done for primitive return types? SetDefaultUnknownArguments() excludes them node.AddUnknownArgument(node.ReturnType); } // Note: adding class as unknown arg indep of checkIO // if checkIO = true, and not checkDO, then DO will be class // if check IO = true and checkDO, then this will be executed // if no prep & DO not in name, class will already be on unused args // list for finding theme. if (checkDO && node.DeclaringClass != null) { node.AddUnknownArgument(node.DeclaringClass); } }
public void TestClearUnknownArguments_ExistingArgs() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.AreEqual(2, mdn.UnknownArguments.Count); mdn.ClearUnknownArguments(); Assert.AreEqual(0, mdn.UnknownArguments.Count); }
public void TestAssignStructuralInformation() { var formals = new FormalParameterRecord[] { new FormalParameterRecord("SGVData*", false, "p"), new FormalParameterRecord("ASSchedule*", false, "p2") }; MethodContext mc = new MethodContext("int", true, "MyClass", formals, true, false, false); MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod", mc); mdn.Parse(new NullSplitter()); mdn.AssignStructuralInformation(new NullSplitter(), new NullTagger()); Assert.AreEqual(Location.Name, mdn.ParsedName.Location); Assert.AreEqual("int", mdn.ReturnType.Name); Assert.IsTrue(mdn.ReturnType.IsPrimitive); Assert.AreEqual("MyClass", mdn.DeclaringClass.Name); Assert.AreEqual(2, mdn.FormalParameters.Count); Assert.AreEqual("SGVData*", mdn.FormalParameters[0].Type.Name); Assert.AreEqual("p", mdn.FormalParameters[0].Name); Assert.AreEqual("ASSchedule*", mdn.FormalParameters[1].Type.Name); Assert.AreEqual("p2", mdn.FormalParameters[1].Name); }
public static object GenerateTag(MethodDeclarationNode method, ITextSnapshot snapshot) { Contract.Requires(method != null); Contract.Requires(snapshot != null); var sb = new StringBuilder(); //Append our name var name = method.GetName(snapshot); if (name == null) return null; sb.Append(name); sb.Append('!'); //Append our return type if (method.ReturnType != null) { var rn = method.ReturnType.GetName(snapshot); if (rn == null) rn = "NONAME"; sb.Append(rn); sb.Append('!'); } //Append parameters if (method.FormalParameterList != null) { foreach (var param in method.FormalParameterList) { //AppendParamter nameS if (param.Identifier != null && param.Identifier.Name != null) { sb.Append(param.Identifier.Name.Text); sb.Append('!'); } //Append parameter type if (param.Type != null) { var tn = param.Type.GetName(snapshot); if (tn != null) { sb.Append(tn); sb.Append('!'); } } //Append attributes? if (param.Attributes != null && param.Attributes.Count > 0) { } //Append flags if (param.Flags != default(NodeFlags)) { sb.Append(param.Flags); sb.Append('!'); } } } //var snapshotSpan = method.GetSpan().Convert(snapshot); //var methodText = snapshotSpan.GetText(); //string methodHeader; //if (methodText.Contains('{')) { // methodHeader = methodText.Substring(0, methodText.IndexOf('{')).Trim(); //} else if (methodText.Contains(';')) { // methodHeader = methodText.Substring(0, methodText.IndexOf(';')).Trim(); //} else // return null; //sb.Append(methodHeader); //sb.Append('!'); //Apend parent information var containingType = method.Parent; while (containingType != null) { var asClass = containingType.AsClass(); if (asClass != null) { sb.Append(asClass.Identifier.Name.Text); sb.Append('!'); goto EndOfLoop; } var asInterface = containingType.AsInterface(); if (asInterface != null) { sb.Append(asInterface.Identifier.Name.Text); sb.Append('!'); goto EndOfLoop; } /*Note: there is nothing we can do about the namespace, we can't seem to get namespace name info from the syntactic model. */ EndOfLoop: containingType = containingType.Parent; } //Append flags if (method.Flags != default(NodeFlags)) { sb.Append(method.Flags); sb.Append('!'); } //Append what kind of node we are sb.Append(MethodTagSuffix); return sb.ToString(); }
public void TestCreateThemeFromPhrases_SecondNull() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); PhraseNode pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" }); mdn.CreateThemeFromPhrases(null, pn2); Assert.AreSame(pn2, mdn.Theme); Assert.AreEqual(2, pn2.Size()); Assert.AreEqual("cowboy", pn2[0].Text); Assert.AreEqual("watermelon", pn2[1].Text); }
public void TestClearSecondaryArguments_Null() { MethodDeclarationNode mdn = new MethodDeclarationNode("foo"); mdn.ClearSecondaryArguments(); Assert.IsNotNull(mdn.SecondaryArguments); Assert.AreEqual(0, mdn.SecondaryArguments.Count); }
public void TestCreateThemeFromPhrases_FirstNull() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); PhraseNode pn1 = new PhraseNode(new string[] { "hello", "World" }); mdn.CreateThemeFromPhrases(pn1, null); Assert.AreSame(pn1, mdn.Theme); Assert.AreEqual(2, pn1.Size()); Assert.AreEqual("hello", pn1[0].Text); Assert.AreEqual("World", pn1[1].Text); }
/// <inheritdoc/> public override void VisitMethodDeclarationNode(MethodDeclarationNode node) { }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { return(node.ParsedName == null || node.ParsedName.IsEmpty()); }
public void enterMethod(MethodDeclarationNode declaration) { methodInfos.add(declaration.getUserData(typeof(MethodInfo))); }
bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler) { MethodDeclarationNode methodNode = (MethodDeclarationNode)node; if (((methodNode.Modifiers & Modifiers.Static) == 0) && ((methodNode.Modifiers & Modifiers.New) != 0)) { errorHandler.ReportError("The new modifier is not supported on instance members.", methodNode.Token.Location); return(false); } if ((methodNode.Modifiers & Modifiers.Extern) != 0) { AttributeNode altSigAttribute = AttributeNode.FindAttribute(methodNode.Attributes, "AlternateSignature"); if (altSigAttribute == null) { errorHandler.ReportError("Extern methods should only be used to declare alternate signatures and marked with [AlternateSignature].", methodNode.Token.Location); return(false); } CustomTypeNode typeNode = (CustomTypeNode)methodNode.Parent; MethodDeclarationNode implMethodNode = null; if (methodNode.NodeType == ParseNodeType.MethodDeclaration) { foreach (MemberNode memberNode in typeNode.Members) { if ((memberNode.NodeType == ParseNodeType.MethodDeclaration) && ((memberNode.Modifiers & Modifiers.Extern) == 0) && memberNode.Name.Equals(methodNode.Name, StringComparison.Ordinal)) { implMethodNode = (MethodDeclarationNode)memberNode; break; } } } else if (methodNode.NodeType == ParseNodeType.ConstructorDeclaration) { foreach (MemberNode memberNode in typeNode.Members) { if ((memberNode.NodeType == ParseNodeType.ConstructorDeclaration) && ((memberNode.Modifiers & Modifiers.Extern) == 0)) { implMethodNode = (MethodDeclarationNode)memberNode; break; } } } if (implMethodNode == null) { errorHandler.ReportError("Extern methods used to declare alternate signatures should have a corresponding non-extern implementation as well.", methodNode.Token.Location); return(false); } if ((methodNode.Modifiers & (Modifiers.Static | Modifiers.AccessMask)) != (implMethodNode.Modifiers & (Modifiers.Static | Modifiers.AccessMask))) { errorHandler.ReportError("The implemenation method and associated alternate signature methods should have the same access type.", methodNode.Token.Location); } } return(true); }
public override void Execute() { if (Pause) { Console.WriteLine("Ready to begin (press Enter)"); Console.ReadLine(); } Console.WriteLine("Using srcML file {0}", this.File); var builder = new UnigramSwumBuilder(); if (!string.IsNullOrWhiteSpace(CountFile)) { Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile); builder.Splitter = new SamuraiIdSplitter(CountFile); } Console.WriteLine("SwumBuilder initialized"); int methodCount = 0, fieldCount = 0; { SrcMLFile testFile = new SrcMLFile(this.File); var functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor }; foreach (XElement file in testFile.FileUnits) { string fileName = file.Attribute("filename").Value; Console.WriteLine("File {0}:", fileName); //compute SWUM on each function foreach (var func in (from func in file.Descendants() where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any() select func)) { var nameElement = SrcMLElement.GetNameForMethod(func); if (nameElement != null) { string funcName = nameElement.Value; string funcSignature = SrcMLElement.GetMethodSignature(func); if (PrintSwum) { Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature); } MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func)); builder.ApplyRules(mdn); methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn; if (PrintSwum) { Console.WriteLine(mdn.ToString() + Environment.NewLine); } methodCount++; } } //compute SWUM on each field foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement) where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name)) select declStmt.Element(SRC.Declaration))) { int declPos = 1; foreach (var nameElement in fieldDecl.Elements(SRC.Name)) { string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value; if (PrintSwum) { Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName); } FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl)); builder.ApplyRules(fdn); fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn; if (PrintSwum) { Console.WriteLine(fdn.ToString() + Environment.NewLine); } fieldCount++; declPos++; } } } } GC.Collect(); Console.WriteLine("{0} functions analyzed", methodCount); Console.WriteLine("{0} functions in dictionary", methodSwum.Count); Console.WriteLine("{0} fields analyzed", fieldCount); Console.WriteLine("{0} fields in dictionary", fieldSwum.Count); if (Pause) { Console.WriteLine("Finished building SWUM (press Enter)"); Console.ReadLine(); } }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { node.AssignStructuralInformation(this.Splitter, this.PosTagger); //necessary to check event handler-ness return(IsEventHandler(node.FormalParameters)); }
static ClassDeclarationNode GetReturnType(ExpressionNode nod, List <string> validVariables, List <string> validMethods, List <string> variableTypes, ClassDeclarationNode currentClass) { if (nod.ExpressionType == "call") //Check Call { ClassDeclarationNode returnTypeClass = null; CallNode node = nod.call; int argCount = 0; if (node.CallerName == "this") //nothing { returnTypeClass = currentClass; } else //variable { if (node.CallerName != null) // variable name check { if (validVariables.Contains(node.CallerName)) { returnTypeClass = findClass(variableTypes[validVariables.IndexOf(node.CallerName)]); } else { Error(node); } } } if (returnTypeClass == null && node.CalleeNames.Count == 0) { Error(node); } foreach (string calleeName in node.CalleeNames) //check Callee Names { MethodDeclarationNode method = null; if (returnTypeClass == null) { if (calleeName != node.CalleeNames[0]) { Error(node); } if (validMethods.Contains(calleeName)) //method { method = GetMethod(currentClass, calleeName); } else if (validVariables.Contains(calleeName)) //variable { method = null; returnTypeClass = findClass(variableTypes[validVariables.IndexOf(calleeName)]); } else if (findClass(calleeName) != null) //constructor { method = null; ClassDeclarationNode constructorClass = findClass(calleeName); bool isArgumentsOK = false; string argumentSignature = ""; foreach (ExpressionNode argument in node.arguments[argCount]) { argumentSignature += GetReturnType(argument, validVariables, validMethods, variableTypes, currentClass).name; } foreach (ConstructorDeclarationNode constructor in constructorClass.ConstructorDeclarations) { if (constructor.ParameterTypes.Count != node.arguments[argCount].Count) { string parameterSignature = ""; foreach (string parameterType in constructor.ParameterTypes) { parameterSignature += parameterType; } if (parameterSignature == argumentSignature) { isArgumentsOK = true; break; } } } if (!isArgumentsOK) { Error(node); } returnTypeClass = constructorClass; } else { Error(node); } } else { if (GetMethods(returnTypeClass)[0].Contains(calleeName)) { method = GetMethod(returnTypeClass, calleeName); } else if (GetVariables(returnTypeClass)[0].Contains(calleeName)) { returnTypeClass = findClass(GetVariables(returnTypeClass)[1][GetVariables(returnTypeClass)[0].IndexOf(calleeName)]); } else { Error(node); } } if (method != null) //if method, check arguments { if (method.ParameterTypes.Count > 0) { if (method.ParameterTypes.Count != node.arguments[argCount].Count) { Error(node); } for (int i = 0; i < node.arguments[argCount].Count; i++) { if (!CheckExpression(node.arguments[argCount][i], validVariables, validMethods, variableTypes, currentClass, method.ParameterTypes[i])) { Error(node.arguments[argCount][i]); } } } returnTypeClass = method.ReturnType == null ? null : findClass(method.ReturnType); } argCount++; } return(returnTypeClass); } else { if (nod.BooleanLiteral != null) { return(findClass("Boolean")); } if (nod.IntegerLiteral != null) { return(findClass("Integer")); } if (nod.FloatLiteral != null) { return(findClass("Real")); } } return(null); }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { return(IsEventHandler(node.ParsedName)); }
public void GenerateEndingSUnit() { /*var dataProject = new DataProject<CompleteWorkingSet>("npp_6.2.3", * Path.GetFullPath("..//..//..//projects//npp_6.2.3"), * "..//..//..//SrcML");*/ var dataProject = new DataProject <CompleteWorkingSet>(folderName, Path.GetFullPath(fullFilePath), "..//..//..//SrcML"); dataProject.UpdateAsync().Wait(); //get srcml stuff in order NamespaceDefinition globalNamespace; Assert.That(dataProject.WorkingSet.TryObtainReadLock(5000, out globalNamespace)); //initialize swum stuff splitter = new ConservativeIdSplitter(); tagger = new UnigramTagger(); posData = new PCKimmoPartOfSpeechData(); //find an example method, uses global methodName variable var testMethod = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName).First(); var testMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, testMethod.PrimaryLocation); //generate swum for method declaration MethodContext mc = ContextBuilder.BuildMethodContext(testMethodXElement); MethodDeclarationNode mdn = new MethodDeclarationNode(methodName, mc); //Console.WriteLine(mdn.ToString()); //returns nothing since it hasn't been written var exp = testMethod.GetDescendants(); //var verb = mdn.Action.ToString(); var expResult = exp.ElementAt(exp.Count() - 1); Console.WriteLine(expResult); MethodDeclarationNode expMDN = null; if (expResult is ReturnStatement) { Console.WriteLine("return"); } else if (expResult.ToString().Contains(" = ")) { Console.WriteLine("Ending S Unit contains '='"); } else { var mCall = expResult.FindExpressions <MethodCall>().First(); expMDN = new MethodDeclarationNode(mCall.Name, mc); } //MethodDeclarationNode mdn2 = new MethodDeclarationNode(expResult.ToString(), mc); //BaseVerbRule rule = new BaseVerbRule(posData, tagger, splitter); //Console.WriteLine("InClass = " + rule.InClass(mdn)); //REQUIRED in order for the ConstructSwum method to work //rule.ConstructSwum(mdn); //rewrites mdn.ToString to a SWUM breakdown //Console.WriteLine(mdn.Action.ToString()); BaseVerbRule rule2 = new BaseVerbRule(posData, tagger, splitter); Console.WriteLine("InClass = " + rule2.InClass(expMDN)); //REQUIRED in order for the ConstructSwum method to work rule2.ConstructSwum(expMDN); //rewrites mdn.ToString to a SWUM breakdown Console.WriteLine(expMDN.Action.ToString()); //Console.WriteLine(mdn.Action.ToString()); }
public void TestCreateThemeFromPhrases() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); PhraseNode pn1 = new PhraseNode(new string[] { "hello", "World" }); PhraseNode pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" }); mdn.CreateThemeFromPhrases(pn1, pn2); Assert.AreSame(pn1, mdn.Theme); Assert.AreEqual(4, pn1.Size()); Assert.AreEqual("hello", pn1[0].Text); Assert.AreEqual("World", pn1[1].Text); Assert.AreEqual("cowboy", pn1[2].Text); Assert.AreEqual("watermelon", pn1[3].Text); }
public void GenerateSameActionSUnit() { // var dataProject = new DataProject<CompleteWorkingSet>("npp_6.2.3", // Path.GetFullPath("..//..//..//projects//npp_6.2.3"), // "..//..//..//SrcML"); //var dataProject = new DataProject<CompleteWorkingSet>("CodeAnalysisToolkit", // Path.GetFullPath("..//..//..//samples"), // "..//..//..//SrcML"); var dataProject = new DataProject <CompleteWorkingSet>(folderName, Path.GetFullPath(fullFilePath), "..//..//..//SrcML"); List <String> success = new List <String>(); Dictionary <SwumRule, bool> inClasses = null; bool debug = false; ///////////////// DEBUGGING // Get SrcML stuff in order dataProject.UpdateAsync().Wait(); NamespaceDefinition globalNamespace; Assert.That(dataProject.WorkingSet.TryObtainReadLock(1000, out globalNamespace)); // Initialize Swum splitter = new ConservativeIdSplitter(); tagger = new UnigramTagger(); posData = new PCKimmoPartOfSpeechData(); var methodList = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName); MethodDefinition topMethod = null; // Check if the method was found try { topMethod = methodList.First(); } catch (System.InvalidOperationException) { Console.WriteLine("--ERROR: Method '" + methodName + "' Not Found--"); Assert.Fail("Method '" + methodName + "' Not Found"); } var guiMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, topMethod.PrimaryLocation); //generate swum for method declaration MethodContext mc = ContextBuilder.BuildMethodContext(guiMethodXElement); MethodDeclarationNode mdn = new MethodDeclarationNode(methodName, mc); BaseVerbRule rule = new BaseVerbRule(posData, tagger, splitter); Console.WriteLine("Method = \t" + methodName); Console.WriteLine("InClass = \t" + rule.InClass(mdn)); // Get the action verb from the SWUM String methodVerb = GetMethodVerb(methodName, mc); Console.WriteLine("Verb = \t\t" + methodVerb); Console.WriteLine("============================"); // Get all of the lines of code that contains the verb in any form var expr = topMethod.GetDescendants().Where(t => t.ToString().Contains(methodVerb)).ToArray(); // Iterate each line foreach (Statement t in expr) { if (debug) { Console.WriteLine("Line: " + t.ToString()); } // This finds any method calls in this line and finds the verb in that method - from GetCallsTo() var methods = t.FindExpressions <MethodCall>(true).Where(c => c.ToString().ToLower().Contains(methodVerb));//c.Name.Contains(methodVerb)); // Iterate through a list of the method calls in a line and find ones that contain the verb foreach (MethodCall i in methods) { if (debug) { Console.WriteLine("===\n" + i.Name); } MethodDeclarationNode mdnNEW = new MethodDeclarationNode(i.Name, mc); inClasses = InClassChecker(i.Name, mc); String foundVerb = GetMethodVerb(i.Name, mc); if (foundVerb.Equals("!NONE!")) { if (debug) { Console.WriteLine(" Method does not contain verb"); } } else { if (debug) { Console.WriteLine("GetMethodVerb= " + GetMethodVerb(i.Name, mc)); } success.Add(i.Name); } if (debug) { Console.WriteLine("CompareSwums= " + CompareSwums(i.Name, mc)); } // Debugging if (debug) { int numbtrue = 0; foreach (KeyValuePair <SwumRule, bool> entry in inClasses) { if (entry.Value) { numbtrue++; mdnNEW = new MethodDeclarationNode(i.Name, mc); entry.Key.InClass(mdnNEW); entry.Key.ConstructSwum(mdnNEW); Console.WriteLine("\t" + entry.Key.GetType().ToString() + new String(' ', 30 - entry.Key.GetType().ToString().Length) + " = " + mdnNEW.ToString()); if (mdnNEW.Action.ToPlainString().Equals(methodVerb, StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("\tMethod contains the verb"); } else { Console.WriteLine("\tMethod does not contain the verb" + "\n"); } } } Console.WriteLine(" Inclasses = " + numbtrue); } //end if debug } // End Method Iteration } // End Line Iteration if (success.Count == 0) { Console.WriteLine("===== No Same-Action Methods Found ====="); //Assert.Fail("No Same-Action Methods found"); } else { Console.WriteLine("\n============= SUCCESSES ==============="); foreach (String i in success) { Console.WriteLine(i); } //Assert.Pass("Same-Action methods found, check Output"); } dataProject.WorkingSet.ReleaseReadLock(); } // End Same-Action main method
public void TestClearSecondaryArguments_Null_ExistingArgs() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); VariableDeclarationNode vdn = new VariableDeclarationNode("foo"); mdn.AddSecondaryArgument(vdn, new WordNode("to", PartOfSpeechTag.Preposition)); Assert.AreEqual(1, mdn.SecondaryArguments.Count); mdn.ClearSecondaryArguments(); Assert.AreEqual(0, mdn.SecondaryArguments.Count); }
/// <summary> /// Checks for semantic overlaps between parts of the given method's name and its UnknownArguments. /// If overlaps are found, appropriate EquivalenceNodes are created. /// </summary> /// <param name="mdn">The MethodDeclarationNode to check for overlaps.</param> /// <param name="checkDO">Indicates whether the Direct Object was taken from the method name, and therefore the Theme must be checked for overlap with UnknownArguments.</param> /// <param name="checkIO">Indicates whether the Indirect Object was taken from the method name, and therefore the SecondaryArguments must be checked for overlap with UnknownArguments.</param> private void CheckOverlap(MethodDeclarationNode mdn, bool checkDO, bool checkIO) { if (mdn.ParsedName[0].Text.ToLower() == "set") { return; //special case } PhraseNode theme = null; ArgumentNode arg = null; //get DO word from name string wordDO = ""; if (checkDO) //theme is in the method name { theme = (PhraseNode)mdn.Theme; wordDO = theme.LastWord().Text; } //get IO word from name string wordIO = ""; if (checkIO) //IO is in the method name { arg = mdn.SecondaryArguments[0]; PhraseNode argn = (PhraseNode)arg.Argument; wordIO = argn.LastWord().Text; if (wordDO == wordIO) { return; //no equivalence if multiple overlap } } //find overlap List <Node> unknownArgs = mdn.UnknownArguments; List <Node> DOOverlappingArgs = new List <Node>(); List <Node> IOOverlappingArgs = new List <Node>(); for (int i = 0; i < unknownArgs.Count; i++) { if (unknownArgs[i] is VariableDeclarationNode) { VariableDeclarationNode var = (VariableDeclarationNode)unknownArgs[i]; PhraseNode name = var.ParsedName; PhraseNode type = var.Type.ParsedName; bool DOOverlaps = false; bool IOOverlaps = false; if (checkDO) { DOOverlaps = HasOverlap(name, wordDO) || HasOverlap(type, wordDO); if (DOOverlaps) { DOOverlappingArgs.Add(unknownArgs[i]); } } if (checkIO) { IOOverlaps = HasOverlap(name, wordIO) || HasOverlap(type, wordIO); if (IOOverlaps) { IOOverlappingArgs.Add(unknownArgs[i]); } } if (DOOverlaps && IOOverlaps) { return; //no equivalence if multiple overlap } } } //Create overlap in SWUM if (DOOverlappingArgs.Count > 0) { EquivalenceNode en = mdn.CreateEquivalenceFromUnknownArguments(theme, DOOverlappingArgs); mdn.Theme = en; //reset theme in MethodDeclarationNode to new equivalence node } if (IOOverlappingArgs.Count > 0) { EquivalenceNode en = mdn.CreateEquivalenceFromUnknownArguments(arg.Argument, IOOverlappingArgs); arg.Argument = en; //reset mdn.SecondaryArguments to point to new equivalence node } }
public void TestToPlainString_DefaultContext() { var mdn = new MethodDeclarationNode("MyMethod", new MethodContext()); var splitter = new ConservativeIdSplitter(); mdn.Parse(splitter); mdn.AssignStructuralInformation(splitter, new UnigramTagger()); Assert.AreEqual("My Method", mdn.ToPlainString()); }