Example #1
0
        private void Match(TK kind)
        {
            if (next < limit && Tokens[next].Kind == kind)
            {
                next += 1;
                return;
            }

            Errors.AtToken(ErrNo.Scan001, Current, $"unknown token in token stream, expected ``{Keywords.String(kind)}´´ but got ``{Keywords.String(Current.Kind)}´´");
            throw new NotImplementedException();
        }
Example #2
0
        public Unit ParseUnit()
        {
            next = 0;

            var unit = Unit();

            if (!Is(TK.Eof))
            {
                Errors.AtToken(ErrNo.Scan002, Current, "inconclusive parse, not at ``EOF´´");
            }

            return(unit);
        }
Example #3
0
        private IExpression?TryInfix(bool next = false)
        {
            Begin();
            var term = TryTerm(next);

            if (term != null)
            {
                var op = BinaryOp();
                if (op == Tree.BinaryOp.As)
                {
                    var types = new List <(BinaryOp op, IType ty)>();

                    do
                    {
                        MatchAny();
                        var nextType = Type();
                        types.Add((op, nextType));
                    }while ((op = BinaryOp()) != Tree.BinaryOp.Missing);

                    var operators = types.Select(t => t.op).Distinct().ToList();

                    if (operators.Count >= 2)
                    {
                        var part  = types.Where(p => p.op != operators[0]).Select(p => p.ty).First();
                        var token = Tokens[part.Span.Start];
                        Errors.AtToken(
                            ErrNo.Scan002,
                            token,
                            "binary operators have no precedence, use ( ) to group binary expressions");
                    }

                    var operands = types.Select(t => t.ty).ToList();
                    return(new As(End(), term, operands));
                }
                if (op != Tree.BinaryOp.Missing)
                {
                    var terms = new List <(BinaryOp op, IExpression ex)>();

                    do
                    {
                        MatchAny();
                        var nextTerm = Term();
                        terms.Add((op, nextTerm));
                    }while ((op = BinaryOp()) != Tree.BinaryOp.Missing);

                    var operators = terms.Select(t => t.op).Distinct().ToList();

                    if (operators.Count >= 2)
                    {
                        var part  = terms.Where(p => p.op != operators[0]).Select(p => p.ex).First();
                        var token = Tokens[part.Span.Start];
                        Errors.AtToken(
                            ErrNo.Scan002,
                            token,
                            "binary operators have no precedence, use ( ) to group binary expressions");
                    }
                    var @operator = operators[0];
                    var operands  = Enumerable.Repeat(term, 1).Concat(terms.Select(t => t.ex)).ToList();
                    return(new Binary(End(), @operator, operands));
                }
            }

            return(Scrap(term));
        }
Example #4
0
 private Exception Expected(string what)
 {
     Errors.AtToken(ErrNo.Scan004, Current, $"can't parse ``{what}´´, at token ``{Keywords.String(Current.Kind)}´´");
     return(new NotImplementedException());
 }