Exemple #1
0
        public static (List <ASTError>, ASTType) Parse(IParser parser, List <ASTAnnotation> annotations, List <ASTDirective> directives)
        {
            List <ASTError> errors = new List <ASTError>();
            ASTType         result = new ASTType();

            result.Annotations = annotations;
            result.Directives  = directives;
            parser.Next();
            if (parser.Current.TokenType == TokenType.Identifier)
            {
                result.Name = parser.Current.Value;
                parser.Next();
            }

            while (parser.Current.TokenType == TokenType.GenericParameter)
            {
                result.Parameters.Add(parser.Current.Value);
                parser.Next();
            }

            /*
             * If there is an '=' sign we know that there will be
             * Fields which we can parse...
             */
            if (parser.Current.TokenType == TokenType.Equal)
            {
                parser.Next();
                while (parser.Current.TokenType != TokenType.ContextEnded)
                {
                    result.Fields.Add(new ASTTypeField(parser));
                }
                if (result.Fields.Count == 0)
                {
                    errors.Add(new ASTError($"Missing type body. If you use an '=' sign you should have at least one field."));
                }
            }
            else
            {
                parser.Next();
            }

            return(errors, result);
        }
        public static (List <ASTError>, ASTType) Parse(IParser parser, List <ASTAnnotation> annotations, List <ASTDirective> directives)
        {
            ASTType result = new ASTType();

            try
            {
                List <ASTError> errors = new List <ASTError>();

                result.Annotations = annotations;
                result.Directives  = directives;

                parser.Next();
                result.Name       = parser.Consume(TokenType.Identifier).Value;
                result.Parameters =
                    parser
                    .ConsumeWhile(TokenType.GenericParameter)
                    .Select(v => v.Value)
                    .ToList();


                /*
                 * If there is an 'extends' extension.
                 */
                var extends = parser.TryConsume(TokenType.KW_Extends);
                if (!(extends is null))
                {
                    result.Extensions =
                        parser
                        .ConsumeWhile(TokenType.Identifier)
                        .Select(v => v.Value)
                        .ToList();
                }


                /*
                 * If there is an '=' sign we know that there will be
                 * Fields which we can parse...
                 */
                var equals = parser.TryConsume(TokenType.Equal);
                if (!(equals is null))
                {
                    List <ASTTypeField> fields = new List <ASTTypeField>();
                    while (parser.TryConsume(TokenType.ContextEnded) == null)
                    {
                        fields.Add(ASTTypeField.Parse(parser));
                    }
                    result.Fields = fields;
                    if (fields.Count == 0)
                    {
                        if (parser.Current.TokenType == TokenType.Paragraph)
                        {
                            var nextPhrase = parser.Current.Value;
                            errors.Add(new ASTError($@"
Missing type body. If you use an '=' sign you should have at least one field.
It might be that you are missing an indentation:

type {result.Name} =
{nextPhrase}

Example:
type {result.Name} =
    {nextPhrase}
", parser.Current));
                        }
                        else
                        {
                            errors.Add(new ASTError($@"
Missing type body. If you use an '=' sign you should have at least one field.", parser.Current));
                        }
                    }
                }

                return(errors, result);
            }
            catch (InvalidTokenException ex)
            {
                return(new List <ASTError>
                {
                    new ASTError(ex.Message, parser.Current)
                }, result);
            }
        }