//
        // 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);
        }
Exemple #2
0
        internal static ParsedSumType Parse(ParseSession parser)
        {
            int totaltokens = 0;

            if (!parser.CheckToken(0, "type"))
            {
                return(null);
            }

            Token sumtypename = parser.PeekToken(1);

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

            if (parser.CheckToken(2, "<"))
            {
                if (!parser.ParseTemplateParameters(3, sumtypename, out totaltokens))
                {
                    return(null);
                }

                if (!parser.CheckToken(totaltokens, ":"))
                {
                    return(null);
                }
            }
            else if (!parser.CheckToken(2, ":"))
            {
                return(null);
            }
            else if (!parser.CheckToken(4, "|"))
            {
                return(null);
            }
            else
            {
                totaltokens = 2;
            }

            do
            {
                ++totaltokens;

                if (parser.CheckToken(totaltokens + 1, "<"))
                {
                    var token = parser.PeekToken(totaltokens);
                    if (token == null)
                    {
                        return(null);
                    }

                    if (!parser.ParseTemplateArguments(totaltokens + 2, token, out totaltokens))
                    {
                        return(null);
                    }
                }
                else
                {
                    ++totaltokens;
                }
            } while (parser.CheckToken(totaltokens, "|"));

            // Success! Consume everything and return the constructed result
            parser.ConsumeTokens(totaltokens);
            var sumtype = new SumType {
            };

            return(new ParsedSumType {
                Name = sumtypename, Type = sumtype
            });
        }