summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/linearize.h
blob: db36343f28d152ecf8a8c62c0a1dda75a6ea8037 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* 
 * Copyright 2010-2011 University of Helsinki.
 *   
 * This file is part of libpgf.
 * 
 * Libpgf is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * Libpgf is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with libpgf. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gu/type.h>
#include <gu/dump.h>
#include <gu/enum.h>
#include <pgf/data.h>

/// Linearization of abstract syntax trees.
/// @file

/** @name Linearizers
 *
 * Linearization begins by choosing a concrete category (#PgfConcr) for some
 * grammar, and creating a new linearizer (#PgfLzr) which can then be used to
 * linearize abstract syntax trees (#PgfExpr) of that grammar into the given
 * concrete category.
 *
 * @{
 */


/// A linearizer.
typedef struct PgfLzr PgfLzr;
/**<
 *
 * A #PgfLzr object transforms abstract syntax trees of a PGF grammar
 * into sequences of token events for a single concrete category of
 * that grammar.
 * 
 */
GU_DECLARE_TYPE(PgfLzr, struct);


/// Create a new linearizer.
PgfLzr*
pgf_new_lzr(PgfConcr* cnc, GuPool* pool);
/**<
 * @param cnc The concrete category to linearize to. 
 *
 * @pool
 *
 * @return A new linearizer.
 */

/** @}
 *
 * @name Enumerating concrete syntax trees
 *
 * Because of the \c variants construct in GF, there may be several
 * possible concrete syntax trees that correspond to a given abstract
 * syntax tree. These can be enumerated with #pgf_lzr_concretize and
 * #pgf_cnc_trees_next.
 *
 * @{
 */


/// A concrete syntax tree
typedef GuVariant PgfCncTree;

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

/// Begin enumerating concrete syntax variants.
PgfCncTreeEnum*
pgf_lzr_concretize(PgfLzr* lzr, PgfExpr expr, GuPool* pool);

/** @}
 *
 * @name Linearizing concrete syntax trees
 *
 * An individual concrete syntax tree has several different
 * linearizations, corresponding to the various fields and cases of
 * corresponding GF values. The number of these linearizations, called
 * the \e dimension of the tree, can be retrieved with
 * #pgf_cnc_tree_dimension.
 *  
 * A single linearization of a concrete syntax tree is performed by
 * #pgf_lzr_linearize. The linearization is realized as a sequence of
 * events that are notified by calling the functions of a #PgfLinFuncs
 * structure that the client provides.
 *
 * @{
 */

/// Callback functions for linearization.
typedef struct PgfLinFuncs PgfLinFuncs;

struct PgfLinFuncs
{
	/// Output tokens
	void (*symbol_tokens)(PgfLinFuncs** self, PgfTokens toks);

	void (*symbol_expr)(PgfLinFuncs** self, 
			    int argno, PgfExpr expr, int lin_idx);

	/// Begin application
	void (*expr_apply)(PgfLinFuncs** self, PgfCId cid, int n_args);

	/// Output literal
	void (*expr_literal)(PgfLinFuncs** self, PgfLiteral lit);

	void (*abort)(PgfLinFuncs** self);
	void (*finish)(PgfLinFuncs** self);
};





/// Linearize a concrete syntax tree.
void
pgf_lzr_linearize(PgfLzr* lzr, PgfCncTree ctree, size_t lin_idx,
		  PgfLinFuncs** fnsp);


/// Linearize a concrete syntax tree as space-separated tokens.
void
pgf_lzr_linearize_simple(PgfLzr* lzr, PgfCncTree ctree,
			 size_t lin_idx, GuWriter* wtr, GuExn* err);


/// Return the dimension of a concrete syntax tree.
int
pgf_cnc_tree_dimension(PgfCncTree ctree);
/**<
 * @param ctree A concrete syntax tree.
 *
 * @return The dimension of the tree, i.e. the number of different
 * linearizations the tree has.
 */

//@}



extern GuTypeTable
pgf_linearize_dump_table;