示例#1
0
        private TechniqueSyntax ParseTechnique()
        {
            var technique = NextToken();

            SyntaxToken name = null;

            if (Current.Kind == SyntaxKind.IdentifierToken)
            {
                name = Match(SyntaxKind.IdentifierToken);
            }

            AnnotationsSyntax annotations = null;

            if (Current.Kind == SyntaxKind.LessThanToken)
            {
                annotations = ParseAnnotations();
            }

            var openBrace = Match(SyntaxKind.OpenBraceToken);

            var passes = new List <PassSyntax>();

            while (Current.Kind != SyntaxKind.CloseBraceToken)
            {
                if (Current.Kind == SyntaxKind.PassKeyword)
                {
                    passes.Add(ParsePass());
                }
                else
                {
                    var action = SkipBadTokens(
                        p => p.Current.Kind != SyntaxKind.PassKeyword,
                        p => p.IsTerminator(),
                        SyntaxKind.CloseBraceToken);
                    if (action == PostSkipAction.Abort)
                    {
                        break;
                    }
                }
            }

            var closeBrace = Match(SyntaxKind.CloseBraceToken);
            var semicolon  = NextTokenIf(SyntaxKind.SemiToken);

            return(new TechniqueSyntax(technique, name, annotations, openBrace, passes, closeBrace, semicolon));
        }
