RaiseEvent.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Linq;
  4. namespace csharp_sccd_compiler
  5. {
  6. public class RaiseEvent : SubAction
  7. {
  8. public enum Scope {
  9. UNKNOWN_SCOPE,
  10. LOCAL_SCOPE,
  11. BROAD_SCOPE,
  12. OUTPUT_SCOPE,
  13. NARROW_SCOPE,
  14. CD_SCOPE
  15. };
  16. public string event_name { get; private set; }
  17. public string target { get; private set; }
  18. public string port { get; private set; }
  19. public Scope scope { get; private set; }
  20. public List<Expression> parameters { get; private set; }
  21. public RaiseEvent(XElement xml)
  22. {
  23. XAttribute event_attribute = xml.Attribute("event");
  24. if (event_attribute == null)
  25. throw new ActionException("Missing \"event\" attribute for <raise> action.");
  26. this.event_name = event_attribute.Value.Trim();
  27. if (this.event_name == "")
  28. throw new ActionException("Empty \"event\" attribute for <raise> action.");
  29. XAttribute target_attribute = xml.Attribute("target");
  30. if (target_attribute == null || target_attribute.Value.Trim() == "")
  31. this.target = null;
  32. else
  33. this.target = target_attribute.Value.Trim();
  34. XAttribute port_attribute = xml.Attribute("port");
  35. if (port_attribute == null || port_attribute.Value.Trim() == "")
  36. this.port = null;
  37. else
  38. this.port = port_attribute.Value.Trim();
  39. string scope_string;
  40. XAttribute scope_attribute = xml.Attribute("scope");
  41. if (scope_attribute == null || scope_attribute.Value.Trim() == "")
  42. scope_string = null;
  43. else
  44. scope_string = scope_attribute.Value.Trim();
  45. if (scope_string == null)
  46. {
  47. //Calculate scope depending on present attributes
  48. if (this.target != null && this.port != null)
  49. throw new ActionException("Both target and port attribute detected for <raise> action without a scope defined.");
  50. else if (this.port != null)
  51. this.scope = Scope.OUTPUT_SCOPE;
  52. else if (this.target != null)
  53. this.scope = Scope.NARROW_SCOPE;
  54. else
  55. this.scope = Scope.LOCAL_SCOPE;
  56. }
  57. else if (scope_string == "local")
  58. this.scope = Scope.LOCAL_SCOPE;
  59. else if (scope_string == "broad")
  60. this.scope = Scope.BROAD_SCOPE;
  61. else if (scope_string == "output")
  62. this.scope = Scope.OUTPUT_SCOPE;
  63. else if (scope_string == "narrow")
  64. this.scope = Scope.NARROW_SCOPE;
  65. else if (scope_string == "cd")
  66. this.scope = Scope.CD_SCOPE;
  67. else
  68. throw new ActionException(string.Format("Illegal scope attribute \"{0}\"; needs to be one of the following : local, broad, narrow, output, cd or nothing.", scope_string));
  69. if (this.scope == Scope.LOCAL_SCOPE || this.scope == Scope.BROAD_SCOPE || this.scope == Scope.CD_SCOPE)
  70. {
  71. if (this.target != null)
  72. throw new ActionException("<raise> target detected, not matching with scope.");
  73. if (this.port != null)
  74. throw new ActionException("<raise> port detected, not matching with scope. Ignored.");
  75. }
  76. if (this.scope == Scope.NARROW_SCOPE && this.port != null)
  77. throw new ActionException("<raise> port detected, not matching with scope");
  78. if (this.scope == Scope.OUTPUT_SCOPE && this.target != null)
  79. throw new ActionException("Raise event target detected, not matching with scope. Ignored.");
  80. this.parameters = new List<Expression>();
  81. foreach (XElement parameter_xml in xml.Elements("parameter"))
  82. {
  83. XAttribute expr_attribute = parameter_xml.Attribute("expr");
  84. if (expr_attribute == null || expr_attribute.Value.Trim() == "")
  85. throw new ActionException("<parameter> in <raise> detected without \"value\" attribute.");
  86. this.parameters.Add( new Expression(expr_attribute.Value.Trim()));
  87. }
  88. }
  89. public override void accept(Visitor visitor)
  90. {
  91. visitor.visit (this);
  92. }
  93. }
  94. }