File indexing completed on 2024-12-15 04:19:54

0001 package mso.generator.utils;
0002 
0003 import org.eclipse.jdt.annotation.Nullable;
0004 
0005 public class Option {
0006     public final Struct type;
0007     public final Type limitsType;
0008     public final Lim lim;
0009 
0010     private Option(Struct type, Type limitsType, Lim lim) {
0011         this.type = type;
0012         this.limitsType = limitsType;
0013         this.lim = lim;
0014     }
0015 
0016     public static Option parseOption(Struct s, @Nullable Type suggestedType) {
0017         return setLimitations(s, s, suggestedType);
0018     }
0019 
0020     private static Option setLimitations(Struct parent, Struct type, Choice c) {
0021         // all restrictions in the
0022         Lim lims[] = new Lim[c.options.size()];
0023         for (int i = 0; i < c.options.size(); ++i) {
0024             lims[i] = c.options.get(i).lim;
0025         }
0026         Type commonType = c.commonType;
0027         if (commonType == null) {
0028             throw new Error("Common type is null.");
0029         }
0030         return new Option(parent, commonType, new Lim(lims));
0031     }
0032 
0033     private static Option setLimitations(Struct parent, Struct type,
0034             @Nullable Type suggestedType) {
0035         Member m = type.members.get(0);
0036         System.out.println(parent.name + " " + type.name + " , " + m.name
0037                 + " . " + m.type());
0038         if (m.isOptional) {
0039             throw new Error("choice member may not be optional");
0040         }
0041         Type t = m.type();
0042         if (t instanceof Choice) {
0043             return setLimitations(parent, type, (Choice) t);
0044         }
0045         // if the type is equal to a previous common type, use that
0046         if (t == suggestedType) {
0047             return new Option(parent, suggestedType, new Lim(m.limitations));
0048         }
0049         // if this struct has no limitations, take its first member
0050         if (m.limitations.length == 0) {
0051             return setLimitations(parent, (Struct) t, suggestedType);
0052         }
0053         return new Option(parent, t, new Lim(m.limitations));
0054     }
0055 }