예제 #1
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()));
        }
예제 #2
0
 public static void CheckType(JsonEntity json, string type)
 {
     if (json.Type != type)
     {
         throw new JsonFormatException(json, $"type expected to be {type}");
     }
 }
예제 #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.ParameterList ParseParameterList(JsonEntity o)
        {
            EntityMixin.CheckType(o, PARAMETER_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new RoutineDeclaration.ParameterList(ParseChildren(o).OfType <RoutineDeclaration.Parameter>()));
        }
예제 #5
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);
            }));
        }
예제 #6
0
        private EntityList ParseEntityList(JsonEntity o)
        {
            EntityMixin.CheckType(o, ENTITY_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new EntityList(ParseChildren(o).ToList()));
        }
예제 #7
0
        private ExpressionList ParseExpressionList(JsonEntity o)
        {
            EntityMixin.CheckType(o, EXPRESSION_LIST);
            EntityMixin.ValueMustBeNull(o);

            return(new ExpressionList(ParseChildren(o).OfType <Expression>()));
        }
예제 #8
0
 public static void ValueMustNotBeNull(JsonEntity json)
 {
     if (json.Value == null)
     {
         throw new JsonFormatException(json, "value must not be null");
     }
 }
예제 #9
0
        private PostCondition ParsePostCondition(JsonEntity o)
        {
            EntityMixin.CheckType(o, POSTCONDITION);
            EntityMixin.ValueMustBeNull(o);

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

            return(new PostCondition(expressions));
        }
예제 #10
0
        private Return ParseReturn(JsonEntity o)
        {
            EntityMixin.CheckType(o, RETURN);
            EntityMixin.ValueMustBeNull(o);

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

            return(new Return(optionalExpression));
        }
예제 #11
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));
        }
예제 #12
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));
        }
예제 #13
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)));
        }
예제 #14
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));
        }
예제 #15
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);
            }));
        }
예제 #16
0
 /// <summary>
 /// Guard against First/Single methods throwing on empty/too long sequences
 /// </summary>
 /// <param name="o"></param>
 /// <param name="f"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 /// <exception cref="JsonFormatException"></exception>
 protected T Guard <T>(JsonEntity o, Func <List <Entity>, T> f)
 {
     try
     {
         var children = ParseChildren(o).ToList();
         return(f(children));
     }
     catch (InvalidOperationException e)
     {
         throw new JsonFormatException(o, $"{o.Type} definition is invalid", e);
     }
 }
예제 #17
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);
            }));
        }
예제 #18
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);
            }));
        }
예제 #19
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);
            }));
        }
예제 #20
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);
            }));
        }
예제 #21
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));
        }
예제 #22
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\"");
            }
        }
예제 #23
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);
            }));
        }
예제 #24
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");
            }
        }
예제 #25
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");
            }
        }
예제 #26
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));
        }
예제 #27
0
        public Entity Parse(JsonEntity o)
        {
            if (o == null)
            {
                return(null);
            }
            var ty = o.Type;

            if (string.IsNullOrEmpty(ty))
            {
                throw new JsonFormatException(o);
            }
            if (!_parsers.TryGetValue(ty, out var parser))
            {
                throw new NotImplementedException(ty);
            }

            return(parser(o));
        }
예제 #28
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);
            }));
        }
예제 #29
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);
            }));
        }
예제 #30
0
 protected IEnumerable <Entity> ParseChildren(JsonEntity o)
 {
     return(o.Children?.Select(Parse) ?? new Entity[0]);
 }