Cthulhu  0.2.10
Cthulhu compiler collection
scan.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "arena/arena.h"
4 #include "base/util.h"
5 #include "json_scan.h"
6 
7 #include "core/macros.h"
8 
10 #include "cthulhu/util/text.h"
11 #include "std/set.h"
12 
13 #include <stdlib.h>
14 
16 {
17  return scan_get_context(scan);
18 }
19 
20 void json_parse_integer(mpz_t integer, scan_t *scan, where_t where, const char *text, int base)
21 {
22  int result = mpz_init_set_str(integer, text, base);
23  if (result != 0)
24  {
25  json_scan_t *ctx = json_scan_context(scan);
26  msg_notify(ctx->reports, &kEvent_InvalidIntegerLiteral, node_new(scan, where), "invalid integer literal");
27  }
28 }
29 
30 void json_parse_float(float *real, scan_t *scan, where_t where, const char *text)
31 {
32  // TODO: dont use strtof
33  float result = strtof(text, NULL);
34  if (result != 1)
35  {
36  json_scan_t *ctx = json_scan_context(scan);
37  msg_notify(ctx->reports, &kEvent_InvalidFloatLiteral, node_new(scan, where), "invalid float literal");
38  }
39  *real = result;
40 }
41 
42 void json_parse_string(text_view_t *string, scan_t *scan, json_where_t where, const char *text, size_t length)
43 {
44  // if we dont have any escapes then we return a view of the source text
45  if (!util_text_has_escapes(text, length))
46  {
47  text_view_t source = scan_source(scan);
48  text_view_t span = text_view_make(source.text + where.offset - length - 1, length);
49  *string = span;
50  }
51  else
52  {
53  // otherwise escape the text
54  json_scan_t *ctx = json_scan_context(scan);
55  arena_t *arena = scan_get_arena(scan);
56  node_t node = node_make(scan, where.where);
57  text_t escaped = util_text_escape(ctx->reports, &node, text, length, arena);
58  *string = text_view_make(escaped.text, escaped.length);
59  }
60 }
61 
62 void jsonerror(where_t *where, void *state, scan_t *scan, const char *msg)
63 {
64  CT_UNUSED(state);
65 
66  json_scan_t *ctx = json_scan_context(scan);
67 
68  evt_scan_error(ctx->reports, node_new(scan, *where), msg);
69 }
STA_DECL void * scan_get_context(const scan_t *scan)
get the context of a scanner
Definition: scan.c:88
STA_DECL text_view_t scan_source(const scan_t *scan)
get a text span of the scanners contents
Definition: scan.c:96
STA_DECL arena_t * scan_get_arena(const scan_t *scan)
get the arena of a scanner
Definition: scan.c:113
CT_CONSTFN CT_BASE_API text_view_t text_view_make(STA_READS(length) const char *text, size_t length)
create a new non-owning text array text must be at least length bytes long
CT_EVENTS_API void evt_scan_error(logger_t *logger, const node_t *node, const char *msg)
signal that a scan error has occurred
Definition: events.c:33
CT_NODISCARD CT_SCAN_API node_t * node_new(const scan_t *scan, where_t where)
create a new node on the heap
Definition: node.c:40
CT_NODISCARD CT_SCAN_API node_t node_make(const scan_t *scan, where_t where)
create a new node on the stack
Definition: node.c:49
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
CT_NOTIFY_API event_builder_t msg_notify(INOUT_NOTNULL logger_t *logs, const diagnostic_t *diagnostic, const node_t *node, STA_FORMAT_STRING const char *fmt,...)
notify the logger of a new message
CT_UTIL_API bool util_text_has_escapes(STA_READS(length) const char *text, size_t length)
CT_BEGIN_API CT_UTIL_API text_t util_text_escape(logger_t *reports, const node_t *node, STA_READS(length) const char *text, size_t length, arena_t *arena)
escape a string literal into a string
an allocator object
Definition: arena.h:86
logger_t * reports
Definition: json_scan.h:25
size_t offset
Definition: json_scan.h:19
where_t where
Definition: json_scan.h:20
a position in a source file
Definition: node.h:23
a source file scanner
Definition: scan.h:24
a range of text
Definition: text.h:14
size_t length
the number of characters in the text
Definition: text.h:19
a non-owning view of text
Definition: text.h:24
a location inside a scanner locations are inclusive and 0-based
Definition: where.h:23
void json_parse_string(text_view_t *string, scan_t *scan, json_where_t where, const char *text, size_t length)
Definition: scan.c:42
void jsonerror(where_t *where, void *state, scan_t *scan, const char *msg)
Definition: scan.c:62
json_scan_t * json_scan_context(scan_t *scan)
Definition: scan.c:15
void json_parse_integer(mpz_t integer, scan_t *scan, where_t where, const char *text, int base)
Definition: scan.c:20
void json_parse_float(float *real, scan_t *scan, where_t where, const char *text)
Definition: scan.c:30