My Project
Loading...
Searching...
No Matches
scanner.c
Go to the documentation of this file.
1#include "gwion_util.h"
2#include "gwion_ast.h"
3#include "parser.h"
4#include "lexer.h"
5
6#define PP_SIZE 127
7
8ANEW static Scanner *new_scanner(AstGetter *arg) {
9 Scanner *scan = (Scanner *)mp_calloc(arg->st->p, Scanner);
10 vector_init(&scan->hashes);
11 gwion_lex_init(&scan->scanner);
12 gwion_set_extra(scan, scan->scanner);
13 scan->pp = new_pp(arg->st->p, PP_SIZE, arg->name);
14 gwion_set_in(arg->f, scan->scanner);
15 scan->st = arg->st;
16 scan->getter = arg;
17 pos_ini(&scan->pos);
18// pos_ini(&scan->old);
19 scan->getter->ppa->ast = NULL; // ???
20 return scan;
21}
22
23ANN static void free_scanner(Scanner *scan) {
24 vector_release(&scan->hashes);
25 free_pp(scan->st->p, scan->pp, scan->scanner);
26 gwion_lex_destroy(scan->scanner);
27 mp_free(scan->st->p, Scanner, scan);
28}
29
30ANN static Ast get_ast(MemPool mp, Scanner *s) {
31 if (!gwion_parse(s)) return s->getter->ppa->ast;
32 if (s->getter->ppa->ast) free_ast(mp, s->getter->ppa->ast);
33 return NULL;
34}
35
36ANN Ast parse_pos(struct AstGetter_ *const arg, const pos_t pos) {
37 Scanner * s = new_scanner(arg);
38 s->pos = pos;
39 const Ast ast = get_ast(arg->ppa->hash.p, s);
40 free_scanner(s);
41 return ast;
42}
43
44ANN static char *get_filename(Scanner *scan, const PPState *ppstate) {
45 m_str filename = ppstate->filename;
46 if (!scan->pp->npar) {
47 m_uint i = vector_size(&scan->pp->state) - 1;
48 if (*filename == '@') {
49 while (*filename == '@') {
50 --i;
51 const PPState *pps = (PPState*)vector_at(&scan->pp->state, i);
52 filename = pps->filename;
53 }
54 }
55 } else {
56 const PPState *pps = (PPState*)vector_front(&scan->pp->state);
57 filename = pps->filename;
58 }
59 return filename;
60}
61
62ANN static void secondary(Scanner *scan) {
63 for (m_uint i = 0; i < vector_size(&scan->pp->state) - 1; i++) {
64 const PPState *pp = (PPState*)vector_at(&scan->pp->state, i);
65 gwlog_related("expanded from here", get_filename(scan, pp), pp->loc);
66 }
67}
68
69ANN2(1, 2)
70int scanner_error(Scanner *scan, const char *main, const char *explain,
71 const loc_t loc, const uint error_code) {
72 const PPState *ppstate = (PPState*)vector_back(&scan->pp->state);
73 const m_str filename = get_filename(scan, ppstate);
74 gwlog_error(main, explain, filename, loc, error_code);
75 secondary(scan);
76 return 0;
77}
78
79ANN int scanner_secondary(Scanner *scan, const char *main, const loc_t loc) {
80 const PPState *ppstate = (PPState*)vector_back(&scan->pp->state);
81 const m_str filename = get_filename(scan, ppstate);
82 gwlog_warning(main, filename, loc);
83 return 0;
84}
ANN void free_ast(MemPool p, Ast)
struct SectionList * Ast
Definition absyn.h:23
include this file to use gwion-ast library
ANN void gwlog_related(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:155
void gwlog_error(const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code)
Definition gwlog.c:115
ANN void pos_ini(pos_t *pos)
Definition gwlog.c:11
ANN void gwlog_warning(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:146
return NULL
Definition macro.c:41
int main(int argc, char **argv)
Definition main.c:4
ANEW PP * new_pp(MemPool p, const uint size, const char *name)
Definition pp.c:11
ANN void free_pp(MemPool p, PP *pp, void *data)
Definition pp.c:38
ANN2(1)
Definition pparg.c:46
#define PP_SIZE
Definition scanner.c:6
static ANEW Scanner * new_scanner(AstGetter *arg)
Definition scanner.c:8
static ANN char * get_filename(Scanner *scan, const PPState *ppstate)
Definition scanner.c:44
static ANN void free_scanner(Scanner *scan)
Definition scanner.c:23
static ANN Ast get_ast(MemPool mp, Scanner *s)
Definition scanner.c:30
ANN Ast parse_pos(struct AstGetter_ *const arg, const pos_t pos)
Definition scanner.c:36
ANN int scanner_secondary(Scanner *scan, const char *main, const loc_t loc)
Definition scanner.c:79
static ANN void secondary(Scanner *scan)
Definition scanner.c:62
int scanner_error(Scanner *scan, const char *main, const char *explain, const loc_t loc, const uint error_code)
Definition scanner.c:70
FILE * f
Definition ast_getter.h:23
const char * name
Definition ast_getter.h:22
SymTable * st
Definition ast_getter.h:24
PPArg * ppa
Definition ast_getter.h:25
Ast ast
Definition pparg.h:10
struct Hash_ hash
Definition pparg.h:9
Definition pp.h:14
char * filename
Definition pp.h:21
loc_t loc
Definition pp.h:22
int npar
Definition pp.h:32
struct Vector_ state
Definition pp.h:27
struct PP_ * pp
Definition scanner.h:10
struct Vector_ hashes
Definition scanner.h:12
void * scanner
Definition scanner.h:8
SymTable * st
Definition scanner.h:9
struct pos_t pos
Definition scanner.h:13
AstGetter * getter
Definition scanner.h:11
Definition gwlog.h:15
Definition gwlog.h:7