const.go 5.2 KB

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