-
Notifications
You must be signed in to change notification settings - Fork 464
/
sass_context.cpp
743 lines (673 loc) · 27.9 KB
/
sass_context.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include "sass.hpp"
#include <cstring>
#include <stdexcept>
#include <sstream>
#include <string>
#include <vector>
#include "sass.h"
#include "file.hpp"
#include "json.hpp"
#include "util.hpp"
#include "context.hpp"
#include "sass_context.hpp"
#include "sass_functions.hpp"
#include "ast_fwd_decl.hpp"
#include "error_handling.hpp"
#define LFEED "\n"
// C++ helper
namespace Sass {
// see sass_copy_c_string(std::string str)
static inline JsonNode* json_mkstream(const std::stringstream& stream)
{
// hold on to string on stack!
std::string str(stream.str());
return json_mkstring(str.c_str());
}
}
extern "C" {
using namespace Sass;
static void sass_clear_options (struct Sass_Options* options);
static void sass_reset_options (struct Sass_Options* options);
static void copy_options(struct Sass_Options* to, struct Sass_Options* from) {
// free assigned memory
sass_clear_options(to);
// move memory
*to = *from;
// Reset pointers on source
sass_reset_options(from);
}
#define IMPLEMENT_SASS_OPTION_ACCESSOR(type, option) \
type ADDCALL sass_option_get_##option (struct Sass_Options* options) { return options->option; } \
void ADDCALL sass_option_set_##option (struct Sass_Options* options, type option) { options->option = option; }
#define IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(type, option, def) \
type ADDCALL sass_option_get_##option (struct Sass_Options* options) { return safe_str(options->option, def); } \
void ADDCALL sass_option_set_##option (struct Sass_Options* options, type option) \
{ free(options->option); options->option = option || def ? sass_copy_c_string(option ? option : def) : 0; }
#define IMPLEMENT_SASS_CONTEXT_GETTER(type, option) \
type ADDCALL sass_context_get_##option (struct Sass_Context* ctx) { return ctx->option; }
#define IMPLEMENT_SASS_CONTEXT_TAKER(type, option) \
type sass_context_take_##option (struct Sass_Context* ctx) \
{ type foo = ctx->option; ctx->option = 0; return foo; }
static int handle_error(Sass_Context* c_ctx) {
try {
throw;
}
catch (Exception::Base& e) {
std::stringstream msg_stream;
std::string cwd(Sass::File::get_cwd());
std::string msg_prefix(e.errtype());
bool got_newline = false;
msg_stream << msg_prefix << ": ";
const char* msg = e.what();
while(msg && *msg) {
if (*msg == '\r') {
got_newline = true;
} else if (*msg == '\n') {
got_newline = true;
} else if (got_newline) {
msg_stream << std::string(msg_prefix.size() + 2, ' ');
got_newline = false;
}
msg_stream << *msg;
++ msg;
}
if (!got_newline) msg_stream << "\n";
if (e.import_stack) {
for (size_t i = 1; i < e.import_stack->size() - 1; ++i) {
std::string path((*e.import_stack)[i]->imp_path);
std::string rel_path(Sass::File::abs2rel(path, cwd, cwd));
msg_stream << std::string(msg_prefix.size() + 2, ' ');
msg_stream << (i == 1 ? " on line " : " from line ");
msg_stream << e.pstate.line+1 << " of " << rel_path << "\n";
}
} else {
std::string rel_path(Sass::File::abs2rel(e.pstate.path, cwd, cwd));
msg_stream << std::string(msg_prefix.size() + 2, ' ');
msg_stream << " on line " << e.pstate.line+1 << " of " << rel_path << "\n";
}
// now create the code trace (ToDo: maybe have util functions?)
if (e.pstate.line != std::string::npos && e.pstate.column != std::string::npos) {
size_t line = e.pstate.line;
const char* line_beg = e.pstate.src;
while (line_beg && *line_beg && line) {
if (*line_beg == '\n') -- line;
++ line_beg;
}
const char* line_end = line_beg;
while (line_end && *line_end && *line_end != '\n') {
if (*line_end == '\n') break;
if (*line_end == '\r') break;
line_end ++;
}
size_t max_left = 42; size_t max_right = 78;
size_t move_in = e.pstate.column > max_left ? e.pstate.column - max_left : 0;
size_t shorten = (line_end - line_beg) - move_in > max_right ?
(line_end - line_beg) - move_in - max_right : 0;
msg_stream << ">> " << std::string(line_beg + move_in, line_end - shorten) << "\n";
msg_stream << " " << std::string(e.pstate.column - move_in, '-') << "^\n";
}
JsonNode* json_err = json_mkobject();
json_append_member(json_err, "status", json_mknumber(1));
json_append_member(json_err, "file", json_mkstring(e.pstate.path));
json_append_member(json_err, "line", json_mknumber((double)(e.pstate.line+1)));
json_append_member(json_err, "column", json_mknumber((double)(e.pstate.column+1)));
json_append_member(json_err, "message", json_mkstring(e.what()));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(e.what());
c_ctx->error_status = 1;
c_ctx->error_file = sass_copy_c_string(e.pstate.path);
c_ctx->error_line = e.pstate.line+1;
c_ctx->error_column = e.pstate.column+1;
c_ctx->error_src = e.pstate.src;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
catch (std::bad_alloc& ba) {
std::stringstream msg_stream;
JsonNode* json_err = json_mkobject();
msg_stream << "Unable to allocate memory: " << ba.what() << std::endl;
json_append_member(json_err, "status", json_mknumber(2));
json_append_member(json_err, "message", json_mkstring(ba.what()));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(ba.what());
c_ctx->error_status = 2;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
catch (std::exception& e) {
std::stringstream msg_stream;
JsonNode* json_err = json_mkobject();
msg_stream << "Internal Error: " << e.what() << std::endl;
json_append_member(json_err, "status", json_mknumber(3));
json_append_member(json_err, "message", json_mkstring(e.what()));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(e.what());
c_ctx->error_status = 3;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
catch (std::string& e) {
std::stringstream msg_stream;
JsonNode* json_err = json_mkobject();
msg_stream << "Internal Error: " << e << std::endl;
json_append_member(json_err, "status", json_mknumber(4));
json_append_member(json_err, "message", json_mkstring(e.c_str()));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(e.c_str());
c_ctx->error_status = 4;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
catch (const char* e) {
std::stringstream msg_stream;
JsonNode* json_err = json_mkobject();
msg_stream << "Internal Error: " << e << std::endl;
json_append_member(json_err, "status", json_mknumber(4));
json_append_member(json_err, "message", json_mkstring(e));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(e);
c_ctx->error_status = 4;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
catch (...) {
std::stringstream msg_stream;
JsonNode* json_err = json_mkobject();
msg_stream << "Unknown error occurred" << std::endl;
json_append_member(json_err, "status", json_mknumber(5));
json_append_member(json_err, "message", json_mkstring("unknown"));
try { c_ctx->error_json = json_stringify(json_err, " "); } catch(...) {}
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string("unknown");
c_ctx->error_status = 5;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
}
return c_ctx->error_status;
}
// allow one error handler to throw another error
// this can happen with invalid utf8 and json lib
static int handle_errors(Sass_Context* c_ctx) {
try { return handle_error(c_ctx); }
catch (...) { return handle_error(c_ctx); }
return c_ctx->error_status;
}
// generic compilation function (not exported, use file/data compile instead)
static Sass_Compiler* sass_prepare_context (Sass_Context* c_ctx, Context* cpp_ctx) throw()
{
try {
// register our custom functions
if (c_ctx->c_functions) {
auto this_func_data = c_ctx->c_functions;
while (this_func_data && *this_func_data) {
cpp_ctx->add_c_function(*this_func_data);
++this_func_data;
}
}
// register our custom headers
if (c_ctx->c_headers) {
auto this_head_data = c_ctx->c_headers;
while (this_head_data && *this_head_data) {
cpp_ctx->add_c_header(*this_head_data);
++this_head_data;
}
}
// register our custom importers
if (c_ctx->c_importers) {
auto this_imp_data = c_ctx->c_importers;
while (this_imp_data && *this_imp_data) {
cpp_ctx->add_c_importer(*this_imp_data);
++this_imp_data;
}
}
// reset error status
c_ctx->error_json = 0;
c_ctx->error_text = 0;
c_ctx->error_message = 0;
c_ctx->error_status = 0;
// reset error position
c_ctx->error_src = 0;
c_ctx->error_file = 0;
c_ctx->error_line = std::string::npos;
c_ctx->error_column = std::string::npos;
// allocate a new compiler instance
Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
compiler->state = SASS_COMPILER_CREATED;
// store in sass compiler
compiler->c_ctx = c_ctx;
compiler->cpp_ctx = cpp_ctx;
cpp_ctx->c_compiler = compiler;
// use to parse block
return compiler;
}
// pass errors to generic error handler
catch (...) { handle_errors(c_ctx); }
// error
return 0;
}
static Block* sass_parse_block (Sass_Compiler* compiler) throw()
{
// assert valid pointer
if (compiler == 0) return 0;
// The cpp context must be set by now
Context* cpp_ctx = compiler->cpp_ctx;
Sass_Context* c_ctx = compiler->c_ctx;
// We will take care to wire up the rest
compiler->cpp_ctx->c_compiler = compiler;
compiler->state = SASS_COMPILER_PARSED;
try {
// get input/output path from options
std::string input_path = safe_str(c_ctx->input_path);
std::string output_path = safe_str(c_ctx->output_path);
// parsed root block
Block* root = 0;
// maybe skip some entries of included files
// we do not include stdin for data contexts
bool skip = c_ctx->type == SASS_CONTEXT_DATA;
// dispatch parse call
root = cpp_ctx->parse();
// abort on errors
if (!root) return 0;
// skip all prefixed files? (ToDo: check srcmap)
// IMO source-maps should point to headers already
// therefore don't skip it for now. re-enable or
// remove completely once this is tested
size_t headers = cpp_ctx->head_imports;
// copy the included files on to the context (dont forget to free later)
if (copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files) == NULL)
throw(std::bad_alloc());
// return parsed block
return root;
}
// pass errors to generic error handler
catch (...) { handle_errors(c_ctx); }
// error
return 0;
}
// generic compilation function (not exported, use file/data compile instead)
static int sass_compile_context (Sass_Context* c_ctx, Context* cpp_ctx)
{
// prepare sass compiler with context and options
Sass_Compiler* compiler = sass_prepare_context(c_ctx, cpp_ctx);
try {
// call each compiler step
sass_compiler_parse(compiler);
sass_compiler_execute(compiler);
}
// pass errors to generic error handler
catch (...) { handle_errors(c_ctx); }
sass_delete_compiler(compiler);
return c_ctx->error_status;
}
inline void init_options (struct Sass_Options* options)
{
options->precision = 5;
options->indent = " ";
options->linefeed = LFEED;
}
Sass_Options* ADDCALL sass_make_options (void)
{
struct Sass_Options* options = (struct Sass_Options*) calloc(1, sizeof(struct Sass_Options));
if (options == 0) { std::cerr << "Error allocating memory for options" << std::endl; return 0; }
init_options(options);
return options;
}
Sass_File_Context* ADDCALL sass_make_file_context(const char* input_path)
{
struct Sass_File_Context* ctx = (struct Sass_File_Context*) calloc(1, sizeof(struct Sass_File_Context));
if (ctx == 0) { std::cerr << "Error allocating memory for file context" << std::endl; return 0; }
ctx->type = SASS_CONTEXT_FILE;
init_options(ctx);
try {
if (input_path == 0) { throw(std::runtime_error("File context created without an input path")); }
if (*input_path == 0) { throw(std::runtime_error("File context created with empty input path")); }
sass_option_set_input_path(ctx, input_path);
} catch (...) {
handle_errors(ctx);
}
return ctx;
}
Sass_Data_Context* ADDCALL sass_make_data_context(char* source_string)
{
struct Sass_Data_Context* ctx = (struct Sass_Data_Context*) calloc(1, sizeof(struct Sass_Data_Context));
if (ctx == 0) { std::cerr << "Error allocating memory for data context" << std::endl; return 0; }
ctx->type = SASS_CONTEXT_DATA;
init_options(ctx);
try {
if (source_string == 0) { throw(std::runtime_error("Data context created without a source string")); }
if (*source_string == 0) { throw(std::runtime_error("Data context created with empty source string")); }
ctx->source_string = source_string;
} catch (...) {
handle_errors(ctx);
}
return ctx;
}
struct Sass_Compiler* ADDCALL sass_make_data_compiler (struct Sass_Data_Context* data_ctx)
{
if (data_ctx == 0) return 0;
Context* cpp_ctx = new Data_Context(*data_ctx);
return sass_prepare_context(data_ctx, cpp_ctx);
}
struct Sass_Compiler* ADDCALL sass_make_file_compiler (struct Sass_File_Context* file_ctx)
{
if (file_ctx == 0) return 0;
Context* cpp_ctx = new File_Context(*file_ctx);
return sass_prepare_context(file_ctx, cpp_ctx);
}
int ADDCALL sass_compile_data_context(Sass_Data_Context* data_ctx)
{
if (data_ctx == 0) return 1;
if (data_ctx->error_status)
return data_ctx->error_status;
try {
if (data_ctx->source_string == 0) { throw(std::runtime_error("Data context has no source string")); }
// empty source string is a valid case, even if not really usefull (different than with file context)
// if (*data_ctx->source_string == 0) { throw(std::runtime_error("Data context has empty source string")); }
}
catch (...) { return handle_errors(data_ctx) | 1; }
Context* cpp_ctx = new Data_Context(*data_ctx);
return sass_compile_context(data_ctx, cpp_ctx);
}
int ADDCALL sass_compile_file_context(Sass_File_Context* file_ctx)
{
if (file_ctx == 0) return 1;
if (file_ctx->error_status)
return file_ctx->error_status;
try {
if (file_ctx->input_path == 0) { throw(std::runtime_error("File context has no input path")); }
if (*file_ctx->input_path == 0) { throw(std::runtime_error("File context has empty input path")); }
}
catch (...) { return handle_errors(file_ctx) | 1; }
Context* cpp_ctx = new File_Context(*file_ctx);
return sass_compile_context(file_ctx, cpp_ctx);
}
int ADDCALL sass_compiler_parse(struct Sass_Compiler* compiler)
{
if (compiler == 0) return 1;
if (compiler->state == SASS_COMPILER_PARSED) return 0;
if (compiler->state != SASS_COMPILER_CREATED) return -1;
if (compiler->c_ctx == NULL) return 1;
if (compiler->cpp_ctx == NULL) return 1;
if (compiler->c_ctx->error_status)
return compiler->c_ctx->error_status;
// parse the context we have set up (file or data)
compiler->root = sass_parse_block(compiler);
// success
return 0;
}
int ADDCALL sass_compiler_execute(struct Sass_Compiler* compiler)
{
if (compiler == 0) return 1;
if (compiler->state == SASS_COMPILER_EXECUTED) return 0;
if (compiler->state != SASS_COMPILER_PARSED) return -1;
if (compiler->c_ctx == NULL) return 1;
if (compiler->cpp_ctx == NULL) return 1;
if (compiler->root == NULL) return 1;
if (compiler->c_ctx->error_status)
return compiler->c_ctx->error_status;
compiler->state = SASS_COMPILER_EXECUTED;
Context* cpp_ctx = compiler->cpp_ctx;
Block* root = compiler->root;
// compile the parsed root block
try { compiler->c_ctx->output_string = cpp_ctx->render(root); }
// pass catched errors to generic error handler
catch (...) { return handle_errors(compiler->c_ctx) | 1; }
// generate source map json and store on context
compiler->c_ctx->source_map_string = cpp_ctx->render_srcmap();
// success
return 0;
}
// helper function, not exported, only accessible locally
static void sass_reset_options (struct Sass_Options* options)
{
// free pointer before
// or copy/move them
options->input_path = 0;
options->output_path = 0;
options->plugin_path = 0;
options->include_path = 0;
options->source_map_file = 0;
options->source_map_root = 0;
options->c_functions = 0;
options->c_importers = 0;
options->c_headers = 0;
options->plugin_paths = 0;
options->include_paths = 0;
}
// helper function, not exported, only accessible locally
static void sass_clear_options (struct Sass_Options* options)
{
if (options == 0) return;
// Deallocate custom functions
if (options->c_functions) {
Sass_Function_List this_func_data = options->c_functions;
while (this_func_data && *this_func_data) {
free(*this_func_data);
++this_func_data;
}
}
// Deallocate custom headers
if (options->c_headers) {
Sass_Importer_List this_head_data = options->c_headers;
while (this_head_data && *this_head_data) {
free(*this_head_data);
++this_head_data;
}
}
// Deallocate custom importers
if (options->c_importers) {
Sass_Importer_List this_imp_data = options->c_importers;
while (this_imp_data && *this_imp_data) {
free(*this_imp_data);
++this_imp_data;
}
}
// Deallocate inc paths
if (options->plugin_paths) {
struct string_list* cur;
struct string_list* next;
cur = options->plugin_paths;
while (cur) {
next = cur->next;
free(cur->string);
free(cur);
cur = next;
}
}
// Deallocate inc paths
if (options->include_paths) {
struct string_list* cur;
struct string_list* next;
cur = options->include_paths;
while (cur) {
next = cur->next;
free(cur->string);
free(cur);
cur = next;
}
}
// Free options strings
free(options->input_path);
free(options->output_path);
free(options->plugin_path);
free(options->include_path);
free(options->source_map_file);
free(options->source_map_root);
// Free custom functions
free(options->c_functions);
// Free custom importers
free(options->c_importers);
free(options->c_headers);
// Reset our pointers
options->input_path = 0;
options->output_path = 0;
options->plugin_path = 0;
options->include_path = 0;
options->source_map_file = 0;
options->source_map_root = 0;
options->c_functions = 0;
options->c_importers = 0;
options->c_headers = 0;
options->plugin_paths = 0;
options->include_paths = 0;
}
// helper function, not exported, only accessible locally
// sass_free_context is also defined in old sass_interface
static void sass_clear_context (struct Sass_Context* ctx)
{
if (ctx == 0) return;
// release the allocated memory (mostly via sass_copy_c_string)
if (ctx->output_string) free(ctx->output_string);
if (ctx->source_map_string) free(ctx->source_map_string);
if (ctx->error_message) free(ctx->error_message);
if (ctx->error_text) free(ctx->error_text);
if (ctx->error_json) free(ctx->error_json);
if (ctx->error_file) free(ctx->error_file);
free_string_array(ctx->included_files);
// play safe and reset properties
ctx->output_string = 0;
ctx->source_map_string = 0;
ctx->error_message = 0;
ctx->error_text = 0;
ctx->error_json = 0;
ctx->error_file = 0;
ctx->included_files = 0;
// now clear the options
sass_clear_options(ctx);
}
void ADDCALL sass_delete_compiler (struct Sass_Compiler* compiler)
{
if (compiler == 0) {
return;
}
Context* cpp_ctx = compiler->cpp_ctx;
if (cpp_ctx) delete(cpp_ctx);
compiler->cpp_ctx = 0;
free(compiler);
}
void ADDCALL sass_delete_options (struct Sass_Options* options)
{
sass_clear_options(options); free(options);
}
// Deallocate all associated memory with file context
void ADDCALL sass_delete_file_context (struct Sass_File_Context* ctx)
{
// clear the context and free it
sass_clear_context(ctx); free(ctx);
}
// Deallocate all associated memory with data context
void ADDCALL sass_delete_data_context (struct Sass_Data_Context* ctx)
{
// clean the source string if it was not passed
// we reset this member once we start parsing
if (ctx->source_string) free(ctx->source_string);
// clear the context and free it
sass_clear_context(ctx); free(ctx);
}
// Getters for sass context from specific implementations
struct Sass_Context* ADDCALL sass_file_context_get_context(struct Sass_File_Context* ctx) { return ctx; }
struct Sass_Context* ADDCALL sass_data_context_get_context(struct Sass_Data_Context* ctx) { return ctx; }
// Getters for context options from Sass_Context
struct Sass_Options* ADDCALL sass_context_get_options(struct Sass_Context* ctx) { return ctx; }
struct Sass_Options* ADDCALL sass_file_context_get_options(struct Sass_File_Context* ctx) { return ctx; }
struct Sass_Options* ADDCALL sass_data_context_get_options(struct Sass_Data_Context* ctx) { return ctx; }
void ADDCALL sass_file_context_set_options (struct Sass_File_Context* ctx, struct Sass_Options* opt) { copy_options(ctx, opt); }
void ADDCALL sass_data_context_set_options (struct Sass_Data_Context* ctx, struct Sass_Options* opt) { copy_options(ctx, opt); }
// Getters for Sass_Compiler options (get conected sass context)
enum Sass_Compiler_State ADDCALL sass_compiler_get_state(struct Sass_Compiler* compiler) { return compiler->state; }
struct Sass_Context* ADDCALL sass_compiler_get_context(struct Sass_Compiler* compiler) { return compiler->c_ctx; }
struct Sass_Options* ADDCALL sass_compiler_get_options(struct Sass_Compiler* compiler) { return compiler->c_ctx; }
// Getters for Sass_Compiler options (query import stack)
size_t ADDCALL sass_compiler_get_import_stack_size(struct Sass_Compiler* compiler) { return compiler->cpp_ctx->import_stack.size(); }
Sass_Import_Entry ADDCALL sass_compiler_get_last_import(struct Sass_Compiler* compiler) { return compiler->cpp_ctx->import_stack.back(); }
Sass_Import_Entry ADDCALL sass_compiler_get_import_entry(struct Sass_Compiler* compiler, size_t idx) { return compiler->cpp_ctx->import_stack[idx]; }
// Calculate the size of the stored null terminated array
size_t ADDCALL sass_context_get_included_files_size (struct Sass_Context* ctx)
{ size_t l = 0; auto i = ctx->included_files; while (i && *i) { ++i; ++l; } return l; }
// Create getter and setters for options
IMPLEMENT_SASS_OPTION_ACCESSOR(int, precision);
IMPLEMENT_SASS_OPTION_ACCESSOR(enum Sass_Output_Style, output_style);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, source_comments);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, source_map_embed);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, source_map_contents);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, source_map_file_urls);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, omit_source_map_url);
IMPLEMENT_SASS_OPTION_ACCESSOR(bool, is_indented_syntax_src);
IMPLEMENT_SASS_OPTION_ACCESSOR(Sass_Function_List, c_functions);
IMPLEMENT_SASS_OPTION_ACCESSOR(Sass_Importer_List, c_importers);
IMPLEMENT_SASS_OPTION_ACCESSOR(Sass_Importer_List, c_headers);
IMPLEMENT_SASS_OPTION_ACCESSOR(const char*, indent);
IMPLEMENT_SASS_OPTION_ACCESSOR(const char*, linefeed);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, input_path, 0);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, output_path, 0);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, plugin_path, 0);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, include_path, 0);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, source_map_file, 0);
IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(const char*, source_map_root, 0);
// Create getter and setters for context
IMPLEMENT_SASS_CONTEXT_GETTER(int, error_status);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_json);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_message);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_text);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_file);
IMPLEMENT_SASS_CONTEXT_GETTER(size_t, error_line);
IMPLEMENT_SASS_CONTEXT_GETTER(size_t, error_column);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_src);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, output_string);
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, source_map_string);
IMPLEMENT_SASS_CONTEXT_GETTER(char**, included_files);
// Take ownership of memory (value on context is set to 0)
IMPLEMENT_SASS_CONTEXT_TAKER(char*, error_json);
IMPLEMENT_SASS_CONTEXT_TAKER(char*, error_message);
IMPLEMENT_SASS_CONTEXT_TAKER(char*, error_text);
IMPLEMENT_SASS_CONTEXT_TAKER(char*, error_file);
IMPLEMENT_SASS_CONTEXT_TAKER(char*, output_string);
IMPLEMENT_SASS_CONTEXT_TAKER(char*, source_map_string);
IMPLEMENT_SASS_CONTEXT_TAKER(char**, included_files);
// Push function for include paths (no manipulation support for now)
void ADDCALL sass_option_push_include_path(struct Sass_Options* options, const char* path)
{
struct string_list* include_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (include_path == 0) return;
include_path->string = path ? sass_copy_c_string(path) : 0;
struct string_list* last = options->include_paths;
if (!options->include_paths) {
options->include_paths = include_path;
} else {
while (last->next)
last = last->next;
last->next = include_path;
}
}
// Push function for plugin paths (no manipulation support for now)
void ADDCALL sass_option_push_plugin_path(struct Sass_Options* options, const char* path)
{
struct string_list* plugin_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (plugin_path == 0) return;
plugin_path->string = path ? sass_copy_c_string(path) : 0;
struct string_list* last = options->plugin_paths;
if (!options->plugin_paths) {
options->plugin_paths = plugin_path;
} else {
while (last->next)
last = last->next;
last->next = plugin_path;
}
}
}