Skip to content

Commit 2f499e2

Browse files
committed
Some more improvements
1 parent d058fe0 commit 2f499e2

33 files changed

+252
-206
lines changed

docs/api-basics.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,35 @@ compilers should catch the case when arguments mismatch on the C-API side.
3838

3939
### List of anonymous structures
4040

41-
These structs have no real implementation are only passed around as pointers.
42-
Internally in LibSass those pointers represent mostly different c++ objects.
41+
These structs have no real implementation and are only passed around as pointers.
42+
Internally in LibSass these pointers represent mostly different c++ objects.
4343

44-
- struct SassError - An error object to get further information
45-
- struct SassTrace - An single stack-trace object to get further information
44+
- struct SassError - An error object holding further information
45+
- struct SassTrace - An single stack-trace object holding further information
4646
- struct SassSource - Imported source with associated import and resolved path.
4747
- struct SassSrcSpan - Parser state for column, line and source information.
4848
- struct SassCompiler - Main object to hold state for whole compilation phase.
4949
- struct SassFunction - Custom function object holding callback and cookie.
5050
- struct SassImport - Single import for entry point or returned by custom importer.
5151
- struct SassImporter - Custom importer function to be hooked into our loading.
52-
- struct SassImportList - Custom importers can return a list of imports.
52+
- struct SassImportList - Custom importers can return a SassImport list.
5353
- struct SassMapIterator - Object to support iteration API over map objects.
5454

5555
## Why using c++11 (or gcc4.4 compatibility)
5656

5757
Since LibSass 3.0 we started to use more and more c++11 features. Some just
58-
creeped in, others were utilized deliberately. With LibSass 4.0 I took the
58+
crept in, others were utilized deliberately. With LibSass 4.0 I took the
5959
decision to fully utilize whatever c++11 could offer. The main reason to
6060
fully embrace c++11 is the move semantics it brings. Earlier we also tried
6161
to support compilers that only had partial c++11 support (e.g. gnu++0x).
6262
With LibSass 4.0 we don't really support this target anymore, as any compiler
6363
not older than 5 years should support the c++11 syntax we use.
6464

6565
Note: currently the LibSass 4.0 release is on going and the final
66-
target compiler is gcc 4.8, as it should fully support c++11.
66+
target compiler is gcc 4.8, as it should fully support c++11.
67+
More testing and tuning needs to be done after a 4.0 release!
6768

68-
## Binary distributions in linkage issues
69+
## Binary distributions and linkage issues
6970

7071
LibSass itself does not have any official binary distribution. The main reason for
7172
this is because it is nearly impossible to do so reliably for each and every

docs/api-compiler-example.md

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
```C:data.c
44
#include <stdio.h>
5-
#include <sass/context.h>
5+
#include <sass.h>
66

7-
int main( int argc, const char* argv[] )
7+
int main(int argc, const char* argv[])
88
{
99

1010
// LibSass will take control of data you pass in
@@ -15,22 +15,47 @@ int main( int argc, const char* argv[] )
1515
// then fill it with data you load from disk or somewhere else.
1616

1717
// create the data context and get all related structs
18-
struct Sass_Data_Context* data_ctx = sass_make_data_context(text);
19-
struct Sass_Context* ctx = sass_data_context_get_context(data_ctx);
20-
struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
21-
22-
// configure some options ...
23-
sass_option_set_precision(ctx_opt, 10);
24-
25-
// context is set up, call the compile step now
26-
int status = sass_compile_data_context(data_ctx);
27-
28-
// print the result or the error to the stdout
29-
if (status == 0) puts(sass_context_get_output_string(ctx));
30-
else puts(sass_context_get_error_message(ctx));
31-
32-
// release allocated memory
33-
sass_delete_data_context(data_ctx);
18+
struct SassCompiler* compiler = sass_make_compiler();
19+
struct SassImport* data = sass_make_content_import(text, "styles");
20+
sass_compiler_set_entry_point(compiler, data);
21+
// everything you make you must delete
22+
sass_delete_import(data); // passed away
23+
24+
// Execute all three phases
25+
sass_compiler_parse(compiler);
26+
sass_compiler_compile(compiler);
27+
sass_compiler_render(compiler);
28+
29+
// Print any warning to console
30+
if (sass_compiler_get_warn_string(compiler)) {
31+
sass_print_stderr(sass_compiler_get_warn_string(compiler));
32+
}
33+
34+
// Print error message if we have an error
35+
if (sass_compiler_get_status(compiler) != 0) {
36+
const struct SassError* error = sass_compiler_get_error(compiler);
37+
sass_print_stderr(sass_error_get_string(error));
38+
}
39+
40+
// Get result code after all compilation steps
41+
int status = sass_compiler_get_status(compiler);
42+
43+
// Write to output if no errors occurred
44+
if (status == 0) {
45+
46+
// Paths where to write stuff to (might be `stream://stdout`)
47+
const char* outfile = sass_compiler_get_output_path(compiler);
48+
const char* mapfile = sass_compiler_get_srcmap_path(compiler);
49+
// Get the parts to be added to the output file (or stdout)
50+
const char* content = sass_compiler_get_output_string(compiler);
51+
const char* footer = sass_compiler_get_footer_string(compiler);
52+
const char* srcmap = sass_compiler_get_srcmap_string(compiler);
53+
54+
// Output all results
55+
if (content) puts(content);
56+
if (footer) puts(footer);
57+
58+
}
3459

