-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmangle.c
4786 lines (3988 loc) · 129 KB
/
mangle.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#line 124 "source/main.md"
#line 50 "README.md"
/****************************************************************************
Copyright (c) 2014 Tim Foley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#line 124 "source/main.md"
#line 136 "source/main.md"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#line 125 "source/main.md"
#line 148 "source/main.md"
#line 11 "source/string.md"
typedef struct MgStringT
{
char const* begin;
char const* end;
} MgString;
#line 43 "source/string.md"
MgString MgMakeString( char const* begin, char const* end );
#line 148 "source/main.md"
#line 493 "source/document.md"
#line 482 "source/document.md"
typedef struct MgAttributeT MgAttribute;
typedef struct MgContextT MgContext;
typedef struct MgElementT MgElement;
typedef struct MgInputFileT MgInputFile;
typedef struct MgLineT MgLine;
typedef struct MgReferenceLinkT MgReferenceLink;
typedef struct MgScrapT MgScrap;
typedef struct MgScrapFileGroupT MgScrapFileGroup;
typedef struct MgScrapNameGroupT MgScrapNameGroup;
#line 493 "source/document.md"
#line 13 "source/document.md"
typedef int MgBool;
#define MG_TRUE (1)
#define MG_FALSE (0)
#line 24 "source/document.md"
typedef struct MgSourceLocT
{
int line;
int col;
} MgSourceLoc;
#line 40 "source/document.md"
struct MgScrapT
{
#line 48 "source/document.md"
MgSourceLoc sourceLoc;
MgElement* body;
#line 125 "source/document.md"
MgScrap* next;
#line 134 "source/document.md"
MgScrapFileGroup* fileGroup;
#line 42 "source/document.md"
};
#line 56 "source/document.md"
typedef enum MgScrapKind
{
#line 68 "source/document.md"
kScrapKind_Unknown,
#line 79 "source/document.md"
kScrapKind_LocalMacro,
#line 88 "source/document.md"
kScrapKind_GlobalMacro,
#line 97 "source/document.md"
kScrapKind_OutputFile,
#line 104 "source/document.md"
kScrapKind_RawMacro,
#line 58 "source/document.md"
} MgScrapKind;
#line 111 "source/document.md"
struct MgScrapFileGroupT
{
#line 119 "source/document.md"
MgInputFile* inputFile;
#line 128 "source/document.md"
MgScrap* firstScrap;
MgScrap* lastScrap;
#line 166 "source/document.md"
MgScrapFileGroup* next;
#line 175 "source/document.md"
MgScrapNameGroup* nameGroup;
#line 113 "source/document.md"
};
#line 141 "source/document.md"
struct MgScrapNameGroupT
{
#line 151 "source/document.md"
MgString id;
MgElement* name;
#line 157 "source/document.md"
MgScrapKind kind;
#line 169 "source/document.md"
MgScrapFileGroup* firstFileGroup;
MgScrapFileGroup* lastFileGroup;
#line 184 "source/document.md"
MgScrapNameGroup* next;
#line 143 "source/document.md"
};
#line 192 "source/document.md"
struct MgLineT
{
MgString text;
char const* originalBegin;
};
#line 218 "source/document.md"
struct MgInputFileT
{
char const* path; /* path of input file (terminated) */
MgString text; /* full text of the input file */
char* allocatedFileData; /* allocated file buffer, if any */
MgLine* beginLines; /* allocated per-line data */
MgLine* endLines;
MgElement* firstElement; /* first element in doc structure*/
MgInputFile* next; /* next input file in context */
MgReferenceLink*firstReferenceLink; /* first reference link parsed */
};
#line 237 "source/document.md"
struct MgContextT
{
MgInputFile* firstInputFile; /* singly-linked list of input files */
MgInputFile* lastInputFile;
MgScrapNameGroup* firstScrapNameGroup; /* singly-linked list of scrap name groups */
MgScrapNameGroup* lastScrapNameGroup;
MgInputFile* metaDataFile;
MgScrapKind defaultScrapKind;
};
#line 259 "source/document.md"
typedef enum MgElementKindT
{
#line 267 "source/document.md"
#line 275 "source/document.md"
kMgElementKind_BlockQuote, /* `<blockquote>` */
kMgElementKind_HorizontalRule, /* `<hr>` */
kMgElementKind_UnorderedList, /* `<ul>` */
kMgElementKind_OrderedList, /* `<ol>` */
kMgElementKind_ListItem, /* `<li>` */
kMgElementKind_Paragraph, /* `<p>` */
kMgElementKind_Table, /* `<table>` */
kMgElementKind_TableRow, /* `<tr>` */
kMgElementKind_TableHeader, /* `<th>` */
kMgElementKind_TableCell, /* `<td>` */
#line 296 "source/document.md"
kMgElementKind_Header1, /* `<h1>` */
kMgElementKind_Header2, /* `<h2>` */
kMgElementKind_Header3, /* `<h3>` */
kMgElementKind_Header4, /* `<h4>` */
kMgElementKind_Header5, /* `<h5>` */
kMgElementKind_Header6, /* `<h6>` */
#line 309 "source/document.md"
kMgElementKind_CodeBlock, /* `<pre><code>` */
#line 316 "source/document.md"
kMgElementKind_ScrapDef,
#line 332 "source/document.md"
kMgElementKind_MetaData,
#line 340 "source/document.md"
kMgElementKind_HtmlBlock,
#line 267 "source/document.md"
#line 287 "source/document.md"
kMgElementKind_Em, /* `<em>` */
kMgElementKind_Strong, /* `<strong>` */
kMgElementKind_InlineCode, /* `<code>` */
#line 325 "source/document.md"
kMgElementKind_ScrapRef,
#line 354 "source/document.md"
kMgElementKind_LessThanEntity, /* `<` */
kMgElementKind_GreaterThanEntity, /* `>` */
kMgElementKind_AmpersandEntity, /* `&` */
#line 364 "source/document.md"
kMgElementKind_NewLine, /* `"\n"` */
#line 371 "source/document.md"
kMgElementKind_Link, /* `<a>` with href attribute */
#line 397 "source/document.md"
kMgElementKind_ReferenceLink,
#line 268 "source/document.md"
#line 347 "source/document.md"
kMgElementKind_Text,
#line 261 "source/document.md"
} MgElementKind;
#line 384 "source/document.md"
struct MgReferenceLinkT
{
MgString id;
MgString url;
MgString title;
MgReferenceLink* next;
};
#line 405 "source/document.md"
struct MgAttributeT
{
#line 419 "source/document.md"
MgString id;
#line 424 "source/document.md"
MgAttribute* next;
#line 407 "source/document.md"
union
{
#line 429 "source/document.md"
MgString val;
#line 434 "source/document.md"
MgReferenceLink* referenceLink;
MgScrap* scrap;
MgScrapFileGroup* scrapFileGroup;
MgSourceLoc sourceLoc;
#line 410 "source/document.md"
};
};
#line 444 "source/document.md"
struct MgElementT
{
#line 452 "source/document.md"
MgElementKind kind;
#line 458 "source/document.md"
MgString text;
#line 463 "source/document.md"
MgAttribute* firstAttr;
#line 468 "source/document.md"
MgElement* firstChild;
MgElement* next;
#line 446 "source/document.md"
};
#line 494 "source/document.md"
#line 149 "source/main.md"
#line 126 "source/main.md"
#line 154 "source/main.md"
#line 13 "source/reader.md"
typedef struct MgReaderT
{
MgString string;
char const* cursor;
} MgReader;
#line 23 "source/reader.md"
enum
{
kMgEndOfFile = -1,
};
#line 31 "source/reader.md"
void MgInitializeStringReader(
MgReader* reader,
MgString string )
{
reader->string = string;
reader->cursor = string.begin;
}
#line 42 "source/reader.md"
MgBool MgAtEnd(
MgReader* reader )
{
return reader->cursor == reader->string.end;
}
#line 52 "source/reader.md"
int MgGetChar(
MgReader* reader )
{
if( MgAtEnd(reader) )
return kMgEndOfFile;
return *(reader->cursor++);
}
#line 65 "source/reader.md"
void MgUnGetChar(
MgReader* reader,
int value )
{
if( value == kMgEndOfFile )
return;
--(reader->cursor);
}
#line 81 "source/reader.md"
int MgPeekChar(
MgReader* reader )
{
if( MgAtEnd(reader) )
return kMgEndOfFile;
return *(reader->cursor);
}
#line 154 "source/main.md"
#line 23 "source/string.md"
static MgBool MgIsEmptyString(
MgString string)
{
return string.begin == string.end;
}
#line 32 "source/string.md"
MgString MgMakeEmptyString()
{
return MgMakeString(NULL, NULL);
}
#line 46 "source/string.md"
MgString MgMakeString(
char const* begin,
char const* end)
{
MgString result;
result.begin = begin;
result.end = end;
return result;
}
#line 60 "source/string.md"
MgString MgTerminatedString(
char const* begin)
{
return MgMakeString(begin, begin + strlen(begin));
}
#line 74 "source/string.md"
MgBool MgStringsAreEqual(
MgString left,
MgString right )
{
char const* leftCursor = left.begin;
char const* rightCursor = right.begin;
for(;;)
{
#line 93 "source/string.md"
MgBool leftAtEnd = leftCursor == left.end;
MgBool rightAtEnd = rightCursor == right.end;
if( leftAtEnd || rightAtEnd )
return leftAtEnd == rightAtEnd;
#line 83 "source/string.md"
#line 101 "source/string.md"
char leftChar = *leftCursor++;
char rightChar = *rightCursor++;
#line 84 "source/string.md"
#line 105 "source/string.md"
if( leftChar != rightChar )
return MG_FALSE;
#line 85 "source/string.md"
}
}
#line 113 "source/string.md"
MgBool MgStringsAreEqualNoCase(
MgString left,
MgString right )
{
char const* leftCursor = left.begin;
char const* rightCursor = right.begin;
for(;;)
{
#line 93 "source/string.md"
MgBool leftAtEnd = leftCursor == left.end;
MgBool rightAtEnd = rightCursor == right.end;
if( leftAtEnd || rightAtEnd )
return leftAtEnd == rightAtEnd;
#line 122 "source/string.md"
#line 101 "source/string.md"
char leftChar = *leftCursor++;
char rightChar = *rightCursor++;
#line 123 "source/string.md"
#line 129 "source/string.md"
if( tolower(leftChar) != tolower(rightChar) )
return MG_FALSE;
#line 124 "source/string.md"
}
}
#line 155 "source/main.md"
#line 5 "source/parse.md"
enum
{
kMaxHeaderLevel = 6,
};
/*
Flags for parsing span-level elements in Markdown source.
*/
enum
{
/* No flags. */
kMgSpanFlags_None = 0x00,
/* Enable escaping of HTML entiteis like `&` into characters like `&`.*/
kMgSpanFlag_EscapeHtmlEntities = 0x01,
/* Disable processing of standard Markdown syntax. */
kMgSpanFlag_DontProcessMarkdown = 0x02,
/* Default behavior: escape HTML entities, process Markdown. */
kMgSpanFlags_Default =
kMgSpanFlag_EscapeHtmlEntities,
/* Inside a code block: escape HTML, but don't process Markdown */
kMgSpanFlags_CodeBlock =
kMgSpanFlag_EscapeHtmlEntities
| kMgSpanFlag_DontProcessMarkdown,
/* Inline code: handle the same as a code block */
kMgSpanFlags_InlineCode = kMgSpanFlags_CodeBlock,
/* Inside an HTML block: don't process Markdown. */
kMgSpanFlags_HtmlBlock =
kMgSpanFlag_DontProcessMarkdown,
};
typedef unsigned MgSpanFlags;
/*
When encountering either a reference to or a definition of a "reference-style"
link in the document, call this function to get or create the object to represent
the link. At a definition site, fill in the `MgReferenceLink*` that was returned,
to provide information that will be used at any sites that reference it.
*/
MgReferenceLink* MgFindOrCreateReferenceLink(
MgInputFile* inputFile,
MgString id )
{
MgReferenceLink* link = inputFile->firstReferenceLink;
while( link )
{
if( MgStringsAreEqualNoCase(
id,
link->id ) )
{
return link;
}
link = link->next;
}
link = (MgReferenceLink*) malloc(sizeof(MgReferenceLink));
link->id = id;
link->url = MgMakeEmptyString();
link->title = MgMakeEmptyString();
link->next = inputFile->firstReferenceLink;
inputFile->firstReferenceLink = link;
return link;
}
/*
Allocate and add a new attribute to an existing element, with the
specified NULL-terminated `id` and value.
*/
MgAttribute* MgAddAttribute(
MgElement* element,
char const* id,
MgString val )
{
MgAttribute* attr = (MgAttribute*) malloc(sizeof(MgAttribute));
attr->next = NULL;
attr->id = MgTerminatedString(id);
attr->val = val;
// this is a linear-time insert, but we expect the
// number of attributes per element to be very low,
// so it probably won't matter for performance.
MgAttribute** link = &element->firstAttr;
while( *link )
link = &((*link)->next);
*link = attr;
return attr;
}
/*
Allocate an add a new atttribute to an existing element, with the
specified NULL-terminated `id`, but with no value. The caller is
expected to fill in of the fields in the `union` inside the
attribute, based on the chosen ID.
*/
MgAttribute* MgAddCustomAttribute(
MgElement* element,
char const* id)
{
return MgAddAttribute(element, id, MgMakeString(NULL, NULL));
}
MgElement* MgCreateElementImpl(
MgElementKind kind,
MgString text,
MgElement* firstChild )
{
MgElement* element = (MgElement*) malloc(sizeof(MgElement));
element->kind = kind;
element->text = text;
element->firstAttr = NULL;
element->firstChild = firstChild;
element->next = NULL;
return element;
}
/*
Create a leaf document element, with the specified kind and text.
*/
MgElement* MgCreateLeafElement(
MgElementKind kind,
MgString text )
{
return MgCreateElementImpl(
kind,
text,
NULL ); // no children
}
/*
Create a parent document element, with the specified first child in
the linked list of child elements.
*/
MgElement* MgCreateParentElement(
MgElementKind kind,
MgElement* firstChild )
{
return MgCreateElementImpl(
kind,
MgMakeString(NULL, NULL), // no text
firstChild );
}
MgScrapNameGroup* MgFindScrapNameGroup(
MgContext* context,
MgString id )
{
MgScrapNameGroup* group = context->firstScrapNameGroup;
while( group )
{
if( MgStringsAreEqual( group->id, id ) )
return group;
group = group->next;
}
return 0;
}
MgScrapFileGroup* MgFindScrapFileGroup(
MgScrapNameGroup* nameGroup,
MgInputFile* inputFile )
{
MgScrapFileGroup* fileGroup = nameGroup->firstFileGroup;
while( fileGroup )
{
if( fileGroup->inputFile == inputFile )
return fileGroup;
fileGroup = fileGroup->next;
}
return 0;
}
/*
When encountering either a reference to or a definition of a scrap,
call this function to get or create the object that represents the scrap
group with that `id` for the given `file`.
The `kind` can either be `kMgScrapKind_Unknown` if you don't care what
kind of scrap it is, or a specific scrap kind if you want to set the
scrap kind as part of retrieving it. (TODO: separate those steps)
*/
MgScrapFileGroup* MgFindOrCreateScrapGroup(
MgContext* context,
MgScrapKind kind,
MgString id,
MgInputFile* file )
{
MgScrapNameGroup* nameGroup = MgFindScrapNameGroup( context, id );
if( !nameGroup )
{
nameGroup = (MgScrapNameGroup*) malloc(sizeof(MgScrapNameGroup));
nameGroup->kind = kind;
nameGroup->id = id;
nameGroup->name = 0;
nameGroup->firstFileGroup = 0;
nameGroup->lastFileGroup = 0;
nameGroup->next = 0;
if( context->lastScrapNameGroup )
{
context->lastScrapNameGroup->next = nameGroup;
}
else
{
context->firstScrapNameGroup = nameGroup;
}
context->lastScrapNameGroup = nameGroup;
}
if( nameGroup->kind == kScrapKind_Unknown )
{
nameGroup->kind = kind;
}
else if( kind != kScrapKind_Unknown
&& kind != nameGroup->kind )
{
fprintf(stderr, "incompatible scrap kinds!\n");
}
MgScrapFileGroup* fileGroup = MgFindScrapFileGroup( nameGroup, file );
if( !fileGroup )
{
fileGroup = (MgScrapFileGroup*) malloc(sizeof(MgScrapFileGroup));
fileGroup->nameGroup = nameGroup;
fileGroup->inputFile = file;
fileGroup->firstScrap = 0;
fileGroup->lastScrap = 0;
fileGroup->next = 0;
if( nameGroup->lastFileGroup )
{
nameGroup->lastFileGroup->next = fileGroup;
}
else
{
nameGroup->firstFileGroup = fileGroup;
}
nameGroup->lastFileGroup = fileGroup;
}
return fileGroup;
}
/*
Add a particular scrap definition to a group of scraps with the same
ID in the same file.
*/
void MgAddScrapToFileGroup(
MgScrapFileGroup* fileGroup,
MgScrap* scrap)
{
if( fileGroup->lastScrap )
{
fileGroup->lastScrap->next = scrap;
}
else
{
fileGroup->firstScrap = scrap;
}
fileGroup->lastScrap = scrap;
}
MgBool MgCheckMatch(
MgReader* reader,
char c,
int count )
{
for( int ii = 0; ii < count; ++ii )
{
int d = MgGetChar( reader );
if( d != c )
return MG_FALSE;
}
return MG_TRUE;
}
/*
Look ahead in the reader for an occurence of character `c`, `count` times
in a row. Return a pointer to right before the first character in the match,
and leave the cursor of the reader pointing right after the match.
If no match is found, returns NULL.
*/
char const* MgFindMatching(
MgReader* reader,
char c,
int count )
{
for(;;)
{
char const* mark = reader->cursor;
int d = MgGetChar(reader);
if( d == -1 )
return 0;
if( d == c )
{
if( MgCheckMatch(reader, c, count-1) )
{
return mark;
}
}
if( d == '\\' )
{
// read the escaped char, if any
int e = MgGetChar(reader);
}
}
}
/*
Similar to `MgFindMatching`, except that it returns the intial cursor of the
reader as the `begin` of the result string, and the result from
`MgFindMatching` as the `end`.
Note: this means that when no match is found, the `end` of the result will
be NULL, but the `begin` won't be.
*/
MgString MgFindMatchingString(
MgReader* reader,
char c,
int count)
{
MgString text;
text.begin = reader->cursor;
text.end = MgFindMatching(reader, c, count);
return text;
}
int MgGetLineNumber(
MgInputFile* inputFile,
MgLine* line)
{
return 1 + (line - inputFile->beginLines);
}
int MgGetColumnNumber(
MgLine* line,
const char* cursor)
{
return 1 + (cursor - line->originalBegin);
}
/*
Return line number and column information (1-based) for a location in the
given input file, represented by the given pointer into the given line.
*/
MgSourceLoc MgGetSourceLoc(
MgInputFile* inputFile,
MgLine* line,
const char* cursor)
{
MgSourceLoc sourceLoc;
sourceLoc.line = MgGetLineNumber( inputFile, line );
sourceLoc.col = MgGetColumnNumber( line, cursor );
return sourceLoc;
}
#line 156 "source/main.md"
#line 5 "source/parse-span.md"
/*
### Span-Level Elements ###
*/
typedef struct SpanWriterT
{
MgElement* firstElement;
MgElement* lastElement;
char const* spanStart;
char const* spanEnd;
} SpanWriter;
void InitializeSpanWriter(
SpanWriter* writer )
{
writer->firstElement = 0;
writer->lastElement = 0;
writer->spanStart = 0;
writer->spanEnd = 0;
}
void BeginSpan(
SpanWriter* writer,
char const* spanStart )
{
writer->spanStart = spanStart;
writer->spanEnd = spanStart;
}
void ExtendSpan(
SpanWriter* writer,
char const* spanEnd )
{
writer->spanEnd = spanEnd;
}
void AddSpanElementImpl(
SpanWriter* writer,
MgElement* element )
{
// add the element to the list
if( writer->lastElement )
{
writer->lastElement->next = element;
}
else
{
writer->firstElement = element;
}
writer->lastElement = element;
writer->spanStart = 0;
writer->spanEnd = 0;
}
void FlushSpan(
SpanWriter* writer )
{
MgElement* element = NULL;
if( writer->spanStart == writer->spanEnd )
return;
element = MgCreateLeafElement(
kMgElementKind_Text,
MgMakeString(writer->spanStart, writer->spanEnd) );
AddSpanElementImpl( writer, element );
}
void AddSpanElement(
SpanWriter* writer,
MgElement* element )
{
FlushSpan( writer );
AddSpanElementImpl( writer, element );
}
#include <memory.h>
void AddLiteralSpan(
SpanWriter* writer,
char const* text )
{
FlushSpan( writer );
BeginSpan( writer, text );
ExtendSpan( writer, text + strlen(text) );
FlushSpan( writer );
}
void ReadLineSpans(
MgContext* context,
MgInputFile* inputFile,
MgLine* line,
char const* textBegin,
char const* textEnd,
MgSpanFlags flags,
SpanWriter* writer );
/*
Read span-level elements from the range of text given by
`textBegin` and `textEnd`, using the given flags. The range
of characters given should belong to `line` in `inputFile`,
which should be loaded in `context`.
*/
MgElement* MgReadSpanElements(
MgContext* context,
MgInputFile* inputFile,
MgLine* line,
MgString text,
MgSpanFlags flags )
{
SpanWriter writer;
InitializeSpanWriter( &writer );
ReadLineSpans(context, inputFile, line, text.begin, text.end, flags, &writer);
return writer.firstElement;