Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 18, 2018
1 parent 13de14a commit 3490217
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/qgsexpressionlexer.ll
Expand Up @@ -198,7 +198,7 @@ string "'"{str_char}*"'"
"ELSE" { return ELSE; }
"END" { return END; }
[()] { return yytext[0]; }
[()\[\]] { return yytext[0]; }
"," { return COMMA; }
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsexpressionparser.yy
Expand Up @@ -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

Expand Down Expand Up @@ -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); }

Expand Down

0 comments on commit 3490217

Please sign in to comment.