Example #1
0
 public void Restore(CppWordReader wr)
 {
     wr.lreader    = this.lreader;
     wr.cword      = this.cword;
     wr.line_count = this.line_count;
     wr.wtype      = this.wtype;
 }
Example #2
0
 public State(CppWordReader wr)
 {
     this.lreader    = wr.lreader;
     this.cword      = wr.cword;
     this.line_count = wr.line_count;
     this.wtype      = wr.wtype;
 }
Example #3
0
        public string Instantiate(string[] args, int line, string file)
        {
            _debug_.PreprocessorAssert(args.Length == parameters.Length, "実引数の長さと仮引数の長さの不整合");

            // a. 識別子を置換
            // b. #@ → 文字化
            // c. #& → 文字列化
            // d. ## トークン接続
            // ■※■ aa##aa 等には対応する。aa ## aa には対応しない。
            System.Text.StringBuilder buf = new System.Text.StringBuilder();
            CppWordReader             wr  = CppWordReader.MacroContentMode(content);

            const int MODE_NON = 0;
            const int MODE_STR = 1;
            const int MODE_CHR = 2;
            int       mode     = MODE_NON;

            while (wr.ReadNext())
            {
                string word = wr.CurrentWord;
                switch (wr.CurrentType.value)
                {
                case WordType.vIdentifier:
                    int i = IndexOfPrameter(word);
                    if (i >= 0)
                    {
                        word = args[i];
                    }
                    goto default;

                case WordType.vOperator:
                    if (mode != MODE_NON)
                    {
                        goto default;
                    }
                    if (word == "#&")
                    {
                        mode = MODE_STR;
                        break;
                    }
                    else if (word == "#@")
                    {
                        mode = MODE_CHR;
                        break;
                    }
                    else if (word == "##")
                    {
                        break;
                    }
                    goto default;

                case WordType.vSpace:
                case WordType.vComment:
                    buf.Append(wr.CurrentWord);
                    break;

                default:
                    if (mode == MODE_CHR)
                    {
                        buf.Append(afh.Text.TextUtils.Characterize(word));
                    }
                    else if (mode == MODE_STR)
                    {
                        buf.Append(afh.Text.TextUtils.Stringize(word));
                    }
                    else
                    {
                        buf.Append(word);
                    }
                    mode = MODE_NON;
                    break;
                }
            }
            //--------------------------------------------------------
            buf.AppendLine();
            if (file != null)
            {
                buf.AppendFormat("#line {0} {1}\r\n", line, file);
            }
            return(buf.ToString());
        }
Example #4
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;
        }