Пример #1
0
        private ExpressionList ParseExpressionList(JsonEntity o)
        {
            EntityMixin.CheckType(o, EXPRESSION_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new ExpressionList(ParseChildren(o).OfType <Expression>()));
        }
Пример #2
0
        private RoutineDeclaration.ParameterList ParseParameterList(JsonEntity o)
        {
            EntityMixin.CheckType(o, PARAMETER_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new RoutineDeclaration.ParameterList(ParseChildren(o).OfType <RoutineDeclaration.Parameter>()));
        }
Пример #3
0
        private DeclarationList ParseDeclarationList(JsonEntity o)
        {
            EntityMixin.CheckType(o, DECLARATION_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new DeclarationList(ParseChildren(o).OfType <Declaration>().ToList()));
        }
Пример #4
0
        private RoutineDeclaration ParseRoutine(JsonEntity o)
        {
            EntityMixin.CheckType(o, ROUTINE);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                // name
                var name = children.OfType <Identifier>().Single();
                // is foreign? (may be null in JSON but not in an object model)
                var isForeign = children.OfType <ForeignSpec>().Select(spec => spec.IsForeign).DefaultIfEmpty(false)
                                .SingleOrDefault();
                // parameters list
                var parameters = children.OfType <RoutineDeclaration.ParameterList>().Single().Parameters;
                // return type (may be null is JSON but not in an object model)
                var returnType = children.OfType <UnitRef>().DefaultIfEmpty(UnitRef.Void).SingleOrDefault();
                // precondition body (may be null)
                var pre = children.OfType <PreCondition>().DefaultIfEmpty(new PreCondition()).SingleOrDefault();
                // routine body
                var body = children.OfType <EntityList>().Single().Children;
                // postcondition body (may be null)
                var post = children.OfType <PostCondition>().DefaultIfEmpty(new PostCondition()).SingleOrDefault();

                // ReSharper disable ArgumentsStyleNamedExpression
                return new RoutineDeclaration(
                    name: name,
                    isForeign: isForeign,
                    parameters: parameters,
                    returnType: returnType,
                    body: body,
                    preCondition: pre,
                    postCondition: post);
            }));
        }
Пример #5
0
        private EntityList ParseEntityList(JsonEntity o)
        {
            EntityMixin.CheckType(o, ENTITY_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new EntityList(ParseChildren(o).ToList()));
        }
Пример #6
0
        private If.StmtIfThenList ParseStmtIfThenList(JsonEntity o)
        {
            EntityMixin.CheckType(o, STMT_IF_THEN_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new If.StmtIfThenList(ParseChildren(o).OfType <If.StmtIfThen>().ToList()));
        }
Пример #7
0
        private Return ParseReturn(JsonEntity o)
        {
            EntityMixin.CheckType(o, RETURN);
            EntityMixin.ValueMustBeNull(o);

            var optionalExpression = ParseChildren(o).OfType <Expression>().FirstOrDefault();

            return(new Return(optionalExpression));
        }
Пример #8
0
        private PostCondition ParsePostCondition(JsonEntity o)
        {
            EntityMixin.CheckType(o, POSTCONDITION);
            EntityMixin.ValueMustBeNull(o);

            var expressions = ParseChildren(o).OfType <ExpressionList>().FirstOrDefault();

            return(new PostCondition(expressions));
        }
Пример #9
0
        private Callee ParseCallee(JsonEntity o)
        {
            EntityMixin.CheckType(o, CALLEE);
            EntityMixin.ValueMustBeNull(o);

            var unit    = o.Children[0] == null ? null : ParseIdentifier(o.Children[0]);
            var routine = ParseIdentifier(o.Children[1]);

            return(new Callee(unit, routine));
        }
Пример #10
0
        private Identifier ParseIdentifier(JsonEntity o)
        {
            EntityMixin.CheckType(o, IDENTIFIER);
            EntityMixin.ValueMustNotBeNull(o);
            if (string.IsNullOrEmpty(o.Value))
            {
                throw new JsonFormatException(o, "Identifier must not be empty");
            }

            return(new Identifier(o.Value));
        }
Пример #11
0
        private UnitRef ParseUnitRef(JsonEntity o)
        {
            EntityMixin.CheckType(o, UNIT_REF);
            EntityMixin.ValueMustNotBeNull(o);
            if (string.IsNullOrEmpty(o.Value))
            {
                throw new JsonFormatException(o, "Unit Ref must not be empty");
            }

            return(new UnitRef(new Identifier(o.Value)));
        }
Пример #12
0
        public Compilation ParseCompilation(JsonEntity o)
        {
            EntityMixin.CheckType(o, COMPILATION);
            EntityMixin.ValueMustBeNull(o);

            var children     = ParseChildren(o).ToList();
            var declarations = children.OfType <DeclarationList>().FirstOrDefault()?.Declarations;
            var routine      = children.OfType <RoutineDeclaration>().FirstOrDefault();

            return(new Compilation(declarations, routine));
        }
Пример #13
0
        private Reference ParseReference(JsonEntity o)
        {
            EntityMixin.CheckType(o, REFERENCE);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var name = children.OfType <Identifier>().Single();

                return new Reference(name);
            }));
        }
Пример #14
0
        private If.StmtIfThen ParseStmtIfThen(JsonEntity o)
        {
            EntityMixin.CheckType(o, STMT_IF_THEN);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var condition = children.OfType <Expression>().Single();
                var body = children.OfType <EntityList>().Single().Children;
                return new If.StmtIfThen(condition, body);
            }));
        }
Пример #15
0
        private If ParseIf(JsonEntity o)
        {
            EntityMixin.CheckType(o, IF);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var pairs = children.OfType <If.StmtIfThenList>().Single();
                var @else = children.OfType <EntityList>().SingleOrDefault();
                return new If(pairs, @else);
            }));
        }
