/// <summary> /// Look for attributes in the previous lines /// </summary> /// <param name="file">All lines of the fie</param> /// <param name="index">Current index in file</param> /// <param name="model">Model to add attributes to</param> private void ParseAttributes(string[] file, int index, CodeElementModel model) { // Keep going back in the file to look for attributes for (var line = index - 1; line >= 0; line--) { var match = _attributeRegex.Match(file[line]); if (!match.Success) { break; } // Create attribute var attribute = new AttributeModel(match.Groups["attributeName"].Value); model.Attributes.Add(attribute); // Set parameters var parameterGroup = match.Groups["parameter"]; for (int i = 0; i < parameterGroup.Captures.Count; i++) { attribute.Parameters.Add(parameterGroup.Captures[i].Value); } // Set properties var propertygroup = match.Groups["property"]; var valueGroup = match.Groups["value"]; for (int i = 0; i < propertygroup.Captures.Count; i++) { var property = new PropertyModel(propertygroup.Captures[i].Value) { Value = valueGroup.Captures[i].Value }; attribute.Properties.Add(property); } } }
/// <seealso cref="IParser"/> public CodeElementModel ParseFile(string filePath) { // Read the file var file = File.ReadAllLines(filePath); CodeElementModel model = null; var @namespace = string.Empty; for (var i = 0; i < file.Length; i++) { var line = file[i]; // First find the namespace if (string.IsNullOrEmpty(@namespace)) { var match = _namespaceRegex.Match(line); if (match.Success) { @namespace = match.Groups["namespace"].Value; } } // First we must find the class definition if (model == null) { model = ParseClass(file, i, @namespace) ?? ParseEnum(file, i, @namespace); continue; } // Try to parse properties or members if (model is ClassModel) { ParseProperty(file, i, (ClassModel)model); } else if (model is EnumModel) { ParseMember(file, i, (EnumModel)model); } } return(model); }
/// <summary> /// Checks if an element has an attribute with given short name /// </summary> /// <param name="element">Code element to check</param> /// <param name="shortName">Name of the attribute</param> /// <returns>True if attribute is set</returns> private static bool HasAttributeShort(this CodeElementModel element, string shortName) { return(element.Attributes.Any(att => att.Name.Replace("Attribute", string.Empty) == shortName)); }
//--------------------------- // Generate length variable //--------------------------- private static string FieldLength(CodeElementModel property, int dimension) { var dimensions = new[] { "Length", "Width", "Height" }; return(property.Name.ToLower() + dimensions[dimension]); }
/// <summary> /// Check if an attribute is defined on an element /// </summary> /// <param name="element">Code element to check</param> /// <param name="name">Name of the attribute</param> /// <returns>True if attribute is set</returns> public static bool HasAttribute(this CodeElementModel element, string name) { var shortName = name.Replace("Attribute", string.Empty); return(element.HasAttributeShort(shortName)); }