Exemplo n.º 1
0
        public static EXPRESSION parse(iSCOPE context)
        {
            Token token = get();

            forget();
            Span begin = token.span;

            CONDITIONAL result   = new CONDITIONAL();
            EXPRESSION  elsePart = null;

            EXPRESSION condition = EXPRESSION.parse(null, context);

            expect(TokenCode.Then);
            EXPRESSION thenPart = EXPRESSION.parse(null, context);

            COND_IF_THEN cit = new COND_IF_THEN(condition, thenPart);

            condition.parent = result;
            thenPart.parent  = result;
            cit.parent       = result;
            result.add(cit);

            token = get();
            while (true)
            {
                if (token.code == TokenCode.Elsif)
                {
                    forget();
                    condition = EXPRESSION.parse(null, context);
                    expect(TokenCode.Then);
                    thenPart = EXPRESSION.parse(null, context);

                    cit = new COND_IF_THEN(condition, thenPart);
                    cit.setSpan(token.span, thenPart.span);

                    condition.parent = result;
                    thenPart.parent  = result;
                    cit.parent       = result;

                    result.add(cit);
                    token = get();
                    // go to next iteration
                }
                else
                {
                    break;
                }
            }
            token           = expect(TokenCode.Else);
            elsePart        = EXPRESSION.parse(null, context);
            elsePart.parent = result;
            result.add(elsePart);
            result.setSpan(begin, elsePart.span);

            // context.add(result);  ?? why ??
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static void parse(bool hidden, bool final, Token start, iSCOPE context)
        {
            Debug.Indent();
            Debug.WriteLine("Entering CONSTANT.parse");

            CONSTANT constant = new CONSTANT();

            // 'const' and 'is' keywords were already parsed
            Token token = get();

            if (token.code == TokenCode.End)
            {
                forget(); goto FinalActions;
            }                                                                 // empty list

            while (true)
            {
                EXPRESSION left = EXPRESSION.parse(null, context);
                token = get();
                if (token.code == TokenCode.DotDot)   // range
                {
                    forget();
                    EXPRESSION right = EXPRESSION.parse(null, context);
                    BINARY     range = new BINARY(left, right);
                    range.setSpan(left.span, right.span);
                    range.parent = constant;
                    constant.constants.Add(range);
                }
                else // single expression
                {
                    left.parent = constant;
                    constant.constants.Add(left);
                }
                token = get();
                if (token.code == TokenCode.Comma)
                {
                    forget(); continue;
                }
                if (token.code == TokenCode.End)
                {
                    forget(); break;
                }
                else
                {
                    error(token, "syntax-error"); break;
                }
            }
FinalActions:
            constant.setSpecs(hidden, final);
            context.add(constant);
            constant.parent = context.self;
            constant.setSpan(start, token);

            Debug.WriteLine("Exiting CONSTANT.parse");
            Debug.Unindent();
        }
Exemplo n.º 3
0
        public static new EXPRESSION parse(Token first, iSCOPE context)
        {
            EXPRESSION result = ADDITIVE.parse(first, context);
            Token      token  = get();

            switch (token.code)
            {
            case TokenCode.Less:
            case TokenCode.LessEqual:
            case TokenCode.Greater:
            case TokenCode.GreaterEqual:
            case TokenCode.Equal:
            case TokenCode.NotEqual:
            case TokenCode.EqualEqual:
            case TokenCode.NotEqualDeep:
                forget(); break;

            case TokenCode.In:
                forget();
                EXPRESSION left = EXPRESSION.parse(null, context);
                token = get();
                if (token.code == TokenCode.DotDot)
                {
                    forget();
                    EXPRESSION right = EXPRESSION.parse(null, context);
                    RANGE_TYPE range = new RANGE_TYPE(left, right);
                    range.setSpan(left.span, right.span);
                    Span          start = result.span;
                    IN_EXPRESSION r     = new IN_EXPRESSION(token, result, range);
                    result.parent = r;
                    result        = r;
                    result.setSpan(start, range.span);
                    goto Out;
                }
                else
                {
                    // Something's wrong: right part of the in-expression
                    // should always be a range-type
                    goto Out;
                }

            default:
                goto Out;
            }
            EXPRESSION second = ADDITIVE.parse(null, context);
            Span       start2 = result.span;

            result = new RELATIONAL(token, result, second);
            result.setSpan(start2, second.span);
Out:
            return(result);
        }
Exemplo n.º 4
0
        private static void parseVariant(LOOP result)
        {
            while (true)
            {
                EXPRESSION inv = EXPRESSION.parse(null, result);
                if (inv == null)
                {
                    return;
                }
                result.addv(inv);
                inv.parent = result;

                Token token = get();
                if (token.code == TokenCode.Comma || wasEOL)
                {
                    forget();
                }
            }
        }
Exemplo n.º 5
0
        public static CALL parse(EXPRESSION reference, iSCOPE context)
        {
            CALL call = new CALL(reference);

            Token token;

            while (true)
            {
                EXPRESSION arg = EXPRESSION.parse(null, context);
                call.add(arg);
                token = get();
                if (token.code == TokenCode.Comma)
                {
                    forget(); continue;
                }
                break;
            }
            token = expect(TokenCode.RParen);
            call.setSpan(reference.span, token.span);

            return(call);
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <syntax>
        /// Loop    : while BooleanExpression
        ///           loop
        ///           [ invariant PredicateList ]
        ///           [ StatementList ]
        ///           [ variant PredicateList ]
        ///           end [ loop ]
        ///
        ///         | loop
        ///           [ invariant PredicateList ]
        ///           [ StatementList ]
        ///           [ variant PredicateList ]
        ///           [ while BooleanExpression ]
        ///           end [ loop ]
        /// </syntax>
        /// <returns></returns>
        public static void parse(Token id, iSCOPE context)
        {
            Debug.Indent();
            Debug.WriteLine("Entering LOOP.parse");

            // If id != null, then 'id' is a loop label.
            // ':' after label is already parsed.
            LOOP loop = new LOOP();

            Context.enter(loop);

            Token token = get();
            Token begin = token;

            if (token.code == TokenCode.While)
            {
                forget();
                loop.prefix = true;
                EXPRESSION whileExpr = EXPRESSION.parse(null, context);
                token = get();
                if (token.code == TokenCode.Loop)
                {
                    forget();
                    loop.addw(whileExpr);
                    whileExpr.parent = loop;
                }
                else // Syntax error
                {
                    error(token, "no-loop");
                }

                token = get();
                if (token.code == TokenCode.Invariant)
                {
                    forget();
                    parseInvariant(loop);
                }

                BODY body = new BODY();
                body.parent = loop;
                loop.body   = body;

                Context.enter(body);
                token = get();
                BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.ERROR, body);
                body.setSpan(token, get());
                Context.exit();

                token = get();
                if (token.code == TokenCode.Variant)
                {
                    forget();
                    parseVariant(loop);
                }
            }
            else if (token.code == TokenCode.Loop)
            {
                loop.prefix = false;
                forget();
                token = get();
                if (token.code == TokenCode.Invariant)
                {
                    forget();
                    parseInvariant(loop);
                }

                BODY body = new BODY();
                body.parent = loop;
                loop.body   = body;

                Context.enter(body);
                token = get();
                BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.While, body);
                body.setSpan(token, get());
                Context.exit();

                token = get();
                if (token.code == TokenCode.Variant)
                {
                    forget();
                    parseVariant(loop);
                }
                token = get();
                if (token.code == TokenCode.While)
                {
                    forget();
                    EXPRESSION whileExpr = EXPRESSION.parse(null, context);
                    loop.addw(whileExpr);
                    whileExpr.parent = loop;
                }
            }
            else // Compiler error
            {
                error(token, "system-bug");
            }

            token = get();
            if (token.code != TokenCode.End)   // Syntax error
            {
                error(token, "no-end", "loop");
            }
            else
            {
                forget();
                token = get();
                if (token.code == TokenCode.Loop)
                {
                    forget();
                }
            }

            if (loop != null)
            {
                loop.setSpan(begin, token);
            }

            context.add(loop);
            Context.exit();

            Debug.WriteLine("Exiting LOOP.parse");
            Debug.Unindent();
        }
