Exemple #1
0
 /// <summary>
 /// Returns a clone of the text unit.
 /// </summary>
 /// <param name="textUnit">TextUnit</param>
 /// <returns>TextUnit</returns>
 public static TextUnit Clone(TextUnit textUnit)
 {
     return new TextUnit(textUnit.Text, textUnit.Line);
 }
        /// <summary>
        /// Visits the syntax node.
        /// </summary>
        /// <param name="parentNode">Node</param>
        /// <param name="isModel">Is model</param>
        /// <param name="accMod">Access modifier</param>
        /// <param name="inhMod">Inheritance modifier</param>
        /// <param name="isAsync">Is async</param>
        internal void Visit(MachineDeclaration parentNode, bool isModel, AccessModifier accMod,
                            InheritanceModifier inhMod, bool isAsync)
        {
            TextUnit textUnit = null;

            new TypeIdentifierVisitor(base.TokenStream).Visit(ref textUnit);
            var typeIdentifier = new Token(textUnit, TokenType.TypeIdentifier);

            if (base.TokenStream.Done ||
                base.TokenStream.Peek().Type != TokenType.Identifier)
            {
                throw new ParsingException("Expected field or method identifier.",
                                           new List <TokenType>
                {
                    TokenType.Identifier
                });
            }

            var identifierToken = base.TokenStream.Peek();

            base.TokenStream.Index++;
            base.TokenStream.SkipWhiteSpaceAndCommentTokens();

            if (base.TokenStream.Done ||
                (base.TokenStream.Peek().Type != TokenType.LeftParenthesis &&
                 base.TokenStream.Peek().Type != TokenType.Semicolon))
            {
                throw new ParsingException("Expected \"(\" or \";\".",
                                           new List <TokenType>
                {
                    TokenType.LeftParenthesis,
                    TokenType.Semicolon
                });
            }

            if (base.TokenStream.Peek().Type == TokenType.LeftParenthesis)
            {
                new MethodDeclarationVisitor(base.TokenStream).Visit(parentNode, typeIdentifier,
                                                                     identifierToken, isModel, accMod, inhMod, isAsync);
            }
            else if (base.TokenStream.Peek().Type == TokenType.Semicolon)
            {
                if (inhMod == InheritanceModifier.Abstract)
                {
                    throw new ParsingException("A field cannot be abstract.",
                                               new List <TokenType>());
                }
                else if (inhMod == InheritanceModifier.Virtual)
                {
                    throw new ParsingException("A field cannot be virtual.",
                                               new List <TokenType>());
                }
                else if (inhMod == InheritanceModifier.Override)
                {
                    throw new ParsingException("A field cannot be overriden.",
                                               new List <TokenType>());
                }

                if (isModel)
                {
                    throw new ParsingException("A field cannot be a model.",
                                               new List <TokenType>());
                }

                if (isAsync)
                {
                    throw new ParsingException("A field cannot be async.",
                                               new List <TokenType>());
                }

                var node = new FieldDeclaration(base.TokenStream.Program, parentNode,
                                                parentNode.IsModel);
                node.AccessModifier = accMod;
                node.TypeIdentifier = typeIdentifier;
                node.Identifier     = identifierToken;
                node.SemicolonToken = base.TokenStream.Peek();

                parentNode.FieldDeclarations.Add(node);
            }
        }
        /// <summary>
        /// Visits the text unit.
        /// </summary>
        /// <param name="textUnit">TextUnit</param>
        public void Visit(ref TextUnit textUnit)
        {
            if (base.TokenStream.Done ||
                (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                base.TokenStream.Peek().Type != TokenType.Void &&
                base.TokenStream.Peek().Type != TokenType.Object &&
                base.TokenStream.Peek().Type != TokenType.String &&
                base.TokenStream.Peek().Type != TokenType.Sbyte &&
                base.TokenStream.Peek().Type != TokenType.Byte &&
                base.TokenStream.Peek().Type != TokenType.Short &&
                base.TokenStream.Peek().Type != TokenType.Ushort &&
                base.TokenStream.Peek().Type != TokenType.Int &&
                base.TokenStream.Peek().Type != TokenType.Uint &&
                base.TokenStream.Peek().Type != TokenType.Long &&
                base.TokenStream.Peek().Type != TokenType.Ulong &&
                base.TokenStream.Peek().Type != TokenType.Char &&
                base.TokenStream.Peek().Type != TokenType.Bool &&
                base.TokenStream.Peek().Type != TokenType.Decimal &&
                base.TokenStream.Peek().Type != TokenType.Float &&
                base.TokenStream.Peek().Type != TokenType.Double &&
                base.TokenStream.Peek().Type != TokenType.Identifier))
            {
                throw new ParsingException("Expected type identifier.",
                    new List<TokenType>
                {
                    TokenType.Identifier
                });
            }

            var line = base.TokenStream.Peek().TextUnit.Line;

            bool expectsDot = false;
            while (!base.TokenStream.Done)
            {
                if (!expectsDot &&
                    (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                    base.TokenStream.Peek().Type != TokenType.Void &&
                    base.TokenStream.Peek().Type != TokenType.Object &&
                    base.TokenStream.Peek().Type != TokenType.String &&
                    base.TokenStream.Peek().Type != TokenType.Sbyte &&
                    base.TokenStream.Peek().Type != TokenType.Byte &&
                    base.TokenStream.Peek().Type != TokenType.Short &&
                    base.TokenStream.Peek().Type != TokenType.Ushort &&
                    base.TokenStream.Peek().Type != TokenType.Int &&
                    base.TokenStream.Peek().Type != TokenType.Uint &&
                    base.TokenStream.Peek().Type != TokenType.Long &&
                    base.TokenStream.Peek().Type != TokenType.Ulong &&
                    base.TokenStream.Peek().Type != TokenType.Char &&
                    base.TokenStream.Peek().Type != TokenType.Bool &&
                    base.TokenStream.Peek().Type != TokenType.Decimal &&
                    base.TokenStream.Peek().Type != TokenType.Float &&
                    base.TokenStream.Peek().Type != TokenType.Double &&
                    base.TokenStream.Peek().Type != TokenType.Identifier) ||
                    (expectsDot && base.TokenStream.Peek().Type != TokenType.Dot))
                {
                    break;
                }

                if (base.TokenStream.Peek().Type == TokenType.MachineDecl ||
                    base.TokenStream.Peek().Type == TokenType.Void ||
                    base.TokenStream.Peek().Type == TokenType.Object ||
                    base.TokenStream.Peek().Type == TokenType.String ||
                    base.TokenStream.Peek().Type == TokenType.Sbyte ||
                    base.TokenStream.Peek().Type == TokenType.Byte ||
                    base.TokenStream.Peek().Type == TokenType.Short ||
                    base.TokenStream.Peek().Type == TokenType.Ushort ||
                    base.TokenStream.Peek().Type == TokenType.Int ||
                    base.TokenStream.Peek().Type == TokenType.Uint ||
                    base.TokenStream.Peek().Type == TokenType.Long ||
                    base.TokenStream.Peek().Type == TokenType.Ulong ||
                    base.TokenStream.Peek().Type == TokenType.Char ||
                    base.TokenStream.Peek().Type == TokenType.Bool ||
                    base.TokenStream.Peek().Type == TokenType.Decimal ||
                    base.TokenStream.Peek().Type == TokenType.Float ||
                    base.TokenStream.Peek().Type == TokenType.Double ||
                    base.TokenStream.Peek().Type == TokenType.Identifier)
                {
                    expectsDot = true;
                }
                else if (base.TokenStream.Peek().Type == TokenType.Dot)
                {
                    expectsDot = false;
                }

                if (base.TokenStream.Peek().Type == TokenType.MachineDecl)
                {
                    base.TokenStream.Swap(new Token(new TextUnit("MachineId", base.TokenStream.
                        Peek().TextUnit.Line), TokenType.MachineDecl));
                }

                if (textUnit == null)
                {
                    textUnit = new TextUnit(base.TokenStream.Peek().TextUnit.Text,
                        line);
                }
                else
                {
                    textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                        line);
                }

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();
            }

            if (base.TokenStream.Peek().Type == TokenType.LeftAngleBracket)
            {
                textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                    line);

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();

                int counter = 1;
                while (!base.TokenStream.Done)
                {
                    if (base.TokenStream.Peek().Type == TokenType.LeftAngleBracket)
                    {
                        counter++;
                    }
                    else if (base.TokenStream.Peek().Type == TokenType.RightAngleBracket)
                    {
                        counter--;
                    }

                    if (counter == 0 ||
                        (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                        base.TokenStream.Peek().Type != TokenType.Void &&
                        base.TokenStream.Peek().Type != TokenType.Object &&
                        base.TokenStream.Peek().Type != TokenType.String &&
                        base.TokenStream.Peek().Type != TokenType.Sbyte &&
                        base.TokenStream.Peek().Type != TokenType.Byte &&
                        base.TokenStream.Peek().Type != TokenType.Short &&
                        base.TokenStream.Peek().Type != TokenType.Ushort &&
                        base.TokenStream.Peek().Type != TokenType.Int &&
                        base.TokenStream.Peek().Type != TokenType.Uint &&
                        base.TokenStream.Peek().Type != TokenType.Long &&
                        base.TokenStream.Peek().Type != TokenType.Ulong &&
                        base.TokenStream.Peek().Type != TokenType.Char &&
                        base.TokenStream.Peek().Type != TokenType.Bool &&
                        base.TokenStream.Peek().Type != TokenType.Decimal &&
                        base.TokenStream.Peek().Type != TokenType.Float &&
                        base.TokenStream.Peek().Type != TokenType.Double &&
                        base.TokenStream.Peek().Type != TokenType.Identifier &&
                        base.TokenStream.Peek().Type != TokenType.Dot &&
                        base.TokenStream.Peek().Type != TokenType.Comma &&
                        base.TokenStream.Peek().Type != TokenType.LeftSquareBracket &&
                        base.TokenStream.Peek().Type != TokenType.RightSquareBracket &&
                        base.TokenStream.Peek().Type != TokenType.LeftAngleBracket &&
                        base.TokenStream.Peek().Type != TokenType.RightAngleBracket))
                    {
                        break;
                    }

                    if (base.TokenStream.Peek().Type == TokenType.MachineDecl)
                    {
                        base.TokenStream.Swap(new Token(new TextUnit("MachineId", base.TokenStream.
                            Peek().TextUnit.Line), TokenType.MachineDecl));
                    }

                    textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                        line);

                    base.TokenStream.Index++;
                    base.TokenStream.SkipWhiteSpaceAndCommentTokens();
                }

                if (base.TokenStream.Done ||
                    base.TokenStream.Peek().Type != TokenType.RightAngleBracket)
                {
                    throw new ParsingException("Expected \">\".",
                        new List<TokenType>
                    {
                        TokenType.RightAngleBracket
                    });
                }

                textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                    line);

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();
            }
        }
        /// <summary>
        /// Visits the text unit.
        /// </summary>
        /// <param name="textUnit">TextUnit</param>
        public void Visit(ref TextUnit textUnit)
        {
            if (base.TokenStream.Done ||
                (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                 base.TokenStream.Peek().Type != TokenType.Void &&
                 base.TokenStream.Peek().Type != TokenType.Object &&
                 base.TokenStream.Peek().Type != TokenType.String &&
                 base.TokenStream.Peek().Type != TokenType.Sbyte &&
                 base.TokenStream.Peek().Type != TokenType.Byte &&
                 base.TokenStream.Peek().Type != TokenType.Short &&
                 base.TokenStream.Peek().Type != TokenType.Ushort &&
                 base.TokenStream.Peek().Type != TokenType.Int &&
                 base.TokenStream.Peek().Type != TokenType.Uint &&
                 base.TokenStream.Peek().Type != TokenType.Long &&
                 base.TokenStream.Peek().Type != TokenType.Ulong &&
                 base.TokenStream.Peek().Type != TokenType.Char &&
                 base.TokenStream.Peek().Type != TokenType.Bool &&
                 base.TokenStream.Peek().Type != TokenType.Decimal &&
                 base.TokenStream.Peek().Type != TokenType.Float &&
                 base.TokenStream.Peek().Type != TokenType.Double &&
                 base.TokenStream.Peek().Type != TokenType.Identifier))
            {
                throw new ParsingException("Expected type identifier.",
                                           new List <TokenType>
                {
                    TokenType.Identifier
                });
            }

            var line = base.TokenStream.Peek().TextUnit.Line;

            bool expectsDot = false;

            while (!base.TokenStream.Done)
            {
                if (!expectsDot &&
                    (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                     base.TokenStream.Peek().Type != TokenType.Void &&
                     base.TokenStream.Peek().Type != TokenType.Object &&
                     base.TokenStream.Peek().Type != TokenType.String &&
                     base.TokenStream.Peek().Type != TokenType.Sbyte &&
                     base.TokenStream.Peek().Type != TokenType.Byte &&
                     base.TokenStream.Peek().Type != TokenType.Short &&
                     base.TokenStream.Peek().Type != TokenType.Ushort &&
                     base.TokenStream.Peek().Type != TokenType.Int &&
                     base.TokenStream.Peek().Type != TokenType.Uint &&
                     base.TokenStream.Peek().Type != TokenType.Long &&
                     base.TokenStream.Peek().Type != TokenType.Ulong &&
                     base.TokenStream.Peek().Type != TokenType.Char &&
                     base.TokenStream.Peek().Type != TokenType.Bool &&
                     base.TokenStream.Peek().Type != TokenType.Decimal &&
                     base.TokenStream.Peek().Type != TokenType.Float &&
                     base.TokenStream.Peek().Type != TokenType.Double &&
                     base.TokenStream.Peek().Type != TokenType.Identifier) ||
                    (expectsDot && base.TokenStream.Peek().Type != TokenType.Dot))
                {
                    break;
                }

                if (base.TokenStream.Peek().Type == TokenType.MachineDecl ||
                    base.TokenStream.Peek().Type == TokenType.Void ||
                    base.TokenStream.Peek().Type == TokenType.Object ||
                    base.TokenStream.Peek().Type == TokenType.String ||
                    base.TokenStream.Peek().Type == TokenType.Sbyte ||
                    base.TokenStream.Peek().Type == TokenType.Byte ||
                    base.TokenStream.Peek().Type == TokenType.Short ||
                    base.TokenStream.Peek().Type == TokenType.Ushort ||
                    base.TokenStream.Peek().Type == TokenType.Int ||
                    base.TokenStream.Peek().Type == TokenType.Uint ||
                    base.TokenStream.Peek().Type == TokenType.Long ||
                    base.TokenStream.Peek().Type == TokenType.Ulong ||
                    base.TokenStream.Peek().Type == TokenType.Char ||
                    base.TokenStream.Peek().Type == TokenType.Bool ||
                    base.TokenStream.Peek().Type == TokenType.Decimal ||
                    base.TokenStream.Peek().Type == TokenType.Float ||
                    base.TokenStream.Peek().Type == TokenType.Double ||
                    base.TokenStream.Peek().Type == TokenType.Identifier)
                {
                    expectsDot = true;
                }
                else if (base.TokenStream.Peek().Type == TokenType.Dot)
                {
                    expectsDot = false;
                }

                if (base.TokenStream.Peek().Type == TokenType.MachineDecl)
                {
                    base.TokenStream.Swap(new Token(new TextUnit("MachineId", base.TokenStream.
                                                                 Peek().TextUnit.Line), TokenType.MachineDecl));
                }

                if (textUnit == null)
                {
                    textUnit = new TextUnit(base.TokenStream.Peek().TextUnit.Text,
                                            line);
                }
                else
                {
                    textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                                            line);
                }

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();
            }

            if (base.TokenStream.Peek().Type == TokenType.LeftAngleBracket)
            {
                textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                                        line);

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();

                int counter = 1;
                while (!base.TokenStream.Done)
                {
                    if (base.TokenStream.Peek().Type == TokenType.LeftAngleBracket)
                    {
                        counter++;
                    }
                    else if (base.TokenStream.Peek().Type == TokenType.RightAngleBracket)
                    {
                        counter--;
                    }

                    if (counter == 0 ||
                        (base.TokenStream.Peek().Type != TokenType.MachineDecl &&
                         base.TokenStream.Peek().Type != TokenType.Void &&
                         base.TokenStream.Peek().Type != TokenType.Object &&
                         base.TokenStream.Peek().Type != TokenType.String &&
                         base.TokenStream.Peek().Type != TokenType.Sbyte &&
                         base.TokenStream.Peek().Type != TokenType.Byte &&
                         base.TokenStream.Peek().Type != TokenType.Short &&
                         base.TokenStream.Peek().Type != TokenType.Ushort &&
                         base.TokenStream.Peek().Type != TokenType.Int &&
                         base.TokenStream.Peek().Type != TokenType.Uint &&
                         base.TokenStream.Peek().Type != TokenType.Long &&
                         base.TokenStream.Peek().Type != TokenType.Ulong &&
                         base.TokenStream.Peek().Type != TokenType.Char &&
                         base.TokenStream.Peek().Type != TokenType.Bool &&
                         base.TokenStream.Peek().Type != TokenType.Decimal &&
                         base.TokenStream.Peek().Type != TokenType.Float &&
                         base.TokenStream.Peek().Type != TokenType.Double &&
                         base.TokenStream.Peek().Type != TokenType.Identifier &&
                         base.TokenStream.Peek().Type != TokenType.Dot &&
                         base.TokenStream.Peek().Type != TokenType.Comma &&
                         base.TokenStream.Peek().Type != TokenType.LeftSquareBracket &&
                         base.TokenStream.Peek().Type != TokenType.RightSquareBracket &&
                         base.TokenStream.Peek().Type != TokenType.LeftAngleBracket &&
                         base.TokenStream.Peek().Type != TokenType.RightAngleBracket))
                    {
                        break;
                    }

                    if (base.TokenStream.Peek().Type == TokenType.MachineDecl)
                    {
                        base.TokenStream.Swap(new Token(new TextUnit("MachineId", base.TokenStream.
                                                                     Peek().TextUnit.Line), TokenType.MachineDecl));
                    }

                    textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                                            line);

                    base.TokenStream.Index++;
                    base.TokenStream.SkipWhiteSpaceAndCommentTokens();
                }

                if (base.TokenStream.Done ||
                    base.TokenStream.Peek().Type != TokenType.RightAngleBracket)
                {
                    throw new ParsingException("Expected \">\".",
                                               new List <TokenType>
                    {
                        TokenType.RightAngleBracket
                    });
                }

                textUnit = new TextUnit(textUnit.Text + base.TokenStream.Peek().TextUnit.Text,
                                        line);

                base.TokenStream.Index++;
                base.TokenStream.SkipWhiteSpaceAndCommentTokens();
            }
        }
Exemple #5
0
 /// <summary>
 /// Returns a clone of the text unit.
 /// </summary>
 /// <param name="textUnit">TextUnit</param>
 /// <returns>TextUnit</returns>
 public static TextUnit Clone(TextUnit textUnit)
 {
     return(new TextUnit(textUnit.Text, textUnit.Line));
 }