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>.
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 
368 size_t lex_base64(const char *begin, const char *end, struct floc *at,
369  void *ptr, size_t *pn);
370 
371 inline int
372 isbreak(int c)
373 {
374  return c == '\n' || c == '\r';
375 }
376 
377 inline int
378 isodigit(int c)
379 {
380  return c >= '0' && c <= '7';
381 }
382 
383 inline int
384 ctoo(int c)
385 {
386  return c - '0';
387 }
388 
389 inline int
390 ctox(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_
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:372
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.
int ctoo(int c)
Returns the octal digit corresponding to the character c.
Definition: lex.h:384
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:390
int isodigit(int c)
Returns 1 if c is an octal digit, and 0 otherwise.
Definition: lex.h:378
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:652
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