const.go 5.0 KB

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