All Projects → robinweser → bredon

robinweser / bredon

Licence: MIT license
A modern CSS value compiler in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
shell
77523 projects

Projects that are alternatives of or similar to bredon

pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (-46.15%)
Mutual labels:  tokenizer, ast, lexer
Php Parser
🌿 NodeJS PHP Parser - extract AST or tokens (PHP5 and PHP7)
Stars: ✭ 400 (+925.64%)
Mutual labels:  tokenizer, ast, lexer
Hippo
PHP standards checker.
Stars: ✭ 82 (+110.26%)
Mutual labels:  tokenizer, ast
Chevrotain
Parser Building Toolkit for JavaScript
Stars: ✭ 1,795 (+4502.56%)
Mutual labels:  tokenizer, lexer
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (+323.08%)
Mutual labels:  tokenizer, ast
Jflex
The fast scanner generator for Java™ with full Unicode support
Stars: ✭ 380 (+874.36%)
Mutual labels:  tokenizer, lexer
Moo
Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo.
Stars: ✭ 434 (+1012.82%)
Mutual labels:  tokenizer, lexer
Lex
Replaced by foonathan/lexy
Stars: ✭ 137 (+251.28%)
Mutual labels:  tokenizer, lexer
Cub
The Cub Programming Language
Stars: ✭ 198 (+407.69%)
Mutual labels:  ast, lexer
snapdragon-lexer
Converts a string into an array of tokens, with useful methods for looking ahead and behind, capturing, matching, et cetera.
Stars: ✭ 19 (-51.28%)
Mutual labels:  tokenizer, lexer
lexertk
C++ Lexer Toolkit Library (LexerTk) https://www.partow.net/programming/lexertk/index.html
Stars: ✭ 26 (-33.33%)
Mutual labels:  tokenizer, lexer
SwiLex
A universal lexer library in Swift.
Stars: ✭ 29 (-25.64%)
Mutual labels:  tokenizer, lexer
Lexmachine
Lex machinary for go.
Stars: ✭ 335 (+758.97%)
Mutual labels:  tokenizer, lexer
Snl Compiler
SNL(Small Nested Language) Compiler. Maven jUnit Tokenizer Lexer Syntax Parser. 编译原理 词法分析 语法分析
Stars: ✭ 19 (-51.28%)
Mutual labels:  tokenizer, lexer
vscode-blockman
VSCode extension to highlight nested code blocks
Stars: ✭ 233 (+497.44%)
Mutual labels:  tokenizer, ast
Works For Me
Collection of developer toolkits
Stars: ✭ 131 (+235.9%)
Mutual labels:  tokenizer, lexer
Participle
A parser library for Go
Stars: ✭ 2,302 (+5802.56%)
Mutual labels:  ast, lexer
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (+361.54%)
Mutual labels:  ast, lexer
stutter
Implement a Lisp, in C, from scratch, no libs
Stars: ✭ 65 (+66.67%)
Mutual labels:  ast, lexer
lex
Lex is an implementation of lex tool in Ruby.
Stars: ✭ 49 (+25.64%)
Mutual labels:  tokenizer, lexer

Bredon

Bredon is a modern, specification-driven CSS value compiler in JavaScript.
It's parser uses very detailed nodes and provides as much information as possible.
The generated AST (Abstract Syntax Tree) is simple and readable which allows efficient and straight-forward transormation. It automatically outputs formatted CSS values.

TravisCI Test Coverage npm downloads gzipped size npm version Gitter

Support Us

Support Robin Frischmann's work on Fela and its ecosystem (Bredon) directly via Patreon.

Or support us on Open Collective to fund community work. This also includes Bredon as well.
Thank you to all our backers!

Installation

yarn add bredon

Alternatively use npm i --save bredon.

Why?

I am heavily involved in the whole CSS in JS movement with Fela and Elodin as well as inline-style-prefixer. While writing Elodin, a plugin-based style object linter, I struggled to do complex value validation and value transformation. As CSS values are just plain strings, we do not have any meaningful types.
But, in order to perform efficient validation and transformation, we have to parse CSS values into useful type-aware components. That's where Bredon joins the game. It uses a specification-driven value parser that provides the required degree of accuracy and detail.

Bredon also serves as a personal project to learn and understand how compilers work.

How?

I heavily used James Kyle's the-super-tiny-compiler to build Bredon. It follows the exact same steps as any other compiler does:

  1. First of all we read the input, one by one, and generate tokens
  2. Then we parse these tokens into syntactic nodes, also know as AST
  3. (optional) We may now traverse the AST and transform nodes
  4. Finally we generate a new CSS value using the transformed AST

The Gist

import { parse, generate } from 'bredon'

const input = '10px solid rgba(255, 0, 255, 0.55)'
const ast = parse(input)

ast === {
  type: 'ValueList',
  body: [{
    type: 'Value',
    important: false,
    body: [{
      type: 'Dimension',
      unit: 'px',
      value: {
        type: 'Integer',
        negative: false,
        value: 10
      }
    }, {
      type: 'Identifier',
      value: 'solid'
    }, {
      type: 'FunctionExpression',
      callee: 'rgba',
      params: [{
        type: 'Integer',
        negative: false,
        value: 255
      }, {
        type: 'Integer',
        negative: false,
        value: 0
      }, {
        type: 'Integer',
        negative: true,
        value: 255
      }, {
        type: 'Float',
        negative: false,
        fractional: 0.55,
        integer: 0,
      }]
    }]
  }]
}

const output = generate(ast)

console.log(output)
// => 10px solid rgba(255, 0, 255, 0.55)

// parsing and generation can be combined
const output = compile(input)

Documentation

Plugins

Bredon's most powerful part is its extendable plugin system.
Plugins are used to analyze and transform AST nodes.

Plugin Version Size Description
calc npm version gzipped size Precalculate calc() expression as much as possible
case npm version gzipped size Normalize letter case for all identifiers
color npm version gzipped size Manipulate, normalize and minify CSS color values
initial npm version gzipped size Replace, normalize and minify initial values
precision npm version gzipped size Normalize decimal number precision
remove-unit npm version gzipped size Remove unnecessary value units
trim-hex npm version gzipped size Minify hex color values if possible
unit npm version gzipped size Convert, normalize and minify unit values

Integrations

To use Bredon within your project, you will need to somehow integrate the compiler into your workflow.
Currently, we support to options to do so. You can either use Bredon with your existing CSS codebase using PostCSS. For JavaScript-based styling solutions (CSS in JS), there is Elodin which can be configured to auto-fix styles.

PostCSS Stand-Alones

You can also use bredon-minify and bredon-validate as a stand-alone plugin for PostCSS:

Support

Join us on Gitter.
We highly appreciate any contribution.
We also love to get feedback.

License

Bredon is licensed under the MIT License.
Documentation is licensed under Creative Common License.
Created with by @rofrischmann.

Sponsor

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].