Esempio n. 1
0
        /// <summary>
        /// 登録されたパターンを使ってトークンに分割する.
        /// マッチに失敗した場合, ParseException 例外を発生させる.
        /// </summary>
        /// <param name="text">処理対象の文字列.</param>
        /// <returns>トークンのリスト.</returns>
        public TokenList <TokenType> Tokenize(string text)
        {
            // 戻り値
            TokenList <TokenType> tokens = new TokenList <TokenType>();

            if (text == null || text == string.Empty)
            {
                return(tokens);
            }

            // コンテキストオブジェクトを作成する
            Context context = new Context();

            context.Text = text;

            // マッチパターンの準備
            context.Patterns = new TokenPattern <TokenType> [patterns.Count];
            patterns.CopyTo(context.Patterns);

            // 改行位置をチェックしておく
            context.LineIndexList = CreateLineIndexList(text);

            // Sleep() を定期的に入れる用
            int loopCount = 0;
            int sleepWait = SleepWait;

            // テキストの終わりまでマッチを試す
            while (context.Index < text.Length)
            {
                // 登録されたパターンでマッチを試す
                TokenMatch <TokenType> tokenMatch = TryMatchToken(context);

                // マッチしなかった時は, 例外を投げる
                if (tokenMatch == null)
                {
                    ThrowParseException(context);
                }

                // マッチした場合

                // インデックスの位置を動かしておく
                int matchLength = tokenMatch.Match.Length;
                context.Index     += matchLength;
                context.LineIndex += matchLength;

                if (BeforeAddToken != null)
                {
                    // 追加前イベントが登録されている場合は実行する
                    // false が返ってきた場合はトークンリストに追加しない
                    BeforeAddTokenEventArgs <TokenType> args
                        = new BeforeAddTokenEventArgs <TokenType>(tokenMatch);
                    BeforeAddToken(this, args);
                    if (args.Cancel == false)
                    {
                        AddToken(tokens, tokenMatch);
                    }
                }
                else
                {
                    // トークンをリストに追加する
                    AddToken(tokens, tokenMatch);
                }

                // 改行位置に到達した時
                if (context.LineIndexList[context.LineNumber] == context.Index)
                {
                    context.LineIndex = 1;
                    context.LineNumber++;
                }

                // Sleep() を定期的に入れる
                if (sleepWait > 0)
                {
                    loopCount++;
                    if (loopCount > sleepWait)
                    {
                        loopCount = 0;
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }

            return(tokens);
        }
Esempio n. 2
0
 /// <summary>
 /// コンストラクタ.
 /// </summary>
 /// <param name="tokenList">処理中のトークンのリスト.</param>
 /// <param name="token">追加されたトークン.</param>
 public TokenAddedEventArgs(TokenList <TokenType> tokenList, Token <TokenType> token)
 {
     this.TokenList = tokenList;
     this.Token     = token;
 }