Cthulhu  0.2.10
Cthulhu compiler collection
flex.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "interop/actions.h"
4 
5 #include "base/panic.h"
6 
7 #include "core/where.h"
8 #include "scan/scan.h"
9 
10 #include <limits.h>
11 
13 void flex_action(where_t *where, const char *text)
14 {
15  CTASSERT(where != NULL);
16  CTASSERT(text != NULL);
17 
18  where_t tmp = *where;
19  tmp.first_line = tmp.last_line;
20  tmp.first_column = tmp.last_column;
21 
22  for (int i = 0; text[i]; i++)
23  {
24  if (text[i] == '\n')
25  {
26  tmp.last_line += 1;
27  tmp.last_column = 0;
28  }
29  else
30  {
31  tmp.last_column += 1;
32  }
33  }
34 
35  *where = tmp;
36 }
37 
39 int flex_input(scan_t *scan, char *out, int size)
40 {
41  CTASSERT(scan != NULL);
42  CTASSERT(out != NULL);
43 
44  CTASSERTF(size <= INT_MAX, "flex_input() size is too large (scan=%s, size=%d)", scan_path(scan), size);
45  return (int)scan_read(scan, out, size);
46 }
47 
49 void flex_init(where_t *where)
50 {
51  CTASSERT(where != NULL);
52 
53  where_t zero = { 0 };
54  *where = zero;
55 }
56 
58 void flex_update(where_t *where, const where_t *offsets, int steps)
59 {
60  CTASSERT(where != NULL);
61  CTASSERT(offsets != NULL);
62 
63  if (steps)
64  {
65  where_t rhs1 = offsets[1];
66  where_t rhsn = offsets[steps];
67 
68  where_t tmp = {
69  .first_line = rhs1.first_line,
70  .first_column = rhs1.first_column,
71  .last_line = rhsn.last_line,
72  .last_column = rhsn.last_column,
73  };
74 
75  *where = tmp;
76  }
77  else
78  {
79  where_t rhs = offsets[0];
80  where->last_line = rhs.last_line;
81  where->last_column = rhs.last_column;
82  }
83 }
CT_NODISCARD CT_PUREFN CT_SCAN_API const char * scan_path(const scan_t *scan)
get the path of a scanner
Definition: scan.c:56
CT_NODISCARD size_t size
Definition: scan.h:128
STA_DECL CT_NOALIAS size_t scan_read(scan_t *scan, void *dst, size_t size)
Definition: scan.c:104
STA_DECL void flex_update(where_t *where, const where_t *offsets, int steps)
Definition: flex.c:58
STA_DECL void flex_init(where_t *where)
Definition: flex.c:49
STA_DECL int flex_input(scan_t *scan, char *out, int size)
Definition: flex.c:39
STA_DECL void flex_action(where_t *where, const char *text)
Definition: flex.c:13
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
#define CTASSERTF(expr,...)
assert a condition with a message and optional format arguments
Definition: panic.h:116
a source file scanner
Definition: scan.h:24
a location inside a scanner locations are inclusive and 0-based
Definition: where.h:23
ctu_column_t first_column
the first column of the location
Definition: where.h:31
ctu_column_t last_column
the last column of the location
Definition: where.h:34
ctu_line_t last_line
the last line of the location
Definition: where.h:28
ctu_line_t first_line
the first line of the location
Definition: where.h:25