#include "grammar.h"
#include "pconf.h"

#ifndef _PARSER_LIB_PARSE_H
#define _PARSER_LIB_PARSE_H

#define	PATH_TREE_DEPTH	50	/* max call-nets in single rewrite rule */
#define HIST_LEN	5
#define NO_SCORE 0xFFFF

extern int	SymBufSize;	/* buffer size to hold char strings */

typedef unsigned short	Id;

typedef struct framedef {
	Id	n_slot;		/* number of slots in form */
	Id	*slot;		/* net numbers for slots */
	char	**prompt;	/* propmt for slot */
} FrameDef;

typedef struct gram
{
    FrameDef	*frame_def;		/* nets used in each form */
    char	**frame_name;		/* frame names */
    int		num_frames;		/* number of frames read in */
    char	**labels;		/* names of nets */
    Gnode	**Nets;			/* pointers to heads of nets */
    int		num_nets;		/* number of nets read in */
    char	**wrds;			/* pointers to strings for words */
    int		num_words;		/* number of words in lexicon */
    int		*node_counts;		/* number of nodes in each net */
    char	*leaf;			/* concept leaf flags */
    unsigned char	*priorities;	/* priorities for nets */
    int		max_pri;		/* max net priority level */
    char	*sym_buf;		/* strings for words and names */
} Gram;


/* cells of chart */
typedef struct edge {
    Id			net;
    Id			sw;
    Id			ew;
    Id			score;
    char		nchld;
    struct edge		**chld;
    struct edge		*link;
} Edge;


/* temporary structure used in matching nets to create edges */
typedef struct buf {
	Gnode	*state;		/* current state */
	Id	net;		/* number of top-level net */
	Id	sw;		/* number of top-level net */
	Id	ew;		/* number of top-level net */
	Id	score;		/* number of top-level net */
	char	nchld;
	Edge	*chld[PATH_TREE_DEPTH];	/* pointers to sub-trees (children) */
	int	word_pos;	/* extended flag */
}Path;



/* structure for linking edges into chart */
typedef struct edge_link {
	int	sw;
	struct edge_link *link;
	Edge *edge;
}EdgeLink;


typedef struct frame_id {
	unsigned short id;
	unsigned short count;
} Fid;

typedef struct seq_node {
	Edge *edge;
	unsigned short	n_frames;	/* frame count for path */
	unsigned short	n_act;	/* number of active frames for path */
	Fid	*frame_id;
	struct seq_node *back_link;
	struct seq_node *link;
	unsigned short *pri;
} SeqNode;

typedef struct seq_cell {
	Id score;
	Id n_slots;
	Id n_frames;
	SeqNode *link;
} SeqCell;

typedef struct frame_node {
	int n_frames;
	int frame;
	SeqNode *slot;
	struct frame_node *bp;
	struct frame_node *link;
} FrameNode;

// functions in match.cc
// File specific, non-public
//   int add_to_chart(Path *path);
Edge *find_edge(int net, int sw, int ew);
int check_final(Path *path);
int expand_path( Path *path, Gram *gram );
int match_net(int net, int word_pos, Gram *gram);
int check_chart(int net, int word_pos, Edge **edge);
int score_phrase(unsigned short sw, unsigned short ew);
void clear_chart(int num_nets);

// functions in dict.cc
int read_dict(char *dir, char *dict_file, Gram *gram, char **sym_buf_start,
	      char *sym_buf_end);
int find_word(char *s, Gram *gram);

// functions in print_structs.cc
// File specific, non-public
//  void strip_underscore(char *str);
char *print_edge(Edge *edge, Gram *gram, char *s);
char *print_extracts(Edge *edge, Gram *gram, char *s, int level, char *fn);
void print_seq(SeqNode *ss, char **lbl);

// functions in print_structs.cc
void print_seq(SeqNode *ss, char **lbl);

// functions in grammar.cc
void read_nets(char *net_file, Gram *gram, char **sym_buf_start,
	       char *sym_buf_end);
void read_frames(char *dir, char *frames_file, Gram *gram,
		 char **sym_buf_start, char *sym_buf_end);
int find_net(char *name, Gram *gram);
void quit(int err, char *fmt, char *str);
int find_frame(char *name, Gram *gram);
Gram *read_grammar(char *dir, char *dict_file,
		   char *grammar_file, char *frames_file, char *priority_file);

// functions in pconf.cc
int pconf (int argc, char *argv[], Config_t config_p[],
	   char **display, char **geometry,
	   char *(*GetDefault)(char *, char *));

int fpconf (FILE *config_fp, Config_t config_p[],
	    char **display, char **geometry,
	    char *(*GetDefault)(char *, char *));

void pusage (char *prog, Config_t *cp);
void strip_underscore(char *str);

// functions in parse.cc
void parse(char *line, Gram *gram);
void read_script(char *line, Gram *gram);
void copy_chart(int low, int high);
void breakpt_reset(int num_nets);
int add_to_seq(int net, int start_word, Gram *gram);
int cmp_pri(unsigned short *p1, unsigned short *p2, int max_pri);
void expand_seq(SeqNode *seq, int ns);
void map_to_frames(SeqNode **seq_start, Gram *gram);
int net_in_frame(int net, int frame, Gram *gram);
int append_slot(int net, Edge *edge, int prev_word_pos, Gram *gram);
void score_pri(unsigned short *pri, int net, int score, Gram *gram);
// Changed from to so that it does not just look for a global Config_t
//void config(int argc, char **argv);
void config(int argc, char **argv, Config_t *conf_in, char *config_fp);
void init_parse(char *dir, char *dict_file, char *grammar_file,
		char *frames_file, char *priority_file);
void print_profile();
void reset(int num_nets);
void print_parse(int parse_num, char *out_str, int extract, Gram *gram);
void start_session();

#endif
