const.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. NodeVALUE = "value" // Simple value
  120. // Constructed tokens
  121. NodeSTATEMENTS = "statements" // List of statements
  122. NodeLIST = "list" // List value
  123. NodeMAP = "map" // Map value
  124. NodeGUARD = "guard" // Guard expressions for conditional statements
  125. // Map entries
  126. NodeMAPENTRY = "entry" // Map entry value
  127. // Boolean operators
  128. NodeOR = "or"
  129. NodeAND = "and"
  130. NodeNOT = "not"
  131. // Condition operators
  132. NodeLIKE = "like"
  133. NodeIN = "in"
  134. NodeBEGINSWITH = "beginswith"
  135. NodeENDSWITH = "endswith"
  136. NodeNOTIN = "notin"
  137. NodeGEQ = ">="
  138. NodeLEQ = "<="
  139. NodeNEQ = "!="
  140. NodeEQ = "=="
  141. NodeGT = ">"
  142. NodeLT = "<"
  143. // Constants
  144. NodeTRUE = "true"
  145. NodeFALSE = "false"
  146. NodeNULL = "null"
  147. // Arithmetic operators
  148. NodePLUS = "plus"
  149. NodeMINUS = "minus"
  150. NodeTIMES = "times"
  151. NodeDIV = "div"
  152. NodeMODINT = "modint"
  153. NodeDIVINT = "divint"
  154. // Assignment statement
  155. NodeASSIGN = ":="
  156. // Function call statement
  157. NodeFUNCCALL = "funccall"
  158. // Data structure access
  159. NodeACCESS = "access"
  160. // Sink definition
  161. NodeSINK = "sink"
  162. NodeKINDMATCH = "kindmatch"
  163. NodeSCOPEMATCH = "scopematch"
  164. NodeSTATEMATCH = "statematch"
  165. NodePRIORITY = "priority"
  166. NodeSUPPRESSES = "suppresses"
  167. // Block statements
  168. NodeCOND = "cond"
  169. NodeLOOP = "loop"
  170. // Single statements
  171. NodeBREAK = "break"
  172. NodeCONTINUE = "continue"
  173. )