@@ -119,6 +119,31 @@ public function __construct($database = null, $table = null, $column = null, $al
119119 }
120120
121121 /**
122+ * Possible options:
123+ *
124+ * `field`
125+ *
126+ * First field to be filled.
127+ * If this is not specified, it takes the value of `parseField`.
128+ *
129+ * `parseField`
130+ *
131+ * Specifies the type of the field parsed. It may be `database`,
132+ * `table` or `column`. These expressions may not include
133+ * parentheses.
134+ *
135+ * `breakOnAlias`
136+ *
137+ * If not empty, breaks when the alias occurs (it is not included).
138+ *
139+ * `breakOnParentheses`
140+ *
141+ * If not empty, breaks when the first parentheses occurs.
142+ *
143+ * `parenthesesDelimited`
144+ *
145+ * If not empty, breaks after last parentheses occurred.
146+ *
122147 * @param Parser $parser The parser that serves as context.
123148 * @param TokensList $list The list of tokens that are being parsed.
124149 * @param array $options Parameters for parsing.
@@ -164,6 +189,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
164189 */
165190 $ prev = array (null , null );
166191
192+ // When a field is parsed, no parentheses are expected.
193+ if (!empty ($ options ['parseField ' ])) {
194+ $ options ['breakOnParentheses ' ] = true ;
195+ $ options ['field ' ] = $ options ['parseField ' ];
196+ }
197+
167198 for (; $ list ->idx < $ list ->count ; ++$ list ->idx ) {
168199
169200 /**
@@ -195,7 +226,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
195226 // A `(` was previously found and this keyword is the
196227 // beginning of a statement, so this is a subquery.
197228 $ ret ->subquery = $ token ->value ;
198- } elseif ($ token ->flags & Token::FLAG_KEYWORD_FUNCTION ) {
229+ } elseif (($ token ->flags & Token::FLAG_KEYWORD_FUNCTION )
230+ && (empty ($ options ['parseField ' ]))
231+ ) {
199232 $ isExpr = true ;
200233 } elseif (($ token ->flags & Token::FLAG_KEYWORD_RESERVED )
201234 && ($ brackets === 0 )
@@ -207,7 +240,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
207240 break ;
208241 }
209242 if ($ token ->value === 'AS ' ) {
210- if (!empty ($ options ['noAlias ' ])) {
243+ if (!empty ($ options ['breakOnAlias ' ])) {
211244 break ;
212245 }
213246 if (!empty ($ ret ->alias )) {
@@ -224,8 +257,24 @@ public static function parse(Parser $parser, TokensList $list, array $options =
224257 }
225258 }
226259
260+ if (($ token ->type === Token::TYPE_NUMBER )
261+ || ($ token ->type === Token::TYPE_BOOL )
262+ || (($ token ->type === Token::TYPE_SYMBOL )
263+ && ($ token ->flags & Token::FLAG_SYMBOL_VARIABLE ))
264+ || (($ token ->type === Token::TYPE_OPERATOR )
265+ && ($ token ->value !== '. ' ))
266+ ) {
267+ if (!empty ($ options ['parseField ' ])) {
268+ break ;
269+ }
270+
271+ // Numbers, booleans and operators (except dot) are usually part
272+ // of expressions.
273+ $ isExpr = true ;
274+ }
275+
227276 if ($ token ->type === Token::TYPE_OPERATOR ) {
228- if ((!empty ($ options ['noBrackets ' ]))
277+ if ((!empty ($ options ['breakOnParentheses ' ]))
229278 && (($ token ->value === '( ' ) || ($ token ->value === ') ' ))
230279 ) {
231280 // No brackets were expected.
@@ -244,7 +293,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
244293 } elseif ($ token ->value === ') ' ) {
245294 --$ brackets ;
246295 if ($ brackets === 0 ) {
247- if (!empty ($ options ['bracketsDelimited ' ])) {
296+ if (!empty ($ options ['parenthesesDelimited ' ])) {
248297 // The current token is the last bracket, the next
249298 // one will be outside the expression.
250299 $ ret ->expr .= $ token ->token ;
@@ -264,19 +313,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
264313 }
265314 }
266315
267- if (($ token ->type === Token::TYPE_NUMBER )
268- || ($ token ->type === Token::TYPE_BOOL )
269- || (($ token ->type === Token::TYPE_SYMBOL )
270- && ($ token ->flags & Token::FLAG_SYMBOL_VARIABLE ))
271- || (($ token ->type === Token::TYPE_OPERATOR )
272- && ($ token ->value !== '. ' ))
273- ) {
274- // Numbers, booleans and operators (except dot) are usually part
275- // of expressions.
276- $ isExpr = true ;
277- }
278-
279- // Saving the previous token.
316+ // Saving the previous tokens.
280317 $ prev [0 ] = $ prev [1 ];
281318 $ prev [1 ] = $ token ;
282319
@@ -323,14 +360,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
323360 $ dot = true ;
324361 $ ret ->expr .= $ token ->token ;
325362 } else {
326- $ field = (! empty ($ options ['skipColumn ' ])) ? 'table ' : ' column ' ;
363+ $ field = empty ($ options ['field ' ]) ? 'column ' : $ options [ ' field ' ] ;
327364 if (empty ($ ret ->$ field )) {
328365 $ ret ->$ field = $ token ->value ;
329366 $ ret ->expr .= $ token ->token ;
330367 $ dot = false ;
331368 } else {
332369 // No alias is expected.
333- if (!empty ($ options ['noAlias ' ])) {
370+ if (!empty ($ options ['breakOnAlias ' ])) {
334371 break ;
335372 }
336373 if (!empty ($ ret ->alias )) {
0 commit comments