Esempio n. 1
0
        private static Datum BuildAll(this IDatumBuilder builder)
        {
            ImmutableHashSet <int> boxReferences = builder.BoxReferences;
            var boxes = ImmutableDictionary <int, MutableBoxDatum> .Empty;

            foreach (int boxReference in boxReferences)
            {
                boxes = boxes.Add(boxReference, new MutableBoxDatum(NullDatum.Value));
            }
            Datum result    = builder.Build(boxes);
            var   boxValues = builder.BoxValues;

            foreach (int boxReference in boxReferences)
            {
                if (boxValues.ContainsKey(boxReference))
                {
                    ImmutableList <IDatumBuilder> builders = boxValues[boxReference];
                    if (builders.Count == 1)
                    {
                        boxes[boxReference].Content = boxValues[boxReference][0].Build(boxes);
                    }
                    else
                    {
                        throw new Exception($"Box {boxReference} has {builders.Count} values; expected 1");
                    }
                }
                else
                {
                    throw new Exception($"Box {boxReference} does not have a value");
                }
            }
            return(result);
        }
Esempio n. 2
0
        private static ICharParser <IDatumBuilder> BuildParseMutableBox(ICharParser <IDatumBuilder> parseItem)
        {
            return(ParseConvert <ImmutableList <object>, IDatumBuilder>
                   (
                       ParseSequence
                       (
                           ParseExact("#b", StringComparison.InvariantCulture).ResultToObject(),
                           ParseOptRep
                           (
                               ParseConvert
                               (
                                   ParseSequence
                                   (
                                       ParseExact("[", StringComparison.InvariantCulture).ResultToObject(),
                                       ParseOptionalWhiteSpace.ResultToObject(),
                                       ParseBigInteger.ResultToObject(),
                                       ParseOptionalWhiteSpace.ResultToObject(),
                                       ParseExact("]", StringComparison.InvariantCulture).ResultToObject()
                                   ),
                                   list => list[2],
                                   null
                               ),
                               true,
                               false
                           )
                           .ResultToObject(),
                           ParseOptRep
                           (
                               ParseConvert
                               (
                                   ParseSequence
                                   (
                                       ParseExact("=", StringComparison.InvariantCulture).ResultToObject(),
                                       ParseOptionalWhiteSpace.ResultToObject(),
                                       parseItem.ResultToObject()
                                   ),
                                   list => list[2],
                                   null
                               ),
                               true,
                               false
                           )
                           .ResultToObject()
                       ),
                       list =>
            {
                ImmutableList <object> keyList = (ImmutableList <object>)list[1];
                ImmutableList <object> valueList = (ImmutableList <object>)list[2];
                if (keyList.Count == 1)
                {
                    BigInteger keyBig = (BigInteger)(keyList[0]);
                    if (keyBig < (BigInteger)int.MinValue || keyBig > (BigInteger)int.MaxValue)
                    {
                        throw new FormatException("Mutable box key out of range");
                    }
                    int key = (int)keyBig;
                    if (valueList.Count >= 1)
                    {
                        System.Diagnostics.Debug.Assert(valueList.Count == 1);

                        IDatumBuilder value = (IDatumBuilder)valueList[0];
                        return new BoxBuilder(key, Option <IDatumBuilder> .Some(value));
                    }
                    else
                    {
                        return new BoxBuilder(key, Option <IDatumBuilder> .None);
                    }
                }
                else
                {
                    if (valueList.Count >= 1)
                    {
                        System.Diagnostics.Debug.Assert(valueList.Count == 1);

                        IDatumBuilder value = (IDatumBuilder)valueList[0];
                        return new BoxBuilder(null, Option <IDatumBuilder> .Some(value));
                    }
                    else
                    {
                        throw new FormatException("Mutable box requires key, value, or both to be specified");
                    }
                }
            },
                       null
                   ));
        }
Esempio n. 3
0
 public DictionaryBuilder Add(IDatumBuilder key, IDatumBuilder value)
 {
     return(new DictionaryBuilder(values.Add(new Tuple <IDatumBuilder, IDatumBuilder>(key, value))));
 }
Esempio n. 4
0
 public SetBuilder Add(IDatumBuilder item)
 {
     return(new SetBuilder(values.Add(item)));
 }