Ejemplo n.º 1
0
        public override Verb CreateVerb(string[] tokens)
        {
            var isAbstract     = tokens[2].IsNotEmpty();
            var type           = tokens[3].Trim();
            var className      = tokens[4];
            var parameterBegin = tokens[5];

            ClassName         = className;
            EnumerationValue  = -1;
            CurrentVisibility = Object.VisibilityType.Public;

            Color(position, tokens[1].Length, Whitespaces);
            Color(tokens[2].Length, KeyWords);
            Color(tokens[3].Length, KeyWords);
            Color(className.Length, Types);
            Color(parameterBegin.Length, Structures);

            Parameters parameters;
            int        index;

            if (parameterBegin == "(" && type != "module")
            {
                Color(parameterBegin.Length, Structures);
                index = position + length;
                var parametersParser = new ParametersParser();
                var parsed           = parametersParser.Parse(source, index);
                if (!parsed.If(out parameters, out index))
                {
                    return(null);
                }

                if (type == "enum")
                {
                    parameters.Unshift(new Parameter("name"));
                    parameters.Unshift(new Parameter("value"));
                }
            }
            else if (type == "enum")
            {
                parameters = new Parameters(new[]
                {
                    new Parameter("value"),
                    new Parameter("name")
                });
                index = position + length;
            }
            else
            {
                parameters = new Parameters();
                index      = position + length;
            }

            var(superClass, _, traits, newIndex) = Ancestors(source, index);
            index = newIndex;

            var endParser = new EndParser();

            if (endParser.Scan(source, index))
            {
                index = endParser.Position;
            }

            Block objectBlock;

            InClassDefinition      = true;
            EnumerationMappingCode = new CodeBuilder();
            try
            {
                if (type == "enum")
                {
                    addEnumerationSupport(className);
                }

                LockedDown = type == "view";
                if (GetBlock(source, index, true, InClass).If(out var block, out newIndex))
                {
                    objectBlock = block;
                    index       = newIndex;
                    if (type == "enum")
                    {
                        addEnumerationInstanceSupport(ref objectBlock);
                    }
                }
                else
                {
                    objectBlock = new Block();
                }
            }
            finally
            {
                InClassDefinition = false;
                if (EnumerationMappingCode != null)
                {
                    AddStaticBlock(EnumerationMappingCode.Block);
                }

                EnumerationMappingCode = null;
                LockedDown             = false;
            }

            var checker = new InheritanceChecker(className, objectBlock, parameters, superClass, isAbstract, traits);

            if (checker.Passes().IfNot(out var exception))
            {
                throw LOCATION.ThrowsWithLocation(() => exception.Message);
            }

            var cls = new Class(parameters, objectBlock, GetStaticBlock(), superClass, traits, type == "view");

            CompilerState.RegisterClass(ClassName, cls);
            result.Value     = cls;
            overridePosition = index;
            ClassName        = "";
            switch (type)
            {
            case "module":
                return(new CreateModule(className, cls, true)
                {
                    Index = position
                });

            case "extend":
                return(new CreateExtender(className, cls));

            default:
                var verb = new CreateClass(className, cls)
                {
                    HelperFunctions = HelperFunctions,
                    HelperBlock     = HelperBlock,
                    Index           = position
                };
                HelperFunctions = null;
                HelperBlock     = null;
                return(verb);
            }
        }
Ejemplo n.º 2
0
        public override Verb CreateVerb(string[] tokens)
        {
            var optionalLength = tokens[2].Length;
            var optional       = tokens[2].Trim() == "optional";

            if (!InClassDefinition && optional)
            {
                return(null);
            }

            Color(position, tokens[1].Length, Whitespaces);
            Color(optionalLength, KeyWords);
            var type = tokens[3].Trim();

            Color(tokens[3].Length, KeyWords);
            var name       = tokens[4];
            var isStandard = tokens[5] == "(";

            Color(name.Length, isStandard ? Invokeables : Messaging);

            name = LongToMangledPrefix(type, name);

            Color(tokens[5].Length, Structures);
            var index = position + length;

            if (isStandard)
            {
                var parameterListParser = new ParameterListParser2();
                if (parameterListParser.Parse(source, index).If(out var list, out var newIndex))
                {
                    var parameterCount = list.Count;
                    var endParser      = new EndParser();
                    index = newIndex;
                    if (endParser.Scan(source, index))
                    {
                        index = endParser.Position;
                    }

                    overridePosition = index;
                    result.Value     = new Signature(name, parameterCount, optional);

                    return(new NullOp());
                }

                return(null);
            }

            var builder = new StringBuilder();
            var messageParameterParser = new MessageParameterParser();
            var variableParser         = new VariableParser();
            var parameterList          = new List <Parameter>();

            if (variableParser.Scan(source, index))
            {
                var variable  = (Variable)variableParser.Result.Value;
                var parameter = new Parameter(variable.Name);
                builder.Append(name);
                builder.Append("_");
                parameterList.Add(parameter);
                index = variableParser.Result.Position;
            }
            else
            {
                return(null);
            }

            while (messageParameterParser.Scan(source, index))
            {
                var parameter = new Parameter(messageParameterParser.ParameterName);
                parameterList.Add(parameter);
                builder.Append(messageParameterParser.MessageName);
                builder.Append("_");
                index = messageParameterParser.Result.Position;
            }

            if (freeParser.Scan(source, index, REGEX_END1))
            {
                freeParser.ColorAll(Structures);
                overridePosition = freeParser.Position;
                name             = builder.ToString();
                result.Value     = new Signature(name, parameterList.Count, optional);

                return(new NullOp());
            }

            return(null);
        }