3560
// exit status
3661
return status;
@@ -51,31 +76,60 @@ echo "foo { margin: 21px * 2; }" > foo.scss
5176

5277
```C:file.c
5378
#include <stdio.h>
54-
#include "sass/context.h"
79+
#include <sass.h>
5580

56-
int main( int argc, const char* argv[] )
81+
int main(int argc, const char* argv[])
5782
{
5883

59-
// get the input file from first argument or use default
60-
const char* input = argc > 1 ? argv[1] : "styles.scss";
61-
62-
// create the file context and get all related structs
63-
struct Sass_File_Context* file_ctx = sass_make_file_context(input);
64-
struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
65-
struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
66-
67-
// configure some options ...
68-
sass_option_set_precision(ctx_opt, 10);
69-
70-
// context is set up, call the compile step now
71-
int status = sass_compile_file_context(file_ctx);
72-
73-
// print the result or the error to the stdout
74-
if (status == 0) puts(sass_context_get_output_string(ctx));
75-
else puts(sass_context_get_error_message(ctx));
84+
// LibSass will take control of data you pass in
85+
// Therefore we need to make a copy of static data
86+
char* text = sass_copy_c_string("a{b:c;}");
87+
// Normally you'll load data into a buffer from i.e. the disk.
88+
// Use `sass_alloc_memory` to get a buffer to pass to LibSass
89+
// then fill it with data you load from disk or somewhere else.
7690

77-
// release allocated memory
78-
sass_delete_file_context(file_ctx);
91+
// create the data context and get all related structs
92+
struct SassCompiler* compiler = sass_make_compiler();
93+
struct SassImport* data = sass_make_file_import("foo.scss");
94+
sass_compiler_set_entry_point(compiler, data);
95+
// everything you make you must delete
96+
sass_delete_import(data); // passed away
97+
98+
// Execute all three phases
99+
sass_compiler_parse(compiler);
100+
sass_compiler_compile(compiler);
101+
sass_compiler_render(compiler);
102+
103+
// Print any warning to console
104+
if (sass_compiler_get_warn_string(compiler)) {
105+
sass_print_stderr(sass_compiler_get_warn_string(compiler));
106+
}
107+
108+
// Print error message if we have an error
109+
if (sass_compiler_get_status(compiler) != 0) {
110+
const struct SassError* error = sass_compiler_get_error(compiler);
111+
sass_print_stderr(sass_error_get_string(error));
112+
}
113+
114+
// Get result code after all compilation steps
115+
int status = sass_compiler_get_status(compiler);
116+
117+
// Write to output if no errors occurred
118+
if (status == 0) {
119+
120+
// Paths where to write stuff to (might be `stream://stdout`)
121+
const char* outfile = sass_compiler_get_output_path(compiler);
122+
const char* mapfile = sass_compiler_get_srcmap_path(compiler);
123+
// Get the parts to be added to the output file (or stdout)
124+
const char* content = sass_compiler_get_output_string(compiler);
125+
const char* footer = sass_compiler_get_footer_string(compiler);
126+
const char* srcmap = sass_compiler_get_srcmap_string(compiler);
127+
128+
// Output all results
129+
if (content) puts(content);
130+
if (footer) puts(footer);
131+
132+
}
79133

