Skip to content

Instantly share code, notes, and snippets.

@Kielan
Last active May 2, 2022 04:02
Show Gist options
  • Select an option

  • Save Kielan/9c0eee634b98d5db70163975ee57cffd to your computer and use it in GitHub Desktop.

Select an option

Save Kielan/9c0eee634b98d5db70163975ee57cffd to your computer and use it in GitHub Desktop.
compiler prettier and
What is concrete syntax tree in compiler design?
Image result for concrete syntax tree
CST(Concrete Syntax Tree) is a tree representation of the Grammar(Rules of how the program should be written).
Depending on compiler architecture, it can be used by the Parser to produce an AST.
AST(Abstract Syntax Tree)is a tree representation of Parsed source, produced by the Parser part of the compiler.
Sass compiler docs pros
- https://github.com/sass/sass/tree/main/js-api-doc
Sass repo has a spec folder detailing entities
- https://github.com/sass/libsass
Libsass repo is deprecated for
https://github.com/sass/dart-sass
[] Change tailwind's stubs folder to seeds.
https://github.com/sass/libsass/blob/master/src/sass_context.cpp
line 245
// 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_file = 0;
c_ctx->error_src = 0;
c_ctx->error_line = sass::string::npos;
c_ctx->error_column = sass::string::npos;
// allocate a new compiler instance
void* ctxmem = calloc(1, sizeof(struct Sass_Compiler));
if (ctxmem == 0) { std::cerr << "Error allocating memory for context" << std::endl; return 0; }
Sass_Compiler* compiler = (struct Sass_Compiler*) ctxmem;
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;
}
https://github.com/sass/dart-sass
https://dart.dev/articles/libraries/dart-io
Sass dart has an io module. Marshmallowcss will likely have
A similar process module marshmallowcss:io
Or marshmellowcss-cli:io for Process
import 'dart:io';
void main() async {
final output = File('output.txt').openWrite();
Process process = await Process.start('ls', ['-l']);
// Wait for the process to finish; get the exit code.
final exitCode = (await Future.wait([
process.stdout.pipe(output), // Send stdout to file.
process.stderr.drain(), // Discard stderr.
process.exitCode
]))[2];
print('exit code: $exitCode');
}
prettier.format("lodash ( )", {
parser(text, { babel }) {
const ast = babel(text);
ast.program.body[0].expression.callee.name = "_";
return ast;
},
});
// -> "_();\n"
nodes = [new Declaration(nodes)]
} else if (nodes.selector) {
nodes = [new Rule(nodes)]
} else if (nodes.name) {
nodes = [new AtRule(nodes)]
} else if (nodes.text) {
nodes = [new Comment(nodes)]
} else {
throw new Error('Unknown node type in node creation')
}
Container.registerParse = dependant => {
parse = dependant
}
Container.registerRule = dependant => {
Rule = dependant
}
Container.registerAtRule = dependant => {
AtRule = dependant
}
lib/tokenize.js
tokenizer(input, options = {}) {
let css = input.css.valueOf()
}
postcss
lib/document.d.ts
/**
* Represents a file and contains all its parsed nodes.
*
* **Experimental:** some aspects of this node could change within minor
* or patch version releases.
*
* ```js
* const document = htmlParser(
* '<html><style>a{color:black}</style><style>b{z-index:2}</style>'
* )
* document.type //=> 'document'
* document.nodes.length //=> 2
* ```
*/
export default class Document extends Container<Root> {
type: 'document'
parent: undefined
constructor(defaults?: DocumentProps)
/**
* Returns a `Result` instance representing the document’s CSS roots.
*
* ```js
* const root1 = postcss.parse(css1, { from: 'a.css' })
* const root2 = postcss.parse(css2, { from: 'b.css' })
* const document = postcss.document()
* document.append(root1)
* document.append(root2)
* const result = document.toResult({ to: 'all.css', map: true })
* ```
*
* @param opts Options.
* @return Result with current document’s CSS.
*/
toResult(options?: ProcessOptions): Result
}
postcss
lib/postcss.d.ts
export interface JSONHydrator {
(data: object[]): Node[]
(data: object): Node
}
postcss/lib/input.js
let { nanoid } = require('nanoid/non-secure')
class Input {
constructor(css, opts = {}) {
}
this.css = css.toString()
if (this.css[0] === '\uFEFF' || this.css[0] === '\uFFFE') {
this.hasBOM = true
this.css = this.css.slice(1)
} else {
this.hasBOM = false
}
if (opts.from) {
if (
!pathAvailable ||
/^\w+:\/\//.test(opts.from) ||
isAbsolute(opts.from)
) {
this.file = opts.from
} else {
this.file = resolve(opts.from)
}
}
if (pathAvailable && sourceMapAvailable) {
let map = new PreviousMap(this.css, opts)
if (map.text) {
this.map = map
let file = map.consumer().file
if (!this.file && file) this.file = this.mapResolve(file)
}
}
test('a color', () => {
let config = {
content: [{ raw: html`<div class="-bg-red"></div>` }],
theme: {
colors: {
red: 'red',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchCss(css``)
})
})
...
if (!this.file) {
this.id = '<input css ' + nanoid(6) + '>'
}
if (this.map) this.map.file = this.from
}
https://github.com/sveltejs/prettier-plugin-svelte/blob/master/src/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment