Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="c">REG_DECL に依る一致結果を指定します。</param>
        /// <param name="content">マクロの内容を指定します。</param>
        public DefineMacro(Rgx.Capture c, string content)
        {
            this.name = c.Groups["name"].Last.Value;

            int nParam = c.Groups["param"].Count;

            this.parameters = new string[nParam];
            for (int i = 0; i < nParam; i++)
            {
                parameters[i] = c.Groups["param"][i].Value;
            }

            this.content = content;
        }
Пример #2
0
        //============================================================
        //		マクロ処理
        //============================================================
        private DefineMacro ReadDefineMacro(Rgx.Capture c)
        {
            System.Text.StringBuilder buf = new System.Text.StringBuilder();
            if (filename != null)
            {
                buf.AppendFormat("\r\n#line {0} {1}\r\n", wr.LineNumber + 1, filename);
            }

            int nest = 0;

            while (wr.ReadNext())
            {
                switch (wr.CurrentType.value)
                {
                case WordType.vTag:
                    if (DefineMacro.REG_BEGIN.IsMatching(wr.CurrentWord))
                    {
                        nest++;
                    }
                    else if (DefineMacro.REG_END.IsMatching(wr.CurrentWord))
                    {
                        if (nest-- == 0)
                        {
                            goto break_loop;
                        }
                    }
                    goto default;

                default:
                    buf.Append(wr.CurrentWord);
                    break;
                }
            }
break_loop:
            string content = buf.ToString();

            if (filename != null)
            {
                this.buf.AppendFormat("#line {0} {1}", wr.LineNumber + 1, filename);
            }

            return(new DefineMacro(c, content));
        }
Пример #3
0
        private void process(string text)
        {
            CppWordReader wr_orig = this.wr;

            this.wr = new CppWordReader(text);
            while (wr.ReadNext())
            {
                switch (wr.CurrentType.value)
                {
                case WordType.vKeyWord:
                    // @ マクロ
                    break;

                case WordType.vTag:
                    // # マクロ
                    // #line
                {
                    Rgx.Capture c = Rgx.Match(@"#:bline:b((?<line>\d+):b)?(?<filename>:"")", wr.CurrentWord);
                    if (c != null)
                    {
                        filename = c.Groups["filename"].Last.Value;
                        if (c.Groups["line"].Count > 0)
                        {
                            int line;
                            if (int.TryParse(c.Groups["line"].Last.Value, out line))
                            {
                                wr.LineNumber = line - 1;
                            }
                        }
                        buf.AppendFormat("#line {0} {1}", wr.LineNumber + 1, filename);
                        break;
                    }
                }
                    // #pragma begin(define)
                    {
                        Rgx.Capture c = DefineMacro.REG_DECL.Match(wr.CurrentWord);
                        if (c != null)
                        {
                            DefineMacro macro = this.ReadDefineMacro(c);
                            if (!macros.ContainsKey(macro.Name))
                            {
                                macros.Add(macro.Name, macro);
                                break;
                            }
                            else
                            {
                                buf.AppendLine();
                                buf.AppendLine("#error マクロ " + macro.Name + " は既に定義されています");
                                buf.AppendFormat("#line {0} {1}", wr.LineNumber + 1, filename);
                            }
                        }
                    }
                    // #pragma undef
                    {
                        Rgx.Capture c = DefineMacro.REG_UNDEF.Match(wr.CurrentWord);
                        if (c != null)
                        {
                            string name = c.Groups["name"].Last.Value;
                            if (macros.Remove(name))
                            {
                                break;
                            }
                            buf.AppendLine("#error " + name + " は定義されていないマクロです");
                            buf.AppendFormat("#line {0} {1}\r\n", wr.LineNumber + 1, filename);
                        }
                    }
                    goto default;

                case WordType.vIdentifier:
                    if (!macros.ContainsKey(wr.CurrentWord))
                    {
                        goto default;
                    }
                    this.ReadMacroRef(wr.CurrentWord);
                    break;

                case WordType.vComment:
                default:
                    buf.Append(wr.CurrentWord);
                    break;
                }
            }
            this.wr = wr_orig;
        }