-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
peppa.h
1824 lines (1674 loc) · 53.6 KB
/
peppa.h
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
/**
* Peppa PEG - Ultra lightweight PEG Parser in ANSI C.
*
* MIT License
*
* Copyright (c) 2021 Ju
*
* 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.
*
* @file peppa.h
* @brief Peppa PEG header file
* @author Ju Lin
* @copyright MIT
* @date 2021
* @see https://github.com/soasme/PeppaPEG
*/
# ifndef P4_H
# define P4_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
# ifdef ENABLE_UNISTR
#include <unictype.h>
# else
/**
* A single unicode character.
* Used when libunistring is not installed.
* */
typedef uint32_t ucs4_t;
# endif
# ifndef P4_MALLOC
/**
* The malloc function. By default, it's `malloc`.
*/
# define P4_MALLOC malloc
# endif
# ifndef P4_FREE
/**
* The free function. By default, it's `free`.
*/
# define P4_FREE free
# endif
# ifndef P4_REALLOC
/**
* The realloc function. By default, it's `realloc`.
*/
# define P4_REALLOC realloc
# endif
/*
*
* ███████╗██╗░░░░░░█████╗░░██████╗░░██████╗
* ██╔════╝██║░░░░░██╔══██╗██╔════╝░██╔════╝
* █████╗░░██║░░░░░███████║██║░░██╗░╚█████╗░
* ██╔══╝░░██║░░░░░██╔══██║██║░░╚██╗░╚═══██╗
* ██║░░░░░███████╗██║░░██║╚██████╔╝██████╔╝
* ╚═╝░░░░░╚══════╝╚═╝░░╚═╝░╚═════╝░╚═════╝░
*/
/** Major version number. */
# define P4_MAJOR_VERSION 1
/** Minor version number. */
# define P4_MINOR_VERSION 16
/** Patch version number. */
# define P4_PATCH_VERSION 0
/** No flag. */
# define P4_FLAG_NONE ((uint32_t)(0x0))
/**
* When the flag is set, the grammar rule will have squash all
* children nodes.
*
* `node->head`, `node->tail` will be NULL.
*
* For example, rule b has P4_FLAG_SQUASHED. After parsing,
* the children nodes d and e are gone:
*
* a ===> a
* (b) c ===> b c
* d e
*
* The flag will impact all of the descendant rules.
**/
# define P4_FLAG_SQUASHED ((uint32_t)(0x1))
/**
* When the flag is set, the grammar rule will replace the
* node with its children nodes.
*
* For example, rule b has P4_FLAG_SQUASHED. After parsing,
* the node (b) is gone, and its children d and e become
* the children of a:
*
* a ===> a
* (b) c ===> d e c
* d e
* */
# define P4_FLAG_LIFTED ((uint32_t)(0x10))
/**
* When the flag is set, the grammar rule will insert
* no P4_FLAG_SPACED rules inside the sequences and repetitions
*
* This flag only works for the repetition and sequence expressions.
*/
# define P4_FLAG_TIGHT ((uint32_t)(0x100))
/**
* When the flag is set, the effect of SQUASHED and TIGHT
* are canceled.
*
* Regardless if the ancestor expression has SQUASHED or TIGHT
* flag, starting from this expression, Peppa PEG will start
* creating nodes and apply SPACED rules for sequences and repetitions.
*/
# define P4_FLAG_SCOPED ((uint32_t)(0x1000))
/**
* When the flag is set, the expression will be inserted
* between every node inside the sequences and repetitions
*
* If there are multiple SPACED expressions, Peppa PEG will
* iterate through all SPACED expressions.
*
* This flag makes the grammar clean and tidy without inserting
* whitespace rule everywhere.
*/
# define P4_FLAG_SPACED ((uint32_t)(0x10000))
/**
* When the flag is set and a sequence/repetition node tree
* has only one child, the child node will replace the parent.
*/
# define P4_FLAG_NON_TERMINAL ((uint32_t)(0x100000))
/**
* The default recursion limit.
*
* It can be adjusted via function P4_SetRecursionLimit.
*/
# define P4_DEFAULT_RECURSION_LIMIT 8192
/**
* The maximum length of a rule name.
*/
# define P4_MAX_RULE_NAME_LEN 32
/*
*
* ███████╗███╗░░██╗██╗░░░██╗███╗░░░███╗░██████╗
* ██╔════╝████╗░██║██║░░░██║████╗░████║██╔════╝
* █████╗░░██╔██╗██║██║░░░██║██╔████╔██║╚█████╗░
* ██╔══╝░░██║╚████║██║░░░██║██║╚██╔╝██║░╚═══██╗
* ███████╗██║░╚███║╚██████╔╝██║░╚═╝░██║██████╔╝
* ╚══════╝╚═╝░░╚══╝░╚═════╝░╚═╝░░░░░╚═╝╚═════╝░
*/
/** The expression kind. */
typedef enum {
/** Rule: Case-Sensitive Literal, Case-Insensitive Literal. */
P4_Literal,
/** Rule: Range. */
P4_Range,
/** Rule: Unicode Category. */
P4_UnicodeCategory,
/** Rule: Reference. */
P4_Reference,
/** Rule: Positive. */
P4_Positive,
/** Rule: Negative. */
P4_Negative,
/** Rule: Sequence. */
P4_Sequence,
/** Rule: Case-Sensitive BackReference, Case-Insensitive BackReference. */
P4_BackReference,
/** Rule: Choice. */
P4_Choice,
/**
* Rule: RepeatMinMax, RepeatMin, RepeatMax, RepeatExact,
* OnceOrMore, ZeroOrMore, ZeroOrOnce.
*/
P4_Repeat,
/** Rule: Cut. */
P4_Cut,
/** Rule: Left Recursion. */
P4_LeftRecursion
} P4_ExpressionKind;
/**
* The error code.
*/
typedef enum {
/** No error is like a bless. */
P4_Ok = 0,
/** When there is an internal error.
* Please raise an issue: https://github.com/soasme/peppapeg/issues.
* */
P4_InternalError = 1,
/** When no text is matched. */
P4_MatchError = 2,
/** When no name is resolved. */
P4_NameError = 3,
/** When the parse gets stuck forever or has reached the end. */
P4_AdvanceError = 4,
/** When out of memory. */
P4_MemoryError = 5,
/** When the given value is of unexpected type. */
P4_ValueError = 6,
/** When the index is out of boundary. */
P4_IndexError = 7,
/** When the id is out of the table. */
P4_KeyError = 8,
/** When null is encountered. */
P4_NullError = 9,
/** When recursion limit is reached. */
P4_StackError = 10,
/** When the given value is not valid peg grammar. */
P4_PegError = 11,
/** When the failure occurs after a `@cut` operator. */
P4_CutError = 12
} P4_Error;
# define E_WRONG_LIT 10000
# define E_BACKREF_OUT_REACHED 10001
# define E_BACKREF_TO_SELF 10002
# define E_TEXT_TOO_SHORT 10003
# define E_OUT_RANGED 10004
# define E_INVALID_UNICODE_CHAR 10005
# define E_INVALID_UNICODE_PROPERTY 10006
# define E_INVALID_UNICODE_CATEGORY 10007
# define E_NO_SUCH_RULE 10008
# define E_NO_ALTERNATIVE 10009
# define E_NO_PROGRESSING 10010
# define E_INSUFFICIENT_REPEAT 10011
# define E_INSUFFICIENT_REPEAT2 10012
# define E_EXCESSIVE_REPEAT 10013
# define E_LEFT_RECUR_NO_LIFT 10014
# define E_VIOLATE_NEGATIVE 10015
# define E_MAX_RECURSION 10016
# define E_INVALID_MATCH_CALLBACK 10017
# define E_INVALID_ERROR_CALLBACK 10018
# define E_NO_EXPR 10019
# define E_WRONG_BACKREF 10020
# define E_RECURSIVE_BACKREF 10021
/*
*
* ████████╗██╗░░░██╗██████╗░███████╗██████╗░███████╗███████╗░██████╗
* ╚══██╔══╝╚██╗░██╔╝██╔══██╗██╔════╝██╔══██╗██╔════╝██╔════╝██╔════╝
* ░░░██║░░░░╚████╔╝░██████╔╝█████╗░░██║░░██║█████╗░░█████╗░░╚█████╗░
* ░░░██║░░░░░╚██╔╝░░██╔═══╝░██╔══╝░░██║░░██║██╔══╝░░██╔══╝░░░╚═══██╗
* ░░░██║░░░░░░██║░░░██║░░░░░███████╗██████╔╝███████╗██║░░░░░██████╔╝
* ░░░╚═╝░░░░░░╚═╝░░░╚═╝░░░░░╚══════╝╚═════╝░╚══════╝╚═╝░░░░░╚═════╝░
*/
/**
* @brief The flag of expression.
**/
typedef uint32_t P4_ExpressionFlag;
/**
* The C string type in locale encoding, by default utf-8.
**/
typedef char* P4_String;
typedef const char* P4_ConstString;
/**
* The utf-8 string type.
*/
typedef uint8_t* P4_Utf8;
/**
* The reference of user data.
*/
typedef void* P4_UserData;
/**
* The function to free user data.
*/
typedef void (*P4_UserDataFreeFunc)(P4_UserData);
/**
* The grammar object that holds all grammar rules.
*/
typedef struct P4_Grammar P4_Grammar;
/**
* The grammar rule expression.
*/
typedef struct P4_Expression P4_Expression;
/**
* The stack frame.
*/
typedef struct P4_Frame P4_Frame;
/**
* The node object of abstract syntax tree.
*/
typedef struct P4_Node P4_Node;
/**
* The source object that holds text to parse.
*/
typedef struct P4_Source P4_Source;
/**
* The slice of a string.
*/
typedef struct P4_Slice P4_Slice;
/**
* The callback for a successful match.
*/
typedef P4_Error (*P4_MatchCallback)(P4_Grammar*, P4_Expression*, P4_Node*);
/**
* The callback for a failure match.
*/
typedef P4_Error (*P4_ErrorCallback)(P4_Grammar*, P4_Expression*);
/**
* A formatter function for the node.
*/
typedef void (*P4_Formatter)(FILE* stream, P4_Node* node);
/*
*
* ░██████╗████████╗██████╗░██╗░░░██╗░█████╗░████████╗░██████╗
* ██╔════╝╚══██╔══╝██╔══██╗██║░░░██║██╔══██╗╚══██╔══╝██╔════╝
* ╚█████╗░░░░██║░░░██████╔╝██║░░░██║██║░░╚═╝░░░██║░░░╚█████╗░
* ░╚═══██╗░░░██║░░░██╔══██╗██║░░░██║██║░░██╗░░░██║░░░░╚═══██╗
* ██████╔╝░░░██║░░░██║░░██║╚██████╔╝╚█████╔╝░░░██║░░░██████╔╝
* ╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝░╚═════╝░░╚════╝░░░░╚═╝░░░╚═════╝░
*/
/**
* The position.
*
* P4_Position does not hold a pointer to the string.
*
* Example:
*
* P4_Position pos = { .pos=10, .lineno=1, .offset=2 };
* printf("%u..%u\n", pos.lineno, pos.offset);
*/
typedef struct P4_Position {
/** The position in the string. */
size_t pos;
/** The line number in the string. */
size_t lineno;
/** The col offset in the line. */
size_t offset;
} P4_Position;
/**
* P4_RuneRange specifies a range between two runes.
*
* Example:
*
* P4_RuneRange range = { .lower='a', .upper='z', .stride=1 };
*/
typedef struct P4_RuneRange {
/** The lower code point of the range (inclusive). */
ucs4_t lower;
/** The upper code point of the range (inclusive). */
ucs4_t upper;
/** The step to jump inside the range. */
size_t stride;
} P4_RuneRange;
/**
*
* P4_Slice does not hold the pointer to the string.
* It only stores the start and stop position of the string.
*
* Example:
*
* P4_Slice slice = {
* i=0,
* j=strlen("hello world")
* };
* printf("%u..%u\n", slice.i, slice.j);
**/
struct P4_Slice {
/** The start position of the slice. */
P4_Position start;
/** The stop position of the slice. */
P4_Position stop;
};
/**
* AST Node.
*/
struct P4_Node {
/** the full text. */
P4_String text;
/** The matched substring.
* slice.start is the beginning (inclusive), and slice.stop is the end (exclusive).
*/
P4_Slice slice;
/** The matched grammar rule name. */
P4_String rule_name;
/** The user data. */
P4_UserData userdata;
/** the sibling node. NULL if not exists. */
struct P4_Node* next;
/** the first child of inner nodes. NULL if not exists. */
struct P4_Node* head;
/** the last child of inner nodes. NULL if not exists. */
struct P4_Node* tail;
};
/**
* The result object that holds either value or errors.
*/
typedef struct P4_Result {
union {
P4_String str;
ucs4_t rune;
size_t num;
P4_ExpressionFlag flag;
/* The expression result. */
P4_Expression* expr;
/* The grammar result. */
P4_Grammar* grammar;
};
/** The error message. */
char errmsg[256];
} P4_Result;
/*
*
* ███████╗██╗░░░██╗███╗░░██╗░█████╗░████████╗██╗░█████╗░███╗░░██╗░██████╗
* ██╔════╝██║░░░██║████╗░██║██╔══██╗╚══██╔══╝██║██╔══██╗████╗░██║██╔════╝
* █████╗░░██║░░░██║██╔██╗██║██║░░╚═╝░░░██║░░░██║██║░░██║██╔██╗██║╚█████╗░
* ██╔══╝░░██║░░░██║██║╚████║██║░░██╗░░░██║░░░██║██║░░██║██║╚████║░╚═══██╗
* ██║░░░░░╚██████╔╝██║░╚███║╚█████╔╝░░░██║░░░██║╚█████╔╝██║░╚███║██████╔╝
* ╚═╝░░░░░░╚═════╝░╚═╝░░╚══╝░╚════╝░░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝╚═════╝░
*/
/**
* Provide the version string for the library.
*
* @return a string like "1.0.0".
*
* Example:
*
* P4_String version = P4_Version();
* printf("version=%s\n", version);
*/
P4_String P4_Version(void);
/**
* Read a single code point (rune) from an UTF-8 string.
*/
size_t P4_ReadRune(P4_String s, ucs4_t* c);
/**
* Read an escaped single code point (rune) from an UTF-8 string.
*
* @param text The text.
* @param rune The rune.
* @return The size of rune.
*
* For example:
*
* ucs4_t rune;
* P4_ReadEscapedRune("\\u000D", rune); // 0xd
*/
size_t P4_ReadEscapedRune(char* text, ucs4_t* rune);
/**
* Append a rune to str (in-place).
*
* @param str The string to be appended.
* @param chr The rune.
* @param n The size of rune. Should be 1, 2, 3, 4.
* @return The string starting from appended rune.
*/
void* P4_ConcatRune(void* str, ucs4_t chr, size_t n);
/**
* Create a P4_Literal expression.
*
* @param literal The exact string to match.
* @param sensitive Whether the string is case-sensitive.
* @return A P4_Expression.
*
* Example:
*
* // It can match "let", "Let", "LET", etc.
* P4_Expression* expr = P4_CreateLiteral("let", false);
*
* // It can only match "let".
* P4_Expression* expr = P4_CreateLiteral("let", true);
*
* The object holds a full copy of the literal.
*
*
*/
P4_Expression* P4_CreateLiteral(const P4_String literal, bool sensitive);
/**
* Add a literal expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param literal The exact string to match.
* @param sensitive Whether the string is case-sensitive.
* @return The error code.
*
* Example:
*
* P4_AddLiteral(grammar, "R1", "let", true);
*/
P4_Error P4_AddLiteral(P4_Grammar* grammar, P4_String name, const P4_String literal, bool sensitive);
/**
* Create a P4_Range expression.
*
* @param lower The lower bound of UTF-8 rule to match (inclusive).
* @param upper The upper bound of UTF-8 rule to match (inclusive).
* @param stride The stride when iterating the characters in range.
* @return A P4_Expression.
*
* Example:
*
* // It can match 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
* P4_Expression* expr = P4_CreateRange('0', '9', 1);
*
* // It can match any code points starting from U+4E00 to U+9FCC (CJK unified ideographs block).
* P4_Expression* expr = P4_CreateRange(0x4E00, 0x9FFF, 1);
*
*
*/
P4_Expression* P4_CreateRange(ucs4_t lower, ucs4_t upper, size_t stride);
/**
* Create a P4_Range expression that holds multiple ranges.
* @param count The total number of ranges.
* @param ranges A list of P4_RuneRange.
* @return A P4_Expression.
*
* Example:
*
* P4_RuneRange alphadigits[] = {{'a', 'Z', 1}, {'0', '9', 1}};
* P4_Expression* range = P4_CreateRanges(
* sizeof(alphadigits) / sizeof(P4_RuneRange),
* alphadigits
* );
*/
P4_Expression* P4_CreateRanges(size_t count, P4_RuneRange* ranges);
/**
* Add a range expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param lower The lower bound of UTF-8 rule to match (inclusive).
* @param upper The upper bound of UTF-8 rule to match (inclusive).
* @param stride The stride when iterating the characters in range.
* @return The error code.
*
* Example:
*
* P4_AddRange(grammar, "R1", '0', '9', 1);
*
* P4_AddRange(grammar, "R1", 0x4E00, 0x9FFF, 1);
*/
P4_Error P4_AddRange(P4_Grammar* grammar, P4_String name, ucs4_t lower, ucs4_t upper, size_t stride);
/**
* Add sub-ranges expression as grammar rule.
* @param grammar The grammar.
* @param name The grammar rule name.
* @param count The total number of ranges.
* @param ranges A list of P4_RuneRange.
* @return The error code.
*
* Example:
*
* P4_RuneRange alphadigits[] = {{'a', 'Z', 1}, {'0', '9', 1}};
* P4_Error err = P4_AddRanges(
* grammar, "R1",
* sizeof(alphadigits) / sizeof(P4_RuneRange),
* alphadigits
* );
*/
P4_Error P4_AddRanges(P4_Grammar* grammar, P4_String name, size_t count, P4_RuneRange* ranges);
/**
* Create a P4_Reference expression.
*
* @param reference The grammar rule name.
* @return A P4_Expression.
*
* Example:
*
* P4_Expression* expr = P4_CreateReference("entry");
*
*
*/
P4_Expression* P4_CreateReference(P4_String reference);
/**
* Add a reference expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param ref_name The name of referenced grammar rule.
* @return The error code.
*
* Example:
*
* P4_AddReference(grammar, "R1", 2);
*/
P4_Error P4_AddReference(P4_Grammar* grammar, P4_String name, P4_String ref_name);
/**
* Create a P4_Positive expression.
*
* @param refexpr The positive pattern to check.
* @return A P4_Expression.
*
* Example:
*
* // If the following text includes "let", the match is successful.
* P4_Expression* expr = P4_CreatePositive(P4_CreateLiteral("let", true));
*
*
*/
P4_Expression* P4_CreatePositive(P4_Expression* refexpr);
/**
* Add a positive expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param refexpr The positive pattern to check.
* @return The error code.
*
* Example:
*
* P4_AddPositive(grammar, "R1", P4_CreateLiteral("let", true));
*/
P4_Error P4_AddPositive(P4_Grammar* grammar, P4_String name, P4_Expression* refexpr);
/**
* Create a P4_Negative expression.
*
* @param expr The negative pattern to check.
* @return A P4_Expression.
*
* Example:
*
* // If the following text does not start with "let", the match is successful.
* P4_Expression* expr = P4_CreateNegative(P4_CreateLiteral("let", true));
*
*/
P4_Expression* P4_CreateNegative(P4_Expression* expr);
/**
* Add a negative expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param refexpr The negative pattern to check.
* @return The error code.
*
* Example:
*
* P4_AddNegative(grammar, "R1", P4_CreateLiteral("let", true));
*/
P4_Error P4_AddNegative(P4_Grammar* grammar, P4_String name, P4_Expression* refexpr);
/**
* Create a P4_Cut expression.
*
* @return A P4_Expression.
*
*/
P4_Expression* P4_CreateCut();
/**
* Create a P4_LeftRecursion expression.
*
* @param lhs Left-hand side of left recursion.
* @param rhs Right-hand side of left recursion.
* @return A P4_Expression.
*/
P4_Expression* P4_CreateLeftRecursion(P4_Expression* lhs, P4_Expression* rhs);
/**
* Add a P4_LeftRecursion expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param lhs Left-hand side of left recursion.
* @param rhs Right-hand side of left recursion.
* @return The error code.
*/
P4_Error P4_AddLeftRecursion(P4_Grammar* grammar, P4_String name, P4_Expression* lhs, P4_Expression* rhs);
/**
* Create a P4_Sequence expression.
*
* @param count The number of sequence members.
* @return A P4_Expression.
*
* Example:
*
* P4_Expression* expr = P4_CreateSequence(3);
*
* Note that such an expression is useless as its members are all empty.
* Please set members using P4_SetMembers.
*
* This function can be useful if you need to add members dynamically.
*
*
*/
P4_Expression* P4_CreateSequence(size_t count);
/**
* Create a P4_Sequence expression.
*
* @param count The number of sequence members.
* @param ... The vararg of every sequence member.
* @return A P4_Expression.
*
* Example:
*
* // It can match { BODY }.
* P4_Expression* expr = P4_CreateSequenceWithMembers(3,
* P4_CreateLiteral("{"),
* P4_CreateReference(BODY),
* P4_CreateLiteral("}")
* );
*/
P4_Expression* P4_CreateSequenceWithMembers(size_t count, ...);
/**
* Add a sequence expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param count The number of sequence members.
* @return The error code.
*
* Example:
*
* P4_AddSequence(grammar, "R1", 3);
*
* Note that such an expression is useless as its members are all empty.
* Please set members using P4_SetMembers.
*
* This function can be useful if you need to add members dynamically.
*/
P4_Error P4_AddSequence(P4_Grammar* grammar, P4_String name, size_t count);
/**
* Add a sequence expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param count The number of sequence members.
* @param ... The members.
* @return The error code.
*
* Example:
*
* P4_AddSequenceWithMembers(grammar, "R1", 3,
* P4_CreateLiteral("{"),
* P4_CreateReference(BODY),
* P4_CreateLiteral("}")
* );
*/
P4_Error P4_AddSequenceWithMembers(P4_Grammar* grammar, P4_String name, size_t count, ...);
/**
* Create a P4_Choice expression.
*
* @param count The number of choice members.
* @return A P4_Expression.
*
* Example:
*
* P4_Expression* expr = P4_CreateChoice(3);
*
* Note that such an expression is useless as its members are all empty.
* Please set members using P4_SetMembers.
*
* This function can be useful if you need to add members dynamically.
*
*
*/
P4_Expression* P4_CreateChoice(size_t count);
/**
* Create a P4_Choice expression.
*
* @param count The number of choice members.
* @param ... The vararg of every choice member.
* @return A P4_Expression.
*
* Example:
*
* // It can match whitespace, tab and newline.
* P4_Expression* expr = P4_CreateChoiceWithMembers(3,
* P4_CreateLiteral(" "),
* P4_CreateReference(\t),
* P4_CreateLiteral("\n")
* );
*/
P4_Expression* P4_CreateChoiceWithMembers(size_t count, ...);
/**
* Add a choice expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param count The number of choice members.
* @return The error code.
*
* Example:
*
* P4_AddChoice(grammar, "R1", 3);
*
* Note that such an expression is useless as its members are all empty.
* Please set members using P4_SetMembers.
*
* This function can be useful if you need to add members dynamically.
*/
P4_Error P4_AddChoice(P4_Grammar* grammar, P4_String name, size_t count);
/**
* Add a choice expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param count The number of choice members.
* @param ... The members.
* @return The error code.
*
* Example:
*
* P4_AddChoiceWithMembers(grammar, "R1", 3,
* P4_CreateLiteral(" "),
* P4_CreateReference(\t),
* P4_CreateLiteral("\n")
* );
*/
P4_Error P4_AddChoiceWithMembers(P4_Grammar* grammar, P4_String name, size_t count, ...);
/**
* Create a P4_Repeat expression minimal min times and maximal max times.
*
* @param repeat_expr The repeated expression.
* @param min The minimum repeat times.
* @param max The maximum repeat times.
* @return A P4_Expression.
*
* Example:
*
* // It can match string "a", "aa", or "aaa".
* P4_Expression* expr = P4_CreateRepeatMinMax(
* P4_CreateLiteral("a", true),
* 1, 3
* );
*/
P4_Expression* P4_CreateRepeatMinMax(P4_Expression* repeat_expr, size_t min, size_t max);
/**
* Create a P4_Repeat expression minimal min times and maximal max times.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param repeat_expr The repeated expression.
* @param min The minimum repeat times.
* @param max The maximum repeat times.
* @return The error code.
*
* Example:
*
* // It can match string "a", "aa", or "aaa".
* P4_AddRepeatMinMax(grammar, "R1",
* P4_CreateLiteral("a", true),
* 1, 3
* );
*/
P4_Error P4_AddRepeatMinMax(P4_Grammar* grammar, P4_String name, P4_Expression* repeat_expr, size_t min, size_t max);
/**
* Create a P4_Repeat expression minimal min times and maximal SIZE_MAX times.
*
* @param repeat_expr The repeated expression.
* @param min The minimum repeat times.
* @return A P4_Expression.
*
* Example:
*
* // It can match string "a", "aa", "aaa", ....
* P4_Expression* expr = P4_CreateRepeatMin(P4_CreateLiteral("a", true), 1);
*
* It's equivalent to P4_CreateRepeatMinMax(expr, min, SIZE_MAX);
*
*/
P4_Expression* P4_CreateRepeatMin(P4_Expression* repeat_expr, size_t min);
/**
* Create a RepeatMin expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param repeat_expr The repeated expression.
* @param min The minimum repeat times.
* @return The error code.
*
* Example:
*
* // It can match string "a", "aa", "aaa", ....
* P4_AddRepeatMin(grammar, "R1", P4_CreateLiteral("a", true), 1);
*/
P4_Error P4_AddRepeatMin(P4_Grammar* grammar, P4_String name, P4_Expression* repeat_expr, size_t min);
/**
* Create a P4_Repeat expression maximal max times.
*
* @param repeat_expr The repeated expression.
* @param max The maximum repeat times.
* @return A P4_Expression.
*
* Example:
*
* // It can match string "", "a", "aa", "aaa".
* P4_Expression* expr = P4_CreateRepeatMax(P4_CreateLiteral("a", true), 3);
*
* It's equivalent to P4_CreateRepeatMinMax(expr, 0, max);
*
*/
P4_Expression* P4_CreateRepeatMax(P4_Expression* repeat_expr, size_t max);
/**
* Add a RepeatMax expression as grammar rule.
*
* @param grammar The grammar.
* @param name The grammar rule name.
* @param repeat_expr The repeated expression.
* @param max The maximum repeat times.
* @return A P4_Expression.
*
* Example:
*
* // It can match string "", "a", "aa", "aaa".
* P4_AddRepeatMax(grammar, "name", P4_CreateLiteral("a", true), 3);
*/
P4_Error P4_AddRepeatMax(P4_Grammar* grammar, P4_String name, P4_Expression* repeat_expr, size_t max);
/**
* Create a P4_Repeat expression exact N times.
*
* @param repeat_expr The repeated expression.