Esempio n. 1
0
        private ScriptDefinition CopyDefine(ScriptDefinition define)
        {
            ScriptDefinition newDefine = new Text.ScriptDefinition();

            newDefine.Name    = define.Name;
            newDefine.Flags   = define.Flags;
            newDefine.BuiltIn = define.BuiltIn;

            List <idToken> tokens     = new List <idToken>();
            List <idToken> parameters = new List <idToken>();

            // copy the define tokens
            newDefine.Tokens = new idToken[define.Tokens.Length];

            foreach (idToken token in define.Tokens)
            {
                tokens.Add(new idToken(token));
            }

            // copy the define parameters
            foreach (idToken token in define.Parameters)
            {
                parameters.Add(new idToken(token));
            }

            newDefine.Tokens     = tokens.ToArray();
            newDefine.Parameters = parameters.ToArray();

            return(newDefine);
        }
Esempio n. 2
0
 private void AddGlobalDefinesToSource()
 {
     foreach (KeyValuePair <string, ScriptDefinition> kvp in _globalDefines)
     {
         ScriptDefinition newDefine = CopyDefine(kvp.Value);
         AddDefineToHash(newDefine, _defineDict);
     }
 }
Esempio n. 3
0
        /*private bool ExpandDefineIntoSource(idToken token, ScriptDefinition define)
         * {
         *
         * idToken *firsttoken, *lasttoken;
         *
         * if ( !idParser::ExpandDefine( deftoken, define, &firsttoken, &lasttoken ) ) {
         * return false;
         * }
         * // if the define is not empty
         * if ( firsttoken && lasttoken ) {
         * firsttoken->linesCrossed += deftoken->linesCrossed;
         * lasttoken->next = idParser::tokens;
         * idParser::tokens = firsttoken;
         * }
         * return true;
         * }*/

        private int FindDefineParameter(ScriptDefinition define, string name)
        {
            int i = 0;

            foreach (idToken token in define.Parameters)
            {
                if (define.ToString() == name)
                {
                    return(i);
                }

                i++;
            }

            return(-1);
        }
