Exemple #1
0
 public StrFormWord(string[] s, SubWord[] SWT)
 {
     strs = s; subwords = SWT;
 }
        /// <summary>
        /// @"などの直後からの開始
        /// return時にはendWithの文字がCurrentになっているはず。終端の適切さの検証は呼び出し元が行う。
        /// </summary>
        /// <returns></returns>
        public static StrFormWord AnalyseFormattedString(StringStream st, FormStrEndWith endWith, bool trim)
        {
            List<string> strs = new List<string>();
            List<SubWord> SWTs = new List<SubWord>();
            StringBuilder buffer = new StringBuilder(100);
            while (true)
            {
                char cur = st.Current;
                switch (cur)
                {
                    case '\n':
                    case '\0':
                        goto end;
                    case '\"':
                        if (endWith == FormStrEndWith.DoubleQuotation)
                            goto end;
                        buffer.Append(cur);
                        break;
                    case '#':
                        if (endWith == FormStrEndWith.Sharp)
                            goto end;
                        buffer.Append(cur);
                        break;
                    case ',':
                        if ((endWith == FormStrEndWith.Comma) || (endWith == FormStrEndWith.LeftParenthesis_Bracket_Comma_Semicolon))
                            goto end;
                        buffer.Append(cur);
                        break;
                    case '(':
                    case '[':
                    case ';':
                        if (endWith == FormStrEndWith.LeftParenthesis_Bracket_Comma_Semicolon)
                            goto end;
                        buffer.Append(cur);
                        break;
                    case '%':
                        strs.Add(buffer.ToString());
                        buffer.Remove(0, buffer.Length);
                        st.ShiftNext();
                        SWTs.Add(new PercentSubWord(Analyse(st, LexEndWith.Percent, false, false)));
                        if (st.Current != '%')
                            throw new CodeEE("\'%\'が使われましたが対応する\'%\'が見つかりません");
                        break;
                    case '{':
                        strs.Add(buffer.ToString());
                        buffer.Remove(0, buffer.Length);
                        st.ShiftNext();
                        SWTs.Add(new CurlyBraceSubWord(Analyse(st, LexEndWith.RightCurlyBrace, false, false)));
                        if (st.Current != '}')
                            throw new CodeEE("\'{\'が使われましたが対応する\'}\'が見つかりません");
                        break;
                    case '*':
                    case '+':
                    case '=':
                    case '/':
                    case '$':
                        if (st.TripleSymbol())
                        {
                            strs.Add(buffer.ToString());
                            buffer.Remove(0, buffer.Length);
                            st.Jump(3);
                            SWTs.Add(new TripleSymbolSubWord(cur));
                            continue;
                        }
                        else
                            buffer.Append(cur);
                        break;
                    case '\\'://エスケープ文字の使用

                        st.ShiftNext();
                        cur = st.Current;
                        switch (cur)
                        {
                            case '\0':
                                throw new CodeEE("エスケープ文字\\の後に文字がありません");
                            case '\n': break;
                            case 's': buffer.Append(' '); break;
                            case 'S': buffer.Append(' '); break;
                            case 't': buffer.Append('\t'); break;
                            case 'n': buffer.Append('\n'); break;
                            case '@'://\@~~?~~#~~\@
                                {
                                    if ((endWith == FormStrEndWith.YenAt) || (endWith == FormStrEndWith.Sharp))
                                        goto end;
                                    strs.Add(buffer.ToString());
                                    buffer.Remove(0, buffer.Length);
                                    st.ShiftNext();
                                    SWTs.Add(AnalyseYenAt(st));
                                    continue;
                                }
                            default:
                                buffer.Append(cur);
                                st.ShiftNext();
                                continue;
                        }
                        break;
                    default:
                        buffer.Append(cur);
                        break;
                }
                st.ShiftNext();
            }
            end:
            strs.Add(buffer.ToString());

            string[] retStr = new string[strs.Count];
            SubWord[] retSWTs = new SubWord[SWTs.Count];
            strs.CopyTo(retStr);
            SWTs.CopyTo(retSWTs);
            if (trim && retStr.Length > 0)
            {
                retStr[0] = retStr[0].TrimStart(new char[] { ' ', '\t' });
                retStr[retStr.Length - 1] = retStr[retStr.Length - 1].TrimEnd(new char[] { ' ', '\t' });
            }
            return new StrFormWord(retStr, retSWTs);
        }