001package Torello.Browser.JsonAST;
002
003import static Torello.Java.C.RESET;
004import static Torello.Java.C.BYELLOW;
005
006import java.io.IOException;
007
008import Torello.Java.FileRW;
009import Torello.Java.StorageWriter;
010import Torello.Java.Additional.Ret2;
011
012import Torello.Java.ReadOnly.ReadOnlyList;
013import Torello.Java.ReadOnly.ROArrayListBuilder;
014
015import Torello.Java.UnreachableError;
016
017/**
018 * This is a "Cute Little Tree Walk" or "Tree Traversal" class that simply iterates every single 
019 * node inside of an AST, and fills the {@link InfoData} with the relevant facts that are needed
020 * to buid an {@link InfoData} object.
021 * 
022 * <BR /><BR />There is absolutely nothing complicated going on here.  This class just "traverses"
023 * an AST, and then calls &amp; invokes all of the Data Collection Methods that are exposed inside
024 * of the {@code InfoData} class.
025 * 
026 * <BR /><BR />
027 * For all intents and purposes, this class functions as "the constructor" for the {@link InfoData}
028 * class.  It actually iterates the nodes in the Abstract Syntax Tree and builds the data-lists 
029 * which are eventually stored into an instance of class {@link InfoData}.  No more, no less.
030 * 
031 * @see InfoData
032 */
033@Torello.JavaDoc.Annotations.StaticFunctional
034@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="SERIALIZER_JDHBI")
035public class ExtraData
036{
037    private ExtraData() { }
038
039
040    // ********************************************************************************************
041    // ********************************************************************************************
042    // Simple String Constants
043    // ********************************************************************************************
044    // ********************************************************************************************
045
046
047    private static final String FS = java.io.File.separator;
048
049    private static final String DIR = 
050        "Torello" + FS + "Browser" + FS + "CodeGen" + FS + "FILE" + FS;
051
052    /**
053     * The package {@code Torello.Browser} uses this filename for event processing
054     * @see Torello.Browser.MarkerEvent
055     * @see Torello.Browser.BrowserEvent
056     */
057    public static final String MARKER_EVENT_TYPES_DATA_FILE_NAME = 
058        "Torello" + FS + "Browser" + FS + "data-files" + FS + "marker-events.roldat";
059
060
061    // ********************************************************************************************
062    // ********************************************************************************************
063    // Top-Level "Run" (or "main") Method.  This method is "Package Private"
064    // ********************************************************************************************
065    // ********************************************************************************************
066
067
068    static Ret2<InfoData, InfoData> run(
069            final StorageWriter sw,
070            final API           browser,
071            final API           js
072        )
073        throws IOException
074    {
075        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
076        // Traverse the AST built for the CDP "Browser API"; and shove everything into the Builder
077        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
078
079        InfoData.Builder b = InfoData.builder();
080        traverseTree(browser, b);
081        final InfoData browserData = b.build();
082
083
084        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
085        // Traverse the AST built for the CDP "JavaScript API"; add stuff to the Builder
086        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
087
088        b = InfoData.builder();
089        traverseTree(js, b);
090        final InfoData jsData = b.build();
091
092
093        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
094        // There is an actual data-file that is needed (and used in real time) by Torello.Browser
095        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
096        // 
097        // Write the "Marker Event" (Events without any fields or "parameters") to a data file
098
099        final ReadOnlyList<String> meROL = markerEventsSeparateDataFile(browserData, jsData);
100        sw.println("Writing File: " + BYELLOW + MARKER_EVENT_TYPES_DATA_FILE_NAME + RESET);
101        FileRW.writeObjectToFileNOCNFE(meROL, MARKER_EVENT_TYPES_DATA_FILE_NAME, true);
102
103        // Return the two instances of "InfoData" in a Ret2
104        return new Ret2<>(browserData, jsData);
105    }
106
107
108    // ********************************************************************************************
109    // ********************************************************************************************
110    // Traverse the complete Abstract Syntax Tree (AST) - PRIVATE METHODS
111    // ********************************************************************************************
112    // ********************************************************************************************
113
114
115    private static void traverseTree(final API api, final InfoData.Builder b)
116    { for (final Domain d : api.domains) traverseTree(d, b); }
117
118    private static void traverseTree(final Domain d, final InfoData.Builder b)
119    {
120        for (final TypeNode tn : d.types)
121
122            if (tn.enumVals != null) b.enumValType(tn);
123
124            else if (tn.isReifiedInnerClass) b.reifiedInnerType(tn);
125
126            else b.simpleType(tn);
127
128        for (final CommandNode cn : d.commands)
129
130            if (cn.hasReifiedRetInnerClass) b.reifiedReturnType(cn);
131
132        for (final EventNode en : d.events)
133
134            if (en.isReifiedInnerClass) b.reifiedEvent(en);
135
136            else b.markerEvent(en);
137    }
138
139    private static ReadOnlyList<String> markerEventsSeparateDataFile
140        (final InfoData browserData, final InfoData jsData)
141    {
142        ROArrayListBuilder<String> roalb = new ROArrayListBuilder<>();
143
144        for (final EventNode en : browserData.markerEventNodes)
145            roalb.add(en.ownerDomain.name + '.' + en.name);
146
147        for (final EventNode en : jsData.markerEventNodes)
148            roalb.add(en.ownerDomain.name + '.' + en.name);
149
150        return roalb.build();
151    }
152}