Beispiel #1
0
        public static bool TryParseDefine(IParser parser, out TypeDefinitionNode defNode, bool isPublic = false)
        {
            defNode = null;
            bool result = false;

            if (parser.PeekToken(TokenCategory.Identifier) || parser.PeekToken(TokenCategory.Keyword))
            {
                defNode = new TypeDefinitionNode();
                result  = true;
                parser.NextToken();
                defNode.StartIndex = parser.Token.Span.Start;
                defNode._location  = parser.TokenLocation;
                defNode.Identifier = parser.Token.Token.Value.ToString();
                defNode._isPublic  = isPublic;

                TypeReference typeRef;
                if (TypeReference.TryParseNode(parser, out typeRef, false, isPublic) && typeRef != null)
                {
                    defNode.Children.Add(typeRef.StartIndex, typeRef);
                    if (defNode.Children.All(x => x.Value.IsComplete))
                    {
                        defNode.EndIndex   = defNode.Children.Last().Value.EndIndex;
                        defNode.IsComplete = true;
                    }
                }
                else
                {
                    parser.ReportSyntaxError("Invalid syntax found in type definition.");
                }
            }
            return(result);
        }
Beispiel #2
0
        public static bool TryParseNode(IParser parser, out TypeDefNode defNode, out bool matchedBreakSequence, List <List <TokenKind> > breakSequences = null)
        {
            defNode = null;
            bool result = false;

            matchedBreakSequence = false;
            AccessModifier?accMod      = null;
            string         accModToken = null;

            if (parser.PeekToken(TokenKind.PublicKeyword))
            {
                accMod      = AccessModifier.Public;
                accModToken = parser.PeekToken().Value.ToString();
            }
            else if (parser.PeekToken(TokenKind.PrivateKeyword))
            {
                accMod      = AccessModifier.Private;
                accModToken = parser.PeekToken().Value.ToString();
            }

            uint lookAheadBy = (uint)(accMod.HasValue ? 2 : 1);

            if (parser.PeekToken(TokenKind.TypeKeyword, lookAheadBy))
            {
                result  = true;
                defNode = new TypeDefNode();
                if (accMod.HasValue)
                {
                    parser.NextToken();
                    defNode.AccessModifier = accMod.Value;
                }
                else
                {
                    defNode.AccessModifier = AccessModifier.Public;
                }

                parser.NextToken(); // move past the Type keyword
                defNode.StartIndex = parser.Token.Span.Start;

                TypeDefinitionNode constDef;
                while (true)
                {
                    if (TypeDefinitionNode.TryParseDefine(parser, out constDef, defNode.AccessModifier == AccessModifier.Public) && constDef != null)
                    {
                        defNode.Children.Add(constDef.StartIndex, constDef);
                    }
                    else
                    {
                        break;
                    }

                    if (parser.PeekToken(TokenKind.Comma))
                    {
                        parser.NextToken();
                    }

                    if (breakSequences != null)
                    {
                        bool matchedBreak = false;
                        foreach (var seq in breakSequences)
                        {
                            bool bsMatch        = true;
                            uint peekaheadCount = 1;
                            foreach (var kind in seq)
                            {
                                if (parser.PeekToken(kind, peekaheadCount))
                                {
                                    peekaheadCount++;
                                }
                                else
                                {
                                    bsMatch = false;
                                    break;
                                }
                            }
                            if (bsMatch)
                            {
                                matchedBreak = true;
                                break;
                            }
                        }

                        if (matchedBreak)
                        {
                            matchedBreakSequence = true;
                            break;
                        }
                    }
                }

                if (defNode.Children.Count > 0 && defNode.Children.All(x => x.Value.IsComplete))
                {
                    defNode.EndIndex   = defNode.Children.Last().Value.EndIndex;
                    defNode.IsComplete = true;
                }
            }
            return(result);
        }