Exemplo n.º 1
0
        /// <summary>
        /// Traduit le jeton en code lisible.
        /// </summary>
        /// <returns></returns>
        public string ToXML()
        {
            Func <string, string> i = delegate(string str)
            {
                return(Tools.StringUtils.Indent(str, 1));
            };
            Func <string, string, string> inlineencapsulate = delegate(string str, string delim)
            {
                return("<" + delim + ">" + str + "</" + delim + ">\n");
            };
            Func <string, string, string> encapsulate = delegate(string str, string delim)
            {
                return("<" + delim + ">\n" + i(str) + "\n</" + delim + ">\n");
            };
            Func <string, string> getChildrenCode = delegate(string name)
            {
                StringBuilder b = new StringBuilder();
                b.Append("<" + name + ">\n");
                for (int j = 0; j < SubTokens.Count; j++)
                {
                    b.AppendLine(i(SubTokens[j].ToXML()));
                }
                b.Append("</" + name + ">\n");
                return(b.ToString());
            };

            switch (TkType)
            {
            case ExpressionTokenType.BracketList:
                return(getChildrenCode("BracketList"));

            case ExpressionTokenType.FunctionCall:
                return("<FunctionCall>\n" +
                       i("<Identifier>\n" + i(FunctionCallIdentifier.ToXML()) + "</Identifier>\n") +
                       i(FunctionCallArgs.ToXML()) +
                       "</FunctionCall>");

            case ExpressionTokenType.GenericType:
                return("<GenericType>\n" +
                       i("<Identifier>\n" + i(GenericTypeIdentifier.ToXML()) + "</Identifier>\n") +
                       i(GenericTypeArgs.ToXML()) +
                       "</GenericType>");

            case ExpressionTokenType.ArrayType:
                return("<ArrayType>\n" +
                       i("<Identifier>\n" + i(ArrayTypeIdentifier.ToXML()) + "</Identifier>\n") +
                       i(ArrayTypeArgs.ToXML()) +
                       "</ArrayType>");

            case ExpressionTokenType.NamedCodeBlock:
                return("<NamedCodeBlock>\n" +
                       i("<Identifier>\n" + i(SubTokens[0].ToXML()) + "</Identifier>\n") +
                       i(SubTokens[1].ToXML()) +
                       "</NamedCodeBlock>");

            case ExpressionTokenType.NamedGenericCodeBlock:
                return("<NamedGenericCodeBlock>\n" +
                       i("<Identifier>\n" + i(NamedGenericCodeBlockIdentifier.ToXML()) + "</Identifier>\n") +
                       i(NamedGenericCodeBlockInstructions.ToXML()) +
                       "</NamedGenericCodeBlock>");

            case ExpressionTokenType.FunctionDeclaration:
                return("<FunctionDeclaration>\n" +
                       i("<FuncName>\n" + i(SubTokens[0].ToXML()) + "</FuncName>\n") +
                       i(SubTokens[1].ToXML()) +
                       "</FunctionDeclaration>");

            case ExpressionTokenType.List:
                return(getChildrenCode("List"));

            case ExpressionTokenType.ArgList:
                return(getChildrenCode("ArgList"));

            case ExpressionTokenType.GenericParametersList:
                return(getChildrenCode("GenList"));

            case ExpressionTokenType.Name:
                return(inlineencapsulate(this.Content, "Name"));

            case ExpressionTokenType.CodeBlock:
                return(getChildrenCode("CodeBlock"));

            case ExpressionTokenType.EndOfInstruction:
                return(inlineencapsulate(this.Content, "EndOfInstruction"));

            case ExpressionTokenType.InstructionList:
                return(getChildrenCode("InstructionList"));

            case ExpressionTokenType.ExpressionGroup:
                if (Operator.IsBinaryOperator)
                {
                    return(encapsulate(
                               encapsulate(Operands1.ToXML(), "Operand1") +
                               Operator.ToXML() +
                               encapsulate(Operands2.ToXML(), "Operand2"),
                               "ExpressionGroup"));
                }

                else
                {
                    return(encapsulate(
                               encapsulate(Operands1.ToXML(), "Operand1") +
                               Operator.ToXML(),
                               "ExpressionGroup"));
                }

            case ExpressionTokenType.NumberLiteral:
                return(inlineencapsulate(this.Content, "NumberLiteral"));

            case ExpressionTokenType.Operator:
                return(inlineencapsulate(this.Content, "Operator"));

            case ExpressionTokenType.Separator:
                return(inlineencapsulate(this.Content, "Separator"));

            case ExpressionTokenType.StringLiteral:
                return(inlineencapsulate(this.Content, "StringLiteral"));

            case ExpressionTokenType.BoolLiteral:
                return(inlineencapsulate(this.Content, "BoolLiteral"));

            case ExpressionTokenType.Modifier:
                return(inlineencapsulate(this.Content, "Modifier"));

            default:
                return("??");
            }
        }