const.go 4.6 KB

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