provider.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "os"
  13. "path/filepath"
  14. "devt.de/krotik/common/timeutil"
  15. "devt.de/krotik/ecal/config"
  16. "devt.de/krotik/ecal/engine"
  17. "devt.de/krotik/ecal/parser"
  18. "devt.de/krotik/ecal/util"
  19. )
  20. /*
  21. ecalRuntimeNew is used to instantiate ECAL runtime components.
  22. */
  23. type ecalRuntimeNew func(*ECALRuntimeProvider, *parser.ASTNode) parser.Runtime
  24. /*
  25. providerMap contains the mapping of AST nodes to runtime components for ECAL ASTs.
  26. */
  27. var providerMap = map[string]ecalRuntimeNew{
  28. parser.NodeEOF: invalidRuntimeInst,
  29. parser.NodeSTRING: stringValueRuntimeInst, // String constant
  30. parser.NodeNUMBER: numberValueRuntimeInst, // Number constant
  31. parser.NodeIDENTIFIER: identifierRuntimeInst, // Idendifier
  32. // Constructed tokens
  33. parser.NodeSTATEMENTS: statementsRuntimeInst, // List of statements
  34. parser.NodeFUNCCALL: voidRuntimeInst, // Function call
  35. parser.NodeCOMPACCESS: voidRuntimeInst, // Composition structure access
  36. parser.NodeLIST: listValueRuntimeInst, // List value
  37. parser.NodeMAP: mapValueRuntimeInst, // Map value
  38. parser.NodePARAMS: voidRuntimeInst, // Function parameters
  39. parser.NodeGUARD: guardRuntimeInst, // Guard expressions for conditional statements
  40. // Condition operators
  41. parser.NodeGEQ: greaterequalOpRuntimeInst,
  42. parser.NodeLEQ: lessequalOpRuntimeInst,
  43. parser.NodeNEQ: notequalOpRuntimeInst,
  44. parser.NodeEQ: equalOpRuntimeInst,
  45. parser.NodeGT: greaterOpRuntimeInst,
  46. parser.NodeLT: lessOpRuntimeInst,
  47. // Separators
  48. parser.NodeKVP: voidRuntimeInst, // Key-value pair
  49. parser.NodePRESET: voidRuntimeInst, // Preset value
  50. // Arithmetic operators
  51. parser.NodePLUS: plusOpRuntimeInst,
  52. parser.NodeMINUS: minusOpRuntimeInst,
  53. parser.NodeTIMES: timesOpRuntimeInst,
  54. parser.NodeDIV: divOpRuntimeInst,
  55. parser.NodeMODINT: modintOpRuntimeInst,
  56. parser.NodeDIVINT: divintOpRuntimeInst,
  57. // Assignment statement
  58. parser.NodeASSIGN: assignmentRuntimeInst,
  59. // Import statement
  60. parser.NodeIMPORT: importRuntimeInst,
  61. parser.NodeAS: voidRuntimeInst,
  62. // Sink definition
  63. parser.NodeSINK: sinkRuntimeInst,
  64. parser.NodeKINDMATCH: kindMatchRuntimeInst,
  65. parser.NodeSCOPEMATCH: scopeMatchRuntimeInst,
  66. parser.NodeSTATEMATCH: stateMatchRuntimeInst,
  67. parser.NodePRIORITY: priorityRuntimeInst,
  68. parser.NodeSUPPRESSES: suppressesRuntimeInst,
  69. // Function definition
  70. parser.NodeFUNC: funcRuntimeInst,
  71. parser.NodeRETURN: returnRuntimeInst,
  72. // Boolean operators
  73. parser.NodeOR: orOpRuntimeInst,
  74. parser.NodeAND: andOpRuntimeInst,
  75. parser.NodeNOT: notOpRuntimeInst,
  76. // Condition operators
  77. parser.NodeLIKE: likeOpRuntimeInst,
  78. parser.NodeIN: inOpRuntimeInst,
  79. parser.NodeHASPREFIX: beginswithOpRuntimeInst,
  80. parser.NodeHASSUFFIX: endswithOpRuntimeInst,
  81. parser.NodeNOTIN: notinOpRuntimeInst,
  82. // Constant terminals
  83. parser.NodeFALSE: falseRuntimeInst,
  84. parser.NodeTRUE: trueRuntimeInst,
  85. parser.NodeNULL: nullRuntimeInst,
  86. // Conditional statements
  87. parser.NodeIF: ifRuntimeInst,
  88. // Loop statements
  89. parser.NodeLOOP: loopRuntimeInst,
  90. parser.NodeBREAK: breakRuntimeInst,
  91. parser.NodeCONTINUE: continueRuntimeInst,
  92. // Try statement
  93. parser.NodeTRY: tryRuntimeInst,
  94. parser.NodeEXCEPT: voidRuntimeInst,
  95. parser.NodeFINALLY: voidRuntimeInst,
  96. }
  97. /*
  98. ECALRuntimeProvider is the factory object producing runtime objects for ECAL ASTs.
  99. */
  100. type ECALRuntimeProvider struct {
  101. Name string // Name to identify the input
  102. ImportLocator util.ECALImportLocator // Locator object for imports
  103. Logger util.Logger // Logger object for log messages
  104. Processor engine.Processor // Processor of the ECA engine
  105. Cron *timeutil.Cron // Cron object for scheduled execution
  106. Debugger Debugger // Optional: Debugger object
  107. }
  108. /*
  109. NewECALRuntimeProvider returns a new instance of a ECAL runtime provider.
  110. */
  111. func NewECALRuntimeProvider(name string, importLocator util.ECALImportLocator, logger util.Logger) *ECALRuntimeProvider {
  112. if importLocator == nil {
  113. // By default imports are located in the current directory
  114. importLocator = &util.FileImportLocator{Root: filepath.Dir(os.Args[0])}
  115. }
  116. if logger == nil {
  117. // By default we just have a memory logger
  118. logger = util.NewMemoryLogger(100)
  119. }
  120. proc := engine.NewProcessor(config.Int(config.WorkerCount))
  121. // By default ECAL should stop the triggering sequence of sinks after the
  122. // first sink that returns a sinkerror.
  123. proc.SetFailOnFirstErrorInTriggerSequence(true)
  124. cron := timeutil.NewCron()
  125. cron.Start()
  126. return &ECALRuntimeProvider{name, importLocator, logger, proc, cron, nil}
  127. }
  128. /*
  129. Runtime returns a runtime component for a given ASTNode.
  130. */
  131. func (erp *ECALRuntimeProvider) Runtime(node *parser.ASTNode) parser.Runtime {
  132. if instFunc, ok := providerMap[node.Name]; ok {
  133. return instFunc(erp, node)
  134. }
  135. return invalidRuntimeInst(erp, node)
  136. }
  137. /*
  138. NewRuntimeError creates a new RuntimeError object.
  139. */
  140. func (erp *ECALRuntimeProvider) NewRuntimeError(t error, d string, node *parser.ASTNode) error {
  141. return util.NewRuntimeError(erp.Name, t, d, node)
  142. }