public static void ParseEnums(List <ProgramElement> programElements, XElement elements, string fileName, int snippetSize) { IEnumerable <XElement> enums = from el in elements.Descendants(SRC.Enum) select el; foreach (XElement enm in enums) { AccessLevel accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(enm); string name; int definitionLineNumber; int definitionColumnNumber; SrcMLParsingUtils.ParseNameAndLineNumber(enm, out name, out definitionLineNumber, out definitionColumnNumber); //parse namespace IEnumerable <XElement> ownerNamespaces = from el in enm.Ancestors(SRC.Namespace) select el; string namespaceName = String.Empty; foreach (XElement ownerNamespace in ownerNamespaces) { foreach (XElement spc in ownerNamespace.Elements(SRC.Name)) { namespaceName += spc.Value + " "; } } namespaceName = namespaceName.TrimEnd(); //parse values XElement block = enm.Element(SRC.Block); string values = String.Empty; if (block != null) { IEnumerable <XElement> exprs = from el in block.Descendants(SRC.Expression) select el; foreach (XElement expr in exprs) { IEnumerable <XElement> enames = expr.Elements(SRC.Name); foreach (XElement ename in enames) { values += ename.Value + " "; } } values = values.TrimEnd(); } string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(enm); programElements.Add(new EnumElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, namespaceName, values)); } }
private ClassElement ParseClass(XElement cls, string fileName) { string name; int definitionLineNumber; int definitionColumnNumber; SrcMLParsingUtils.ParseNameAndLineNumber(cls, out name, out definitionLineNumber, out definitionColumnNumber); AccessLevel accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(cls); //parse namespace IEnumerable <XElement> ownerNamespaces = from el in cls.Ancestors(SRC.Namespace) select el; string namespaceName = String.Empty; foreach (XElement ownerNamespace in ownerNamespaces) { foreach (XElement spc in ownerNamespace.Elements(SRC.Name)) { namespaceName += spc.Value + " "; } } namespaceName = namespaceName.TrimEnd(); //parse extended classes and implemented interfaces (interfaces are treated as extended classes in SrcML for now) string extendedClasses = String.Empty; XElement super = cls.Element(SRC.Super); if (super != null) { IEnumerable <XElement> impNames = from el in super.Descendants(SRC.Name) select el; foreach (XElement impName in impNames) { extendedClasses += impName.Value + " "; } extendedClasses = extendedClasses.TrimEnd(); } //interfaces are treated as extended classes in SrcML for now string implementedInterfaces = String.Empty; string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(cls); string body = cls.Value; return(new ClassElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, namespaceName, extendedClasses, implementedInterfaces, String.Empty, body)); }
// End of code changes private void ParseProperties(List <ProgramElement> programElements, XElement elements, string fileName) { IEnumerable <XElement> props = from el in elements.Descendants(SRC.Declaration) where el.Element(SRC.Name) != null && el.Element(SRC.Type) != null && el.Element(SRC.Block) != null && el.Elements().Count() == 3 select el; foreach (XElement prop in props) { string name; int definitionLineNumber; int definitionColumnNumber; SrcMLParsingUtils.ParseNameAndLineNumber(prop, out name, out definitionLineNumber, out definitionColumnNumber); ClassElement classElement = SrcMLParsingUtils.RetrieveClassElement(prop, programElements); Guid classId = classElement != null ? classElement.Id : Guid.Empty; string className = classElement != null ? classElement.Name : String.Empty; //parse access level and type XElement accessElement = prop.Element(SRC.Type); AccessLevel accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(accessElement); IEnumerable <XElement> types = prop.Element(SRC.Type).Elements(SRC.Name); //oops, namespaces have the same structure in srcml so need this check if (types.Count() == 0 || types.First().Value == "namespace") { continue; } string propertyType = String.Empty; foreach (XElement type in types) { propertyType += type.Value + " "; } propertyType = propertyType.TrimEnd(); string body = SrcMLParsingUtils.ParseBody(prop); string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(prop); programElements.Add(new PropertyElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, propertyType, body, classId, className, String.Empty)); } }
private MethodPrototypeElement ParseCppFunctionPrototype(XElement function, string fileName, bool isConstructor) { try { string name = String.Empty; int definitionLineNumber = 0; int definitionColumnNumber = 0; string returnType = String.Empty; SrcMLParsingUtils.ParseNameAndLineNumber(function, out name, out definitionLineNumber, out definitionColumnNumber); if (name.Contains("::")) { name = name.Substring(name.LastIndexOf("::") + 2); } AccessLevel accessLevel = RetrieveCppAccessLevel(function); XElement type = function.Element(SRC.Type); if (type != null) { XElement typeName = type.Element(SRC.Name); returnType = typeName.Value; } XElement paramlist = function.Element(SRC.ParameterList); IEnumerable <XElement> argumentElements = from el in paramlist.Descendants(SRC.Name) select el; string arguments = String.Empty; foreach (XElement elem in argumentElements) { arguments += elem.Value + " "; } arguments = arguments.TrimEnd(); string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(function); return(new MethodPrototypeElement(name, definitionLineNumber, definitionColumnNumber, returnType, accessLevel, arguments, fullFilePath, source, isConstructor)); } catch (Exception error) { FileLogger.DefaultLogger.Info("Exception in SrcMLCppParser " + error.Message + "\n" + error.StackTrace); return(null); } }
public List <ProgramElement> Parse(string filename) { if (File.Exists(filename) && GetSizeInMb(filename) > 15) { return(new List <ProgramElement>()); } var list = new List <ProgramElement>(); try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (var sr = new StreamReader(filename)) { String line; int linenum = 0; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { linenum++; if (String.IsNullOrWhiteSpace(line)) { continue; } //var name = Regex.Replace(line, @"(\w+)\W+", "$1 "); var name = line.TrimStart(' ', '\n', '\r', '\t'); name = name.TrimEnd(' '); var snippet = SrcMLParsingUtils.RetrieveSource(name); var element = new TextLineElement(name, linenum, -1000, filename, snippet, line); list.Add(element); } } } catch (Exception e) { LogEvents.ParserGenericFileError(this, filename); } return(list); }
private StructElement ParseStruct(XElement strct, string fileName) { string name; int definitionLineNumber; int definitionColumnNumber; SrcMLParsingUtils.ParseNameAndLineNumber(strct, out name, out definitionLineNumber, out definitionColumnNumber); AccessLevel accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(strct); var anc = strct.Ancestors(); var x = anc; //parse namespace IEnumerable <XElement> ownerNamespaces = from el in strct.Ancestors(SRC.Namespace) select el; string namespaceName = String.Empty; foreach (XElement ownerNamespace in ownerNamespaces) { foreach (XElement spc in ownerNamespace.Elements(SRC.Name)) { namespaceName += spc.Value + " "; } } namespaceName = namespaceName.TrimEnd(); //TODO: extended structs are pretty difficult to parse in SrcML string extendedStructs = String.Empty; string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(strct); string body = strct.Value; return(new StructElement(name, definitionLineNumber, definitionColumnNumber, fileName, source, accessLevel, namespaceName, body, extendedStructs, String.Empty)); }
public List <ProgramElement> Parse(string filename) { var programElements = new List <ProgramElement>(); XmlTextReader reader = new XmlTextReader(filename); while (reader.Read()) { string text = String.Empty; if (reader.NodeType == XmlNodeType.Text) { text = reader.Value; } else if (reader.NodeType == XmlNodeType.Element) { while (reader.MoveToNextAttribute()) { text += reader.Value + " "; } } if (!String.IsNullOrWhiteSpace(text)) { var cleanedText = text.TrimStart(' ', '\n', '\r', '\t'); cleanedText = cleanedText.TrimEnd(' ', '\n', '\r', '\t'); var linenum = reader.LineNumber; var snippet = SrcMLParsingUtils.RetrieveSource(cleanedText); var pe = new TextLineElement(cleanedText, linenum, -1000, filename, snippet, cleanedText); programElements.Add(pe); } } return(programElements); }
public virtual MethodElement ParseMethod(XElement method, List <ProgramElement> programElements, string fileName, Type myType, bool isConstructor = false) { string name = String.Empty; int definitionLineNumber = 0; int definitionColumnNumber = 0; string returnType = String.Empty; SrcMLParsingUtils.ParseNameAndLineNumber(method, out name, out definitionLineNumber, out definitionColumnNumber); AccessLevel accessLevel; XElement type = method.Element(SRC.Type); if (type != null) { accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(type); XElement typeName = type.Element(SRC.Name); if (typeName != null) { returnType = typeName.Value; } else { returnType = "void"; } } else { accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(method); } if (String.IsNullOrEmpty(returnType)) { if (name.Equals("get")) { try { var myName = method.Ancestors(SRC.DeclarationStatement).Descendants(SRC.Declaration). Descendants(SRC.Type).Elements(SRC.Name); returnType = myName.First().Value; } catch (NullReferenceException nre) { returnType = ""; } } else if (name.Equals("set")) { returnType = "void"; } else if (name.Equals("add") || name.Equals("remove")) { try { var myName = method.Parent.Parent.Elements(SRC.Type).First().Elements(SRC.Name).First(); returnType = myName.Value; } catch (NullReferenceException nre) { returnType = ""; } } } //parse arguments XElement paramlist = method.Element(SRC.ParameterList); string arguments = String.Empty; if (paramlist != null) { IEnumerable <XElement> argumentElements = from el in paramlist.Descendants(SRC.Name) select el; foreach (XElement elem in argumentElements) { arguments += elem.Value + " "; } } arguments = arguments.TrimEnd(); string body = SrcMLParsingUtils.ParseBody(method); ClassElement classElement = SrcMLParsingUtils.RetrieveClassElement(method, programElements); Guid classId = classElement != null ? classElement.Id : Guid.Empty; string className = classElement != null ? classElement.Name : String.Empty; string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(method); return(Activator.CreateInstance(myType, name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, arguments, returnType, body, classId, className, String.Empty, isConstructor) as MethodElement); }
private static EnumElement ParseEnum(string fileName, XElement enm) { try { //SrcML doesn't seem to parse access level specifiers for enums, so just pretend they are all public for now AccessLevel accessLevel = AccessLevel.Public; string name = ""; int definitionLineNumber = 0; int definitionColumnNumber = 0; if (enm.Element(SRC.Name) != null) { SrcMLParsingUtils.ParseNameAndLineNumber(enm, out name, out definitionLineNumber, out definitionColumnNumber); } else { //enums in C++ aren't required to have a name name = ProgramElement.UndefinedName; definitionLineNumber = Int32.Parse(enm.Attribute(POS.Line).Value); } //parse namespace IEnumerable <XElement> ownerNamespaces = from el in enm.Ancestors(SRC.Declaration) where el.Element(SRC.Type) != null && el.Element(SRC.Type).Element(SRC.Name) != null && el.Element(SRC.Type).Element(SRC.Name).Value == "namespace" select el; string namespaceName = String.Empty; foreach (XElement ownerNamespace in ownerNamespaces) { foreach (XElement spc in ownerNamespace.Elements(SRC.Name)) { namespaceName += spc.Value + " "; } } namespaceName = namespaceName.TrimEnd(); //parse values XElement block = enm.Element(SRC.Block); string values = String.Empty; if (block != null) { IEnumerable <XElement> exprs = from el in block.Descendants(SRC.Expression) select el; foreach (XElement expr in exprs) { IEnumerable <XElement> enames = expr.Elements(SRC.Name); foreach (XElement ename in enames) { values += ename.Value + " "; } } values = values.TrimEnd(); } string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(enm); var enumParsed = new EnumElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, namespaceName, values); return(enumParsed); } catch (Exception error) { FileLogger.DefaultLogger.Info("Exception in SrcMLCppParser " + error.Message + "\n" + error.StackTrace); return(null); } }
public virtual MethodElement ParseCppFunction(XElement function, List <ProgramElement> programElements, string fileName, string[] includedFiles, Type resolvedType, Type unresolvedType, bool isConstructor = false) { try { MethodElement methodElement = null; string source = String.Empty; int definitionLineNumber = 0; int definitionColumnNumber = 0; string returnType = String.Empty; XElement type = function.Element(SRC.Type); if (type != null) { XElement typeName = type.Element(SRC.Name); returnType = typeName.Value; } XElement paramlist = function.Element(SRC.ParameterList); IEnumerable <XElement> argumentElements = from el in paramlist.Descendants(SRC.Name) select el; string arguments = String.Empty; foreach (XElement elem in argumentElements) { arguments += elem.Value + " "; } arguments = arguments.TrimEnd(); string body = SrcMLParsingUtils.ParseBody(function); string fullFilePath = System.IO.Path.GetFullPath(fileName); XElement nameElement = function.Element(SRC.Name); string wholeName = nameElement.Value; if (wholeName.Contains("::")) { //class function string[] twonames = wholeName.Split("::".ToCharArray()); string funcName = twonames[2]; string className = twonames[0]; definitionLineNumber = Int32.Parse(nameElement.Element(SRC.Name).Attribute(POS.Line).Value); definitionColumnNumber = Int32.Parse(nameElement.Element(SRC.Name).Attribute(POS.Column).Value); source = SrcMLParsingUtils.RetrieveSource(function); return(Activator.CreateInstance(unresolvedType, funcName, definitionLineNumber, definitionColumnNumber, fullFilePath, source, arguments, returnType, body, className, isConstructor, includedFiles) as MethodElement); } else { //regular C-type function, or an inlined class function string funcName = wholeName; definitionLineNumber = Int32.Parse(nameElement.Attribute(POS.Line).Value); definitionColumnNumber = Int32.Parse(nameElement.Attribute(POS.Column).Value); source = SrcMLParsingUtils.RetrieveSource(function); AccessLevel accessLevel = RetrieveCppAccessLevel(function); Guid classId = Guid.Empty; string className = String.Empty; ClassElement classElement = SrcMLParsingUtils.RetrieveClassElement(function, programElements); StructElement structElement = RetrieveStructElement(function, programElements); if (classElement != null) { classId = classElement.Id; className = classElement.Name; } else if (structElement != null) { classId = structElement.Id; className = structElement.Name; } methodElement = Activator.CreateInstance(resolvedType, funcName, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, arguments, returnType, body, classId, className, String.Empty, isConstructor) as MethodElement; } return(methodElement); } catch (Exception error) { FileLogger.DefaultLogger.Info("Exception in SrcMLCppParser " + error.Message + "\n" + error.StackTrace); return(null); } }
private ProgramElement ParseClassOrStruct(XElement cls, string fileName, bool parseStruct) { string name; int definitionLineNumber; int definitionColumnNumber; SrcMLParsingUtils.ParseNameAndLineNumber(cls, out name, out definitionLineNumber, out definitionColumnNumber); AccessLevel accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(cls, AccessLevel.Public); //parse namespace IEnumerable <XElement> ownerNamespaces = from el in cls.Ancestors(SRC.Declaration) where el.Element(SRC.Type).Element(SRC.Name).Value == "namespace" select el; string namespaceName = String.Empty; foreach (XElement ownerNamespace in ownerNamespaces) { foreach (XElement spc in ownerNamespace.Elements(SRC.Name)) { namespaceName += spc.Value + " "; } } namespaceName = namespaceName.TrimEnd(); //parse extended classes string extendedClasses = String.Empty; XElement super = cls.Element(SRC.Super); if (super != null) { XElement implements = super.Element(SRC.Implements); if (implements != null) { IEnumerable <XElement> impNames = from el in implements.Descendants(SRC.Name) select el; foreach (XElement impName in impNames) { extendedClasses += impName.Value + " "; } extendedClasses = extendedClasses.TrimEnd(); } } string fullFilePath = System.IO.Path.GetFullPath(fileName); string source = SrcMLParsingUtils.RetrieveSource(cls); string body = cls.Value; if (parseStruct) { return(new StructElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, namespaceName, body, extendedClasses, String.Empty)); } else { string implementedInterfaces = String.Empty; return(new ClassElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, namespaceName, extendedClasses, implementedInterfaces, String.Empty, body)); } }