fts5parse.y 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ** 2014 May 31
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. ******************************************************************************
  12. **
  13. */
  14. // All token codes are small integers with #defines that begin with "TK_"
  15. %token_prefix FTS5_
  16. // The type of the data attached to each token is Token. This is also the
  17. // default type for non-terminals.
  18. //
  19. %token_type {Fts5Token}
  20. %default_type {Fts5Token}
  21. // The generated parser function takes a 4th argument as follows:
  22. %extra_argument {Fts5Parse *pParse}
  23. // This code runs whenever there is a syntax error
  24. //
  25. %syntax_error {
  26. UNUSED_PARAM(yymajor); /* Silence a compiler warning */
  27. sqlite3Fts5ParseError(
  28. pParse, "fts5: syntax error near \"%.*s\"",TOKEN.n,TOKEN.p
  29. );
  30. }
  31. %stack_overflow {
  32. sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
  33. }
  34. // The name of the generated procedure that implements the parser
  35. // is as follows:
  36. %name sqlite3Fts5Parser
  37. // The following text is included near the beginning of the C source
  38. // code file that implements the parser.
  39. //
  40. %include {
  41. #include "fts5Int.h"
  42. #include "fts5parse.h"
  43. /*
  44. ** Disable all error recovery processing in the parser push-down
  45. ** automaton.
  46. */
  47. #define YYNOERRORRECOVERY 1
  48. /*
  49. ** Make yytestcase() the same as testcase()
  50. */
  51. #define yytestcase(X) testcase(X)
  52. /*
  53. ** Indicate that sqlite3ParserFree() will never be called with a null
  54. ** pointer.
  55. */
  56. #define YYPARSEFREENOTNULL 1
  57. /*
  58. ** Alternative datatype for the argument to the malloc() routine passed
  59. ** into sqlite3ParserAlloc(). The default is size_t.
  60. */
  61. #define YYMALLOCARGTYPE u64
  62. } // end %include
  63. %left OR.
  64. %left AND.
  65. %left NOT.
  66. %left TERM.
  67. %left COLON.
  68. input ::= expr(X). { sqlite3Fts5ParseFinished(pParse, X); }
  69. %destructor input { (void)pParse; }
  70. %type cnearset {Fts5ExprNode*}
  71. %type expr {Fts5ExprNode*}
  72. %type exprlist {Fts5ExprNode*}
  73. %destructor cnearset { sqlite3Fts5ParseNodeFree($$); }
  74. %destructor expr { sqlite3Fts5ParseNodeFree($$); }
  75. %destructor exprlist { sqlite3Fts5ParseNodeFree($$); }
  76. %type colset {Fts5Colset*}
  77. %destructor colset { sqlite3_free($$); }
  78. %type colsetlist {Fts5Colset*}
  79. %destructor colsetlist { sqlite3_free($$); }
  80. colset(A) ::= MINUS LCP colsetlist(X) RCP. {
  81. A = sqlite3Fts5ParseColsetInvert(pParse, X);
  82. }
  83. colset(A) ::= LCP colsetlist(X) RCP. { A = X; }
  84. colset(A) ::= STRING(X). {
  85. A = sqlite3Fts5ParseColset(pParse, 0, &X);
  86. }
  87. colset(A) ::= MINUS STRING(X). {
  88. A = sqlite3Fts5ParseColset(pParse, 0, &X);
  89. A = sqlite3Fts5ParseColsetInvert(pParse, A);
  90. }
  91. colsetlist(A) ::= colsetlist(Y) STRING(X). {
  92. A = sqlite3Fts5ParseColset(pParse, Y, &X); }
  93. colsetlist(A) ::= STRING(X). {
  94. A = sqlite3Fts5ParseColset(pParse, 0, &X);
  95. }
  96. expr(A) ::= expr(X) AND expr(Y). {
  97. A = sqlite3Fts5ParseNode(pParse, FTS5_AND, X, Y, 0);
  98. }
  99. expr(A) ::= expr(X) OR expr(Y). {
  100. A = sqlite3Fts5ParseNode(pParse, FTS5_OR, X, Y, 0);
  101. }
  102. expr(A) ::= expr(X) NOT expr(Y). {
  103. A = sqlite3Fts5ParseNode(pParse, FTS5_NOT, X, Y, 0);
  104. }
  105. expr(A) ::= colset(X) COLON LP expr(Y) RP. {
  106. sqlite3Fts5ParseSetColset(pParse, Y, X);
  107. A = Y;
  108. }
  109. expr(A) ::= LP expr(X) RP. {A = X;}
  110. expr(A) ::= exprlist(X). {A = X;}
  111. exprlist(A) ::= cnearset(X). {A = X;}
  112. exprlist(A) ::= exprlist(X) cnearset(Y). {
  113. A = sqlite3Fts5ParseImplicitAnd(pParse, X, Y);
  114. }
  115. cnearset(A) ::= nearset(X). {
  116. A = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, X);
  117. }
  118. cnearset(A) ::= colset(X) COLON nearset(Y). {
  119. A = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, Y);
  120. sqlite3Fts5ParseSetColset(pParse, A, X);
  121. }
  122. %type nearset {Fts5ExprNearset*}
  123. %type nearphrases {Fts5ExprNearset*}
  124. %destructor nearset { sqlite3Fts5ParseNearsetFree($$); }
  125. %destructor nearphrases { sqlite3Fts5ParseNearsetFree($$); }
  126. nearset(A) ::= phrase(Y). { A = sqlite3Fts5ParseNearset(pParse, 0, Y); }
  127. nearset(A) ::= CARET phrase(Y). {
  128. sqlite3Fts5ParseSetCaret(Y);
  129. A = sqlite3Fts5ParseNearset(pParse, 0, Y);
  130. }
  131. nearset(A) ::= STRING(X) LP nearphrases(Y) neardist_opt(Z) RP. {
  132. sqlite3Fts5ParseNear(pParse, &X);
  133. sqlite3Fts5ParseSetDistance(pParse, Y, &Z);
  134. A = Y;
  135. }
  136. nearphrases(A) ::= phrase(X). {
  137. A = sqlite3Fts5ParseNearset(pParse, 0, X);
  138. }
  139. nearphrases(A) ::= nearphrases(X) phrase(Y). {
  140. A = sqlite3Fts5ParseNearset(pParse, X, Y);
  141. }
  142. /*
  143. ** The optional ", <integer>" at the end of the NEAR() arguments.
  144. */
  145. neardist_opt(A) ::= . { A.p = 0; A.n = 0; }
  146. neardist_opt(A) ::= COMMA STRING(X). { A = X; }
  147. /*
  148. ** A phrase. A set of primitives connected by "+" operators. Examples:
  149. **
  150. ** "the" + "quick brown" + fo *
  151. ** "the quick brown fo" *
  152. ** the+quick+brown+fo*
  153. */
  154. %type phrase {Fts5ExprPhrase*}
  155. %destructor phrase { sqlite3Fts5ParsePhraseFree($$); }
  156. phrase(A) ::= phrase(X) PLUS STRING(Y) star_opt(Z). {
  157. A = sqlite3Fts5ParseTerm(pParse, X, &Y, Z);
  158. }
  159. phrase(A) ::= STRING(Y) star_opt(Z). {
  160. A = sqlite3Fts5ParseTerm(pParse, 0, &Y, Z);
  161. }
  162. /*
  163. ** Optional "*" character.
  164. */
  165. %type star_opt {int}
  166. star_opt(A) ::= STAR. { A = 1; }
  167. star_opt(A) ::= . { A = 0; }