@@ -342,7 +342,7 @@ extern "C" {
342
342
}
343
343
344
344
// generic compilation function (not exported, use file/data compile instead)
345
- static Context * sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) throw()
345
+ static Sass_Compiler * sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) throw()
346
346
{
347
347
try {
348
348
@@ -452,8 +452,17 @@ extern "C" {
452
452
c_ctx->error_line = string::npos;
453
453
c_ctx->error_column = string::npos;
454
454
455
+ // allocate a new compiler instance
456
+ Sass_Compiler* compiler = (struct Sass_Compiler *) calloc (1 , sizeof (struct Sass_Compiler ));
457
+ compiler->state = SASS_COMPILER_CREATED;
458
+
459
+ // store in sass compiler
460
+ compiler->c_ctx = c_ctx;
461
+ compiler->cpp_ctx = cpp_ctx;
462
+ cpp_ctx->c_compiler = compiler;
463
+
455
464
// use to parse block
456
- return cpp_ctx ;
465
+ return compiler ;
457
466
458
467
}
459
468
// pass errors to generic error handler
@@ -464,8 +473,18 @@ extern "C" {
464
473
465
474
}
466
475
467
- static Block* sass_parse_block (Sass_Context* c_ctx, Context* cpp_ctx ) throw()
476
+ static Block* sass_parse_block (Sass_Compiler* compiler ) throw()
468
477
{
478
+
479
+ // assert valid pointer
480
+ if (compiler == 0 ) return 0 ;
481
+ // The cpp context must be set by now
482
+ Context* cpp_ctx = compiler->cpp_ctx ;
483
+ Sass_Context* c_ctx = compiler->c_ctx ;
484
+ // We will take care to wire up the rest
485
+ compiler->cpp_ctx ->c_compiler = compiler;
486
+ compiler->state = SASS_COMPILER_PARSED;
487
+
469
488
try {
470
489
471
490
// get input/output path from options
@@ -509,29 +528,18 @@ extern "C" {
509
528
static int sass_compile_context (Sass_Context* c_ctx, Context::Data cpp_opt)
510
529
{
511
530
512
- // first prepare the c++ context
513
- Context* cpp_ctx = sass_prepare_context (c_ctx, cpp_opt);
514
-
515
- // parse given context and return root block
516
- Block* root = cpp_ctx ? sass_parse_block (c_ctx, cpp_ctx) : 0 ;
517
-
518
- if (cpp_ctx && root) {
519
-
520
- try {
521
-
522
- // now compile the parsed root block
523
- c_ctx->output_string = cpp_ctx->compile_block (root);
524
-
525
- // generate source map json and store on context
526
- c_ctx->source_map_string = cpp_ctx->generate_source_map ();
527
-
528
- }
529
- // pass errors to generic error handler
530
- catch (...) { handle_errors (c_ctx); }
531
+ // prepare sass compiler with context and options
532
+ Sass_Compiler* compiler = sass_prepare_context (c_ctx, cpp_opt);
531
533
534
+ try {
535
+ // call each compiler step
536
+ sass_compiler_parse (compiler);
537
+ sass_compiler_execute (compiler);
532
538
}
539
+ // pass errors to generic error handler
540
+ catch (...) { handle_errors (c_ctx); }
533
541
534
- delete cpp_ctx ;
542
+ sass_delete_compiler (compiler) ;
535
543
536
544
return c_ctx->error_status ;
537
545
}
@@ -586,29 +594,17 @@ extern "C" {
586
594
struct Sass_Compiler * ADDCALL sass_make_file_compiler (struct Sass_File_Context * c_ctx)
587
595
{
588
596
if (c_ctx == 0 ) return 0 ;
589
- struct Sass_Compiler * compiler = (struct Sass_Compiler *) calloc (1 , sizeof (struct Sass_Compiler ));
590
- if (compiler == 0 ) { cerr << " Error allocating memory for file compiler" << endl; return 0 ; }
591
- compiler->state = SASS_COMPILER_CREATED;
592
- compiler->c_ctx = c_ctx;
593
597
Context::Data cpp_opt = Context::Data ();
594
598
cpp_opt.entry_point (c_ctx->input_path );
595
- compiler->cpp_ctx = sass_prepare_context (c_ctx, cpp_opt);
596
- compiler->cpp_ctx ->c_compiler = compiler;
597
- return compiler;
599
+ return sass_prepare_context (c_ctx, cpp_opt);
598
600
}
599
601
600
602
struct Sass_Compiler * ADDCALL sass_make_data_compiler (struct Sass_Data_Context * c_ctx)
601
603
{
602
604
if (c_ctx == 0 ) return 0 ;
603
- struct Sass_Compiler * compiler = (struct Sass_Compiler *) calloc (1 , sizeof (struct Sass_Compiler ));
604
- if (compiler == 0 ) { cerr << " Error allocating memory for data compiler" << endl; return 0 ; }
605
- compiler->state = SASS_COMPILER_CREATED;
606
- compiler->c_ctx = c_ctx;
607
605
Context::Data cpp_opt = Context::Data ();
608
606
cpp_opt.source_c_str (c_ctx->source_string );
609
- compiler->cpp_ctx = sass_prepare_context (c_ctx, cpp_opt);
610
- compiler->cpp_ctx ->c_compiler = compiler;
611
- return compiler;
607
+ return sass_prepare_context (c_ctx, cpp_opt);
612
608
}
613
609
614
610
int ADDCALL sass_compile_data_context (Sass_Data_Context* data_ctx)
@@ -652,10 +648,8 @@ extern "C" {
652
648
if (compiler->cpp_ctx == NULL ) return 1 ;
653
649
if (compiler->c_ctx ->error_status )
654
650
return compiler->c_ctx ->error_status ;
655
- compiler->state = SASS_COMPILER_PARSED;
656
- Context* cpp_ctx = (Context*) compiler->cpp_ctx ;
657
651
// parse the context we have set up (file or data)
658
- compiler->root = sass_parse_block (compiler-> c_ctx , cpp_ctx );
652
+ compiler->root = sass_parse_block (compiler);
659
653
// success
660
654
return 0 ;
661
655
}
0 commit comments