Class.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. namespace csharp_sccd_compiler
  6. {
  7. public class Class : Visitable
  8. {
  9. public string name { get; private set; }
  10. public bool is_default { get; private set; }
  11. public List<Constructor> constructors { get; private set; }
  12. public List<Destructor> destructors { get; private set; }
  13. public List<Method> methods { get; private set; }
  14. public List<Attribute> attributes { get; private set; }
  15. public List<Association> associations { get; private set; }
  16. public StateChart statechart { get; private set; }
  17. public string super_class { get; private set; }
  18. public Class(XElement xml)
  19. {
  20. this.name = xml.Attribute("name").Value;
  21. XAttribute default_attribute = xml.Attribute("default");
  22. if (default_attribute != null && default_attribute.Value.ToLower() == "true")
  23. {
  24. this.is_default = true;
  25. }
  26. else
  27. {
  28. this.is_default = false;
  29. }
  30. this.attributes = new List<Attribute>();
  31. foreach(XElement attribute_xml in xml.Elements("attribute"))
  32. {
  33. this.attributes.Add(new Attribute(attribute_xml));
  34. }
  35. this.methods = new List<Method>();
  36. this.constructors = new List<Constructor>();
  37. this.destructors = new List<Destructor>();
  38. foreach(XElement method_xml in xml.Elements("method"))
  39. {
  40. this.processMethod(method_xml);
  41. }
  42. if (this.destructors.Count > 1)
  43. throw new CompilerException("Multiple destructors defined.");
  44. if (this.constructors.Count == 0)
  45. this.constructors.Add(new Constructor(this.name));
  46. var associations = new List<XElement>();
  47. var inheritances = new List<XElement>();
  48. foreach (XElement relationships_xml in xml.Elements("relationships"))
  49. {
  50. associations.AddRange(relationships_xml.Elements("association"));
  51. inheritances.AddRange(relationships_xml.Elements("inheritance"));
  52. }
  53. this.associations = new List<Association>();
  54. foreach( XElement association_xml in associations)
  55. this.associations.Add(new Association(association_xml));
  56. if(inheritances.Count > 1)
  57. throw new CompilerException("Multiple inheritance detected which is not supported.");
  58. if (inheritances.Count == 1)
  59. this.super_class = inheritances[0].Attribute("class").Value;
  60. else
  61. this.super_class = null;
  62. XElement[] statecharts = xml.Elements("scxml").ToArray();
  63. if (statecharts.Length > 1)
  64. throw new CompilerException("Multiple statecharts found.");
  65. if (statecharts.Length == 1)
  66. this.statechart = new StateChart(statecharts[0]);
  67. }
  68. private void processMethod(XElement method_xml)
  69. {
  70. XAttribute method_name_attribute = method_xml.Attribute("name");
  71. if (method_name_attribute == null)
  72. throw new CompilerException("Missing method name.");
  73. string method_name = method_name_attribute.Value;
  74. if (method_name == "")
  75. throw new CompilerException("Empty method name.");
  76. if (Constants.Reserved.Contains(method_name))
  77. throw new CompilerException(string.Format("Reserved word '{0}' used as method name.", method_name));
  78. if (method_name == this.name)
  79. this.constructors.Add(new Constructor(method_xml));
  80. else if (method_name == string.Concat("~",this.name))
  81. this.destructors.Add(new Destructor(method_xml));
  82. else
  83. this.methods.Add(new Method(method_xml));
  84. }
  85. public override void accept(Visitor visitor)
  86. {
  87. visitor.visit (this);
  88. }
  89. }
  90. }