provider.go 5.0 KB

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