Lely core libraries 2.3.4
lex.h
Go to the documentation of this file.
1
22#ifndef LELY_UTIL_LEX_H_
23#define LELY_UTIL_LEX_H_
24
25#include <lely/libc/uchar.h>
26#include <lely/util/util.h>
27
28#include <ctype.h>
29#include <stdint.h>
30
31#ifndef LELY_UTIL_LEX_INLINE
32#define LELY_UTIL_LEX_INLINE static inline
33#endif
34
35// The file location struct from <lely/util/diag.h>.
36struct floc;
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
43LELY_UTIL_LEX_INLINE int isbreak(int c);
44
46LELY_UTIL_LEX_INLINE int isodigit(int c);
47
49LELY_UTIL_LEX_INLINE int ctoo(int c);
50
56LELY_UTIL_LEX_INLINE int ctox(int c);
57
72size_t lex_char(int c, const char *begin, const char *end, struct floc *at);
73
90size_t lex_ctype(int (*ctype)(int), const char *begin, const char *end,
91 struct floc *at);
92
106size_t lex_break(const char *begin, const char *end, struct floc *at);
107
127size_t lex_utf8(const char *begin, const char *end, struct floc *at,
128 char32_t *pc32);
129
148size_t lex_c99_id(const char *begin, const char *end, struct floc *at, char *s,
149 size_t *pn);
150
172size_t lex_c99_esc(const char *begin, const char *end, struct floc *at,
173 char32_t *pc32);
174
197size_t lex_c99_str(const char *begin, const char *end, struct floc *at, char *s,
198 size_t *pn);
199
214size_t lex_c99_pp_num(const char *begin, const char *end, struct floc *at);
215
216// clang-format off
217#define LELY_UTIL_DEFINE_LEX_SIGNED(type, suffix, strtov, pname) \
218 \
234 size_t lex_c99_##suffix(const char *begin, \
235 const char *end, struct floc *at, type *pname);
236// clang-format on
237
238// clang-format off
239#define LELY_UTIL_DEFINE_LEX_UNSIGNED(type, suffix, strtov, pname) \
240 \
256 size_t lex_c99_##suffix(const char *begin, \
257 const char *end, struct floc *at, type *pname);
258// clang-format on
259
260LELY_UTIL_DEFINE_LEX_SIGNED(long, long, strtol, pl)
261LELY_UTIL_DEFINE_LEX_UNSIGNED(unsigned long, ulong, strtoul, pul)
262LELY_UTIL_DEFINE_LEX_SIGNED(long long, llong, strtoll, pll)
263LELY_UTIL_DEFINE_LEX_UNSIGNED(unsigned long long, ullong, strtoull, pul)
264LELY_UTIL_DEFINE_LEX_SIGNED(float, flt, strtof, pf)
265LELY_UTIL_DEFINE_LEX_SIGNED(double, dbl, strtod, pd)
266LELY_UTIL_DEFINE_LEX_SIGNED(long double, ldbl, strtold, pld)
267
268#undef LELY_UTIL_DEFINE_LEX_UNSIGNED
269#undef LELY_UTIL_DEFINE_LEX_SIGNED
270
271// clang-format off
272#define LELY_UTIL_DEFINE_LEX_SIGNED(type, suffix, pname) \
273 \
288 size_t lex_c99_##suffix(const char *begin, \
289 const char *end, struct floc *at, type *pname);
290// clang-format on
291
292// clang-format off
293#define LELY_UTIL_DEFINE_LEX_UNSIGNED(type, suffix, pname) \
294 \
309 size_t lex_c99_##suffix(const char *begin, \
310 const char *end, struct floc *at, type *pname);
311// clang-format on
312
313LELY_UTIL_DEFINE_LEX_SIGNED(int_least8_t, i8, pi8)
314LELY_UTIL_DEFINE_LEX_SIGNED(int_least16_t, i16, pi16)
315LELY_UTIL_DEFINE_LEX_SIGNED(int_least32_t, i32, pi32)
316LELY_UTIL_DEFINE_LEX_SIGNED(int_least64_t, i64, pi64)
317LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least8_t, u8, pu8)
318LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least16_t, u16, pu16)
319LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least32_t, u32, pu32)
320LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least64_t, u64, pu64)
321
322#undef LELY_UTIL_DEFINE_LEX_UNSIGNED
323#undef LELY_UTIL_DEFINE_LEX_SIGNED
324
342size_t lex_line_comment(const char *delim, const char *begin, const char *end,
343 struct floc *at);
344
365size_t lex_hex(const char *begin, const char *end, struct floc *at, void *ptr,
366 size_t *pn);
367
391size_t lex_base64(const char *begin, const char *end, struct floc *at,
392 void *ptr, size_t *pn);
393
394LELY_UTIL_LEX_INLINE int
395isbreak(int c)
396{
397 return c == '\n' || c == '\r';
398}
399
400LELY_UTIL_LEX_INLINE int
401isodigit(int c)
402{
403 return c >= '0' && c <= '7';
404}
405
406LELY_UTIL_LEX_INLINE int
407ctoo(int c)
408{
409 return c - '0';
410}
411
412LELY_UTIL_LEX_INLINE int
413ctox(int c)
414{
415 if (isdigit(c))
416 return c - '0';
417#if __STDC_ISO_10646__ && !__STDC_MB_MIGHT_NEQ_WC__
418 return 10 + (isupper(c) ? c - 'A' : c - 'a');
419#else
420 switch (c) {
421 case 'A':
422 case 'a': return 0xa;
423 case 'B':
424 case 'b': return 0xb;
425 case 'C':
426 case 'c': return 0xc;
427 case 'D':
428 case 'd': return 0xd;
429 case 'E':
430 case 'e': return 0xe;
431 case 'F':
432 case 'f': return 0xf;
433 default: return -1;
434 }
435#endif
436}
437
438#ifdef __cplusplus
439}
440#endif
441
442#endif // !LELY_UTIL_LEX_H_
This is the public header file of the utilities library.
int isodigit(int c)
Returns 1 if c is an octal digit, and 0 otherwise.
Definition: lex.h:343
size_t lex_char(int c, const char *begin, const char *end, struct floc *at)
Lexes the specified character from a memory buffer.
Definition: lex.c:41
size_t lex_ctype(int(*ctype)(int), const char *begin, const char *end, struct floc *at)
Greedily lexes a sequence of characters of the specified class from a memory buffer.
Definition: lex.c:54
size_t lex_utf8(const char *begin, const char *end, struct floc *at, char32_t *pc32)
Lexes a UTF-8 encoded Unicode character from a memory buffer.
Definition: lex.c:86
size_t lex_c99_id(const char *begin, const char *end, struct floc *at, char *s, size_t *pn)
Lexes a C99 identifier from a memory buffer.
Definition: lex.c:162
size_t lex_line_comment(const char *delim, const char *begin, const char *end, struct floc *at)
Lexes a single line-comment (excluding the line break) starting with the specified delimiter from a m...
Definition: lex.c:633
size_t lex_c99_esc(const char *begin, const char *end, struct floc *at, char32_t *pc32)
Lexes a C99 character escape sequence from a memory buffer if the buffer begins with '\',...
Definition: lex.c:187
size_t lex_hex(const char *begin, const char *end, struct floc *at, void *ptr, size_t *pn)
Lexes and decodes the hexadecimal representation of binary data from a memory buffer.
Definition: lex.c:655
size_t lex_base64(const char *begin, const char *end, struct floc *at, void *ptr, size_t *pn)
Lexes and decodes the Base64 representation of binary data from a memory buffer.
Definition: lex.c:685
int isbreak(int c)
Returns 1 if c is a line break character, and 0 otherwise.
Definition: lex.h:337
int ctoo(int c)
Returns the octal digit corresponding to the character c.
Definition: lex.h:349
size_t lex_c99_str(const char *begin, const char *end, struct floc *at, char *s, size_t *pn)
Lexes a UTF-8 encoded Unicode string from a memory buffer.
Definition: lex.c:250
size_t lex_break(const char *begin, const char *end, struct floc *at)
Lexes a single line break from a memory buffer.
Definition: lex.c:69
int ctox(int c)
Returns the hexadecimal digit corresponding to the character c.
Definition: lex.h:355
size_t lex_c99_pp_num(const char *begin, const char *end, struct floc *at)
Lexes a C99 preprocessing number from a memory buffer.
Definition: lex.c:288
This header file is part of the C11 and POSIX compatibility library; it includes <stdint....
A location in a text file.
Definition: diag.h:39
This header file is part of the C11 and POSIX compatibility library; it includes <uchar....