//-----------------------------------------------------------------------------
        // Constructors
        //-----------------------------------------------------------------------------
        public CommandParam(string str)
        {
            stringValue = str;
            parent = null;
            type = CommandParamType.String;
            count = 0;
            // TODO: parse int/float.

            float.TryParse(str, out floatValue);
            int.TryParse(str, out intValue);
            bool.TryParse(str, out boolValue);
        }
        // Parse and interpret the given text stream as a script, line by line.
        public override void ReadScript(StreamReader reader, string path)
        {
            this.fileName = path;
            this.streamReader = reader;

            BeginReading();

            parameterParent	= new CommandParam("");
            parameterParent.Type = CommandParamType.Array;
            parameterRoot	= parameterParent;
            parameter		= null;

            // Read all lines.
            lineIndex = -1;
            while (!reader.EndOfStream) {
                NextLine();
                ReadLine(line);
            }

            EndReading();
        }
        protected void CompleteStatement()
        {
            if (parameterRoot.Children != null) {
                string commandName = parameterRoot.Children.Str;
                parameterRoot.Children = parameterRoot.Children.NextParam;
                parameterRoot.Count--;

                if (!ReadCommand(commandName, parameterRoot)) {
                    ThrowParseError(commandName + " is not a valid command", false);
                }
            }

            parameterParent	= new CommandParam("");
            parameterParent.Type = CommandParamType.Array;
            parameterRoot	= parameterParent;
            parameter		= null;
        }
 //-----------------------------------------------------------------------------
 // Constructor
 //-----------------------------------------------------------------------------
 public NewScriptReader()
 {
     parameter		= null;
     parameterRoot	= null;
     commands		= new List<ScriptCommand>();
 }
 private CommandParam AddParam()
 {
     CommandParam newParam = new CommandParam(word);
     if (parameter == null)
         parameterParent.Children = newParam;
     else
         parameter.NextParam = newParam;
     parameterParent.Count++;
     newParam.Parent = parameterParent;
     parameter = newParam;
     return newParam;
 }
        // Read a single line of the script.
        protected override void ReadLine(string line)
        {
            word = "";
            bool quotes = false;
            charIndex = 0;

            // Parse line character by character.
            for (int i = 0; i < line.Length; i++) {
                char c = line[i];
                charIndex = i;

                // Parse quotes.
                if (quotes) {
                    // Closing quotes.
                    if (c == '\"') {
                        quotes = false;
                        CompleteWord(true);
                    }
                    else
                        word += c;
                }

                // Whitespace.
                else if (c == ' ' || c == '\t')
                    CompleteWord();

                // Commas.
                else if (c == ',')
                    CompleteWord();

                // Semicolons.
                else if (c == ';') {
                    CompleteWord();
                    int prevLineIndex = lineIndex;
                    CompleteStatement();
                    if (lineIndex > prevLineIndex)
                        return;
                }

                // Single-line comment.
                else if (c == '#') {
                    break; // Ignore the rest of the line.
                }

                // Opening quotes.
                else if (word.Length == 0 && c == '\"')
                    quotes = true;

                // Opening parenthesis.
                else if (word.Length == 0 && c == '(') {
                    parameterParent = AddParam();
                    parameterParent.Type = CommandParamType.Array;
                    parameter = null;
                }

                // Closing parenthesis.
                else if (c == ')') {
                    CompleteWord();
                    if (parameterParent == parameterRoot) {
                        ThrowParseError("Unexpected symbol ')'");
                    }
                    else {
                        parameter = parameterParent;
                        parameterParent = parameterParent.Parent;
                    }
                }

                // Valid keyword character.
                else if (IsValidKeywordCharacter(c))
                    word += c;

                // Error: Unexpected character.
                else
                    ThrowParseError("Unexpected symbol '" + c + "'");
            }

            charIndex++;

            // Make sure quotes are closed and statements are ended.
            if (quotes)
                ThrowParseError("Expected \"");

            CompleteWord();
        }
        //-----------------------------------------------------------------------------
        // Virtual methods
        //-----------------------------------------------------------------------------
        // Begins reading the script.
        //protected virtual void BeginReading() {}
        // Ends reading the script.
        //protected virtual void EndReading() {}
        // Reads a line in the script as a command.
        protected virtual bool ReadCommand(string commandName, CommandParam parameters)
        {
            for (int i = 0; i < commands.Count; i++) {
                if (String.Compare(commands[i].Name, commandName,
                    StringComparison.CurrentCultureIgnoreCase) == 0)
                {
                    commands[i].Action(parameters);
                    return true;
                }
            }

            return false;
        }
        protected void PrintParementers(CommandParam param)
        {
            CommandParam p = param.Children;
            while (p != null) {
                if (p.Type == CommandParamType.Array) {
                    Console.Write("(");
                    PrintParementers(p);
                    Console.Write(")");
                }
                else
                    Console.Write(p.Str);

                p = p.NextParam;
                if (p != null)
                    Console.Write(", ");
            }
        }
Exemple #9
0
 private void ReadSpecialCommand(string commandName, CommandParam parameters)
 {
     bool exists = false;
     switch (loadingMode) {
     case LoadingModes.Tilesets:
         for (int i = 0; i < tilesetCommands.Count; i++) {
             if (String.Compare(tilesetCommands[i].Name, commandName,
                 StringComparison.CurrentCultureIgnoreCase) == 0) {
                     tilesetCommands[i].Action(parameters);
                 exists = true;
                 break;
             }
         }
         if (!exists)
             ThrowParseError(commandName + " is not a valid command while loading tilesets", false);
         break;
     case LoadingModes.Animations:
         for (int i = 0; i < animationCommands.Count; i++) {
             if (String.Compare(animationCommands[i].Name, commandName,
                 StringComparison.CurrentCultureIgnoreCase) == 0) {
                     animationCommands[i].Action(parameters);
                 exists = true;
                 break;
             }
         }
         if (!exists)
             ThrowParseError(commandName + " is not a valid command while loading animations", false);
         break;
     case LoadingModes.Sprites:
         for (int i = 0; i < spriteCommands.Count; i++) {
             if (String.Compare(spriteCommands[i].Name, commandName,
                 StringComparison.CurrentCultureIgnoreCase) == 0) {
                     spriteCommands[i].Action(parameters);
                 exists = true;
                 break;
             }
         }
         if (!exists)
             ThrowParseError(commandName + " is not a valid command while loading sprites", false);
         break;
     }
 }