示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Process"></param>
        /// <param name="Affirmative"></param>
        private void ParseDirectiveIfdef(bool Process, bool Affirmative)
        {
            //Console.WriteLine("[1]");
            Tokens.ExpectCurrentAndMoveNextNoSpace("ifdef", "ifndef");
            Tokens.ExpectCurrentType(CTokenType.Identifier);
            var Identifier = Tokens.Current.Raw;

            ReadTokensUntilLineEnd();
            bool IsDefined = (Context.Macros.ContainsKey(Identifier));

            //Console.WriteLine("[2]");

            if (!Affirmative)
            {
                IsDefined = !IsDefined;
            }

            ParseFile(Process && IsDefined);
            if (Tokens.Current.Raw == "else")
            {
                ReadTokensUntilLineEnd();
                ParseFile(Process && !IsDefined);
            }
            //Console.WriteLine("[3]");

            Tokens.ExpectCurrentAndMoveNextNoSpace("endif");

            //Console.WriteLine("[4]");

            //throw(new NotImplementedException());
            //ParseFile();
        }
示例#2
0
        /// <summary>
        /// TODO: Have to refactor ParseIdentifier + Expact. They have repeated code!!!
        /// </summary>
        private void ParseIdentifier(CTokenReader Tokens)
        {
            Tokens.ExpectCurrentType(CTokenType.Identifier);

            var Identifier = Tokens.Current.Raw;

            Tokens.MoveNextSpace();

            if (Context.Macros.ContainsKey(Identifier))
            {
                var Macro         = Context.Macros[Identifier];
                var MacroFunction = Context.Macros[Identifier] as MacroFunction;

                if (MacroFunction != null)
                {
                    if (Tokens.Current.Type == CTokenType.Space)
                    {
                        Tokens.MoveNextNoSpace();
                    }

                    if (Tokens.Current.Raw != "(")
                    {
                        throw (new Exception(String.Format("Trying to use a function-like macro without calling it? MACRO: {0}, Token: {1}", Identifier, Tokens.Current)));
                    }
                }

                if (MacroFunction != null)
                {
                    var Parameters = ParseParameterList(Tokens, JustIdentifiers: false);
                    for (int n = 0; n < Parameters.Length; n++)
                    {
                        //Console.WriteLine("  {0}", Parameters[n]);
                        Parameters[n] = Expand(Parameters[n], null, null);
                        //Console.WriteLine("    -> {0}", Parameters[n]);
                    }
                    var Map = MapFunctionParameters(MacroFunction.Parameters, Parameters);

                    Identifier = Expand(MacroFunction.Replacement, Map, new HashSet <string>(new[] { Identifier }));
                }
                else
                {
                    var MacroConstant = Macro as MacroConstant;

                    Identifier = Expand(MacroConstant.Replacement, null, new HashSet <string>(new[] { Identifier }));
                    //Console.WriteLine("a: {0}", MacroConstant.Replacement);
                }
            }
            else
            {
                //Identifier = Identifier;
            }

            Context.TextWriter.Write(ReplaceSimpleVariable(Identifier));
        }