80134
// exit status
81135
return status;

docs/api-doc.md

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ First you will need to include the header file!
2626
This will automatically load all other headers too!
2727

2828
```C
29-
#include "sass/context.h"
29+
#include <sass.h>
3030
```
3131

3232
## Basic C Example
3333

3434
```C
3535
#include <stdio.h>
36-
#include "sass/context.h"
36+
#include <sass.h>
3737

3838
int main() {
3939
puts(libsass_version());
@@ -54,61 +54,59 @@ gcc -Wall version.c -lsass -o version && ./version
5454

5555
## Compiling your code
5656

57-
The most important is your sass file (or string of sass code). With this, you
58-
will want to start a LibSass compiler. Here is some pseudocode describing the
59-
process. The compiler has two different modes: direct input as a string with
60-
`Sass_Data_Context` or LibSass will do file reading for you by using
61-
`Sass_File_Context`. See the code for a list of options available
62-
[Sass_Options](https://github.com/sass/libsass/blob/36feef0/include/sass/interface.h#L18)
63-
64-
The general rule is if the API takes `const char*` it will make a copy,
65-
but where the API is `char*` it will take over memory ownership, so make sure to pass
66-
in memory that is allocated via `sass_copy_c_string` or `sass_alloc_memory`.
67-
68-
**Building a file compiler**
69-
70-
context = sass_make_file_context("file.scss")
71-
options = sass_file_context_get_options(context)
72-
sass_option_set_precision(options, 1)
73-
sass_option_set_source_comments(options, true)
74-
75-
sass_file_context_set_options(context, options)
76-
77-
compiler = sass_make_file_compiler(sass_context)
78-
sass_compiler_parse(compiler)
79-
sass_compiler_execute(compiler)
80-
81-
output = sass_context_get_output_string(context)
82-
// Retrieve errors during compilation
83-
error_status = sass_context_get_error_status(context)
84-
json_error = sass_context_get_error_json(context)
85-
// Release memory dedicated to the C compiler
86-
sass_delete_compiler(compiler)
87-
88-
**Building a data compiler**
89-
90-
// LibSass takes over memory owenership, make sure to allocate
91-
// a buffer via `sass_alloc_memory` or `sass_copy_c_string`.
92-
buffer = sass_copy_c_string("div { a { color: blue; } }")
93-
94-
context = sass_make_data_context(buffer)
95-
options = sass_data_context_get_options(context)
96-
sass_option_set_precision(options, 1)
97-
sass_option_set_source_comments(options, true)
98-
99-
sass_data_context_set_options(context, options)
100-
101-
compiler = sass_make_data_compiler(context)
102-
sass_compiler_parse(compiler)
103-
sass_compiler_execute(compiler)
104-
105-
output = sass_context_get_output_string(context)
106-
// div a { color: blue; }
107-
// Retrieve errors during compilation
108-
error_status = sass_context_get_error_status(context)
109-
json_error = sass_context_get_error_json(context)
110-
// Release memory dedicated to the C compiler
111-
sass_delete_compiler(compiler)
57+
LibSass parsing starts with some entry point, which can either be a
58+
file or some code you provide directly. Further relative includes are
59+
resolved against CWD. In order to tell LibSass the entry point, there
60+
are two main ways, either defining the content directly or let LibSass
61+
load a file.
62+
63+
`struct SassImport* data = sass_make_content_import(text, "styles.scss");`
64+
`struct SassImport* data = sass_make_file_import("styles.scss");`
65+
66+
**Building a compiler**
67+
68+
sass_compiler_set_entry_point(compiler, entrypoint);
69+
// everything you make you must delete
70+
sass_delete_import(data); // passed away
71+
72+
// Execute all three phases
73+
sass_compiler_parse(compiler);
74+
sass_compiler_compile(compiler);
75+
sass_compiler_render(compiler);
76+
77+
// Print any warning to console
78+
if (sass_compiler_get_warn_string(compiler)) {
79+
sass_print_stderr(sass_compiler_get_warn_string(compiler));
80+
}
81+
82+
// Print error message if we have an error
83+
if (sass_compiler_get_status(compiler) != 0) {
84+
const struct SassError* error = sass_compiler_get_error(compiler);
85+
sass_print_stderr(sass_error_get_string(error));
86+
}
87+
88+
// Get result code after all compilation steps
89+
int status = sass_compiler_get_status(compiler);
90+
91+
// Write to output if no errors occurred
92+
if (status == 0) {
93+
94+
// Paths where to write stuff to (might be `stream://stdout`)
95+
const char* outfile = sass_compiler_get_output_path(compiler);
96+
const char* mapfile = sass_compiler_get_srcmap_path(compiler);
97+
// Get the parts to be added to the output file (or stdout)
98+
const char* content = sass_compiler_get_output_string(compiler);
99+
const char* footer = sass_compiler_get_footer_string(compiler);
100+
const char* srcmap = sass_compiler_get_srcmap_string(compiler);
101+
102+
// Output all results
103+
if (content) puts(content);
104+
if (footer) puts(footer);
105+
106+
}
107+
108+
// exit status
109+
return status;
112110

