aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/ao_read_source_files.d
blob: ef9b8b498450058afb24df6555d287cb338e9e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/+
  ao_read_source_files.d
  - open markup files
  - if master file scan for addional files to import/insert
+/
// module ao_read_source_files;
template SiSUmarkupRaw() {
  private import
    std.exception,
    std.regex,
    std.stdio,
    std.utf,
    std.conv : to;
  private import
    ao_rgx;       // ao_defaults.d
  mixin RgxInit;
  auto rgx = Rgx();
  struct MarkupRaw {
    auto sourceContent(in string fn_src) {
      auto raw = MarkupRawUnit();
      auto t =
        raw.markupSourceHeaderContentRawLineTupleArray(fn_src, rgx.src_pth);
      auto header_raw = t[0];
      auto sourcefile_content = t[1];
      if (match(fn_src, rgx.src_fn_master)) {
        auto ins = Inserts();
        sourcefile_content =
          ins.scan_master_src_for_insert_files_and_import_content(sourcefile_content, fn_src);
        // auto ins = SiSUdocInserts.Inserts();
      }
      t = tuple(
        header_raw,
        sourcefile_content
      );
      return t;
    }
  }
  private
  struct HeaderExtractSDL {
    final private auto headerMakeSDLang(in string src_header) {
      scope(failure) {
        stderr.writefln(
          "%s\n%s\n%s:%s failed here:\n  src_header: %s",
          __MODULE__, __FUNCTION__,
          __FILE__, __LINE__,
          src_header,
        );
      }
      Tag sdl_root_header;
      try {
        sdl_root_header = parseSource(src_header);
      }
      catch(SDLangParseException e) {
        stderr.writeln("SDLang problem with this document header:");
        stderr.writeln(src_header);
        // Error messages of the form:
        // myFile.sdl(5:28): Error: Invalid integer suffix.
        stderr.writeln(e.msg);
      }
      debug(sdlang) {
        // // Value is a std.variant.Algebraic
        // Value output_dir_structure_by = sdl_root_header.tags["output_dir_structure_by"][0].values[0];
        // assert(output_dir_structure_by.type == typeid(string));
        // writeln(output_dir_structure_by);
    
        // Tag person = sdl_root_header.namespaces["myNamespace"].tags["person"][0];
        // writeln("Name: ", person.attributes["name"][0].value);
        //
        // int age = person.tags["age"][0].values[0].get!int();
        // writeln("Age: ", age);
    
        writeln("header SDL:");
        writeln(sdl_root_header.toSDLDocument());
      }
      return sdl_root_header;
    }
    private auto headerSDLang(in char[] src_header) {
      char[][] source_header_arr =
        split(cast(char[]) src_header, rgx.line_delimiter);
      char[] header_clean;
      foreach(header_line; source_header_arr) {
        if (!match(header_line, rgx.comments)) {
          header_clean ~= header_line ~ "\n";
          // writeln(header_line);
        }
      }
      // writeln(header_clean); // consider
      auto header_sdlang=headerMakeSDLang(to!string(header_clean));
      return header_sdlang;
    }
  }
  struct MarkupRawUnit {
    private import std.file;
    // enum State { off, on }
    final private string readInMarkupSource(in string fn_src) {
      enforce(
        exists(fn_src)!=0,
        "file not found"
      );
      string source_txt_str;
      try {
        if (exists(fn_src)) {
          source_txt_str = readText(fn_src);
        }
      }
      catch (ErrnoException ex) {
      //// Handle errors
      // switch(ex.errno) {
      // case EPERM:
      // case EACCES:
      //   // Permission denied
      //   break;
      // case ENOENT:
      //   // File does not exist
      //   break;
      // default:
      //   // Handle other errors
      //   break;
      // }
      }
      catch (UTFException ex) {
        // Handle validation errors
      }
      catch (FileException ex) {
        // Handle errors
      }
      std.utf.validate(source_txt_str);
      return source_txt_str;
    }
    final private char[][] header0Content1(in string src_text) {
      /+ split string on first match of "^:?A~\s" into [header, content] tuple +/
      char[][] header_and_content =
        split(cast(char[]) src_text, rgx.heading_a);
      return header_and_content;
    }
    final private char[][] markupSourceLineArray(in char[] src_text) {
      char[][] source_line_arr =
        split(cast(char[]) src_text, rgx.line_delimiter);
      return source_line_arr;
    }
    auto markupSourceHeaderContentRawLineTupleArray(in string fn_src, Regex!(char) rgx_file ) {
      enforce(
        match(fn_src, rgx_file),
        "not a sisu markup filename"
      );
      auto source_txt_str = readInMarkupSource(fn_src);
      auto hc = header0Content1(source_txt_str);
      auto header = hc[0];
      char[] la;
      la ~= "A~ ";
      char[] source_txt = la ~ hc[1];
      auto source_line_arr = markupSourceLineArray(source_txt);
      auto t = tuple(
        header,
        source_line_arr
      );
      return t;
    }
    final char[][] markupSourceContentRawLineArray(in string fn_src, Regex!(char) rgx_file ) {
      enforce(
        match(fn_src, rgx_file),
        "not a sisu markup filename"
      );
      auto source_txt_str = readInMarkupSource(fn_src);
      auto source_line_arr = markupSourceLineArray(source_txt_str);
      return source_line_arr;
    }
  }
  struct Inserts {
    private import ao_defaults;    // ao_defaults.d
    private import ao_ansi_colors; // ao_ansi_colors.d
    auto scan_subdoc_source(
      char[][] markup_sourcefile_insert_content,
      string fn_src
    ) {
      mixin SiSUrgxInitFlags;
      char[][] contents_insert;
      auto type1 = flags_type_init;
      auto fn_pth_full = match(fn_src, rgx.src_pth);
      auto markup_src_file_path = fn_pth_full.captures[1];
      foreach (line; markup_sourcefile_insert_content) {
        if (type1["curly_code"] == 1) {
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          if (matchFirst(line, rgx.block_curly_code_close)) {
            type1["curly_code"] = 0;
          }
          contents_insert ~= line;
        } else if (matchFirst(line, rgx.block_curly_code_open)) {
          type1["curly_code"] = 1;
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          contents_insert ~= line;
        } else if (type1["tic_code"] == 1) {
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          if (matchFirst(line, rgx.block_tic_close)) {
            type1["tic_code"] = 0;
          }
          contents_insert ~= line;
        } else if (matchFirst(line, rgx.block_tic_code_open)) {
          type1["tic_code"] = 1;
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          contents_insert ~= line;
        } else if (
          (type1["header_make"] == 1)
          && matchFirst(line, rgx.header_sub)
        ) {
            type1["header_make"] = 1;
            type1["header_meta"] = 0;
            // cont_dynamic_array ~= "% " ~ line;
        } else if (
          (type1["header_meta"] == 1)
          && matchFirst(line, rgx.header_sub)
        ) {
            type1["header_meta"] = 1;
            type1["header_make"] = 0;
            // cont_dynamic_array ~= "% " ~ line;
        } else if (auto m = match(line, rgx.insert_src_fn_ssi_or_sst)) {
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          auto insert_fn = m.captures[2];
          auto insert_sub_pth = m.captures[1];
          auto fn_src_insert =
            to!string(markup_src_file_path ~ insert_sub_pth ~ insert_fn);
          auto raw = MarkupRawUnit();
          auto markup_sourcesubfile_insert_content =
            raw.markupSourceContentRawLineArray(fn_src_insert, rgx.src_fn_find_inserts);
          debug(insert) {                              // insert file
            tell_l("red", line);
            tell_l("red", fn_src_insert);
            tell_l("fuchsia", "ERROR");
            writeln(
              "  length contents insert array: ",
              markup_sourcesubfile_insert_content.length
            );
          }
          auto ins = Inserts();
          /+
            1. load file,
            2. read lines;
            3. scan lines,
            4. if filename insert, and insert filename
            5.   repeat 1
            6. else
            7.   add line to new array;
          +/
        } else {
          type1["header_make"] = 0;
          type1["header_meta"] = 0;
          contents_insert ~= line;
        }
      } // end src subdoc (inserts) loop
      return contents_insert;
    }
    auto scan_master_src_for_insert_files_and_import_content(
      char[][] sourcefile_content,
      string fn_src
    ) {
      mixin SiSUrgxInitFlags;
      char[][] contents;
      auto type = flags_type_init;
      auto fn_pth_full = match(fn_src, rgx.src_pth);
      auto markup_src_file_path = fn_pth_full.captures[1];
      foreach (line; sourcefile_content) {
        if (type["curly_code"] == 1) {
          if (matchFirst(line, rgx.block_curly_code_close)) {
            type["curly_code"] = 0;
          }
          contents ~= line;
        } else if (matchFirst(line, rgx.block_curly_code_open)) {
          type["curly_code"] = 1;
          contents ~= line;
        } else if (type["tic_code"] == 1) {
          if (matchFirst(line, rgx.block_tic_close)) {
            type["tic_code"] = 0;
          }
          contents ~= line;
        } else if (matchFirst(line, rgx.block_tic_code_open)) {
          type["tic_code"] = 1;
          contents ~= line;
        } else if (auto m = match(line, rgx.insert_src_fn_ssi_or_sst)) {
          auto insert_fn = m.captures[2];
          auto insert_sub_pth = m.captures[1];
          auto fn_src_insert =
            to!string(markup_src_file_path ~ insert_sub_pth ~ insert_fn);
          auto raw = MarkupRawUnit();
          auto markup_sourcefile_insert_content =
            raw.markupSourceContentRawLineArray(fn_src_insert, rgx.src_fn_find_inserts);
          debug(insert) {                              // insert file
            tell_l("red", line);
            tell_l("red", fn_src_insert);
            writeln(
              "  length contents insert array: ",
              markup_sourcefile_insert_content.length
            );
          }
          auto ins = Inserts();
          auto contents_insert = ins.scan_subdoc_source(
            markup_sourcefile_insert_content,
            to!string(fn_src_insert)
          );
          contents ~= contents_insert;
          /+
            1. load file,
            2. read lines;
            3. scan lines,
            4. if filename insert, and insert filename
            5.   repeat 1
            6. else
            7.   add line to new array;
          +/
        } else {
          contents ~= line;
        }
      } // end src doc loop
      debug(insert) {                              // insert file
        writeln(__LINE__);
        writeln(contents.length);
      }
      return contents;
    }
  }
}