const.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /*
  11. Package parser contains a ECAL parser.
  12. Lexer for Source Text
  13. Lex() is a lexer function to convert a given search query into a list of tokens.
  14. Based on a talk by Rob Pike: Lexical Scanning in Go
  15. https://www.youtube.com/watch?v=HxaD_trXwRE
  16. The lexer's output is pushed into a channel which is consumed by the parser.
  17. This design enables the concurrent processing of the input text by lexer and
  18. parser.
  19. Parser
  20. Parse() is a parser which produces a parse tree from a given set of lexer tokens.
  21. Based on an article by Douglas Crockford: Top Down Operator Precedence
  22. http://crockford.com/javascript/tdop/tdop.html
  23. which is based on the ideas of Vaughan Pratt and his paper: Top Down Operator Precedence
  24. http://portal.acm.org/citation.cfm?id=512931
  25. https://tdop.github.io/
  26. ParseWithRuntime() parses a given input and decorates the resulting parse tree
  27. with runtime components which can be used to interpret the parsed query.
  28. */
  29. package parser
  30. /*
  31. LexTokenID represents a unique lexer token ID
  32. */
  33. type LexTokenID int
  34. /*
  35. Available meta data types
  36. */
  37. const (
  38. MetaDataPreComment = "MetaDataPreComment"
  39. MetaDataPostComment = "MetaDataPostComment"
  40. MetaDataGeneral = "MetaDataGeneral"
  41. )
  42. /*
  43. Available lexer token types
  44. */
  45. const (
  46. TokenError LexTokenID = iota // Lexing error token with a message as val
  47. TokenEOF // End-of-file token
  48. TokenANY // Unspecified token (used when building an AST from a Go map structure)
  49. TokenPRECOMMENT // Comment /* ... */
  50. TokenPOSTCOMMENT // Comment # ...
  51. // Value tokens
  52. TokenSTRING // String constant
  53. TokenNUMBER // Number constant
  54. TokenIDENTIFIER // Idendifier
  55. // Constructed tokens which are generated by the parser not the lexer
  56. TokenSTATEMENTS // A code block
  57. TokenFUNCCALL // A function call
  58. TokenCOMPACCESS // Access to a composition structure
  59. TokenLIST // List value
  60. TokenMAP // MAP value
  61. TokenPARAMS // Function parameters
  62. TokenGUARD // Conditional statements
  63. TOKENodeSYMBOLS // Used to separate symbols from other tokens in this list
  64. // Condition operators
  65. TokenGEQ
  66. TokenLEQ
  67. TokenNEQ
  68. TokenEQ
  69. TokenGT
  70. TokenLT
  71. // Grouping symbols
  72. TokenLPAREN
  73. TokenRPAREN
  74. TokenLBRACK
  75. TokenRBRACK
  76. TokenLBRACE
  77. TokenRBRACE
  78. // Separators
  79. TokenDOT
  80. TokenCOMMA
  81. TokenSEMICOLON
  82. // Grouping
  83. TokenCOLON
  84. TokenEQUAL
  85. // Arithmetic operators
  86. TokenPLUS
  87. TokenMINUS
  88. TokenTIMES
  89. TokenDIV
  90. TokenDIVINT
  91. TokenMODINT
  92. // Assignment statement
  93. TokenASSIGN
  94. TOKENodeKEYWORDS // Used to separate keywords from other tokens in this list
  95. // Import statement
  96. TokenIMPORT
  97. TokenAS
  98. // Sink definition
  99. TokenSINK
  100. TokenKINDMATCH
  101. TokenSCOPEMATCH
  102. TokenSTATEMATCH
  103. TokenPRIORITY
  104. TokenSUPPRESSES
  105. // Function definition
  106. TokenFUNC
  107. TokenRETURN
  108. // Boolean operators
  109. TokenAND
  110. TokenOR
  111. TokenNOT
  112. // Condition operators
  113. TokenLIKE
  114. TokenIN
  115. TokenHASPREFIX
  116. TokenHASSUFFIX
  117. TokenNOTIN
  118. // Constant terminals
  119. TokenFALSE
  120. TokenTRUE
  121. TokenNULL
  122. // Conditional statements
  123. TokenIF
  124. TokenELIF
  125. TokenELSE
  126. // Loop statements
  127. TokenFOR
  128. TokenBREAK
  129. TokenCONTINUE
  130. TokenENDLIST
  131. )
  132. /*
  133. IsValidTokenID check if a given token ID is valid.
  134. */
  135. func IsValidTokenID(value int) bool {
  136. return value < int(TokenENDLIST)
  137. }
  138. /*
  139. Available parser AST node types
  140. */
  141. const (
  142. NodeEOF = "EOF"
  143. NodeSTRING = "string" // String constant
  144. NodeNUMBER = "number" // Number constant
  145. NodeIDENTIFIER = "identifier" // Idendifier
  146. // Constructed tokens
  147. NodeSTATEMENTS = "statements" // List of statements
  148. NodeFUNCCALL = "funccall" // Function call
  149. NodeCOMPACCESS = "compaccess" // Composition structure access
  150. NodeLIST = "list" // List value
  151. NodeMAP = "map" // Map value
  152. NodePARAMS = "params" // Function parameters
  153. NodeGUARD = "guard" // Guard expressions for conditional statements
  154. // Condition operators
  155. NodeGEQ = ">="
  156. NodeLEQ = "<="
  157. NodeNEQ = "!="
  158. NodeEQ = "=="
  159. NodeGT = ">"
  160. NodeLT = "<"
  161. // Separators
  162. NodeKVP = "kvp" // Key-value pair
  163. NodePRESET = "preset" // Preset value
  164. // Arithmetic operators
  165. NodePLUS = "plus"
  166. NodeMINUS = "minus"
  167. NodeTIMES = "times"
  168. NodeDIV = "div"
  169. NodeMODINT = "modint"
  170. NodeDIVINT = "divint"
  171. // Assignment statement
  172. NodeASSIGN = ":="
  173. // Import statement
  174. NodeIMPORT = "import"
  175. // Sink definition
  176. NodeSINK = "sink"
  177. NodeKINDMATCH = "kindmatch"
  178. NodeSCOPEMATCH = "scopematch"
  179. NodeSTATEMATCH = "statematch"
  180. NodePRIORITY = "priority"
  181. NodeSUPPRESSES = "suppresses"
  182. // Function definition
  183. NodeFUNC = "function"
  184. NodeRETURN = "return"
  185. // Boolean operators
  186. NodeAND = "and"
  187. NodeOR = "or"
  188. NodeNOT = "not"
  189. // Condition operators
  190. NodeLIKE = "like"
  191. NodeIN = "in"
  192. NodeHASPREFIX = "hasprefix"
  193. NodeHASSUFFIX = "hassuffix"
  194. NodeNOTIN = "notin"
  195. // Constant terminals
  196. NodeTRUE = "true"
  197. NodeFALSE = "false"
  198. NodeNULL = "null"
  199. // Conditional statements
  200. NodeIF = "if"
  201. // Loop statements
  202. NodeLOOP = "loop"
  203. NodeBREAK = "break"
  204. NodeCONTINUE = "continue"
  205. )