Пример #16
0
        private Call ParseCall(JsonEntity o)
        {
            EntityMixin.CheckType(o, CALL);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var callee = children.OfType <Callee>().Single();
                var arguments = children.OfType <ExpressionList>().SingleOrDefault();

                return new Call(callee, arguments);
            }));
        }
Пример #17
0
        private RoutineDeclaration.Parameter ParseParameter(JsonEntity o)
        {
            EntityMixin.CheckType(o, PARAMETER);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                // TODO: replace with more general TYPE (when it will be specified and implemented)
                var type = children.OfType <UnitRef>().Single();
                var name = children.OfType <Identifier>().Single();
                return new RoutineDeclaration.Parameter(type, name);
            }));
        }
Пример #18
0
        private Literal ParseLiteral(JsonEntity o)
        {
            EntityMixin.CheckType(o, LITERAL);
            EntityMixin.ValueMustNotBeNull(o);

            var unitRef = ParseChildren(o).OfType <UnitRef>().FirstOrDefault();

            if (unitRef == null)
            {
                throw new JsonFormatException(o, "literal unit reference not found");
            }

            return(new Literal(o.Value, unitRef));
        }
Пример #19
0
        private ForeignSpec ParseForeignSpec(JsonEntity o)
        {
            EntityMixin.CheckType(o, FOREIGN_SPEC);
            switch (o.Value)
            {
            case null:
                return(new ForeignSpec(false));

            case "foreign":
                return(new ForeignSpec(true));

            default:
                throw new JsonFormatException(o, "invalid foreign spec: value must be either null or \"foreign\"");
            }
        }
Пример #20
0
        private Loop ParseLoop(JsonEntity o)
        {
            EntityMixin.CheckType(o, LOOP);

            return(Guard(o, children =>
            {
                var exit_condition = children.OfType <Expression>().DefaultIfEmpty().SingleOrDefault();
                var body = children.OfType <EntityList>().SingleOrDefault();

                // var invariants = children.OfType<Expression>().First();
                // var variants = children.OfType<Expression>().Skip(1).First();

                return new Loop(exit_condition, body);
            }));
        }
Пример #21
0
        private UnitDeclaration.ConcurrentSpec ParseConcurrentSpec(JsonEntity o)
        {
            EntityMixin.CheckType(o, CONCURRENT_SPEC);

            switch (o.Value)
            {
            case null:
                return(new UnitDeclaration.ConcurrentSpec(concurrent: false));

            case "concurrent":
                return(new UnitDeclaration.ConcurrentSpec(concurrent: true));

            default:
                throw new JsonFormatException(o, $"{CONCURRENT_SPEC} must be either \"concurrent\" or null");
            }
        }
Пример #22
0
        private UnitDeclaration.RefValSpec ParseRefValSpec(JsonEntity o)
        {
            EntityMixin.CheckType(o, REF_VAL_SPEC);
            EntityMixin.ValueMustNotBeNull(o);

            switch (o.Value)
            {
            case "ref":
                return(new UnitDeclaration.RefValSpec(isRef: true));

            case "val":
                return(new UnitDeclaration.RefValSpec(isRef: false));

            default:
                throw new JsonFormatException(o, $"{REF_VAL_SPEC} must be either ref or val");
            }
        }
Пример #23
0
        private Assignment ParseAssignment(JsonEntity o)
        {
            EntityMixin.CheckType(o, ASSIGNMENT);
            EntityMixin.ValueMustBeNull(o);

            var children = ParseChildren(o).OfType <Expression>().ToList();

            if (children.Count != 2)
            {
                throw new JsonFormatException(o,
                                              $"assignment must have exactly 2 expression children, got: {children.Count}");
            }

            var lValue = children[0];
            var rValue = children[1];

            return(new Assignment(lValue, rValue));
        }
Пример #24
0
        private VariableDeclaration ParseVariable(JsonEntity o)
        {
            EntityMixin.CheckType(o, VARIABLE);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var name = children.OfType <Identifier>().Single();
                var refValSpec = children.OfType <UnitDeclaration.RefValSpec>()
                                 .DefaultIfEmpty(new UnitDeclaration.RefValSpec()).SingleOrDefault();
                var concurrentSpec = children.OfType <UnitDeclaration.ConcurrentSpec>().SingleOrDefault();
                var foreignSpec = children.OfType <ForeignSpec>().SingleOrDefault();

                var type = children.OfType <UnitRef>().Single();
                var init = children.OfType <Expression>().Single();

                return new VariableDeclaration(name, type, init);
            }));
        }
Пример #25
0
        private UnitDeclaration ParseUnit(JsonEntity o)
        {
            EntityMixin.CheckType(o, UNIT);
            EntityMixin.ValueMustBeNull(o);

            return(Guard(o, children =>
            {
                var name = children.OfType <Identifier>().Single();
                var refValSpec = children.OfType <UnitDeclaration.RefValSpec>().Single();
                var concurrentSpec = children.OfType <UnitDeclaration.ConcurrentSpec>().SingleOrDefault();
                var concurrent = concurrentSpec?.Concurrent ?? false;
                var foreignSpec = children.OfType <ForeignSpec>().SingleOrDefault();
                var isForeign = foreignSpec?.IsForeign ?? false;
                var declarations = children.OfType <DeclarationList>().SingleOrDefault()?.Declarations;
                var invariants = children.OfType <ExpressionList>().SingleOrDefault()?.Expressions;

                return new UnitDeclaration(name, refValSpec, concurrent, isForeign, declarations, invariants);
            }));
        }