Lely core libraries 1.9.2
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
368size_t lex_base64(const char *begin, const char *end, struct floc *at,
369 void *ptr, size_t *pn);
370
371inline int
372isbreak(int c)
373{
374 return c == '\n' || c == '\r';
375}
376
377inline int
378isodigit(int c)
379{
380 return c >= '0' && c <= '7';
381}
382
383inline int
384ctoo(int c)
385{
386 return c - '0';
387}
388
389inline int
390ctox(int c)
391{
392 if (isdigit(c))
393 return c - '0';
394#if __STDC_ISO_10646__ && !__STDC_MB_MIGHT_NEQ_WC__
395 return 10 + (isupper(c) ? c - 'A' : c - 'a');
396#else
397 switch (c) {
398 case 'A':
399 case 'a': return 0xa;
400 case 'B':
401 case 'b': return 0xb;
402 case 'C':
403 case 'c': return 0xc;
404 case 'D':
405 case 'd': return 0xd;
406 case 'E':
407 case 'e': return 0xe;
408 case 'F':
409 case 'f': return 0xf;
410 default: return -1;
411 }
412#endif
413}
414
415#ifdef __cplusplus
416}
417#endif
418
419#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:320
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:38
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:51
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:83
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:159
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:634
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:184
size_t lex_base64(const char *begin, const char *end, struct floc *at, void *ptr, size_t *pn)
Lexwes and decodes the Base64 representation of binary data from a memory buffer.
Definition: lex.c:656
int isbreak(int c)
Returns 1 if c is a line break character, and 0 otherwise.
Definition: lex.h:314
int ctoo(int c)
Returns the octal digit corresponding to the character c.
Definition: lex.h:326
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:247
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:66
int ctox(int c)
Returns the hexadecimal digit corresponding to the character c.
Definition: lex.h:332
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:285
This header file is part of the C11 and POSIX compatibility library; it includes <stdint....
A location in a text file.
Definition: diag.h:31
This header file is part of the C11 and POSIX compatibility library; it includes <uchar....