const.go 4.9 KB

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