Update copyright
[oweals/jsonpath.git] / parser.y
index acca9e12425b2f78f5768b97b8a26a78c33fb024..513fea608ca86cac5acac44d7a917b941ce9d755 100644 (file)
--- a/parser.y
+++ b/parser.y
@@ -1,6 +1,6 @@
 %{
 /*
- * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -41,7 +41,7 @@ void yyerror(struct jp_state *s, const char *msg);
 %parse-param { struct jp_state *s }
 %lex-param { struct jp_state *s }
 
-%code provides {
+%code requires {
 
 #ifndef __PARSER_H_
 #define __PARSER_H_
@@ -58,7 +58,8 @@ struct jp_opcode {
 struct jp_state {
        struct jp_opcode *pool;
        struct jp_opcode *path;
-       const char *error;
+       char *error;
+       char str_quote;
        char str_buf[128];
        char *str_ptr;
 };
@@ -79,11 +80,11 @@ void jp_free(struct jp_state *s);
 
 
 %token T_ROOT T_THIS T_DOT T_BROPEN T_BRCLOSE
-%token T_OR T_AND T_LT T_LE T_GT T_GE T_EQ T_NE T_POPEN T_PCLOSE T_NOT
+%token T_OR T_AND T_LT T_LE T_GT T_GE T_EQ T_NE T_POPEN T_PCLOSE T_NOT T_UNION
 
 %token <op> T_BOOL T_NUMBER T_STRING T_LABEL T_WILDCARD
 
-%type <op> expr path segments segment or_exps or_exp and_exps and_exp cmp_exp unary_exp
+%type <op> expr path segments segment union_exps union_exp or_exps or_exp and_exps and_exp cmp_exp unary_exp
 
 %error-verbose
 
@@ -111,7 +112,16 @@ segments
 segment
        : T_DOT T_LABEL                                 { $$ = $2; }
        | T_DOT T_WILDCARD                              { $$ = $2; }
-       | T_BROPEN or_exps T_BRCLOSE    { $$ = $2; }
+       | T_BROPEN union_exps T_BRCLOSE { $$ = $2; }
+       ;
+
+union_exps
+       : union_exp                                             { $$ = $1->sibling ? jp_alloc_op(T_UNION, 0, NULL, $1) : $1; }
+       ;
+
+union_exp
+       : union_exp T_UNION or_exps             { $$ = append_op($1, $3); }
+       | or_exps                                               { $$ = $1; }
        ;
 
 or_exps
@@ -157,7 +167,7 @@ unary_exp
 void
 yyerror(struct jp_state *s, const char *msg)
 {
-       s->error = msg;
+       s->error = strdup(msg);
 }
 
 static struct jp_opcode *
@@ -243,5 +253,8 @@ jp_free(struct jp_state *s)
                op = tmp;
        }
 
+       if (s->error)
+               free(s->error);
+
        free(s);
 }