Exemplo n.º 7
0
        public static void parse(Token routineName, bool hidden, bool final, bool isOverride, int pure_safe, iSCOPE context)
        {
            ROUTINE routine = null;

            if (routineName.code == TokenCode.Init)
            {
                Debug.Indent();
                Debug.WriteLine("Entering INITIALIZER.parse");

                routine = new INITIALIZER();
                goto Init;
            }

            Debug.Indent();
            Debug.WriteLine("Entering ROUTINE.parse (" + routineName.image + ")");

            string image = routineName.image;
            Token  token = get(); // What's after the routine name?

            switch (routineName.image)
            {
            case "and":
                if (token.code == TokenCode.Then)
                {
                    forget();
                    image += " then";
                    Span span = new Span(routineName.span, token.span);
                    routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier));
                }
                break;

            case "or":
                token = get();
                if (token.code == TokenCode.Else)
                {
                    forget();
                    image += " else";
                    Span span = new Span(routineName.span, token.span);
                    routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier));
                }
                break;
            }
            routine = new ROUTINE(pure_safe, isOverride, routineName);
Init:
            routine.setSpecs(hidden, final);
            Context.enter(routine);

            if (routineName.code == TokenCode.Init)
            {
                goto Init2;
            }

            token = get();
            if (token.code == TokenCode.Alias)
            {
                forget();
                token         = expect(TokenCode.Identifier);
                routine.alias = new IDENTIFIER(token.image);
            }

            if (token.code == TokenCode.LBracket)
            {
                // Generic routine
                forget();
                while (true)
                {
                    var generic = FORMAL_GENERIC.parse(context);
                    routine.add(generic);

                    token = get();
                    switch (token.code)
                    {
                    case TokenCode.Comma:
                    case TokenCode.Semicolon: forget(); continue;

                    case TokenCode.RBracket:  forget(); goto Finish;

                    default: { /* Syntax error */ break; }
                    }
                }
Finish:
                ;
            }

