//***************************************************************************
// This source code is copyrighted 2002 by Google Inc.  All rights
// reserved.  You are given a limited license to use this source code for
// purposes of participating in the Google programming contest.  If you
// choose to use or distribute the source code for any other purpose, you
// must either (1) first obtain written approval from Google, or (2)
// prominently display the foregoing copyright notice and the following
// warranty and liability disclaimer on each copy used or distributed.
// 
// The source code and repository (the "Software") is provided "AS IS",
// with no warranty, express or implied, including but not limited to the
// implied warranties of merchantability and fitness for a particular
// use.  In no event shall Google Inc. be liable for any damages, direct
// or indirect, even if advised of the possibility of such damages.
//***************************************************************************


// Define parse-element codes for the preparse parsehandler. 

#define kWhitespaceBit 0x20     // OR into Term or Punct code if whitespace follows
#define MAX_ENCODED_LENGTH 0x1F
#define SET_WHITESPACE_FOLLOWS(x) ((x) | kWhitespaceBit)
#define SET_LENGTH(x, len) ((x) | (len & 0x1F))
#define SET_LENGTH_FOLLOWS(x) (x) // use low-order bits of 0 to mean length 
                                  // follows

#define GET_WHITESPACE_FOLLOWS(x) ((x) & kWhitespaceBit)
#define GET_LENGTH(x) ((x) & 0x1F)
#define GET_LENGTH_FOLLOWS(x) (((x) & 0x1F) == 0)

// Parse elements are represented by a 1 byte parse code followed by
// any relevant arguments. The parse code byte is itself broken down
// into bit fields that provide more information about the parse element.
// We use bit twiddling operations to access the bits, but the following
// definition in terms of structs of unions may make the representation
// clearer. We don't use this definition in the code because of potential
// portability issues.
//
//   struct parse_code {
//     unsigned is_term_or_punct : 1;
//     union {
//       struct { // term or punctuation
//         unsigned is_punct           : 1;
//         unsigned whitespace_follows : 1;
//         unsigned length             : 5; // if this equals LengthFollows
//         static const int LengthFollows = 0; // then length is next varint32
//       } t_p;
//       enum { // other parse element
//         Header = 1, ResponseCode, BaseURL, ..., MAX_ELEMENT
//       } el;
//     } u;
//   };
 
#define kParseElt_Term            0x80        // char* term
#define kParseElt_Punctuation     0xC0        // char* text

#define IS_TERM_OR_PUNCT(x) ((x) & 0x80)
#define IS_TERM(x) (((x) & 0x40)==0)  // assuming IS_TERM_OR_PUNCT(x) != 0
#define IS_PUNCT(x) (((x) & 0x40)!=0) // assuming IS_TERM_OR_PUNCT(x) != 0

// Use for codes that are not Terms or Punct. Note: x must be less then 0x80!
// Note: These are used as array indices in the code that reads preparsed
// repositories, so be careful about changing their values.
#define PCODE(x) (x)   // MSB is 0

#define kParseElt_Header          PCODE(0x1)  // char* key, char* value
#define kParseElt_ResponseCode    PCODE(0x2)  // int
#define kParseElt_BaseURL         PCODE(0x3)  // char*
#define kParseElt_Anchor          PCODE(0x4)  // char*
#define kParseElt_LocalName       PCODE(0x5)  // char*
#define kParseElt_AnchorDone      PCODE(0x6)
#define kParseElt_ChangeFontColor PCODE(0x7)  // char*
#define kParseElt_ChangeFontColorEnd PCODE(0x8)
#define kParseElt_ChangeBGColor   PCODE(0x9)  // char*
#define kParseElt_ChangeBGColorEnd PCODE(0xa)
#define kParseElt_Image           PCODE(0xb) // char*
/* removed - redundant with Image
#define kParseElt_ImageHeight     PCODE(0xc) // char*
#define kParseElt_ImageWidth      PCODE(0xd) // char*
*/
#define kParseElt_Applet          PCODE(0xe)
#define kParseElt_AppletDone      PCODE(0xf)
#define kParseElt_IFrame          PCODE(0x10) // char*
#define kParseElt_IFrameDone      PCODE(0x11)
#define kParseElt_Frame           PCODE(0x12) // char*
#define kParseElt_Area            PCODE(0x13) // char*
#define kParseElt_Meta            PCODE(0x14) // char*
#define kParseElt_Frameset        PCODE(0x15) // char*
#define kParseElt_FramesetDone    PCODE(0x16)
#define kParseElt_Body            PCODE(0x17) // char*
#define kParseElt_BodyDone        PCODE(0x18)
#define kParseElt_ParagraphStart  PCODE(0x19) // char*
#define kParseElt_ParagraphEnd    PCODE(0x1a)
#define kParseElt_Break           PCODE(0x1b)
#define kParseElt_HorizontalRule  PCODE(0x1c)
#define kParseElt_ListItem        PCODE(0x1d)
#define kParseElt_UnorderedList   PCODE(0x1e)
#define kParseElt_OrderedList     PCODE(0x1f)
#define kParseElt_ListDone        PCODE(0x20)
#define kParseElt_Div             PCODE(0x21) // char*
#define kParseElt_DivDone         PCODE(0x22)
#define kParseElt_Span            PCODE(0x23) // char*
#define kParseElt_SpanDone        PCODE(0x24) // char*
#define kParseElt_Table           PCODE(0x25)
#define kParseElt_TableDone       PCODE(0x26)
#define kParseElt_Caption         PCODE(0x27)
#define kParseElt_CaptionDone     PCODE(0x28)
#define kParseElt_TableHCell      PCODE(0x29) // char*
#define kParseElt_TableDCell      PCODE(0x2a) // char*
#define kParseElt_TableCellDone   PCODE(0x2b)
#define kParseElt_TableRow        PCODE(0x2c)
#define kParseElt_TableRowDone    PCODE(0x2d)
#define kParseElt_Form            PCODE(0x2e) // char*
#define kParseElt_FormDone        PCODE(0x2f)
#define kParseElt_Select          PCODE(0x30) // char*
#define kParseElt_SelectDone      PCODE(0x31)
#define kParseElt_Option          PCODE(0x32) // char*
#define kParseElt_OptionDone      PCODE(0x33)
#define kParseElt_TextArea        PCODE(0x34) // char*
#define kParseElt_TextAreaDone    PCODE(0x35)
#define kParseElt_Input           PCODE(0x36) // char*
#define kParseElt_Heading         PCODE(0x37) // int
#define kParseElt_HeadingDone     PCODE(0x38)
#define kParseElt_Noframes        PCODE(0x39)
#define kParseElt_NoframesDone    PCODE(0x3a)
#define kParseElt_Object          PCODE(0x3b) // char*
#define kParseElt_ObjectDone      PCODE(0x3c)
#define kParseElt_Param           PCODE(0x3d) // char*
#define kParseElt_Embed           PCODE(0x3e) // char*
#define kParseElt_Head            PCODE(0x3f) // char*
#define kParseElt_HeadDone        PCODE(0x40)

// These don't turn into callbacks, but they are used for providing
// args to the AddTerm and AddPunctuation callbacks. 
// We include the codes in the output only when the values change.
#define kParseElt_SetFace         PCODE(0x41)     // int
#define kParseElt_SetSize         PCODE(0x42)     // int

// should be equal to largest parse code above
#define kParseElt_LASTCODE        kParseElt_SetSize 

#define kBeginDocMarker           PCODE(0x43)
#define kEndDocMarker             PCODE(0x44)
// no more codes bigger than 0x7f

