CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
parse_private.h
1#pragma once
2
3#include "parse.h"
4
5#include "cx/container.h"
6#include "cx/string.h"
7
8// Text patterns the matcher knows how to recognize. This is about how much text a
9// placeholder eats, not about what it eventually turns into -- the destination decides
10// that, which is why the same "${uint}" can fill a uint16 or a string.
11enum PARSE_TYPES {
12 PARSE_string,
13 PARSE_int,
14 PARSE_uint,
15 PARSE_float,
16 PARSE_bool,
17 PARSE_object,
18 PARSE_count
19};
20
21extern strref _parseTypeNames[PARSE_count];
22
23typedef struct ParseField {
24 string key; // binding key, or NULL for a positional field
25 int32 posidx; // ordinal among positional fields, or -1 if keyed
26 uint8 ptype; // PARSE_TYPES
27 bool isgroup; // records which alternative of a group matched, not text
28 bool skip; // matched but never bound anywhere
29
30 string def; // default text
31 bool hasdef;
32
33 // int / uint
34 int32 base;
35 int32 digits; // exact digit count, or 0 for any
36 int32 maxdigits; // 0 for no limit
37 bool hasmin, hasmax;
38 int64 minval, maxval;
39 bool ws; // allow whitespace before the number
40
41 // float
42 bool fixed; // refuse scientific notation
43
44 // string
45 int32 len; // exact byte count, or 0
46 string until;
47 string chars; // character set for chars / notchars
48 bool notchars;
49 bool word, rest, quoted, trim, upper, lower;
50
51 // object
52 sa_string objopts; // options the compiler did not recognize, passed through verbatim
53} ParseField;
54
55stDeclare(ParseField);
56saDeclare(ParseField);
57
58typedef enum {
59 PE_Literal, // fixed text
60 PE_WS, // a run of one or more whitespace bytes
61 PE_Field, // a placeholder
62 PE_Group // ( ... | ... )?
63} ParseElemKind;
64
65// Elements and alternatives nest through each other, so both are declared before either is
66// defined. Their destructors recurse the same way, which is what lets one saDestroy on the
67// root take a whole compiled pattern apart.
68typedef struct ParseElem ParseElem;
69stDeclare(ParseElem);
70saDeclare(ParseElem);
71
72typedef struct ParseAlt {
73 sa_ParseElem elems;
74} ParseAlt;
75
76stDeclare(ParseAlt);
77saDeclare(ParseAlt);
78
79struct ParseElem {
80 uint8 kind;
81
82 string lit; // PE_Literal
83 int32 field; // PE_Field, or PE_Group's own key field (-1 if it has none)
84
85 sa_ParseAlt alts; // PE_Group
86 bool optional;
87};
88
89struct StrPattern {
90 ParseAlt root;
91 sa_ParseField fields;
92 int32 npositional;
93 flags_t flags;
94
95 int32 nelems; // total elements anywhere in the pattern
96 int32 complexity; // product of the branch counts, saturated -- feeds the match budget
97};
98
99// One recorded field position. The matcher produces only these; nothing is written into a
100// caller's variable until the whole pattern has matched.
101//
102// For a normal field, off/len are a span of the input, and an off of -1 means "the field
103// did not match, use its default". For a group's key field, off is the 1-based number of
104// the alternative that matched, or 0 if the group did not match at all.
105typedef struct StrPatSpan {
106 int32 field;
107 int32 off;
108 int32 len;
109} StrPatSpan;
110
111// parsematch.c
112//
113// Match the pattern against s starting at `start`. On success fills spans (which must
114// have room for saSize(pat->fields) entries), sets *nspans and *endpos, and returns true.
115bool _parseRun(_In_ const StrPattern* pat, _In_opt_ strref s, int32 start, bool requireEnd,
116 _Out_ StrPatSpan* spans, _Out_ int32* nspans, _Out_ int32* endpos);
117
118// parsebind.c
119//
120// Write the matched spans into the caller's destinations. Conversions that can fail are
121// all checked before anything is written.
122bool _parseBind(_In_ const StrPattern* pat, _In_opt_ strref s, _In_ const StrPatSpan* spans,
123 int32 nspans, int n, _In_ stvp* dests);
124
125// Text a span stands for, with the field's own transforms applied.
126void _parseSpanText(_Inout_ strhandle out, _In_ const ParseField* f, _In_opt_ strref s,
127 _In_ const StrPatSpan* sp);
Generic type-safe containers with runtime type system integration.
#define saDeclare(name)
Definition sarray.h:93
string * strhandle
Pointer to a string variable.
Definition strbase.h:64
struct StrPattern StrPattern
Definition parse.h:219
#define stDeclare(name)
Definition stype.h:1908
Pattern-based text parsing, the reading counterpart to strFormat.
Copy-on-write strings with automatic memory management and rope optimization.
Definition stvar.h:293