summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/linearizer.h
blob: 8a056291386de5520e24cdb267e434b68ac24fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef PGF_LINEARIZER_H_
#define PGF_LINEARIZER_H_

#include <gu/enum.h>

/// Linearization of abstract syntax trees.

//
// PgfCncTree
//

/// A concrete syntax tree
typedef GuVariant PgfCncTree;

#ifdef PGF_DATA_H_

typedef enum {
	PGF_CNC_TREE_APP,
	PGF_CNC_TREE_CHUNKS,
	PGF_CNC_TREE_LIT,
} PgfCncTreeTag;

typedef struct {
	PgfCCat* ccat;
	PgfCId abs_id;
	PgfCncFun* fun;
	int fid;

	size_t n_vars;
	PgfPrintContext* context;

	size_t n_args;
	PgfCncTree args[];
} PgfCncTreeApp;

typedef struct {
	PgfMetaId id;

	size_t n_vars;
	PgfPrintContext* context;

	size_t n_args;
	PgfCncTree args[];
} PgfCncTreeChunks;

typedef struct {
	size_t n_vars;
	PgfPrintContext* context;

	int fid;
	PgfLiteral lit;
} PgfCncTreeLit;

#endif

/// An enumeration of #PgfCncTree trees.
typedef GuEnum PgfCncTreeEnum;

/// Begin enumerating concrete syntax variants.
PGF_API_DECL PgfCncTreeEnum*
pgf_lzr_concretize(PgfConcr* concr, PgfExpr expr, GuExn* err, GuPool* pool);

typedef struct {
	char nothing[0]; // Empty struct
} PgfLinNonExist;

PGF_API_DECL PgfCncTree
pgf_lzr_wrap_linref(PgfCncTree ctree, GuPool* pool);



typedef struct PgfLinFuncs PgfLinFuncs;

typedef enum {
	PGF_CAPIT_NONE,
	PGF_CAPIT_FIRST,
	PGF_CAPIT_ALL,
	PGF_CAPIT_NEXT
} PgfCapitState;

struct PgfLinFuncs
{
	/// Output tokens
	void (*symbol_token)(PgfLinFuncs** self, PgfToken tok);

	/// Begin phrase
	void (*begin_phrase)(PgfLinFuncs** self, PgfCId cat, int fid, GuString ann, PgfCId fun);

	/// End phrase
	void (*end_phrase)(PgfLinFuncs** self, PgfCId cat, int fid, GuString ann, PgfCId fun);

	/// handling nonExist
	void (*symbol_ne)(PgfLinFuncs** self);

	/// token binding
	void (*symbol_bind)(PgfLinFuncs** self);

	/// capitalization
	void (*symbol_capit)(PgfLinFuncs** self, PgfCapitState capit);
	
	/// meta variable
	void (*symbol_meta)(PgfLinFuncs** self, PgfMetaId id);
};

/// Linearize a concrete syntax tree.
PGF_API_DECL void
pgf_lzr_linearize(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, 
                  PgfLinFuncs** funcs, GuPool* tmp_pool);

/// Linearize a concrete syntax tree as space-separated tokens.
PGF_API_DECL void
pgf_lzr_linearize_simple(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, 
                         GuOut* out, GuExn* err,
                         GuPool* tmp_pool);


PGF_API_DECL void
pgf_lzr_get_table(PgfConcr* concr, PgfCncTree ctree, 
                  size_t* n_lins, GuString** labels);

#ifdef PGF_DATA_H_
// Used internally in the parser
PGF_INTERNAL_DECL GuString
pgf_get_tokens(PgfSymbols* sym, uint16_t sym_idx, GuPool* pool);
#endif

#endif