コード例 #1
0
        public bool AugmentProject(Project project)
        {
            try
            {
                while (!Lexer.Empty)
                {
                    var sumtype = SumType.Parse(this);
                    if (sumtype != null)
                    {
                        project.RegisterSumType(sumtype.Name, sumtype.Type);
                        continue;
                    }

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

                    var weakalias = WeakAlias.Parse(this);
                    if (weakalias != null)
                    {
                        project.RegisterWeakAlias(weakalias.Name, weakalias.Type);
                        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     += (sender, e) =>
                {
                    ErrorProvider.Navigate(errorTask, VSConstants.LOGVIEWID.Code_guid);
                };

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

            return(true);
        }
コード例 #2
0
        //
        // Helper routine for parsing a structure definition.
        //
        // Consumes tokens and returns a wrapped Structure on success.
        // Returns null on parsing failures, either due to syntactical
        // mistakes, or due to legitimate code that isn't a structure.
        //
        internal static ParsedObject <Structure> Parse(ParseSession parser)
        {
            int totaltokens = 0;

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

            var nametoken = parser.PeekToken(1);

            if (nametoken == null)
            {
                return(null);
            }

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


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

            ++totaltokens;

            var structure = new Structure {
                Name = nametoken
            };

            structure.Members = new List <Member>();
            var parsed = new ParsedObject <Structure> {
                Name = nametoken, Object = structure
            };

            bool moremembers = true;

            while (moremembers)
            {
                if (parser.CheckToken(totaltokens, "("))
                {
                    ++totaltokens;

                    var membername = parser.PeekToken(totaltokens);

                    ++totaltokens;

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

                    ++totaltokens;

                    bool moreparams = true;
                    while (moreparams)
                    {
                        ++totaltokens;
                        if (!parser.CheckToken(totaltokens, ","))
                        {
                            moreparams = false;
                        }
                        else
                        {
                            ++totaltokens;
                        }
                    }

                    if (parser.CheckToken(totaltokens, "->"))
                    {
                        totaltokens += 2;
                    }

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

                    ++totaltokens;

                    // TODO - register function-typed structure members
                }
                else
                {
                    int typestarttoken = totaltokens;
                    var membertype     = parser.PeekToken(totaltokens);

                    ++totaltokens;
                    int typeendtoken = totaltokens;

                    var membername = parser.PeekToken(totaltokens);

                    ++totaltokens;

                    if (membername.Text.Equals("<"))
                    {
                        int starttotal = totaltokens;
                        if (!parser.ParseTemplateArguments(totaltokens, membertype, out totaltokens))
                        {
                            return(null);
                        }

                        if (totaltokens <= starttotal)
                        {
                            return(null);
                        }

                        typeendtoken = totaltokens;

                        membername = parser.PeekToken(totaltokens);
                        ++totaltokens;
                    }

                    if (membername.Text.Equals("ref"))
                    {
                        typeendtoken = totaltokens;
                        membername   = parser.PeekToken(totaltokens);
                        ++totaltokens;
                    }

                    parsed.Object.Members.Add(new Member {
                        Name = membername, Type = TypeSignatureInstantiated.Construct(parser, typestarttoken, typeendtoken)
                    });
                }

                if (!parser.CheckToken(totaltokens, ","))
                {
                    moremembers = false;
                }
                else
                {
                    ++totaltokens;
                }
            }

            parser.ConsumeTokens(totaltokens);
            return(parsed);
        }