Cthulhu  0.2.10
Cthulhu compiler collection
compile.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include <ctu_interop_api.h>
6 
7 #include "scan/scan.h"
8 
9 #include "base/panic.h"
10 
11 #include <limits.h>
12 
13 typedef struct io_t io_t;
14 typedef struct scan_t scan_t;
15 
17 
22 
25 typedef struct scan_callbacks_t
26 {
27  int (*init)(scan_t *extra, void *scanner);
28  int (*parse)(void *scanner, scan_t *extra);
29  void *(*scan)(const char *text, size_t size, void *scanner);
30  void (*destroy_buffer)(void *buffer, void *scanner);
31  void (*destroy)(void *scanner);
33 
41 
42 #define CT_CALLBACKS(id, prefix) \
43  static int prefix##_##id##_##init(scan_t *extra, void *scanner) \
44  { \
45  return prefix##lex_init_extra(extra, (yyscan_t *)scanner); \
46  } \
47  static int prefix##_##id##_parse(void *scanner, scan_t *extra) \
48  { \
49  return prefix##parse(scanner, (scan_t *)extra); \
50  } \
51  static void *prefix##_##id##_scan(const char *text, size_t size, void *scanner) \
52  { \
53  CTASSERTF(size <= INT_MAX, #prefix "_scan (size = %zu > %d, name = %s)", size, INT_MAX, \
54  scan_path((scan_t *)scanner)); \
55  return prefix##_scan_bytes(text, (int)size, scanner); \
56  } \
57  static void prefix##_##id##_destroy_buffer(void *buffer, void *scanner) \
58  { \
59  prefix##_delete_buffer((YY_BUFFER_STATE)buffer, scanner); \
60  } \
61  static void prefix##_##id##_destroy(void *scanner) \
62  { \
63  prefix##lex_destroy(scanner); \
64  } \
65  static const scan_callbacks_t id = { \
66  .init = prefix##_##id##_##init, \
67  .parse = prefix##_##id##_parse, \
68  .scan = prefix##_##id##_scan, \
69  .destroy_buffer = prefix##_##id##_destroy_buffer, \
70  .destroy = prefix##_##id##_destroy, \
71  }
72 
73 typedef enum parse_error_t
74 {
77 
80 
83 
86 
89 
90 typedef struct parse_result_t
91 {
93 
94  union {
95  void *tree;
96  int error;
97  };
99 
107 CT_INTEROP_API parse_result_t scan_buffer(
108  IN_NOTNULL scan_t *scan,
109  IN_NOTNULL const scan_callbacks_t *callbacks);
110 
112 
CT_NODISCARD size_t size
Definition: scan.h:128
#define IN_NOTNULL
annotate a parameter as not being null
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
CT_INTEROP_API parse_result_t scan_buffer(scan_t *scan, const scan_callbacks_t *callbacks)
parse the contents of a scanner into a language specific ast
Definition: compile.c:26
parse_error_t
Definition: compile.h:74
@ eParseInitError
error initializing the scanner internals (our fault)
Definition: compile.h:79
@ eParseOk
parse was successful
Definition: compile.h:76
@ eParseScanError
entered invalid state during scanning (our fault)
Definition: compile.h:82
@ eParseReject
failed due to invalid input (users fault)
Definition: compile.h:85
@ eParseCount
Definition: compile.h:87
io object implementation
Definition: impl.h:122
parse_error_t result
Definition: compile.h:92
void * tree
Definition: compile.h:95
scanner function callbacks for flex and bison
Definition: compile.h:26
void(* destroy_buffer)(void *buffer, void *scanner)
yy_delete_buffer
Definition: compile.h:30
int(* parse)(void *scanner, scan_t *extra)
yyparse
Definition: compile.h:28
void(* destroy)(void *scanner)
yylex_destroy
Definition: compile.h:31
int(* init)(scan_t *extra, void *scanner)
yylex_init_extra
Definition: compile.h:27
a source file scanner
Definition: scan.h:24