//
        // Parse the code that was provided in construction, and stash the results
        // in the given Project instance. This routine should do its best to avoid
        // leaking exceptions generated by the parsing process itself, since those
        // errors will propagate upward until VS detects them and turns off future
        // operations by the VSIX package (until restarted).
        //
        public bool AugmentProject(Project project)
        {
            try
            {
                while (!Lexer.Empty)
                {
                    var sumtype = SumType.Parse(this);
                    if (sumtype != null)
                    {
                        project.RegisterSumType(sumtype.Name, sumtype.Object);
                        continue;
                    }

                    var strongalias = StrongAlias.Parse(this);
                    if (strongalias != null)
                    {
                        project.RegisterStrongAlias(strongalias.Name, strongalias.Object);
                        continue;
                    }

                    var weakalias = WeakAlias.Parse(this);
                    if (weakalias != null)
                    {
                        project.RegisterWeakAlias(weakalias.Name, weakalias.Object);
                        continue;
                    }

                    var structure = Structure.Parse(this);
                    if (structure != null)
                    {
                        project.RegisterStructureType(structure.Name, structure.Object);
                        continue;
                    }

                    var globals = GlobalBlock.Parse(this);
                    if (globals != null)
                    {
                        globals.AugmentProject(project);
                        continue;
                    }

                    var function = FunctionSignature.Parse(this);
                    if (function != null)
                    {
                        project.RegisterFunction(function);
                        continue;
                    }

                    if (!Lexer.Empty)
                    {
                        throw new SyntaxError("Syntax error", PeekToken(0));
                    }
                }
            }
            catch (SyntaxError ex)
            {
                var errorTask = new ErrorTask();
                errorTask.Text          = ex.Message;
                errorTask.Category      = TaskCategory.CodeSense;
                errorTask.ErrorCategory = TaskErrorCategory.Error;
                errorTask.Document      = Lexer.FileName;
                errorTask.Line          = (ex.Origin != null) ? (ex.Origin.Line) : 0;
                errorTask.Column        = (ex.Origin != null) ? (ex.Origin.Column) : 0;
                errorTask.Navigate     += project.NavigationHandler;

                ErrorProvider.Tasks.Add(errorTask);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public static FunctionSignature Parse(ParseSession parser)
        {
            var nametoken = parser.PeekToken(0);

            if (nametoken == null || string.IsNullOrEmpty(nametoken.Text))
            {
                return(null);
            }

            int totaltokens = 1;

            if (parser.CheckToken(totaltokens, "<"))
            {
                ++totaltokens;
                if (!parser.ParseTemplateParameters(totaltokens, nametoken, out totaltokens))
                {
                    return(null);
                }
            }

            if (!parser.CheckToken(totaltokens, ":"))
            {
                return(null);
            }

            ++totaltokens;

            var overload = new FunctionOverload();

            if (!parser.CheckToken(totaltokens, "["))
            {
                var paramlist  = ParseFunctionParams(parser, totaltokens, out totaltokens);
                var funcreturn = ParseFunctionReturn(parser, totaltokens, out totaltokens);

                overload.Parameters = paramlist;
                overload.ReturnType = funcreturn?.Type;
            }

            var tags = ParseFunctionTags(parser, totaltokens, out totaltokens);

            overload.Tags = tags;

            if (parser.CheckToken(totaltokens, "{"))
            {
                ++totaltokens;
                var scope = ParseCodeBlock(parser, null, totaltokens, out totaltokens);
                overload.Scope = scope;

                if (overload.Scope != null && overload.Parameters != null)
                {
                    foreach (var p in overload.Parameters)
                    {
                        var v = new Variable {
                            Name = p.Name, Type = p.Type, Origin = Variable.Origins.Parameter
                        };
                        overload.Scope.Variables.Add(v);
                    }
                }
            }

            parser.ConsumeTokens(totaltokens);

            var ret = new FunctionSignature {
                Name = nametoken, Overloads = new List <FunctionOverload>()
            };

            ret.Overloads.Add(overload);

            return(ret);
        }