Esempio n. 4
0
        private bool Directive_Define()
        {
            idToken          token, t;
            ScriptDefinition define;

            if ((token = ReadLine()) == null)
            {
                Error("#define without name");
                return(false);
            }
            else if (token.Type != TokenType.Name)
            {
                UnreadSourceToken(token);
                Error("expected name after #define, found '{0}'", token.ToString());

                return(false);
            }

            // check if the define already exists
            if (_defineDict.TryGetValue(token.ToString(), out define) == true)
            {
                if ((define.Flags & DefineFlags.Fixed) == DefineFlags.Fixed)
                {
                    Error("can't redefine '{0}'", token.ToString());
                    return(false);
                }

                Warning("redefinition of '{0}'", token.ToString());

                // unread the define name before executing the #undef directive
                UnreadSourceToken(token);

                if (Directive_UnDefine() == false)
                {
                    return(false);
                }

                // if the define was not removed (define->flags & DEFINE_FIXED)
                define = _defineDict[token.ToString()];
            }

            // allocate define
            define            = new ScriptDefinition();
            define.Name       = token.ToString();
            define.Parameters = new idToken[] { };
            define.Tokens     = new idToken[] { };

            // add the define to the source
            AddDefineToHash(define, _defineDict);

            // if nothing is defined, just return
            if ((token = ReadLine()) == null)
            {
                return(true);
            }


            // if it is a define with parameters
            if ((token.WhiteSpaceBeforeToken == 0) && (token.ToString() == "("))
            {
                List <idToken> parameters = new List <idToken>();

                // read the define parameters
                if (CheckTokenString(")") == false)
                {
                    while (true)
                    {
                        if ((token = ReadLine()) == null)
                        {
                            Error("expected define parameter");
                            return(false);
                        }
                        // if it isn't a name
                        else if (token.Type != TokenType.Name)
                        {
                            Error("invalid define parameter");
                            return(false);
                        }
                        else if (FindDefineParameter(define, token.ToString()) >= 0)
                        {
                            Error("two of the same define parameters");
                            return(false);
                        }

                        // add the define parm
                        t = new idToken(token);
                        t.ClearTokenWhiteSpace();

                        parameters.Add(t);

                        // read next token
                        if ((token = ReadLine()) == null)
                        {
                            Error("define parameters not terminated");
                            return(false);
                        }

                        if (token.ToString() == ")")
                        {
                            break;
                        }

                        // then it must be a comma
                        if (token.ToString() != ",")
                        {
                            Error("define not terminated");
                            return(false);
                        }
                    }
                }

                define.Parameters = parameters.ToArray();

                if ((token = ReadLine()) == null)
                {
                    return(true);
                }
            }

            List <idToken> tokens = new List <idToken>();

            do
            {
                t = new idToken(token);

                if ((t.Type == TokenType.Name) && (t.ToString() == define.Name))
                {
                    t.Flags |= TokenFlags.RecursiveDefine;
                    Warning("recursive define (removed recursion)");
                }

                t.ClearTokenWhiteSpace();

                tokens.Add(t);
            }while((token = ReadLine()) != null);

            define.Tokens = tokens.ToArray();

            if (define.Tokens.Length > 0)
            {
                // check for merge operators at the beginning or end
                if ((define.Tokens[0].ToString() == "##") || (define.Tokens[define.Tokens.Length - 1].ToString() == "##"))
                {
                    Error("define with misplaced ##");
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 5
0
 private void AddDefineToHash(ScriptDefinition define, Dictionary <string, ScriptDefinition> dict)
 {
     dict.Add(define.Name, define);
 }
Esempio n. 6
0
		private bool Directive_Define()
		{
			idToken token, t;
			ScriptDefinition define;

			if((token = ReadLine()) == null)
			{
				Error("#define without name");
				return false;
			}
			else if(token.Type != TokenType.Name)
			{
				UnreadSourceToken(token);
				Error("expected name after #define, found '{0}'", token.ToString());

				return false;
			}

			// check if the define already exists
			if(_defineDict.TryGetValue(token.ToString(), out define) == true)
			{
				if((define.Flags & DefineFlags.Fixed) == DefineFlags.Fixed)
				{
					Error("can't redefine '{0}'", token.ToString());
					return false;
				}

				Warning("redefinition of '{0}'", token.ToString());

				// unread the define name before executing the #undef directive
				UnreadSourceToken(token);

				if(Directive_UnDefine() == false)
				{
					return false;
				}

				// if the define was not removed (define->flags & DEFINE_FIXED)
				define = _defineDict[token.ToString()];
			}

			// allocate define
			define = new ScriptDefinition();
			define.Name = token.ToString();
			define.Parameters = new idToken[] { };
			define.Tokens = new idToken[] { };

			// add the define to the source
			AddDefineToHash(define, _defineDict);

			// if nothing is defined, just return
			if((token = ReadLine()) == null)
			{
				return true;
			}


			// if it is a define with parameters
			if((token.WhiteSpaceBeforeToken == 0) && (token.ToString() == "("))
			{
				List<idToken> parameters = new List<idToken>();

				// read the define parameters
				if(CheckTokenString(")") == false)
				{
					while(true)
					{
						if((token = ReadLine()) == null)
						{
							Error("expected define parameter");
							return false;
						}
						// if it isn't a name
						else if(token.Type != TokenType.Name)
						{
							Error("invalid define parameter");
							return false;
						}
						else if(FindDefineParameter(define, token.ToString()) >= 0)
						{
							Error("two of the same define parameters");
							return false;
						}

						// add the define parm
						t = new idToken(token);
						t.ClearTokenWhiteSpace();

						parameters.Add(t);

						// read next token
						if((token = ReadLine()) == null)
						{
							Error("define parameters not terminated");
							return false;
						}

						if(token.ToString() == ")")
						{
							break;
						}

						// then it must be a comma
						if(token.ToString() != ",")
						{
							Error("define not terminated");
							return false;
						}
					}
				}

				define.Parameters = parameters.ToArray();

				if((token = ReadLine()) == null)
				{
					return true;
				}
			}

			List<idToken> tokens = new List<idToken>();

			do
			{
				t = new idToken(token);

				if((t.Type == TokenType.Name) && (t.ToString() == define.Name))
				{
					t.Flags |= TokenFlags.RecursiveDefine;
					Warning("recursive define (removed recursion)");
				}

				t.ClearTokenWhiteSpace();

				tokens.Add(t);
			}
			while((token = ReadLine()) != null);

			define.Tokens = tokens.ToArray();

			if(define.Tokens.Length > 0)
			{
				// check for merge operators at the beginning or end
				if((define.Tokens[0].ToString() == "##") || (define.Tokens[define.Tokens.Length - 1].ToString() == "##"))
				{
					Error("define with misplaced ##");
					return false;
				}
			}

			return true;
		}
Esempio n. 7
0
		private ScriptDefinition CopyDefine(ScriptDefinition define)
		{
			ScriptDefinition newDefine = new Text.ScriptDefinition();
			newDefine.Name = define.Name;
			newDefine.Flags = define.Flags;
			newDefine.BuiltIn = define.BuiltIn;

			List<idToken> tokens = new List<idToken>();
			List<idToken> parameters = new List<idToken>();

			// copy the define tokens
			newDefine.Tokens = new idToken[define.Tokens.Length];

			foreach(idToken token in define.Tokens)
			{
				tokens.Add(new idToken(token));
			}

			// copy the define parameters
			foreach(idToken token in define.Parameters)
			{
				parameters.Add(new idToken(token));
			}

			newDefine.Tokens = tokens.ToArray();
			newDefine.Parameters = parameters.ToArray();

			return newDefine;
		}
Esempio n. 8
0
		private void AddDefineToHash(ScriptDefinition define, Dictionary<string, ScriptDefinition> dict)
		{
			dict.Add(define.Name, define);
		}
Esempio n. 9
0
		/*private bool ExpandDefineIntoSource(idToken token, ScriptDefinition define)
		{

	idToken *firsttoken, *lasttoken;

	if ( !idParser::ExpandDefine( deftoken, define, &firsttoken, &lasttoken ) ) {
		return false;
	}
	// if the define is not empty
	if ( firsttoken && lasttoken ) {
		firsttoken->linesCrossed += deftoken->linesCrossed;
		lasttoken->next = idParser::tokens;
		idParser::tokens = firsttoken;
	}
	return true;
}*/

		private int FindDefineParameter(ScriptDefinition define, string name)
		{
			int i = 0;

			foreach(idToken token in define.Parameters)
			{
				if(define.ToString() == name)
				{
					return i;
				}

				i++;
			}

			return -1;
		}