Exemplo n.º 1
0
        internal static GlobalBlock Parse(ParseSession parser)
        {
            if (!parser.CheckToken(0, "global"))
            {
                return(null);
            }

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

            var block = new GlobalBlock();

            int totaltokens = 2;

            do
            {
                var variable = Variable.Parse(parser, totaltokens, out totaltokens, Variable.Origins.Global);
                if (variable != null)
                {
                    block.Variables.Add(variable);
                }
                else
                {
                    return(null);
                }

                parser.ConsumeTokens(totaltokens);
                totaltokens = 0;
            } while (!parser.CheckToken(totaltokens, "}"));

            ++totaltokens;
            parser.ConsumeTokens(totaltokens);
            return(block);
        }
        //
        // 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);
        }