Blame docs/source-map-internals.md

Packit bfcc33
This document is mainly intended for developers!
Packit bfcc33
Packit bfcc33
# Documenting some of the source map internals
Packit bfcc33
Packit bfcc33
Since source maps are somewhat a black box to all LibSass maintainers, [I](@mgreter) will try to document my findings with source maps in LibSass, as I come across them. This document will also brievely explain how LibSass parses the source and how it outputs the result.
Packit bfcc33
Packit bfcc33
The main storage for SourceMap mappings is the `mappings` vector:
Packit bfcc33
Packit bfcc33
```
Packit bfcc33
# in source_map.hpp
Packit bfcc33
vector<Mapping> mappings
Packit bfcc33
# in mappings.hpp
Packit bfcc33
struct Mapping ...
Packit bfcc33
  Position original_position;
Packit bfcc33
  Position generated_position;
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## Every parsed token has its source associated
Packit bfcc33
Packit bfcc33
LibSass uses a lexical parser. Whenever LibSass finds a token of interest, it creates a specific `AST_Node`, which will hold a reference to the input source with line/column information. `AST_Node` is the base class for all parsed items. They are declared in `ast.hpp` and are used in `parser.hpp`. Here a simple example:
Packit bfcc33
Packit bfcc33
```
Packit bfcc33
if (lex< custom_property_name >()) {
Packit bfcc33
  Sass::String* prop = new (ctx.mem) String_Constant(path, source_position, lexed);
Packit bfcc33
  return new (ctx.mem) Declaration(path, prop->position(), prop, ...);
Packit bfcc33
}
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## How is the `source_position` calculated
Packit bfcc33
Packit bfcc33
This is automatically done with `lex` in `parser.hpp`. Whenever something is lexed, the `source_position` is updated. But be aware that `source_position` points to the begining of the parsed text. If you need a mapping for the position where the parsing ended, you need to add another call to `lex` (to match nothing)!
Packit bfcc33
Packit bfcc33
```
Packit bfcc33
lex< exactly < empty_str > >();
Packit bfcc33
end = new (ctx.mem) String_Constant(path, source_position, lexed);
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## How are mappings for the output created
Packit bfcc33
Packit bfcc33
So far we have collected all needed data for all tokens in the input stream. We can now use this information to create mappings when we put things into the output stream. Mappings are created via the `add_mappings` method:
Packit bfcc33
Packit bfcc33
```
Packit bfcc33
# in source_map.hpp
Packit bfcc33
void add_mapping(AST_Node* node);
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
This method is called in two places:
Packit bfcc33
- `Inspect::append_to_buffer`
Packit bfcc33
- `Output_[Nested|Compressed]::append_to_buffer`
Packit bfcc33
Packit bfcc33
Mappings can only be created for things that have been parsed into a `AST_Node`. Otherwise we do not have the information to create the mappings, which is the reason why LibSass currently only maps the most important tokens in source maps.