/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#ifndef BP_MORPH_PARSER_H
#define BP_MORPH_PARSER_H

#include <stdio.h>

#include <libxml/parser.h>

#include <foostring/foostring.h>
#include <sakura/abi.h>
#include <sakura/common/bitstream.h>

#include "binary-pool.h"
#include "bp-parser.h"
#include "syntax-parser.h"

struct chunk_info {
   FILE *stream;
   char *name;
   char *pathname;
};

#define UNKNOWN   ((uint32_t)(-1))

/** The type of groups as the information about them is collected.
 * As an on-disk format, it is only used for partial files. */
struct group_info {
   /** structural data, initially UNKNOWN */
   uint32_t from, to, synh, semh; 

   uint32_t id;     /**< unique identifier of this group */
   uint32_t type;   /**< type of this group -- index into the binary pool */
   uint32_t length; /**< number of subsequent groups forming a coordination */
};

typedef struct morph_parser {

   /* pos/subpos things */
   struct poliqarp_binary_segment pos;

   struct poliqarp_binary_interp disamb[1024];
   size_t disamb_index;
   struct poliqarp_binary_interp amb[1024];
   size_t amb_index;

   /* chunk things */

   struct chunk_info chunk_info[1024];
   size_t chunk_stack[64];
   size_t chunk_index;
   uint32_t pos_index;

   /* buffer for putting <tag>text between tags</tag> together */
   string_t concat_buffer;
   bool accumulate;

   /* memory arena */
   struct marena arena;
   bool have_disamb;

   /* dictionaries */
   struct binary_pool orth_pool;
   struct binary_pool tag_pool;
   struct binary_pool base_disamb_pool;
   struct binary_pool base_amb_pool;
   struct binary_pool interp_disamb_pool;
   struct binary_pool interp_amb_pool;
   struct binary_pool subchunk_pool; /* subchunk name actually */
   struct binary_pool group_type_pool; 

   /* broken XML flag */
   bool broken_tok;
   bool broken_lex;

   /* no-space tag detected */
   bool space;

   char *base_name;
   FILE *corpus_stream;

   /* currently processing */
   const char *filename;
   const char *dirname;

   /* syntax/group stuff */
   struct syntax_parser *syntax;
   struct group_info *groups_ready;
   int groups_size, groups_max, nest;
   FILE *syntax_stream;
   FILE *syntax_sync_stream;
   struct obs syntax_bs;
   uint32_t last_from;
   uint32_t last_to;
   uint32_t max_to;
   bool synchro;
   bool merge_first;

   xmlSAXHandler sax_handler;
   xmlParserCtxtPtr xml_parser;
   char error_buffer[1024];

   size_t num_errors;

} morph_parser;

struct bp_parser *morph_parser_create(struct bp_parser *syntax_parser, 
   const char *base_name, bool incremental);

#endif /* BP_MORPH_PARSER_H */
