Inheritance: CiSymbol
Example #1
0
        CiMacro ParseMacro()
        {
            CiMacro macro = new CiMacro();

            macro.Name = ParseId();
            Expect(CiToken.LeftParenthesis);
            List <string> paramz = new List <string>();

            if (See(CiToken.Id))
            {
                do
                {
                    string name = ParseId();
                    if (paramz.Contains(name))
                    {
                        throw new ParseException("Duplicate macro parameter {0}", name);
                    }
                    paramz.Add(name);
                } while (Eat(CiToken.Comma));
            }
            Expect(CiToken.RightParenthesis);
            macro.Params = paramz.ToArray();
            StringBuilder sb = new StringBuilder();

            this.CopyTo = sb;
            try {
                if (See(CiToken.LeftParenthesis))
                {
                    sb.Append('(');
                    ParseBody(CiToken.LeftParenthesis, CiToken.RightParenthesis);
                }
                else if (See(CiToken.LeftBrace))
                {
                    ParseBody(CiToken.LeftBrace, CiToken.RightBrace);
                    Trace.Assert(sb[sb.Length - 1] == '}');
                    sb.Length--;
                    macro.IsStatement = true;
                }
                else
                {
                    throw new ParseException("Macro definition must be wrapped in parentheses or braces");
                }
            }
            finally {
                this.CopyTo = null;
            }
            macro.Body = sb.ToString();
            NextToken();
            return(macro);
        }
Example #2
0
        void Expand(CiMacro macro)
        {
            Dictionary <string, string> args = new Dictionary <string, string>();
            StringBuilder sb = new StringBuilder();

            this.CopyTo = sb;
            try {
                Expect(CiToken.LeftParenthesis);
                bool first = true;
                foreach (string name in macro.Params)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        Expect(CiToken.Comma);
                    }
                    ParseArg();
                    char c = sb[sb.Length - 1];
                    Trace.Assert(c == ',' || c == ')');
                    sb.Length--;
                    args.Add(name, sb.ToString().Trim());
                    sb.Length = 0;
                }
            }
            finally {
                this.CopyTo = null;
            }
            Check(CiToken.RightParenthesis);
            if (macro.IsStatement)
            {
                NextToken();
                Check(CiToken.Semicolon);
            }
            BeginExpand("macro " + macro.Name, macro.Body, args);
            NextToken();
        }
Example #3
0
 CiMacro ParseMacro()
 {
     CiMacro macro = new CiMacro();
     macro.Name = ParseId();
     Expect(CiToken.LeftParenthesis);
     List<string> paramz = new List<string>();
     if (See(CiToken.Id)) {
     do {
         string name = ParseId();
         if (paramz.Contains(name))
             throw new ParseException("Duplicate macro parameter {0}", name);
         paramz.Add(name);
     } while (Eat(CiToken.Comma));
     }
     Expect(CiToken.RightParenthesis);
     macro.Params = paramz.ToArray();
     StringBuilder sb = new StringBuilder();
     this.CopyTo = sb;
     try {
     if (See(CiToken.LeftParenthesis)) {
         sb.Append('(');
         ParseBody(CiToken.LeftParenthesis, CiToken.RightParenthesis);
     }
     else if (See(CiToken.LeftBrace)) {
         ParseBody(CiToken.LeftBrace, CiToken.RightBrace);
         Trace.Assert(sb[sb.Length - 1] == '}');
         sb.Length--;
         macro.IsStatement = true;
     }
     else
         throw new ParseException("Macro definition must be wrapped in parentheses or braces");
     }
     finally {
     this.CopyTo = null;
     }
     macro.Body = sb.ToString();
     NextToken();
     return macro;
 }
Example #4
0
 void Expand(CiMacro macro)
 {
     Dictionary<string, string> args = new Dictionary<string, string>();
     StringBuilder sb = new StringBuilder();
     this.CopyTo = sb;
     try {
     Expect(CiToken.LeftParenthesis);
     bool first = true;
     foreach (string name in macro.Params) {
         if (first)
             first = false;
         else
             Expect(CiToken.Comma);
         ParseArg();
         char c = sb[sb.Length - 1];
         Trace.Assert(c == ',' || c == ')');
         sb.Length--;
         args.Add(name, sb.ToString().Trim());
         sb.Length = 0;
     }
     }
     finally {
     this.CopyTo = null;
     }
     Check(CiToken.RightParenthesis);
     if (macro.IsStatement) {
     NextToken();
     Check(CiToken.Semicolon);
     }
     BeginExpand("macro " + macro.Name, macro.Body, args);
     NextToken();
 }