const.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. TokenCOLON
  79. TokenSEMICOLON
  80. // Arithmetic operators
  81. TokenPLUS
  82. TokenMINUS
  83. TokenTIMES
  84. TokenDIV
  85. TokenDIVINT
  86. TokenMODINT
  87. // Assignment statement
  88. TokenASSIGN
  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. // 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"
  144. NodeCOMPACCESS = "compaccess"
  145. // Condition operators
  146. NodeGEQ = ">="
  147. NodeLEQ = "<="
  148. NodeNEQ = "!="
  149. NodeEQ = "=="
  150. NodeGT = ">"
  151. NodeLT = "<"
  152. // Arithmetic operators
  153. NodePLUS = "plus"
  154. NodeMINUS = "minus"
  155. NodeTIMES = "times"
  156. NodeDIV = "div"
  157. NodeMODINT = "modint"
  158. NodeDIVINT = "divint"
  159. // Assignment statement
  160. NodeASSIGN = ":="
  161. // Import statement
  162. NodeIMPORT = "import"
  163. // Boolean operators
  164. NodeAND = "and"
  165. NodeOR = "or"
  166. NodeNOT = "not"
  167. // Condition operators
  168. NodeLIKE = "like"
  169. NodeIN = "in"
  170. NodeHASPREFIX = "hasprefix"
  171. NodeHASSUFFIX = "hassuffix"
  172. NodeNOTIN = "notin"
  173. // Constant terminals
  174. NodeTRUE = "true"
  175. NodeFALSE = "false"
  176. NodeNULL = "null"
  177. /*
  178. NodeLIST = "list" // List value
  179. NodeMAP = "map" // Map value
  180. NodeGUARD = "guard" // Guard expressions for conditional statements
  181. // Map entries
  182. NodeMAPENTRY = "entry" // Map entry value
  183. // Function call statement
  184. NodeFUNCCALL = "funccall"
  185. // Data structure access
  186. NodeACCESS = "access"
  187. // Sink definition
  188. NodeSINK = "sink"
  189. NodeKINDMATCH = "kindmatch"
  190. NodeSCOPEMATCH = "scopematch"
  191. NodeSTATEMATCH = "statematch"
  192. NodePRIORITY = "priority"
  193. NodeSUPPRESSES = "suppresses"
  194. // Block statements
  195. NodeCOND = "cond"
  196. NodeLOOP = "loop"
  197. // Single statements
  198. NodeBREAK = "break"
  199. NodeCONTINUE = "continue"
  200. */
  201. )