const.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. /*
  11. Package parser contains the EQL parser.
  12. Lexer
  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. Parser
  17. Parse() is a parser which produces a parse tree from a given set of lexer tokens.
  18. Based on an article by Douglas Crockford: Top Down Operator Precedence
  19. http://crockford.com/javascript/tdop/tdop.html
  20. which is based on the ideas of Vaughan Pratt and his paper: Top Down Operator Precedence
  21. http://portal.acm.org/citation.cfm?id=512931
  22. https://tdop.github.io/
  23. ParseWithRuntime() parses a given input and decorates the resulting parse tree
  24. with runtime components which can be used to interpret the parsed query.
  25. */
  26. package parser
  27. /*
  28. LexTokenID represents a unique lexer token ID
  29. */
  30. type LexTokenID int
  31. /*
  32. Available lexer token types
  33. */
  34. const (
  35. TokenError LexTokenID = iota // Lexing error token with a message as val
  36. TokenEOF // End-of-file token
  37. TokenVALUE // Simple value
  38. TokenNODEKIND // Node kind value
  39. TokenGeneral // General token used for plain ASTs
  40. TOKENodeSYMBOLS // Used to separate symbols from other tokens in this list
  41. TokenGEQ
  42. TokenLEQ
  43. TokenNEQ
  44. TokenEQ
  45. TokenGT
  46. TokenLT
  47. TokenLPAREN
  48. TokenRPAREN
  49. TokenLBRACK
  50. TokenRBRACK
  51. TokenCOMMA
  52. TokenAT
  53. TokenPLUS
  54. TokenMINUS
  55. TokenTIMES
  56. TokenDIV
  57. TokenDIVINT
  58. TokenMODINT
  59. // The colon ':' has a context specific meaning and is not processed by the parser
  60. TOKENodeKEYWORDS // Used to separate keywords from other tokens in this list
  61. TokenGET
  62. TokenLOOKUP
  63. TokenFROM
  64. TokenGROUP
  65. TokenWITH
  66. TokenLIST
  67. TokenNULLTRAVERSAL
  68. TokenFILTERING
  69. TokenORDERING
  70. TokenWHERE
  71. TokenTRAVERSE
  72. TokenEND
  73. TokenPRIMARY
  74. TokenSHOW
  75. TokenAS
  76. TokenFORMAT
  77. TokenAND
  78. TokenOR
  79. TokenLIKE
  80. TokenIN
  81. TokenCONTAINS
  82. TokenBEGINSWITH
  83. TokenENDSWITH
  84. TokenCONTAINSNOT
  85. TokenNOT
  86. TokenNOTIN
  87. TokenFALSE
  88. TokenTRUE
  89. TokenUNIQUE
  90. TokenUNIQUECOUNT
  91. TokenNULL
  92. TokenISNULL
  93. TokenISNOTNULL
  94. TokenASCENDING
  95. TokenDESCENDING
  96. )
  97. /*
  98. Available parser AST node types
  99. */
  100. const (
  101. NodeEOF = "EOF"
  102. NodeVALUE = "value"
  103. NodeTRUE = "true"
  104. NodeFALSE = "false"
  105. NodeNULL = "null"
  106. NodeFUNC = "func"
  107. NodeORDERING = "ordering"
  108. NodeFILTERING = "filtering"
  109. NodeNULLTRAVERSAL = "nulltraversal"
  110. // Special tokens - always handled in a denotation function
  111. NodeCOMMA = "comma"
  112. NodeGROUP = "group"
  113. NodeEND = "end"
  114. NodeAS = "as"
  115. NodeFORMAT = "format"
  116. // Keywords
  117. NodeGET = "get"
  118. NodeLOOKUP = "lookup"
  119. NodeFROM = "from"
  120. NodeWHERE = "where"
  121. NodeUNIQUE = "unique"
  122. NodeUNIQUECOUNT = "uniquecount"
  123. NodeISNOTNULL = "isnotnull"
  124. NodeASCENDING = "asc"
  125. NodeDESCENDING = "desc"
  126. NodeTRAVERSE = "traverse"
  127. NodePRIMARY = "primary"
  128. NodeSHOW = "show"
  129. NodeSHOWTERM = "showterm"
  130. NodeWITH = "with"
  131. NodeLIST = "list"
  132. // Boolean operations
  133. NodeOR = "or"
  134. NodeAND = "and"
  135. NodeNOT = "not"
  136. NodeGEQ = ">="
  137. NodeLEQ = "<="
  138. NodeNEQ = "!="
  139. NodeEQ = "="
  140. NodeGT = ">"
  141. NodeLT = "<"
  142. // List operations
  143. NodeIN = "in"
  144. NodeNOTIN = "notin"
  145. // String operations
  146. NodeLIKE = "like"
  147. NodeCONTAINS = "contains"
  148. NodeBEGINSWITH = "beginswith"
  149. NodeENDSWITH = "endswith"
  150. NodeCONTAINSNOT = "containsnot"
  151. // Simple arithmetic expressions
  152. NodePLUS = "plus"
  153. NodeMINUS = "minus"
  154. NodeTIMES = "times"
  155. NodeDIV = "div"
  156. NodeMODINT = "modint"
  157. NodeDIVINT = "divint"
  158. // Brackets
  159. NodeLPAREN = "("
  160. NodeRPAREN = ")"
  161. NodeLBRACK = "["
  162. NodeRBRACK = "]"
  163. )