Example #1
0
        private string Evaluate(string text)
        {
            PreprocTokenizer preproc = new PreprocTokenizer(text);
            for (int index = 0; index < preproc.Count; ++index)
            {
                string token = preproc[index];
                if (Processor.PreProcMacros.ContainsKey(token))
                {
                    PreprocMacro macro = Processor.PreProcMacros[token];
                    if (macro.HasArguments)
                    {
                        // check to see if the arguments are found
                        Dictionary<string, string> ArgumentValues = new Dictionary<string, string>();
                        int nNextIndex = ExtractArguments(macro, preproc, index + 2, ArgumentValues);
                        if (nNextIndex > 0)
                        {
                            while (nNextIndex >= index)
                            {
                                preproc.RemoveAt(nNextIndex--);
                            }

                            // insert definition of macro, replacing the arguments as specified
                            macro.Expand(preproc, index, ArgumentValues);
                        }
                        else
                        {
                            Trace.TraceError("Warning: Unable to expand macro.\r\nText = '{0}'.\r\nFile = {1} [Line {2}]", text, Filename, Line);
                            return text;
                        }
                    }
                    else
                    {
                        preproc[index] = macro.Definition;
                    }
                }
            }
            return preproc.Join();
        }
Example #2
0
        private void StopRecordingMacro()
        {
            if (IsRecordingPreProcMacro)
            {
                if (MacroText == "")
                {
                    MacroText = Data.Substring(StartOfMacro, ReadIndex - StartOfMacro);
                }
                else
                {
                    MacroText = MacroText + Data.Substring(StartOfMacro, ReadIndex - StartOfMacro);
                }

                if (MacroText.EndsWith("\\"))
                {
                    if (Data[ReadIndex] == '\r')
                        ++ReadIndex;

                    MacroText = MacroText.Remove(MacroText.Length - 1);
                    StartOfMacro = ReadIndex + 1;
                    return;
                }

                IsRecordingPreProcMacro = false;

                // better: tokenize whole line now, feed to classes
                Tokens = new PreprocTokenizer(MacroText);

                if (IsLineIncluded())
                {
                    if (MacroText.StartsWith("#define"))
                    {
                        PreProc_Define();
                    }
                    else if (MacroText.StartsWith("#ifndef"))
                    {
                        PreProc_Ifndef();
                    }
                    else if (MacroText.StartsWith("#ifdef"))
                    {
                        PreProc_Ifdef();
                    }
                    else if (MacroText.StartsWith("#else"))
                    {
                        PreProc_Else();
                    }
                    else if (MacroText.StartsWith("#endif"))
                    {
                        PreProc_Endif();
                    }
                    else if (MacroText.StartsWith("#if"))
                    {
                        PreProc_If();
                    }
                    else if (MacroText.StartsWith("#elif"))
                    {
                        PreProc_ElseIf();
                    }
                    else if (MacroText.StartsWith("#undef"))
                    {
                        PreProc_ElseIf();
                    }
                    else if (MacroText.StartsWith("#include"))
                    {
                        PreProc_Include();
                    }
                    else if (MacroText.StartsWith("#pragma"))
                    {
                        PreProc_Pragma();
                    }
                    else if (MacroText.StartsWith("#line"))
                    {
                        // do nothing
                    }
                    else if (MacroText.StartsWith("#error"))
                    {
                        // do nothing
                    }
                    else if ((Tokens.Count > 2) && (Tokens[1] == "define"))
                    {
                        // do nothing
                    }
                    else
                    {
                        Trace.TraceError("unknown preproc cmd: <{0}>", MacroText);
                    }
                }
                else
                {
                    if (MacroText.StartsWith("#ifndef"))
                    {
                        PreProc_Ifndef();
                    }
                    else if (MacroText.StartsWith("#ifdef"))
                    {
                        PreProc_Ifdef();
                    }
                    else if (MacroText.StartsWith("#else"))
                    {
                        PreProc_Else();
                    }
                    else if (MacroText.StartsWith("#endif"))
                    {
                        PreProc_Endif();
                    }
                    else if (MacroText.StartsWith("#if"))
                    {
                        PreProc_If();
                    }
                    else if (MacroText.StartsWith("#elif"))
                    {
                        PreProc_ElseIf();
                    }
                    else
                    {
                        Trace.TraceInformation("Ignore '{0}' in line {1}", MacroText, Line);
                    }
                }
                MacroText = "";
            }
        }
Example #3
0
 private void PreProc_Pragma()
 {
     PreprocTokenizer tokens = new PreprocTokenizer(MacroText);
     if ((tokens.Count > 1) && (tokens[1] == "once"))
     {
         Processor.ReadOnceList.Add(Filename);
     }
 }