113111
## Sass Context Internals
114112

docs/api-import.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Imports on the C-API side can either be compilation entry points or imports
55
returned from custom importers. Overall they behave very similarly, but entry
6-
points are a bit more strict, as we know better what to expect. Import from
6+
points are a bit more strict, as we know better what to expect. Imports from
77
custom importers can be more versatile. An import can either have a path,
88
some loaded content or both. Custom importer can return imports in either
99
of that state, while with entry points we know what to expect.

include/sass/variable.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ extern "C" {
1818
ADDAPI struct SassValue* ADDCALL sass_env_get_lexical (struct SassCompiler*, const char*);
1919

2020
// Setter for lexical variable (lexical to scope where function is called).
21+
// Returns true if variable was set or false if it does not exist (we can't create it)
2122
// Note: C-API function can only access existing variables and not create new ones!
22-
ADDAPI void ADDCALL sass_env_set_lexical(struct SassCompiler*, const char*, struct SassValue*);
23+
ADDAPI bool ADDCALL sass_env_set_lexical(struct SassCompiler*, const char*, struct SassValue*);
2324

24-
// Getter for local variable (local only to scope where function is called).
25+
// Getter for global variable (only variables on the root scope are considered).
2526
// Note: C-API function can only access existing variables and not create new ones!
2627
ADDAPI struct SassValue* ADDCALL sass_env_get_global (struct SassCompiler*, const char*);
2728

28-
// Setter for local variable (local only to scope where function is called).
29+
// Setter for global variable (only variables on the root scope are considered).
30+
// Returns true if variable was set or false if it does not exist (we can't create it)
2931
// Note: C-API function can only access existing variables and not create new ones!
30-
ADDAPI void ADDCALL sass_env_set_global(struct SassCompiler*, const char*, struct SassValue*);
32+
ADDAPI bool ADDCALL sass_env_set_global(struct SassCompiler*, const char*, struct SassValue*);
3133

3234
/////////////////////////////////////////////////////////////////////////
3335
/////////////////////////////////////////////////////////////////////////

src/ast_callables.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Sass {
2424
const EnvKey& envkey,
2525
ArgumentDeclaration* parameters,
2626
const SassFnSig& callback) :
27-
Callable(SourceSpan::tmp("[BUILTIN]")),
27+
Callable(SourceSpan::internal("[BUILTIN]")),
2828
envkey_(envkey),
2929
// Create a single entry in overloaded function
3030
function_(SassFnPair{ parameters, callback })
@@ -54,7 +54,7 @@ namespace Sass {
5454
BuiltInCallables::BuiltInCallables(
5555
const EnvKey& envkey,
5656
const SassFnPairs& overloads) :
57-
Callable(SourceSpan::tmp("[BUILTINS]")),
57+
Callable(SourceSpan::internal("[BUILTINS]")),
5858
envkey_(envkey),
5959
overloads_(overloads)
6060
{
@@ -121,7 +121,7 @@ namespace Sass {
121121
const EnvKey& fname,
122122
ArgumentDeclaration* parameters,
123123
SassFunctionLambda lambda) :
124-
Callable(SourceSpan::tmp("[EXTERNAL]")),
124+
Callable(SourceSpan::internal("[EXTERNAL]")),
125125
envkey_(fname),
126126
declaration_(parameters),
127127
lambda_(lambda)

0 commit comments

Comments
 (0)