示例#2
0
        private PassSyntax ParsePass()
        {
            var pass = Match(SyntaxKind.PassKeyword);

            SyntaxToken name = null;

            if (Current.Kind == SyntaxKind.IdentifierToken)
            {
                name = Match(SyntaxKind.IdentifierToken);
            }

            AnnotationsSyntax annotations = null;

            if (Current.Kind == SyntaxKind.LessThanToken)
            {
                annotations = ParseAnnotations();
            }

            var openBrace = Match(SyntaxKind.OpenBraceToken);

            var statements = new List <StatementSyntax>();

            while (Current.Kind != SyntaxKind.CloseBraceToken)
            {
                if (IsPossiblePassStatement())
                {
                    statements.Add(ParsePassStatement());
                }
                else
                {
                    var action = SkipBadTokens(
                        p => !p.IsPossiblePassStatement(),
                        p => p.IsTerminator(),
                        SyntaxKind.CloseBraceToken);
                    if (action == PostSkipAction.Abort)
                    {
                        break;
                    }
                }
            }

            var closeBrace = Match(SyntaxKind.CloseBraceToken);

            return(new PassSyntax(pass, name, annotations, openBrace, statements, closeBrace));
        }
        private TypeAliasSyntax ParseTypeAlias()
        {
            var name = Match(SyntaxKind.IdentifierToken);

            var arrayRankSpecifiers = new List <ArrayRankSpecifierSyntax>();

            if (Current.Kind == SyntaxKind.OpenBracketToken)
            {
                ParseArrayRankSpecifiers(arrayRankSpecifiers, false);
            }

            var qualifiers = new List <VariableDeclaratorQualifierSyntax>();

            while (Current.Kind == SyntaxKind.ColonToken)
            {
                if (IsPossibleVariableDeclaratorQualifier(Lookahead))
                {
                    qualifiers.Add(ParseVariableDeclaratorQualifier());
                }
                else
                {
                    var action = SkipBadTokens(
                        p => !p.IsPossibleVariableDeclaratorQualifier(Current),
                        p => p.Current.Kind == SyntaxKind.EqualsToken || p.Current.Kind == SyntaxKind.OpenBraceToken || p.IsTerminator(),
                        SyntaxKind.RegisterKeyword);
                    if (action == PostSkipAction.Abort)
                    {
                        break;
                    }
                }
            }

            AnnotationsSyntax annotations = null;

            if (Current.Kind == SyntaxKind.LessThanToken)
            {
                annotations = ParseAnnotations();
            }

            return(new TypeAliasSyntax(name, arrayRankSpecifiers, qualifiers, annotations));
        }
        private VariableDeclaratorSyntax ParseVariableDeclarator(TypeSyntax parentType, bool isExpressionContext = false)
        {
            if (!isExpressionContext)
            {
                // Check for the common pattern of:
                //
                // C                    //<-- here
                // Console.WriteLine();
                //
                // Standard greedy parsing will assume that this should be parsed as a variable
                // declaration: "C Console".  We want to avoid that as it can confused parts of the
                // system further up.  So, if we see certain things following the identifier, then we can
                // assume it's not the actual name.
                //
                // So, if we're after a newline and we see a name followed by the list below, then we
                // assume that we're accidently consuming too far into the next statement.
                //
                // <dot>, <arrow>, any binary operator (except =), <question>.  None of these characters
                // are allowed in a normal variable declaration.  This also provides a more useful error
                // message to the user.  Instead of telling them that a semicolon is expected after the
                // following token, then instead get a useful message about an identifier being missing.
                // The above list prevents:
                //
                // C                    //<-- here
                // Console.WriteLine();
                //
                // C                    //<-- here
                // Console->WriteLine();
                //
                // C
                // A + B; // etc.
                //
                // C
                // A ? B : D;
                var resetPoint = GetResetPoint();
                try
                {
                    var currentTokenKind = Current.Kind;
                    if (currentTokenKind == SyntaxKind.IdentifierToken && !parentType.IsMissing)
                    {
                        var isAfterNewLine = parentType.GetLastChildToken().TrailingTrivia.Any(t => t.Kind == SyntaxKind.EndOfLineTrivia);
                        if (isAfterNewLine)
                        {
                            NextToken();
                            currentTokenKind = Current.Kind;

                            var isNonEqualsBinaryToken =
                                currentTokenKind != SyntaxKind.EqualsToken &&
                                SyntaxFacts.IsBinaryExpression(currentTokenKind);

                            if (currentTokenKind == SyntaxKind.DotToken ||
                                isNonEqualsBinaryToken)
                            {
                                var missingIdentifier = InsertMissingToken(SyntaxKind.IdentifierToken);
                                return(new VariableDeclaratorSyntax(missingIdentifier,
                                                                    new List <ArrayRankSpecifierSyntax>(),
                                                                    new List <VariableDeclaratorQualifierSyntax>(),
                                                                    null, null));
                            }
                        }
                    }
                }
                finally
                {
                    Reset(ref resetPoint);
                }
            }

            var name = Match(SyntaxKind.IdentifierToken);

            var arrayRankSpecifiers = new List <ArrayRankSpecifierSyntax>();

            if (Current.Kind == SyntaxKind.OpenBracketToken)
            {
                ParseArrayRankSpecifiers(arrayRankSpecifiers, false);
            }

            var qualifiers = new List <VariableDeclaratorQualifierSyntax>();

            while (Current.Kind == SyntaxKind.ColonToken)
            {
                if (IsPossibleVariableDeclaratorQualifier(Lookahead))
                {
                    qualifiers.Add(ParseVariableDeclaratorQualifier());
                }
                else
                {
                    var action = SkipBadTokens(
                        p => !p.IsPossibleVariableDeclaratorQualifier(Current),
                        p => p.Current.Kind == SyntaxKind.EqualsToken || p.Current.Kind == SyntaxKind.OpenBraceToken || p.IsTerminator(),
                        SyntaxKind.RegisterKeyword);
                    if (action == PostSkipAction.Abort)
                    {
                        break;
                    }
                }
            }

            AnnotationsSyntax annotations = null;

            if (Current.Kind == SyntaxKind.LessThanToken)
            {
                annotations = ParseAnnotations();
            }

            InitializerSyntax initializer = null;

            if (Current.Kind == SyntaxKind.EqualsToken)
            {
                if (Lookahead.Kind == SyntaxKind.SamplerStateLegacyKeyword)
                {
                    initializer = ParseSamplerStateInitializer();
                }
                else
                {
                    var equals = NextToken();
                    var init   = ParseVariableInitializer();
                    initializer = new EqualsValueClauseSyntax(equals, init);
                }
            }
            else if (Current.Kind == SyntaxKind.OpenBraceToken)
            {
                if (Lookahead.Kind == SyntaxKind.OpenBraceToken)
                {
                    initializer = ParseStateArrayInitializer();
                }
                else
                {
                    initializer = ParseStateInitializer();
                }
            }

            return(new VariableDeclaratorSyntax(name, arrayRankSpecifiers, qualifiers, annotations, initializer));
        }
示例#5
0
 public virtual void VisitAnnotations(AnnotationsSyntax node)
 {
     DefaultVisit(node);
 }
 public virtual void VisitAnnotations(AnnotationsSyntax node)
 {
     DefaultVisit(node);
 }