Skip to content
Permalink
Browse files

[FEATURE][expression] Enable use of square brackets to access map and…

… array elements

Allows expressions like:

- array(1,2,3)[0] -> 1
- array(1,2,3)[2] -> 3
- array(1,2,3)[-1] -> 3 (Python style, negative indices count from end of array)
- array(1,2,3)[-3] -> 1
- map('a',1,'b',2)['a'] -> 1
- map('a',1,'b',2)['b'] -> 2
  • Loading branch information
nirvn authored and nyalldawson committed Dec 14, 2018
1 parent 13de14a commit 3490217209b785fb5f74ff82da6c086e6b3a20be
Showing with 11 additions and 2 deletions.
  1. +1 −1 src/core/qgsexpressionlexer.ll
  2. +10 −1 src/core/qgsexpressionparser.yy
@@ -198,7 +198,7 @@ string "'"{str_char}*"'"
"ELSE" { return ELSE; }
"END" { return END; }
[()] { return yytext[0]; }
[()\[\]] { return yytext[0]; }
"," { return COMMA; }
@@ -115,7 +115,7 @@ void addParserLocation(YYLTYPE* yyloc, QgsExpressionNode *node)
//

// operator tokens
%token <b_op> OR AND EQ NE LE GE LT GT REGEXP LIKE IS PLUS MINUS MUL DIV INTDIV MOD CONCAT POW
%token <b_op> OR AND EQ NE LE GE LT GT REGEXP LIKE IS PLUS MINUS MUL DIV INTDIV MOD CONCAT POW SQUARE_BRAKET_OPENING SQUARE_BRAKET_CLOSING
%token <u_op> NOT
%token IN

@@ -284,6 +284,15 @@ expression:
| expression IN '(' exp_list ')' { $$ = new QgsExpressionNodeInOperator($1, $4, false); }
| expression NOT IN '(' exp_list ')' { $$ = new QgsExpressionNodeInOperator($1, $5, true); }

| expression '[' expression ']'
{
QgsExpressionNode::NodeList* args = new QgsExpressionNode::NodeList();
args->append( $1 );
args->append( $3 );
QgsExpressionNodeFunction *f = new QgsExpressionNodeFunction( QgsExpression::functionIndex( "map_get" ), args );
$$ = f;
}

| PLUS expression %prec UMINUS { $$ = $2; }
| MINUS expression %prec UMINUS { $$ = new QgsExpressionNodeUnaryOperator( QgsExpressionNodeUnaryOperator::uoMinus, $2); }

0 comments on commit 3490217

Please sign in to comment.
You can’t perform that action at this time.