/*
 * Copyright (c) 2015 Balabit
 * Copyright (c) 2015 Vincent Bernat <Vincent.Bernat@exoscale.ch>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

%code requires {

#include "timestamp-parser.h"

}

%code {

#include "cfg-grammar-internal.h"
#include "cfg-parser.h"
#include "plugin.h"
#include "parser/parser-expr.h"
#include "date-parser.h"
#include "rewrite-fix-timezone.h"
#include "rewrite-set-timezone.h"
#include "rewrite-guess-timezone.h"

}

%define api.prefix {timestamp_}
%lex-param {CfgLexer *lexer}
%parse-param {CfgLexer *lexer}
%parse-param {gpointer *instance}
%parse-param {gpointer arg}

/* INCLUDE_DECLS */

%token KW_DATE_PARSER
%token KW_TIME_STAMP
%token KW_FIX_TIME_ZONE
%token KW_SET_TIME_ZONE
%token KW_GUESS_TIME_ZONE

%type <num> date_parser_stamp

%%

start
        : LL_CONTEXT_PARSER date_parser { YYACCEPT; }
	| LL_CONTEXT_REWRITE fix_timezone { YYACCEPT; }
	| LL_CONTEXT_REWRITE set_timezone { YYACCEPT; }
	| LL_CONTEXT_REWRITE guess_timezone { YYACCEPT; }
        ;

date_parser
        : KW_DATE_PARSER '('
          {
            last_parser = *instance = date_parser_new(configuration);
          }
          date_parser_options ')'
        ;

date_parser_options
        : date_parser_option date_parser_options
        |
        ;

date_parser_option
        : KW_FORMAT '(' string_list ')'             { date_parser_set_formats(last_parser, $3); }
        | KW_TIME_ZONE '(' string ')'               { date_parser_set_timezone(last_parser, $3); free($3); }
        | KW_TIME_STAMP '(' date_parser_stamp ')'   { date_parser_set_time_stamp(last_parser, $3); }
        | KW_FLAGS '(' date_parser_flags ')'
        | parser_opt
        ;

date_parser_stamp
        : string
          {
            $$ = log_msg_lookup_time_stamp_name($1);
            CHECK_ERROR($$ != -1, @1, "unknown time stamp name %s", $1);
            free($1);
          }

date_parser_flags
        : string date_parser_flags         { CHECK_ERROR(date_parser_process_flag(last_parser, $1), @1, "Unknown flag %s", $1); free($1); }
        |
        ;

fix_timezone
        : KW_FIX_TIME_ZONE
          {
            last_rewrite = *instance = rewrite_fix_time_zone_new(configuration);
          }
          '(' template_content fix_timezone_opts ')'
          {
            rewrite_fix_time_zone_set_zone_template_ref(*instance, $4);
          }
        ;

fix_timezone_opts
        : fix_timezone_opt fix_timezone_opts
        |
        ;

fix_timezone_opt
        : KW_TIME_STAMP '(' date_parser_stamp ')'  { rewrite_fix_time_zone_set_time_stamp(*instance, $3); }
	| rewrite_condition_opt
        ;

set_timezone
        : KW_SET_TIME_ZONE
          {
            last_rewrite = *instance = rewrite_set_time_zone_new(configuration);
          }
          '(' template_content set_timezone_opts ')'
          {
            rewrite_set_time_zone_set_zone_template_ref(*instance, $4);
          }
        ;

set_timezone_opts
        : set_timezone_opt set_timezone_opts
        |
        ;

set_timezone_opt
        : KW_TIME_STAMP '(' date_parser_stamp ')'  { rewrite_set_time_zone_set_time_stamp(*instance, $3); }
	| rewrite_condition_opt
        ;

guess_timezone
        : KW_GUESS_TIME_ZONE
          {
            last_rewrite = *instance = rewrite_guess_time_zone_new(configuration);
          }
          '(' guess_timezone_opts ')'
        ;

guess_timezone_opts
        : guess_timezone_opt guess_timezone_opts
        |
        ;

guess_timezone_opt
        : KW_TIME_STAMP '(' date_parser_stamp ')'  { rewrite_guess_time_zone_set_time_stamp(*instance, $3); }
	| rewrite_condition_opt
        ;

/* INCLUDE_RULES */

%%
