Lely core libraries  2.2.5
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>.
36 struct floc;
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
43 LELY_UTIL_LEX_INLINE int isbreak(int c);
44 
46 LELY_UTIL_LEX_INLINE int isodigit(int c);
47 
49 LELY_UTIL_LEX_INLINE int ctoo(int c);
50 
56 LELY_UTIL_LEX_INLINE int ctox(int c);
57 
72 size_t lex_char(int c, const char *begin, const char *end, struct floc *at);
73 
90 size_t lex_ctype(int (*ctype)(int), const char *begin, const char *end,
91  struct floc *at);
92 
106 size_t lex_break(const char *begin, const char *end, struct floc *at);
107 
127 size_t lex_utf8(const char *begin, const char *end, struct floc *at,
128  char32_t *pc32);
129 
148 size_t lex_c99_id(const char *begin, const char *end, struct floc *at, char *s,
149  size_t *pn);
150 
172 size_t lex_c99_esc(const char *begin, const char *end, struct floc *at,
173  char32_t *pc32);
174 
197 size_t lex_c99_str(const char *begin, const char *end, struct floc *at, char *s,
198  size_t *pn);
199 
214 size_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 
260 LELY_UTIL_DEFINE_LEX_SIGNED(long, long, strtol, pl)
261 LELY_UTIL_DEFINE_LEX_UNSIGNED(unsigned long, ulong, strtoul, pul)
262 LELY_UTIL_DEFINE_LEX_SIGNED(long long, llong, strtoll, pll)
263 LELY_UTIL_DEFINE_LEX_UNSIGNED(unsigned long long, ullong, strtoull, pul)
264 LELY_UTIL_DEFINE_LEX_SIGNED(float, flt, strtof, pf)
265 LELY_UTIL_DEFINE_LEX_SIGNED(double, dbl, strtod, pd)
266 LELY_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 
313 LELY_UTIL_DEFINE_LEX_SIGNED(int_least8_t, i8, pi8)
314 LELY_UTIL_DEFINE_LEX_SIGNED(int_least16_t, i16, pi16)
315 LELY_UTIL_DEFINE_LEX_SIGNED(int_least32_t, i32, pi32)
316 LELY_UTIL_DEFINE_LEX_SIGNED(int_least64_t, i64, pi64)
317 LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least8_t, u8, pu8)
318 LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least16_t, u16, pu16)
319 LELY_UTIL_DEFINE_LEX_UNSIGNED(uint_least32_t, u32, pu32)
320 LELY_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 
342 size_t lex_line_comment(const char *delim, const char *begin, const char *end,
343  struct floc *at);
344 
365 size_t lex_hex(const char *begin, const char *end, struct floc *at, void *ptr,
366  size_t *pn);
367 
391 size_t lex_base64(const char *begin, const char *end, struct floc *at,
392  void *ptr, size_t *pn);
393 
394 inline int
395 isbreak(int c)
396 {
397  return c == '\n' || c == '\r';
398 }
399 
400 inline int
401 isodigit(int c)
402 {
403  return c >= '0' && c <= '7';
404 }
405 
406 inline int
407 ctoo(int c)
408 {
409  return c - '0';
410 }
411 
412 inline int
413 ctox(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_
A location in a text file.
Definition: diag.h:31
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
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
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_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_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_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
int isbreak(int c)
Returns 1 if c is a line break character, and 0 otherwise.
Definition: lex.h:395
This header file is part of the C11 and POSIX compatibility library; it includes <uchar.h>, if it exists, and defines any missing functionality.
This header file is part of the C11 and POSIX compatibility library; it includes <stdint.h> and defines any missing functionality.
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:652
int ctoo(int c)
Returns the octal digit corresponding to the character c.
Definition: lex.h:407
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 &#39;\&#39;...
Definition: lex.c:184
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:630
int ctox(int c)
Returns the hexadecimal digit corresponding to the character c.
Definition: lex.h:413
int isodigit(int c)
Returns 1 if c is an octal digit, and 0 otherwise.
Definition: lex.h:401
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:682
This is the public header file of the utilities library.
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