BLEX2.H

[目次 | 型・クラス・構造体 | マクロ]

目次

型・クラス・構造体一覧

マクロ一覧


   1|/**************************************************************************
   2|*  1. <<< 基本レキシカルアナライザ (BLex2) >>> 
   3|***************************************************************************/
   4|
   5|#ifndef __BLEX2_H
   6|#define __BLEX2_H
   7|
   8|/*----------------------------------------------------------------------
   9|[Module Property]
  10|name = BLex2
  11|title = 基本字句解析
  12|category = ファイル
  13|src = blex2.c
  14|depend = Types, FileX, Except3, StrX
  15|priority = StrX
  16|accord =
  17|----------------------------------------------------------------------*/
  18|
  19|#ifndef USES_PRIORITY_HEADER
  20|/*[START_OF_PRIORITY_HEADER]*/
  21|
  22|#define  USES_BLEX2
  23|struct _BLex2;  typedef struct _BLex2  BLex2;
  24|typedef long  BLex2_Pos;
  25|struct _BLex2_Line;  typedef struct _BLex2_Line  BLex2_Line;
  26|struct _BLex2_Kakko;  typedef struct _BLex2_Kakko  BLex2_Kakko;
  27|struct _BLex2_CComment;  typedef struct _BLex2_CComment  BLex2_CComment;
  28|struct _BLex2_CStr;  typedef struct _BLex2_CStr  BLex2_CStr;
  29|struct _BLex2_HTMLTags;  typedef struct _BLex2_HTMLTags  BLex2_HTMLTags;
  30|
  31|#define  BLex2_Token_Unknown         257   /* [Compone_GID:BLex2_TokenType] */
  32|#define  BLex2_Token_CCommentStart   258   /* [Compone_GID:BLex2_TokenType] */
  33|#define  BLex2_Token_CCommentEnd     259   /* [Compone_GID:BLex2_TokenType] */
  34|#define  BLex2_Token_CPPCommentStart 260   /* [Compone_GID:BLex2_TokenType] */
  35|#define  BLex2_Token_EscChar         261   /* [Compone_GID:BLex2_TokenType] */
  36|
  37|typedef  int  (*BLex_FStrnmp)( const char* s1, const char* s2, unsigned n );
  38|
  39|#define  BLex2_Err_TooManyKakkoEnd     321  /* [Compone_GID:Errors_Code] */
  40|#define  BLex2_Err_DefferentKakkoType  322  /* [Compone_GID:Errors_Code] */
  41|#define  BLex2_Err_BinaryCannotParse   323  /* [Compone_GID:Errors_Code] */
  42|#define  BLex2_Err_NoCommentEnd        324  /* [Compone_GID:Errors_Code] */
  43|#define  BLex2_Err_RecBufFull          325  /* [Compone_GID:Errors_Code] */
  44|
  45|#define  STDLIBS_INCLUDE
  46|#define  STDLIBS_INCLUDE_STDIO_H
  47|#define  STDLIBS_INCLUDE_STDLIB_H
  48|
  49|/*[END_OF_PRIORITY_HEADER]*/
  50|#endif /* USES_PRIORITY_HEADER */
  51|
  52|
  53|
  54|
  55|#ifdef _K_AND_R
  56|#error  not support K&R
  57|#endif
  58|
  59|#ifdef __cplusplus
  60|extern "C" {
  61|#endif
  62|
  63|
  64| 
  65|/********************************************************************
  66|*  2. <<< モジュール設定 >>> 
  67|*【補足】
  68|*・BLEX2_USE_SIZEUBUF を定義すると、BLex2_getPos 関数において、
  69|*  改行コードがどんなタイプのテキストファイルでも正常に動きます。
  70|********************************************************************/
  71|#ifndef  BLEX2_SETTING
  72|  /*#define  BLEX2_USE_SIZEBUF*/  /* サイズバッファを使う(→補足)*/
  73|  #define  BLEX2_BIN_CHKER        /* バイナリチェックコードを含むか */
  74|  #define  BLEX2_USE_PATH         /* エラーメッセージにファイルパスを表示する */
  75|#endif
  76|
  77|
  78| 
  79|/*----------------------------------------------------------------------*/
  80|/* 3. <<< Interface Area --------------------------------------------- >>> */ 
  81|/*----------------------------------------------------------------------*/
  82|
  83|
  84| 
  85|/********************************************************************
  86|*  4. <<< バッファつき汎用レキシカルアナライザ [BLex2] ★>>> 
  87|*【補足】
  88|*・buf[0] が、ファイル・ポインタのある位置の文字です。
  89|*  ただし、fp のファイル・ポインタの位置と一致しません。
  90|*・記録用バッファについては BLex2_recStart 関数を参考にしてください。
  91|********************************************************************/
  92|struct _BLex2 {
  93|  FILE* fp;         /* 読み込みファイル・ストリーム */
  94|  char* buf;        /* 先読みバッファ、文字の配列 */
  95|  #ifdef  BLEX2_USE_SIZEBUF
  96|    char* sizeBuf;    /* 先読みバッファに入った文字の実際のバイト数 */
  97|  #endif
  98|  int   buf_sizeof;  /* buf のサイズ */
  99|  int   buf_n;       /* buf に読み込まれている文字の数(byte) */
 100|
 101|  int   tokenType;
 102|  int   tokenSize;
 103|
 104|  #ifdef USES_ARRX
 105|    /* 記録用バッファ */
 106|    ArrX_Buf  recBuf;
 107|    bool      recBufF;   /* recBuf が初期化されているかどうか */
 108|    bool      recF;      /* 記録状態かどうか */
 109|  #endif /* USES_ARRX */
 110|
 111|  #ifndef NDEBUG
 112|    int  stopper;       /* 同じ位置で peek した回数 */
 113|  #endif
 114|
 115|  #ifdef  BLEX2_USE_PATH
 116|    char  path[_MAX_PATH];  /* ファイルパス */
 117|  #endif
 118|};
 119|
 120|/* 基本操作 */
 121|void  BLex2_init( BLex2*, const char* fname, char* buf, size_t buf_sizeof );
 122|void  BLex2_finish( BLex2* );
 123|void  BLex2_setSizeBuf( BLex2*, void* buf, int buf_size );
 124|int   BLex2_peek( BLex2*, int offset );
 125|void  BLex2_next( BLex2*, int count );
 126|int   BLex2_read( BLex2* );
 127|void  BLex2_watch( BLex2* );
 128|
 129|/* トークン処理 */
 130|void  BLex2_setToken( BLex2*, int type, int size );
 131|void  BLex2_getToken( BLex2*, char* s, int s_size );
 132|void  BLex2_nextToken( BLex2* );
 133|
 134|/* 応用操作 */
 135|void  BLex2_reset( BLex2* );
 136|void  BLex2_readLine( BLex2*, char* line, int size );
 137|void  BLex2_nextLine( BLex2* );
 138|void  BLex2_nextFor( BLex2*, char* key );
 139|void  BLex2_nextFor2( BLex2*, char* key, bool i_case );
 140|void  BLex2_skipTo( BLex2*, char* key );
 141|void  BLex2_skipTo2( BLex2*, char* key, bool i_case );
 142|int   BLex2_skipSpcTab( BLex2* );
 143|int   BLex2_skipSpcTabRet( BLex2* );
 144|void  BLex2_getPos( BLex2*, BLex2_Pos* pos );
 145|void  BLex2_setPos( BLex2*, BLex2_Pos* pos );
 146|
 147|/* 文字列など */
 148|int   BLex2_strncmpX( BLex2*, const char* kword, BLex_FStrnmp pStrNCmp );
 149|int   BLex2_strncmpEx( BLex2*, const char* kword, bool i_case );
 150|int   BLex2_getBySpace( BLex2*, char* buf, int buf_len, int termCh );
 151|void  BLex2_copyStr( BLex2*, char* buf, int buf_size );
 152|int   BLex2_readStr( BLex2*, char* buf, size_t buf_len, char* terms );
 153|int  BLex2_copyIdiom2( BLex2*, int nWord,
 154|  char* idiom, int idiom_size );
 155|char* BLex2_readCSV( BLex2*, char* buf, int buf_maxLen );
 156|bool  BLex2_isHoriLine( BLex2*, int ch, int nCh );
 157|int   BLex2_strchrInLine( BLex2*, int ch );
 158|#ifdef USES_STRX
 159|int   BLex2_getJisIOType( BLex2*, int offset );
 160|int   BLex2_readHTML( BLex2*, BLex2_HTMLTags* tags, StrX_Mem* mem,
 161|  char** mem2, size_t mem2_sizeof );
 162|#endif
 163|#ifdef USES_ARRX
 164|char* BLex2_strfind_ArrX( BLex2*, ArrX* );
 165|#endif /* USES_ARRX */
 166|
 167|/* 記録バッファ */
 168|#ifdef USES_ARRX
 169|void  BLex2_setRecBuf( BLex2*, void* mem, size_t mem_sizeof );
 170|ArrX_Buf*  BLex2_getRecBuf( BLex2* );
 171|void  BLex2_recToEmpty( BLex2* );
 172|void  BLex2_recStart( BLex2* );
 173|ArrX_Buf*  BLex2_recEnd( BLex2* );
 174|bool  BLex2_isRec( BLex2* );
 175|char* BLex2_refRec( BLex2* );
 176|int   BLex2_getLeftSize( BLex2* );
 177|#endif /* USES_ARRX */
 178|
 179|/* 数値 */
 180|int   BLex2_readInt( BLex2* );
 181|int   BLex2_readInt16( BLex2* );
 182|bool  BLex2_isUnsignedDouble( BLex2* );
 183|bool  BLex2_isDouble( BLex2* );
 184|double  BLex2_readDouble( BLex2* );
 185|
 186|/* C/C++ 言語関連 */
 187|int   BLex2_skipC( BLex2* );
 188|int   BLex2_skipCLine( BLex2* );
 189|bool  BLex2_isCIdent( BLex2* );
 190|void  BLex2_readCIdent( BLex2*, char* ident, size_t ident_sizeof );
 191|void  BLex2_copyCIdent( BLex2*, char* ident, size_t ident_sizeof );
 192|int   BLex2_getCIdentSize( BLex2* );
 193|bool BLex2_isCComment( BLex2* );
 194|int  BLex2_skipCComment( BLex2* );
 195|bool BLex2_skipCCommentNest( BLex2*, int* lines );
 196|bool BLex2_isCPPComment( BLex2* );
 197|int  BLex2_skipCPPComment( BLex2* );
 198|
 199|/* 内部処理 */
 200|void  BLex2_fillBuf( BLex2*, int len );
 201|
 202|
 203| 
 204|/********************************************************************
 205|*  5. <<< トークン定数 [BLex2_Token] >>> 
 206|********************************************************************/
 207|/* ソースの始めの方の上の START_OF_COMPONE_HEADER 節に書かれています */
 208|
 209| 
 210|/********************************************************************
 211|*  6. <<< 文字列比較関数型 [BLex_FStrNCmp] >>> 
 212|*【補足】
 213|*・最初の n 文字だけ判定する関数の型です。
 214|*   返り値が 0より大なら s1 > s2
 215|*   返り値が 0なら       s1 == s2
 216|*   返り値が 0より小なら s1 < s2
 217|********************************************************************/
 218|/* ソースの始めの方の上の START_OF_COMPONE_HEADER 節に書かれています */
 219|
 220|
 221| 
 222|/********************************************************************
 223|*  7. <<< [BLex2_Pos] ファイルポインタ >>> 
 224|*【補足】
 225|*・BLex2_getPos, BLex2_setPos から BLex2 と協調します。
 226|********************************************************************/
 227|int   BLex2_Pos_cmp( BLex2_Pos*, BLex2_Pos* );
 228|void  BLex2_Pos_copy( BLex2_Pos*, BLex2_Pos* );
 229| 
 230|/********************************************************************
 231|*  8. <<< HTML タグのサブレックス [BLex2_HTMLTags] >>> 
 232|*【役割】
 233|*・BLex2 と連携して、HTML タグの構文解析をします。
 234|*・BLex2_readHTML 関数を参照してください。
 235|********************************************************************/
 236|struct _BLex2_HTMLTags {
 237|  char*   name;        /* タグ名(メイン)・小文字のみ */
 238|  char**  attr_names;  /* 属性名(サブ)の配列・小文字のみ */
 239|  char**  attr_vars;   /* 属性の値の配列 */
 240|  int     attr_n;      /* 属性の数 */
 241|};
 242|
 243|char*  BLex2_HTMLTags_getName( BLex2_HTMLTags* );
 244|char*  BLex2_HTMLTags_getVar( BLex2_HTMLTags*, const char* attr_name );
 245|void  BLex2_HTMLTags_print( BLex2_HTMLTags* );
 246|void  BLex2_HTMLTags_fprint( BLex2_HTMLTags*, FILE* out );
 247|
 248|
 249| 
 250|/********************************************************************
 251|*  9. <<< 行のサブレックス [BLex2_Line] >>> 
 252|*【補足】
 253|*・'\n' をセンテンス・トークンとして扱います。
 254|*・行頭、行末、カラム数、行番号を管理します。
 255|*・bLast は、次の文字が改行文字の場合に true になります。
 256|*・参照:サブレックス [BLex2_SubLex]
 257|********************************************************************/
 258|struct _BLex2_Line {
 259|  int   lineNum;   /* 行番号(1から) */
 260|  int   column;    /* カラム数(1から)またはトークン数 */
 261|  bool  bFirst;    /* 行頭かどうか */
 262|  bool  bLast;     /* 行末かどうか(→補足) */
 263|};
 264|void  BLex2_Line_init( BLex2_Line* );
 265|void  BLex2_Line_fetch( BLex2_Line*, BLex2* );
 266|void  BLex2_Line_nextFetch( BLex2_Line*, BLex2* );
 267|
 268|
 269| 
 270|/********************************************************************
 271|*  10. <<< 括弧のサブレックス [BLex2_Kakko] >>> 
 272|*【補足】
 273|*・( ), { }, [ ] の3種類の括弧をセンテンス・トークンとして扱います。
 274|*・level が typeStack_m より多くなった場合、typeStack に
 275|*  格納しないで、level が増えます。
 276|*・typeStack は、スタックポインタです。
 277|*  スタックの内容は、括弧開始文字、行、カラムの集合です
 278|*・参照:サブレックス [BLex2_SubLex]
 279|********************************************************************/
 280|struct _BLex2_Kakko {
 281|  int    level;        /* 括弧の深さ,全ての括弧を合わせた値 */
 282|  int*   typeStack;    /* 入っている括弧の文字(タイプ)(→補足)*/
 283|  int    typeStack_m;  /* typeStack に格納できる最大の文字数 */
 284|};
 285|void  BLex2_Kakko_init( BLex2_Kakko*, int* typeStack,
 286|  int typeStack_m );
 287|void  BLex2_Kakko_fetch( BLex2_Kakko*, BLex2*, BLex2_Line* );
 288|void  BLex2_Kakko_nextFetch( BLex2_Kakko*, BLex2* lex, BLex2_Line* line );
 289|
 290|
 291| 
 292|/********************************************************************
 293|*  11. <<< C言語のコメントのサブレックス [BLex2_CComment] >>> 
 294|*【補足】
 295|*・コメントのネストは対応していません。
 296|*・BLex2_C は /*, */、BLex2_CPP は//です。
 297|********************************************************************/
 298|struct _BLex2_CComment {
 299|  int    level;        /* コメントの深さ */
 300|  int    type;         /* コメントのタイプ(BLex2_C,BLex2_CPP)*/
 301|};
 302|void  BLex2_CComment_init( BLex2_CComment* );
 303|void  BLex2_CComment_fetch( BLex2_CComment*, BLex2* );
 304|void  BLex2_CComment_nextFetch( BLex2_CComment*, BLex2* );
 305|enum { BLex2_C, BLex2_CPP };
 306|
 307|
 308| 
 309|/********************************************************************
 310|*  12. <<< C言語の文字列のサブレックス [BLex2_CStr] >>> 
 311|*【補足】
 312|*・'、"、\" をセンテンス・トークンとして扱います。
 313|*・文字列中の \" を '"' 文字とします
 314|*・ダブルクォーテーション文字は、文字列センテンスの中とします。
 315|********************************************************************/
 316|struct _BLex2_CStr {
 317|  bool   bInChar;    /* 文字中かどうか */
 318|  bool   bInStr;     /* 文字列中かどうか */
 319|  bool   bInNow;     /* 今、文字/文字列中に入ったばかりかどうか */
 320|};
 321|void  BLex2_CStr_init( BLex2_CStr* );
 322|void  BLex2_CStr_fetch( BLex2_CStr*, BLex2* );
 323|void  BLex2_CStr_nextFetch( BLex2_CStr*, BLex2* );
 324|
 325|
 326| 
 327|/*----------------------------------------------------------------------*/
 328|/* 13. <<< Implement Area --------------------------------------------- >>> */ 
 329|/*----------------------------------------------------------------------*/
 330|
 331|
 332| 
 333|/************************************************************************
 334|*  14. <<< strncmp で文字列と比較  kword の文字数だけ比較 [BLex2_strncmp()] >>> 
 335|*【引数】
 336|*  ・kword : 比較する文字列  文字数はバッファの大きさを越えないこと
 337|*  ・返り値 : strcmp と同じ
 338|*【補足】
 339|*・int  BLex2_strncmp( BLex2*, char* kword ); に相当します。
 340|*************************************************************************/
 341|#define  BLex2_strncmp( th, kw )  BLex2_strncmpX( th, kw, (BLex_FStrnmp)strncmp )
 342|
 343| 
 344|/************************************************************************
 345|*  15. <<< 記録中かどうかを取得する [BLex2_isRec] >>> 
 346|*************************************************************************/
 347|#define  BLex2_isRec( this )  ( (this)->recF )
 348|
 349| 
 350|/************************************************************************
 351|*  16. <<< 記録した内容を参照する [BLex2_refRec] >>> 
 352|*【引数】
 353|*  ・char*  返り値;    記録した内容(末尾に'\0'文字あり)
 354|*************************************************************************/
 355|#define  BLex2_refRec( this )   ((char*)((this)->recBuf.first))
 356|
 357| 
 358|/************************************************************************
 359|*  17. <<< 記録バッファの残りサイズを取得する [BLex2_getLeftSize] >>> 
 360|*************************************************************************/
 361|#define   BLex2_getLeftSize( this )   ( ArrX_Buf_getLeftSize(&(this)->recBuf,char) )
 362|
 363| 
 364|/************************************************************************
 365|*  18. <<< [BLex2_Pos_copy] アドレスを代入する >>> 
 366|*【補足】
 367|*・void  BLex2_Pos_copy( BLex2_Pos*, BLex2_Pos* );
 368|*************************************************************************/
 369|#define   BLex2_Pos_copy( dst, src )\
 370|  ( *(dst) = *(src) )
 371|
 372| 
 373|/************************************************************************
 374|*  19. <<< HTML のタグ名(メイン)を参照する [BLex2_HTMLTags_getName] >>> 
 375|*************************************************************************/
 376|#define  BLex2_HTMLTags_getName( this )   ((this)->name)
 377|
 378|
 379|/************************************************************************
 380|*  20. <<< [BLex2_Kakko_getStackSize] 初期化に必要なメモリサイズを返す >>>
 381|*【引数】
 382|*  ・int  mLevel;  判定できる深さの最大段数
 383|*************************************************************************/
 384|#define  BLex2_Kakko_getStackSize( mLevel )     ((mLevel) * sizeof(int)*3)
 385|
 386| 
 387|#ifdef __cplusplus
 388|}
 389|#endif
 390|
 391|#endif 
 392| 
 393|