/*****************************************************************************
* General definitions of the Praser module goes into here:		     *
*****************************************************************************/

#ifndef PARSER_H
#define PARSER_H

#ifndef LINE_LEN
#define LINE_LEN	255
#endif  LINE_LEN

#define NAME_LEN	6

#ifndef TRUE
#define TRUE		1
#define FALSE		0
#endif

#ifndef NULL
#define NULL		0
#endif

#define FILE_TYPE_DATA	1
#define FILE_TYPE_VIEW	2

#define	UNGET_STACK_SIZE	5

/*****************************************************************************
* Tokens definitions goes into here                                          *
*****************************************************************************/
#define TOKEN_OPEN_PAREN	1
#define TOKEN_CLOSE_PAREN	2

#define TOKEN_NUMBER		10 /* Not used as number & names are decoded */
				    /* according to their places in grammer. */

#define TOKEN_VERTEX	20
#define TOKEN_POLYGON	21
#define TOKEN_POLYLINE	22
#define TOKEN_POINTLIST	23
#define TOKEN_OBJECT	24
#define TOKEN_COLOR	25
#define TOKEN_INTERNAL	26
#define TOKEN_NORMAL	27
#define TOKEN_PLANE	28

#define TOKEN_OTHER	100			/* Probably names & numbers. */
#define TOKEN_EOF	-1

#define VERTEX_ENTRY	0x0001		      /* Entry type, up to 16 types. */
#define POLYGON_ENTRY	0x0002
#define POLYLINE_ENTRY	0x0004
#define OBJECT_ENTRY	0x0008
#define COLOR_ENTRY	0x0010

#define POLYGON		1
#define POLYLINE	2
#define POINTLIST	3

/*****************************************************************************
* Parser error codes are following:                                          *
*****************************************************************************/
#define P_ERR_NumberExpected	1
#define P_ERR_CloseParanExpected 2
#define P_ERR_ListCompUndef	3
#define P_ERR_UndefExprHeader	4
#define P_ERR_NameTooLong	5

#define P_WRN_ObjectNameTrunc	100

/*****************************************************************************
* And some more definitions ...                                              *
*****************************************************************************/

typedef unsigned char Byte;

typedef struct BinTree {	   /* The entries are saved as binary trees. */
    struct BinTree *right, *left;		      /* Classic, isnt it !? */
    union {
	VoidPtr		      PVoid;
        struct VertexStruct  *PVertex;
        struct PolygonStruct *PPolygon;
        struct ObjectStruct  *PObject;
    } Data;
    char Name[NAME_LEN];
    Byte EntryType;				/* Kind of pointer in union. */
    Byte Used;	     /* TRUE if allready has been used (multi - referenced). */
} BinTree;

typedef struct FileDescription {  /* Each data file loaded gets such struct. */
    BinTree *VertexPointer,	        /* Pointers on the binary trees ...  */
	    *PolygonPointer,
	    *ObjectPointer;
} FileDescription;

/* Use the following structure if linear list operations are to be performed */
/* on VertexStruct/PolygonStruct/ObjectStruct (Virtual functions...).	     */
typedef struct LinearListStruct {
    struct LinearListStruct *Pnext;
} LinearListStruct;

typedef struct VertexStruct {
    struct VertexStruct *Pnext;
    float Coord[3];					   /* The 3D coords. */
    float Normal[3];					   /* The 3D normal. */
    Byte Transform;
    Byte Internal;		       /* If edge is Internal (IRIT output). */
    Byte HasNormal;			    /* If edge has a normal defined. */
} VertexStruct;

typedef struct PolygonStruct {
    struct PolygonStruct *Pnext;
    VertexStruct *PVertex;			    /* The polygon vertices. */
    float Plane[4];			      /* The Polygon plane equation. */
    Byte PolyType;		     /* One of POLYGON, POLYLINE, POINTLIST. */
    Byte HasPlane;			  /* If polygon has a plane defined. */
} PolygonStruct;

typedef struct ObjectStruct {
    struct ObjectStruct *Pnext;
    PolygonStruct *PPolygon;			    /* The objects polygons. */
    int Color;
} ObjectStruct;

/* And function prototypes: */

FileDescription *GetDataFile(FILE *f);
void GetViewFile(FILE *f, int FileExists);
BinTree *GetBinTree(char *RecName, BinTree *Tree);

#endif PARSER_H