Init2:
            token = get();
            if (token.code == TokenCode.LParen)
            {
                forget(); token = get();
                if (token.code == TokenCode.RParen)
                {
                    // Empty parameter list
                    forget();
                    goto Weiter;
                }
                while (true)
                {
                    VARIABLE.parse(false, false, false, false, null, null, routine);

                    token = get();
                    if (token.code == TokenCode.Comma)
                    {
                        forget(); continue;
                    }
                    if (token.code == TokenCode.Semicolon)
                    {
                        forget(); continue;
                    }
                    break;
                }
                expect(TokenCode.RParen);
            }

Weiter:
            token = get();
            if (token.code == TokenCode.Colon)
            {
                forget();

                bool ref_val, conc;  // TEMP SOLUTION
                Span span2;
                routine.type = VARIABLE.parseTypeSpecifier(routine, out ref_val, out conc, out span2);
                if (routine.type != null)
                {
                    routine.type.parent = routine;
                    routine.type.setSpan(span2);
                }
            }

            token = get();
            if (token.code == TokenCode.Require)
            {
                forget(); token = get();
                if (token.code == TokenCode.Else)
                {
                    forget(); routine.requireElse = true;
                }
                while (true)
                {
                    EXPRESSION precondition = EXPRESSION.parse(null, routine);
                    routine.addPre(precondition);
                    precondition.parent = routine;

                    token = get();
                    if (token.code == TokenCode.Is || token.code == TokenCode.Arrow)
                    {
                        break;
                    }
                }
            }

            if (token.code == TokenCode.Arrow)
            {
                forget();

                BODY body = new BODY();
                routine.routineBody = body;
                body.parent         = routine;

                Context.enter(body);
                EXPRESSION expression = EXPRESSION.parse(null, body);
                RETURN     ret        = new RETURN(expression);
                expression.parent = ret;
                ret.setSpan(expression.span);

                ret.parent = body;
                body.add(ret);
                body.setSpan(ret.span);

                Context.exit();
            }
            else if (token.code == TokenCode.Is)
            {
                forget(); token = get();
                if (token.code == TokenCode.Abstract)
                {
                    forget();
                    routine.isAbstract = true;
                }
                else if (token.code == TokenCode.Foreign)
                {
                    forget();
                    routine.isForeign = true;
                }
                else
                {
                    BODY body = new BODY();
                    body.parent         = routine;
                    routine.routineBody = body;

                    Context.enter(body);
                    BODY.parse(TokenCode.End, TokenCode.Ensure, TokenCode.ERROR, body);
                    Context.exit();
                }
                token = get();
                if (token.code == TokenCode.Ensure)
                {
                    forget();
                    token = get();
                    if (token.code == TokenCode.Then)
                    {
                        forget(); routine.ensureThen = true;
                    }
                    ENTITY.weAreWithinEnsure = true;
                    while (true)
                    {
                        EXPRESSION postcondition = EXPRESSION.parse(null, routine);
                        routine.addPre(postcondition);
                        postcondition.parent = routine;

                        token = get();
                        if (token.code == TokenCode.End)
                        {
                            forget(); break;
                        }
                    }
                    ENTITY.weAreWithinEnsure = false;
                }
                else if (!routine.isAbstract && !routine.isForeign)
                {
                    expect(TokenCode.End);
                }
            }
            token = get();
            if (token.code == TokenCode.Semicolon)
            {
                forget();
            }

            Context.exit();
            context.add(routine);

            routine.parent = context.self;
            routine.setSpan(routineName, token);

            if (routineName.code == TokenCode.Init)
            {
                Debug.WriteLine("Exiting INITIALIZER.parse");
            }
            else
            {
                Debug.WriteLine("Exiting ROUTINE.parse");
            }
            Debug.Unindent();
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <syntax>
        /// UnitTypeName         : CompoundName [ GenericInstantiation ]
        ///
        /// GenericInstantiation : "[" (Type|Expression) { "," (Type|Expression) } "]"
        /// </syntax>
        /// <returns></returns>
        public static UNIT_REF parse(Token id, bool opt, iSCOPE context)
        {
            Debug.Indent();
            Debug.WriteLine("Entering UNIT_REF.parse");

            Token token = null;

            // We assume that 'id' is 'identifier'.
            if (id == null)
            {
                token = get(); forget();
            }
            else
            {
                token = id;
            }

            token = IDENTIFIER.parseCompoundName(token);
            if (token == null) /* an error was detected earlier */ return {
                (null);
            }
            Token start = token;

            UNIT_REF unit_ref = new UNIT_REF(token.image);

            unit_ref.opt     = opt;
            unit_ref.as_sign = true;
            DECLARATION unit = Context.find(token);

            if (unit != null && (unit is UNIT || unit is FORMAL_TYPE))
            {
                unit_ref.unit_ref = unit;
            }

            token = get();
            if (token.code == TokenCode.LBracket)
            {
                forget();
                while (true)
                {
                    TYPE type = null;
                    token = get();
                    if (token.code == TokenCode.LParen)
                    {
                        type = TUPLE_TYPE.parse(context);
                        unit_ref.add(type);
                        goto Delimiter;
                    }
                    EXPRESSION expr = EXPRESSION.parse(null, context);
                    if (expr is REFERENCE || expr is UNRESOLVED)
                    {
                        string name = null;
                        if (expr is REFERENCE)
                        {
                            if ((expr as REFERENCE).declaration is UNIT)
                            {
                                name = (expr as REFERENCE).declaration.name.identifier;
                            }
                            else
                            {
                                goto NonType;
                            }
                        }
                        else // UNRESOLVED
                        {
                            name = (expr as UNRESOLVED).name.identifier;
                        }

                        id   = new Token(expr.span, TokenCode.Identifier, name, new Category(CategoryCode.identifier));
                        type = UNIT_REF.parse(id, false, context); // Recursive call

                        unit_ref.add(type);
                        type.parent = unit_ref;
                        goto Delimiter;
                    }
                    // else -- expr is perhaps a non-type argument
NonType:
                    token = get();
                    if (token.code == TokenCode.DotDot)
                    {
                        // This is actually a range _type_
                        forget();
                        EXPRESSION right = EXPRESSION.parse(null, context);
                        RANGE_TYPE range = new RANGE_TYPE(expr, right);
                        range.setSpan(expr.span, right.span);
                        unit_ref.add(range);
                        range.parent = unit_ref;
                    }
                    else // Definitely a non-type argument
                    {
                        unit_ref.add(expr);
                        expr.parent = unit_ref;
                    }
Delimiter:
                    token = get();
                    switch (token.code)
                    {
                    case TokenCode.Comma:    forget(); continue;

                    case TokenCode.RBracket: forget(); goto Finish;

                    default: { /* Syntax error in generic actuals */ break; }
                    }
                }
Finish:
                unit_ref.setSpan(start.span, token.span);
            }
            else
            {
                unit_ref.setSpan(start);
            }

            Debug.WriteLine("Exiting UNIT_REF.parse");
            Debug.Unindent();

            return(unit_ref);
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        new public static TUPLE_TYPE parse(iSCOPE context)
        {
            Debug.Indent();
            Debug.WriteLine("Entering TUPLE_TYPE.parse");

            TUPLE_TYPE tuple = new TUPLE_TYPE();

            Token begin = expect(TokenCode.LParen);

            int   count = 0;
            Token token = get();

            if (token.code == TokenCode.RParen)
            {
                // Empty tuple
                forget(); goto OutLoop;
            }

            var      ids = new List <Token>();
            UNIT_REF unit_type;

            while (true)
            {
                token = expect(TokenCode.Identifier);

                Token delimiter = get();
                switch (delimiter.code)
                {
                case TokenCode.Comma:
                    // Identifier is the current name in the list of fields
                    forget();
                    ids.Add(token);
                    continue;

                case TokenCode.Colon:
                    // Identifier is the last name in the field of fields
                    forget();
                    ids.Add(token);

                    // Now we treat collected ids as names of tuple fields,
                    // and the construct following ':' as unit name.
                    unit_type        = UNIT_REF.parse(null, false, context);
                    unit_type.parent = tuple;

                    foreach (Token id in ids)
                    {
                        if (tuple.exists(id.image))       // Error: duplicate name
                        {
                        }
                        else
                        {
                            tuple.add(id.image, unit_type);
                        }
                    }
                    token = get();
                    if (token.code == TokenCode.Is)
                    {
                        forget();
                        EXPRESSION expr = EXPRESSION.parse(null, context);
                        foreach (Token id in ids)
                        {
                            if (tuple.exists(id.image))
                            {
                            }
                            else
                            {
                                tuple.add(id.image, expr);
                            }
                        }
                    }
                    ids.Clear();
                    break;

                case TokenCode.Is:
                    ids.Add(token);
                    // No explicit type for the field(s) but only initializer
                    forget();
                    EXPRESSION expr2 = EXPRESSION.parse(null, context);
                    foreach (Token id in ids)
                    {
                        if (tuple.exists(id.image))        // Error: duplicate name
                        {
                        }
                        else
                        {
                            tuple.add(id.image, expr2);
                        }
                    }
                    ids.Clear();
                    break;

                case TokenCode.RParen:
                case TokenCode.Semicolon:
                    forget();
                    Token stop = delimiter;
                    // ')': The end of the tuple type; this means that all previous ids were
                    //      actually simple type names.
                    // ';': The end of the current part of the tuple type. Again, this means
                    //      that all ids were type names.
                    // In both cases, field names were omitted, and we have to assign
                    // artifical names for those fields.
                    ids.Add(token);
                    foreach (Token id in ids)
                    {
                        unit_type = UNIT_REF.parse(id, false, context);
                        count++;
                        string n = "$" + count.ToString();
                        tuple.add(n, unit_type);
                    }
                    ids.Clear();
                    if (stop.code == TokenCode.Semicolon)
                    {
                        continue;
                    }
                    else
                    {
                        goto OutLoop;
                    }

                case TokenCode.LBracket:
                    ids.Add(token);
                    // 'ids' collected before, are actually unit names:
                    // all of them before the last one, were simple names,
                    // and the last one is like 'name[...'.
                    foreach (Token id in ids)
                    {
                        unit_type = UNIT_REF.parse(id, false, context);
                        count++;
                        string n = "$" + count.ToString();
                        tuple.add(n, unit_type);
                    }
                    break;
                } // switch
            }     // while

OutLoop:
            tuple.setSpan(begin, token);

            Debug.WriteLine("Exiting TUPLE_TYPE.parse");
            Debug.Unindent();

            return(tuple);
        }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static new EXPRESSION parse(Token first, iSCOPE context)
        {
            EXPRESSION result = null;
            Token      token  = (first == null) ? get() : first;
            Token      begin  = token;

            switch (token.code)
            {
            case TokenCode.This:
                forget();
                UNIT unit = Context.unit();
                if (unit == null)
                {
                    // Error message!
                    result = new THIS(null);
                }
                else
                {
                    result = new THIS(unit);
                    result.setSpan(token);
                }
                break;

            case TokenCode.Return:
                if (!ENTITY.weAreWithinEnsure)
                {
                    break;
                }
                forget();
                ROUTINE routine = Context.routine();
                if (routine == null)
                {
                }       // error
                else
                {
                    result = new RETURN_EXPR(routine);
                    result.setSpan(token);
                }
                break;

            case TokenCode.Old:
                forget();
                result = EXPRESSION.parse(null, context);
                Span end = result.span;
                OLD  old = new OLD(result);
                result.parent = old;
                result        = old;
                result.setSpan(begin.span, end);
                break;

            case TokenCode.If:
                result = CONDITIONAL.parse(context);
                break;

            case TokenCode.LParen:
                forget();
                Span start_tuple = token.span;
                result = EXPRESSION.parse(null, context);
                token  = get();
                if (token.code == TokenCode.Comma)
                {
                    // Seems to be a tuple
                    forget();
                    TUPLE_EXPR tuple = new TUPLE_EXPR();
                    tuple.add(result);
                    while (true)
                    {
                        EXPRESSION expr = EXPRESSION.parse(null, context);
                        tuple.add(expr);

                        token = get();
                        if (token.code != TokenCode.Comma)
                        {
                            break;
                        }
                        forget();
                    }
                    result = tuple;
                }
                end = expect(TokenCode.RParen).span;
                result.setSpan(token.span, end);
                break;

            case TokenCode.Identifier:
                if (first == null)
                {
                    forget();                      ////// perhaps the same condition should be added for all cases?
                }
                DECLARATION d = Context.find(token);
                if (d == null)
                {
                    result = new UNRESOLVED(context, new IDENTIFIER(token));
                }
                else
                {
                    result = new REFERENCE(d);
                }

                Token token2 = get();
                if (token2.code == TokenCode.LBracket)
                {
                    UNIT_REF unitRef = UNIT_REF.parse(token, false, context);
                    result = new NEW(unitRef);
                    result.setSpan(unitRef.span);
                }
                else
                {
                    result.setSpan(token);
                }
                break;

            case TokenCode.Integer:
            case TokenCode.Real:
            case TokenCode.String:
            case TokenCode.Character:
                result = new LITERAL(token.value, token.span, token.code);
                result.setSpan(token);
                forget();
                break;

            default:
                return(null);
            }
            // result.setSpan(token);
            return(result);
        }
Exemplo n.º 11
0
        public static new EXPRESSION parse(Token first, iSCOPE context)
        {
            EXPRESSION result = PRIMARY.parse(first, context);

            if (result == null)
            {
                return(null);
            }

            Span begin = result.span;

            while (true)
            {
                Token token = get();
                switch (token.code)
                {
                case TokenCode.Dot:
                    forget();
                    IDENTIFIER identifier;
                    token = get();
                    if (token.code == TokenCode.Init)
                    {
                        forget();
                        // Initializer call in full form
                        identifier = new IDENTIFIER(INITIALIZER.initName);
                    }
                    else if (token.code == TokenCode.Identifier)
                    {
                        forget();
                        identifier = new IDENTIFIER(token);
                    }
                    else     // syntax error
                    {
                        identifier = new IDENTIFIER("ERROR");
                    }
                    result = new MEMBER(result, identifier);
                    result.setSpan(begin, token.span);
                    break;

                case TokenCode.LParen:
                    forget();
                    result = new CALL(result);
                    while (true)
                    {
                        token = get();
                        if (token.code == TokenCode.RParen)
                        {
                            forget(); break;
                        }
                        EXPRESSION actual = EXPRESSION.parse(null, context);
                        (result as CALL).add(actual);
                        token = get();
                        if (token.code == TokenCode.Comma)
                        {
                            forget(); continue;
                        }
                        token = expect(TokenCode.RParen);
                        break;
                    }
                    result.setSpan(begin, token.span);
                    break;

                default:
                    goto Out;
                }
            }
Out:
            return(result);
        }
Exemplo n.º 12
0
        /// <summary>
        /// The function processes all kinds of statements including
        /// assignments, calls, and variable/constant declarations.
        /// </summary>
        /// <param name="context"></param>
        /// <returns>true, if any construct (a statement, or a simple declaration)
        /// was processed, and false otherwise.</returns>
        public static bool parse(iSCOPE context, TokenCode stop1, TokenCode stop2, TokenCode stop3)
        {
            Debug.Indent();
            Debug.WriteLine("Entering STATEMENT.parse");

            bool result = true;

            Token token = get();
            Token begin = token;

            if (token.code == stop1 && stop1 != TokenCode.ERROR)
            {
                goto Finish;
            }
            if (token.code == stop2 && stop2 != TokenCode.ERROR)
            {
                goto Finish;
            }
            if (token.code == stop3 && stop3 != TokenCode.ERROR)
            {
                goto Finish;
            }

            switch (token.code)
            {
            // case TokenCode.Pure: -- doesn't make any sense
            case TokenCode.Safe:
            case TokenCode.Routine:
                ROUTINE.parse(token, false, false, false, 0, context);
                break;

            case TokenCode.If:
                IF.parse(context);
                break;

            case TokenCode.While:
            case TokenCode.Loop:
                LOOP.parse(null, context);
                break;

            // Break
            //     : break [ Label ]
            //
            // Label
            //     : Identifier
            case TokenCode.Break:
                BREAK.parse(context);
                break;

            // Statement
            //     : ...
            //     | raise [ Expression ]
            case TokenCode.Raise:
                forget();
                RAISE.parse(token.span, context);
                break;

            // Statement
            //     : ...
            //     | check PredicatesList end
            //     | ...
            case TokenCode.Check:
                CHECK.parse(context);
                break;

            // Statement
            //     : ...
            //     | return [ Expression ]
            //     | ...
            case TokenCode.Return:
                forget();
                EXPRESSION expr = EXPRESSION.parse(null, context);    // can be null
                RETURN     ret  = new RETURN(expr);
                if (expr != null)
                {
                    expr.parent = ret;
                    ret.setSpan(begin.span, expr.span);
                }
                else
                {
                    ret.setSpan(begin);
                }

                context.add(ret);
                break;

            // Statement
            //     : ...
            //     | Try
            //     | ...
            //
            // Try
            //     : try { Statement } Catches [ Else ] end
            //
            // Catches
            //     : catch [ "(" [ Identifier ":" ] Type ")" ] { Statement }
            //
            // Else
            //     : else { Statement }
            case TokenCode.Try:
                TRY.parse(context);
                break;

            // Statement
            //     : ...
            //     | ? Identifier
            //     | ...
            //
            case TokenCode.Question:

                break;

            case TokenCode.Init:
                // Initializer call
                forget();
                Token start = token;

                DECLARATION init = Context.find(INITIALIZER.initName);
                EXPRESSION  initRef;
                if (init != null)
                {
                    initRef = new REFERENCE(init);
                }
                else
                {
                    initRef = new UNRESOLVED(context, new IDENTIFIER(INITIALIZER.initName));
                }

                CALL call = new CALL(initRef);
                token = expect(TokenCode.LParen);
                while (true)
                {
                    EXPRESSION argument = EXPRESSION.parse(null, context);
                    call.add(argument);
                    argument.parent = call;

                    token = get();
                    if (token.code == TokenCode.Comma)
                    {
                        forget(); continue;
                    }
                    break;
                }
                token = expect(TokenCode.RParen);
                call.setSpan(start.span, token.span);
                context.add(call);
                break;

            case TokenCode.Identifier:
            {
                // Several different cases:
                //   - a label in front of while/loop
                //   - a declaration
                //   - a statement

                forget();
                Token next = get();
                if (next.code == TokenCode.LParen)
                {
                    forget();
                    TokenCode codeAfter = saveTokensUntilRightParenth(next);
                    switch (codeAfter)
                    {
                    case TokenCode.Colon:
                    case TokenCode.Is:
                    // case TokenCode.Do:
                    case TokenCode.Arrow:
                        //This as a routine declaration!!
                        ROUTINE.parse(token, false, false, false, 0, context);
                        goto Weiter;
                    }
                }
                EXPRESSION attempt = EXPRESSION.parse(token, context);
                if (attempt is UNRESOLVED)
                {
                    // Might be a label or a declaration...
                    Token idd = new Token(attempt.span, TokenCode.Identifier,
                                          (attempt as UNRESOLVED).name.identifier,
                                          new Category(CategoryCode.identifier));
                    token = get();
                    switch (token.code)
                    {
                    case TokenCode.Is:
                    case TokenCode.Comma:
                        // This is definitely a declaration
                        forget();
                        VARIABLE.parse(false, false, false, false, idd, token, context);
                        goto Weiter;

                    case TokenCode.Colon:
                        forget();
                        Token token2 = get();
                        if (token2.code == TokenCode.While || token2.code == TokenCode.Loop)
                        {
                            // This is a real label! Don't 'forget()'.
                            LOOP.parse(idd, context);
                        }
                        else
                        {
                            // This is definitely a variable declaration.
                            // Don't forget()
                            VARIABLE.parse(false, false, false, false, idd, token, context);
                        }
                        goto Weiter;

                    default:
                        // Nothing to do; just going further
                        break;
                    }
                }
                // 'attempt' is something else: a call or the left part of an assignment
                token = get();
                if (token.code == TokenCode.Assign)
                {
                    forget();
                    EXPRESSION right = EXPRESSION.parse(null, context);
                    ASSIGNMENT res   = new ASSIGNMENT(attempt, right);
                    res.setSpan(attempt.span, right.span);
                    context.add(res);
                }
                else
                {
                    if (!(attempt is CALL))       // something's wrong
                    {
                        result = false;
                    }
                    context.add(attempt);
                }
Weiter:
                break;
            }

            case TokenCode.Const:
                // Something like
                //     const a is 5...
                // OR
                //     const is a, b, ... end
                forget(); token = get();
                if (token.code == TokenCode.Is)
                {
                    forget();
                    CONSTANT.parse(context);
                }
                else
                {
                    VARIABLE.parse(false, false, true, false, null, null, context);
                }
                break;

            default:
                // Something else, e.g., (a... or this...
                // Either a function call or an assignment.
                //
                //     this := ...
                //     (if cond then a else b).f ...
                //     ^

                EXPRESSION e = EXPRESSION.parse(null, context);
                if (e == null)
                {
                    // Either an error or just the end of statement sequence
                    result = false;
                    break;
                }
                token = get();
                if (token.code == TokenCode.Assign)
                {
                    forget();
                    EXPRESSION right      = EXPRESSION.parse(null, context);
                    ASSIGNMENT assignment = new ASSIGNMENT(e, right);
                    assignment.setSpan(e.span, right.span);
                    context.add(assignment);
                }
                else
                {
                    context.add(e);
                }
                break;
            }
Finish:
            Debug.WriteLine("Exiting STATEMENT.parse");
            Debug.Unindent();

            return(result);
        }
Exemplo n.º 13
0
        /// <summary>
        ///
        /// <syntax><code>
        /// Объявление-переменных
        ///         : Список-идентификаторов [ : Спецификатор-типа ] is Выражение
        ///         | Список-идентификаторов : Спецификатор-типа
        ///
        /// Спецификатор-типа
        ///         : [ ? | ref | val | concurrent ] Тип
        ///         | as Составное-имя
        /// </code></syntax>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static void parse(bool hidden, bool ffinal, bool isConst, bool isOverride, Token id, Token afterId, iSCOPE context)
        {
            Debug.Indent();
            Debug.WriteLine("Entering VARIABLE.parse");

            TYPE       type        = null;
            EXPRESSION initializer = null;

            bool RefVal  = false; // 'val' by default
            bool Conc    = false;
            bool Abstr   = false;
            bool Foreign = false;

            Token token = id;
            Span  final = null;

            // If id==null, then we just start processing the declaration.
            // In this case, always afterId==null.
            //
            // If id != null then it contains an identifier.
            // In this case, afterId contains ':', ',' or 'is' - three
            // options starting a declaration.

            List <Token> identifiers = new List <Token>();

            if (token == null)
            {
                // We just start parsing variable declaration
                token = get(); forget();
                identifiers.Add(token);

                // Parsing the rest of identifiers in the list (if any)
                while (true)
                {
                    token = get();
                    if (token.code != TokenCode.Comma)
                    {
                        break;
                    }
                    forget();
                    token = expect(TokenCode.Identifier);
                    identifiers.Add(token);
                }
            }
            else
            {
                if (afterId.code == TokenCode.Is)
                {
                    // id is ...
                    identifiers.Add(id);
                    token = afterId;
                }
                else if (afterId.code == TokenCode.Colon)
                {
                    // 'token' contains an identifier.
                    // Check what's after this identifier?
                    //
                    //  id : ...
                    //       ^
                    identifiers.Add(id);
                    token = afterId;
                }
                else if (afterId.code == TokenCode.Comma)
                {
                    // 'token' contains an identifier,
                    // and comma goes after it (comma was parsed already).
                    //
                    //  id, ...
                    //      ^
                    identifiers.Add(id);

                    // Parsing the rest of the list of identifiers.
                    while (true)
                    {
                        token = expect(TokenCode.Identifier);
                        identifiers.Add(token);
                        if (token.code != TokenCode.Comma)
                        {
                            break;
                        }
                        forget();
                    }
                }
                else
                {
                    // Syntax error
                    error(afterId, "syntax-error", "declaration");
                }
            }
            // Here, we have parsed the list of identifiers.
            // 'token' contains the token after the list.
            // Only two valid options are ':' or 'is'.

            if (token.code == TokenCode.Colon)
            {
                // Type is expected.
                if (afterId == null)
                {
                    forget();
                }
                type  = parseTypeSpecifier(context, out RefVal, out Conc, out final);
                token = get();
            }
            if (token.code == TokenCode.Is)
            {
                forget(); token = get();
                if (token.code == TokenCode.Abstract)
                {
                    forget();
                    Abstr = true;
                    final = token.span;
                }
                else if (token.code == TokenCode.Foreign)
                {
                    forget();
                    Foreign = true;
                    final   = token.span;
                }
                else
                {
                    forget();
                    initializer = EXPRESSION.parse(token, context);
                    if (type == null)
                    {
                        // Type inference
                        // initializer.calculateType();
                        type = initializer.type;
                    }
                    final = initializer.span;
                }
            }

            // Here, we have the list of identifiers, their type,
            // and perhaps the initializer.
            // Create the final node(s) for variable declaration(s)
            // out of identifier(s), type and initializer.

            foreach (Token ident in identifiers)
            {
                VARIABLE varDecl = new VARIABLE(new IDENTIFIER(ident), type, initializer);
                varDecl.setSpecs(hidden, ffinal);
                varDecl.setVarSpecs(isConst, isOverride, RefVal, Conc, Abstr, Foreign);
                varDecl.setSpan(ident.span, final != null ? final : ident.span);
                context.add(varDecl);

                varDecl.parent = context.self;

                if (type != null)
                {
                    type.parent = varDecl;
                }
                if (initializer != null)
                {
                    initializer.parent = varDecl;
                }
            }

            Debug.WriteLine("Exiting VARIABLE.parse");
            Debug.Unindent();
        }