const.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 lexer token types
  35. */
  36. const (
  37. TokenError LexTokenID = iota // Lexing error token with a message as val
  38. TokenEOF // End-of-file token
  39. TokenANY // Unspecified token (used when building an AST from a Go map structure)
  40. TokenPRECOMMENT // Comment /* ... */
  41. TokenPOSTCOMMENT // Comment # ...
  42. TokenSTRING // String constant
  43. TokenNUMBER // Number constant
  44. TokenIDENTIFIER // Idendifier
  45. // Constructed tokens which are generated by the parser not the lexer
  46. TokenSTATEMENTS // A code block
  47. TokenLIST // List value
  48. TokenMAP // MAP value
  49. TokenGUARD // Guard expressions for conditional statements
  50. TOKENodeSYMBOLS // Used to separate symbols from other tokens in this list
  51. // Condition operators
  52. TokenGEQ
  53. TokenLEQ
  54. TokenNEQ
  55. TokenEQ
  56. TokenGT
  57. TokenLT
  58. // Grouping symbols
  59. TokenLPAREN
  60. TokenRPAREN
  61. TokenLBRACK
  62. TokenRBRACK
  63. TokenLBRACE
  64. TokenRBRACE
  65. // Separators
  66. TokenDOT
  67. TokenCOMMA
  68. TokenCOLON
  69. TokenSEMICOLON
  70. // Arithmetic operators
  71. TokenPLUS
  72. TokenMINUS
  73. TokenTIMES
  74. TokenDIV
  75. TokenDIVINT
  76. TokenMODINT
  77. // Assignment statement
  78. TokenASSIGN
  79. // Data structure access
  80. TokenACCESS
  81. // The colon '' has a context specific meaning and is checked by the parser
  82. TOKENodeKEYWORDS // Used to separate keywords from other tokens in this list
  83. // Sink definition
  84. TokenSINK
  85. TokenKINDMATCH
  86. TokenSCOPEMATCH
  87. TokenSTATEMATCH
  88. TokenPRIORITY
  89. TokenSUPPRESSES
  90. // Function definition
  91. TokenFUNC
  92. // Boolean operators
  93. TokenAND
  94. TokenOR
  95. TokenNOT
  96. // Condition operators
  97. TokenLIKE
  98. TokenIN
  99. TokenHASPREFIX
  100. TokenHASSUFFIX
  101. TokenNOTIN
  102. // Constant terminals
  103. TokenFALSE
  104. TokenTRUE
  105. TokenNULL
  106. // Conditional statements
  107. TokenIF
  108. TokenELIF
  109. TokenELSE
  110. // Loop statements
  111. TokenFOR
  112. TokenBREAK
  113. TokenCONTINUE
  114. TokenENDLIST
  115. )
  116. /*
  117. IsValidTokenID check if a given token ID is valid.
  118. */
  119. func IsValidTokenID(value int) bool {
  120. return value < int(TokenENDLIST)
  121. }
  122. /*
  123. Available parser AST node types
  124. */
  125. const (
  126. NodeEOF = "EOF"
  127. NodeSTRING = "string" // String constant
  128. NodeNUMBER = "number" // Number constant
  129. NodeIDENTIFIER = "identifier" // Idendifier
  130. // Constructed tokens
  131. NodeSTATEMENTS = "statements" // List of statements
  132. // Assignment statement
  133. NodeASSIGN = ":="
  134. // Arithmetic operators
  135. NodePLUS = "plus"
  136. NodeMINUS = "minus"
  137. NodeTIMES = "times"
  138. NodeDIV = "div"
  139. NodeMODINT = "modint"
  140. NodeDIVINT = "divint"
  141. // Boolean operators
  142. NodeOR = "or"
  143. NodeAND = "and"
  144. NodeNOT = "not"
  145. // Condition operators
  146. NodeLIKE = "like"
  147. NodeIN = "in"
  148. NodeHASPREFIX = "hasprefix"
  149. NodeHASSUFFIX = "hassuffix"
  150. NodeNOTIN = "notin"
  151. NodeGEQ = ">="
  152. NodeLEQ = "<="
  153. NodeNEQ = "!="
  154. NodeEQ = "=="
  155. NodeGT = ">"
  156. NodeLT = "<"
  157. // Constants
  158. NodeTRUE = "true"
  159. NodeFALSE = "false"
  160. NodeNULL = "null"
  161. /*
  162. NodeLIST = "list" // List value
  163. NodeMAP = "map" // Map value
  164. NodeGUARD = "guard" // Guard expressions for conditional statements
  165. // Map entries
  166. NodeMAPENTRY = "entry" // Map entry value
  167. // Function call statement
  168. NodeFUNCCALL = "funccall"
  169. // Data structure access
  170. NodeACCESS = "access"
  171. // Sink definition
  172. NodeSINK = "sink"
  173. NodeKINDMATCH = "kindmatch"
  174. NodeSCOPEMATCH = "scopematch"
  175. NodeSTATEMATCH = "statematch"
  176. NodePRIORITY = "priority"
  177. NodeSUPPRESSES = "suppresses"
  178. // Block statements
  179. NodeCOND = "cond"
  180. NodeLOOP = "loop"
  181. // Single statements
  182. NodeBREAK = "break"
  183. NodeCONTINUE = "continue"
  184. */
  185. )