1 #ifndef _GNM_PARSE_UTIL_H_
2 # define _GNM_PARSE_UTIL_H_
3 
4 #include <gnumeric.h>
5 #include <libgnumeric.h>
6 
7 G_BEGIN_DECLS
8 
9 char const *col_name  (int col);
10 char const *cols_name (int start_col, int end_col);
11 char const *col_parse (char const *str, GnmSheetSize const *ss,
12 		       int *res, unsigned char *relative);
13 
14 char const *row_name  (int row);
15 char const *rows_name (int start_row, int end_row);
16 char const *row_parse (char const *str, GnmSheetSize const *ss,
17 		       int *res, unsigned char *relative);
18 
19 char const *cellpos_as_string	(GnmCellPos const *pos);
20 char const *cellpos_parse	(char const *cell_str, GnmSheetSize const *ss,
21 				 GnmCellPos *res, gboolean strict);
22 void        cellref_as_string   (GnmConventionsOut *out,
23 				 GnmCellRef const *cell_ref,
24 				 gboolean no_sheetname);
25 char const *cellref_parse	(GnmCellRef *out, GnmSheetSize const *ss,
26 				 char const *in, GnmCellPos const *pos);
27 
28 void        rangeref_as_string  (GnmConventionsOut *out,
29 				 GnmRangeRef const *ref);
30 char const *rangeref_parse	(GnmRangeRef *res, char const *start,
31 				 GnmParsePos const *pp,
32 				 GnmConventions const *convs);
33 				 /* GError **err); */
34 
35 char const *cell_coord_name	(int col, int row);
36 char const *cell_name		(GnmCell const *cell);
37 
38 char const *parsepos_as_string	(GnmParsePos const *pp);
39 
40 /* backwards compatibility version */
41 void gnm_1_0_rangeref_as_string (GnmConventionsOut *out,
42 				 GnmRangeRef const *ref);
43 
44 
45 struct _GnmConventionsOut {
46 	GString	*accum;
47 	GnmParsePos const *pp;
48 	GnmConventions const *convs;
49 };
50 
51 typedef enum {
52 	PERR_NONE,
53 	PERR_MISSING_PAREN_OPEN,
54 	PERR_MISSING_PAREN_CLOSE,
55 	PERR_MISSING_CLOSING_QUOTE,
56 	PERR_INVALID_EXPRESSION,
57 	PERR_INVALID_ARRAY_SEPARATOR,
58 	PERR_UNKNOWN_WORKBOOK,
59 	PERR_UNKNOWN_SHEET,
60 	PERR_UNKNOWN_NAME,
61 	PERR_UNEXPECTED_TOKEN,
62 	PERR_OUT_OF_RANGE,
63 	PERR_SHEET_IS_REQUIRED,
64 	PERR_SINGLE_RANGE,
65 	PERR_3D_NAME,
66 	PERR_MULTIPLE_EXPRESSIONS,
67 	PERR_INVALID_EMPTY,
68 	PERR_ASYMETRIC_ARRAY,
69 	PERR_SET_CONTENT_MUST_BE_RANGE
70 } ParseErrorID;
71 
72 /* In parser.y  */
73 struct _GnmParseError {
74 	GError	*err;
75 	int begin_char, end_char;
76 };
77 
78 typedef struct {
79 	gsize start, end;
80 	int token;
81 } GnmLexerItem;
82 GType       gnm_lexer_item_get_type (void); /* Boxed type */
83 
84 GType       gnm_parse_error_get_type (void); /* Boxed type */
85 GnmParseError *parse_error_init (GnmParseError *pe);
86 void        parse_error_free (GnmParseError *pe);
87 
88 typedef enum {
89 	GNM_EXPR_PARSE_DEFAULT = 0, /* default is Excel */
90 	GNM_EXPR_PARSE_FORCE_ABSOLUTE_REFERENCES	   = 1 << 0,
91 	GNM_EXPR_PARSE_FORCE_RELATIVE_REFERENCES	   = 1 << 0,
92 	GNM_EXPR_PARSE_FORCE_EXPLICIT_SHEET_REFERENCES	   = 1 << 2,
93 	GNM_EXPR_PARSE_PERMIT_MULTIPLE_EXPRESSIONS	   = 1 << 3,
94 	GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_STRINGS	   = 1 << 4,
95 	GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_INVALID	   = 1 << 5
96 } GnmExprParseFlags;
97 
98 struct _GnmConventions {
99 	int ref_count;
100 
101 #if 0
102 	/* Not yet.  */
103 	gboolean force_absolute_col_references;
104 	gboolean force_absolute_row_references;
105 	gboolean force_explicit_sheet_references;
106 #endif
107 	gboolean r1c1_addresses;
108 
109 	/* Whether function names should be translated.  */
110 	gboolean localized_function_names;
111 
112 	/* Separate elements in lists, 0 will use go_locale. */
113 	gunichar arg_sep;
114 	/* Separate array columns, 0 will use go_locale. */
115 	gunichar array_col_sep;
116 	/* Separate array rows, 0 will use go_locale.  */
117 	gunichar array_row_sep;
118 
119 	/* What character denotes range intersection?  */
120 	gunichar intersection_char;
121 	/* What character denotes range union?  */
122 	gunichar union_char;
123 
124 	/* What characters are range separators?  */
125 	gboolean range_sep_colon;  /* A1:B2 */
126 	gboolean range_sep_dotdot; /* A1..B2 */
127 
128 	/* Separates sheet name from the cell ref */
129 	gunichar sheet_name_sep;
130 
131 	/* Formerly USE_APPLIX_CONVENTIONS.  */
132 	gboolean ignore_whitespace;
133 
134 	/* Formerly more or less part of USE_APPLIX_CONVENTIONS.  */
135 	gboolean allow_absolute_sheet_references;
136 
137 	/* Formerly part of USE_OPENCALC_CONVENTIONS.  */
138 	gboolean decode_ampersands;
139 
140 	/* Is the decimal separator "." (as opposed to locale's)?  */
141 	gboolean decimal_sep_dot;
142 
143 	/* Accept prefix #NOT# and infixs #AND# and #OR#.  */
144 	gboolean accept_hash_logicals;
145 
146 	/* If TRUE, parse x^y^z as (x^y)^z.  */
147 	gboolean exp_is_left_associative;
148 
149 /* Import specific functions ------------------------------------- */
150 	struct _GnmConventionsImport {
151 		/* Called a lot for anything that might be a reference.  */
152 		char const *(*range_ref) (GnmRangeRef *res, char const *in,
153 					  GnmParsePos const *pp,
154 					  GnmConventions const *convs);
155 					/* GError **err); */
156 
157 		/* Called to unescape strings */
158 		char const *(*string) (char const *in, GString *target,
159 				       GnmConventions const *convs);
160 
161 		/* Called a lot for anything that might be a function name or
162 		 * defined name.  */
163 		char const *(*name) (char const *in,
164 				     GnmConventions const *convs);
165 		/* Returns true if a tentative expression name is legal. */
166 		gboolean (*name_validate) (const char *name);
167 
168 		/* Must return non-NULL, and absorb the args, including the list. */
169 		GnmExpr const *(*func) (GnmConventions const *convs,
170 					/* make scope more useful, eg a
171 					 * ParsePos * to allow for
172 					 * sheet/object specific functions
173 					 * */
174 					Workbook *scope,
175 					char const *name,
176 					GnmExprList *args);
177 		Workbook *(*external_wb) (GnmConventions const *convs,
178 					 Workbook *ref_wb,
179 					 char const *unquoted_name);
180 	} input;
181 
182 /* Export specific functions ----------------------------------- */
183 	struct _GnmConventionsExport {
184 		int decimal_digits;
185 
186 		gboolean translated;
187 
188 		void (*string)	  (GnmConventionsOut *out,
189 				   GOString const *str);
190 		void (*func)	  (GnmConventionsOut *out,
191 				   GnmExprFunction const *func);
192 		void (*name)	  (GnmConventionsOut *out,
193 				   GnmExprName const *name);
194 		void (*cell_ref)  (GnmConventionsOut *out,
195 				   GnmCellRef const *cell_ref,
196 				   gboolean no_sheetname);
197 		void (*range_ref) (GnmConventionsOut *out,
198 				   GnmRangeRef const *range_ref);
199 		void (*boolean)	  (GnmConventionsOut *out,
200 				   gboolean val);
201 
202 		GString * (*quote_sheet_name) (GnmConventions const *convs,
203 					       char const *name);
204 	} output;
205 };
206 GType           gnm_conventions_get_type (void);
207 GnmConventions *gnm_conventions_new	 (void);
208 GnmConventions *gnm_conventions_new_full (unsigned size);
209 
210 GnmConventions *gnm_conventions_ref	 (GnmConventions const *c);
211 void		gnm_conventions_unref	 (GnmConventions *c);
212 
213 
214 GNM_VAR_DECL GnmConventions const *gnm_conventions_default;
215 GNM_VAR_DECL GnmConventions const *gnm_conventions_xls_r1c1;
216 
217 /**********************************************/
218 
219 void parse_util_init (void);
220 void parse_util_shutdown (void);
221 
222 GnmExprTop const *gnm_expr_parse_str (char const *str, GnmParsePos const *pp,
223 				      GnmExprParseFlags flags,
224 				      GnmConventions const *convs,
225 				      GnmParseError *error);
226 
227 GnmLexerItem *gnm_expr_lex_all (char const *str, GnmParsePos const *pp,
228 				GnmExprParseFlags flags,
229 				GnmConventions const *convs);
230 
231 
232 /* Is this string potentially the start of an expression */
233 char const *gnm_expr_char_start_p (char const *c);
234 
235 void	    parse_text_value_or_expr (GnmParsePos const *pos,
236 				      char const *text,
237 				      GnmValue **val,
238 				      GnmExprTop const **texpr);
239 
240 GString	*gnm_expr_conv_quote (GnmConventions const *convs, char const *str);
241 
242 G_END_DECLS
243 
244 #endif /* _GNM_PARSE_UTIL_H_ */
245