Add 'vendor/tree-sitter-css/' from commit '94e10230939e702b4fa3fa2cb5c3bc7173b95d07'

git-subtree-dir: vendor/tree-sitter-css
git-subtree-mainline: e31b5b4925
git-subtree-split: 94e1023093
ida_star
Wilfred Hughes 2021-08-15 16:41:30 +07:00
commit 26c6438fa2
27 changed files with 27830 additions and 0 deletions

@ -0,0 +1,22 @@
image: Visual Studio 2015
environment:
nodejs_version: "10"
platform:
- x64
install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install
test_script:
- npm run test-windows
build: off
branches:
only:
- master

@ -0,0 +1,2 @@
/src/** linguist-vendored
/examples/* linguist-vendored

@ -0,0 +1,6 @@
node_modules
build
target
*.log
package-lock.json
Cargo.lock

@ -0,0 +1,3 @@
build
target
Cargo.lock

@ -0,0 +1,7 @@
language: node_js
node_js: 10
branches:
only:
- master

@ -0,0 +1,27 @@
[package]
name = "tree-sitter-css"
description = "css grammar for the tree-sitter parsing library"
version = "0.19.0"
keywords = ["incremental", "parsing", "css"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-javascript"
edition = "2018"
license = "MIT"
authors = ["Max Brunsfeld <maxbrunsfeld@gmail.com>"]
build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]
[lib]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "0.19"
[build-dependencies]
cc = "1.0"

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 Max Brunsfeld
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,11 @@
tree-sitter-css
===============
[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter-css.svg?branch=master)](https://travis-ci.org/tree-sitter/tree-sitter-css)
[![Build status](https://ci.appveyor.com/api/projects/status/smdphgf4ns9jglw5/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter-css/branch/master)
CSS grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).
References
* [CSS Syntax Guide](https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax)

@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_css_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"src/parser.c",
"src/scanner.c",
"bindings/node/binding.cc"
],
"cflags_c": [
"-std=c99",
]
}
]
}

@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_css();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_css());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("css").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_css_binding, Init)
} // namespace

@ -0,0 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_css_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_css_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}

@ -0,0 +1,16 @@
fn main() {
let src_dir = std::path::Path::new("src");
let mut c_config = cc::Build::new();
c_config.include(&src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
let scanner_path = src_dir.join("scanner.c");
c_config.file(&parser_path);
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");
}

@ -0,0 +1,47 @@
//! This crate provides css language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_css::language()).expect("Error loading css grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter::Language;
extern "C" {
fn tree_sitter_css() -> Language;
}
/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_css() }
}
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading css language");
}
}

@ -0,0 +1,246 @@
==========================
Function calls
==========================
a {
color: rgba(0, 255, 0, 0.5);
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration
(property_name)
(call_expression (function_name) (arguments
(integer_value)
(integer_value)
(integer_value)
(float_value)))))))
=============================================
Calls where each argument has multiple values
=============================================
div {
background: repeating-linear-gradient(red, orange 50px);
clip-path: polygon(50% 0%, 60% 40%, 100% 50%, 60% 60%, 50% 100%, 40% 60%, 0% 50%, 40% 40%);
}
---
(stylesheet
(rule_set (selectors (tag_name)) (block
(declaration
(property_name)
(call_expression (function_name) (arguments
(plain_value)
(plain_value)
(integer_value (unit)))))
(declaration
(property_name)
(call_expression (function_name) (arguments
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))))))))
============================
Color literals
============================
a {
b: #fafd04;
c: #fafd0401;
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (color_value))
(declaration (property_name) (color_value)))))
============================
Numbers
============================
a {
b: 0.5%;
c: 5em;
margin: 10E3px;
margin: -456.8px;
margin: -5px;
margin: -0.0px;
}
---
(stylesheet
(rule_set (selectors (tag_name)) (block
(declaration (property_name) (float_value (unit)))
(declaration (property_name) (integer_value (unit)))
(declaration (property_name) (float_value (unit)))
(declaration (property_name) (float_value (unit)))
(declaration (property_name) (integer_value (unit)))
(declaration (property_name) (float_value (unit))))))
============================
Binary arithmetic operators
============================
a {
width: calc(100% - 80px);
aspect-ratio: 1/2;
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration
(property_name)
(call_expression (function_name) (arguments (binary_expression (integer_value (unit)) (integer_value (unit))))))
(declaration
(property_name)
(binary_expression (integer_value) (integer_value)))
(declaration
(property_name)
(call_expression
(function_name)
(arguments
(binary_expression
(binary_expression
(integer_value (unit))
(parenthesized_value (binary_expression (integer_value) (integer_value))))
(parenthesized_value
(binary_expression
(parenthesized_value (binary_expression (integer_value (unit)) (integer_value (unit))))
(parenthesized_value (binary_expression (integer_value) (integer_value))))))))))))
============================
Strings
============================
a {
b: '';
c: '\'hi\'';
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (string_value))
(declaration (property_name) (string_value)))))
============================
URLs
============================
a {
b: http://something-else?foo=bar;
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (plain_value)))))
============================
Important declarations
============================
a {
b: c !important;
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (plain_value) (important)))))
============================
Declarations without trailing semicolons
============================
a {
b: c;
d: e
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (plain_value))
(declaration (property_name) (plain_value)))))
=======================================
Comments right after numbers
=======================================
a {
shape-outside: circle(20em/*=*/at 50% 50%);
shape-outside: inset(1em, 1em, 1em, 1em);
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(declaration (property_name) (call_expression (function_name) (arguments
(integer_value (unit))
(comment)
(plain_value)
(integer_value (unit))
(integer_value (unit)))))
(declaration (property_name) (call_expression (function_name) (arguments
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))
(integer_value (unit))))))))
=================================
Declarations at the top level
=================================
--a-variable: -5px;
a-property: calc(5px + var(--a-variable));
---
(stylesheet
(declaration (property_name) (integer_value (unit)))
(declaration (property_name) (call_expression (function_name) (arguments (binary_expression (integer_value (unit)) (call_expression (function_name) (arguments (plain_value))))))))

@ -0,0 +1,204 @@
=========================
Universal selectors
=========================
* {}
---
(stylesheet
(rule_set (selectors (universal_selector)) (block)))
=========================
Type selectors
=========================
div, span {}
h1, h2, h3, h4 {}
---
(stylesheet
(rule_set (selectors (tag_name) (tag_name)) (block))
(rule_set (selectors (tag_name) (tag_name) (tag_name) (tag_name)) (block)))
=========================
Class selectors
=========================
.class-a {}
div.class-b, .class-c.class-d {}
---
(stylesheet
(rule_set
(selectors (class_selector (class_name)))
(block))
(rule_set
(selectors
(class_selector (tag_name) (class_name))
(class_selector (class_selector (class_name)) (class_name)))
(block)))
=========================
Id selectors
=========================
#some-id, a#another-id {}
---
(stylesheet
(rule_set
(selectors (id_selector (id_name)) (id_selector (tag_name) (id_name)))
(block)))
=========================
Attribute selectors
=========================
[a] {}
[b=c] {}
[d~=e] {}
a[b] {}
---
(stylesheet
(rule_set (selectors (attribute_selector (attribute_name))) (block))
(rule_set (selectors (attribute_selector (attribute_name) (plain_value))) (block))
(rule_set (selectors (attribute_selector (attribute_name) (plain_value))) (block))
(rule_set (selectors (attribute_selector (tag_name) (attribute_name))) (block)))
=========================
Pseudo-class selectors
=========================
a:hover {}
:nth-child(2) {}
---
(stylesheet
(rule_set
(selectors (pseudo_class_selector (tag_name) (class_name)))
(block))
(rule_set
(selectors (pseudo_class_selector (class_name) (arguments (integer_value))))
(block)))
=========================
Pseudo-element selectors
=========================
a::first-line {}
---
(stylesheet
(rule_set
(selectors (pseudo_element_selector (tag_name) (tag_name)))
(block)))
=========================
Child selectors
=========================
a > b {}
c > d > e {}
---
(stylesheet
(rule_set
(selectors (child_selector (tag_name) (tag_name)))
(block))
(rule_set
(selectors (child_selector
(child_selector (tag_name) (tag_name))
(tag_name)))
(block)))
=========================
Descendant selectors
=========================
a b {}
c d e {}
---
(stylesheet
(rule_set
(selectors (descendant_selector (tag_name) (tag_name)))
(block))
(rule_set
(selectors (descendant_selector
(descendant_selector (tag_name) (tag_name))
(tag_name)))
(block)))
===========================
Nesting selectors
===========================
a {
&.b {}
& c {}
& > d {}
}
---
(stylesheet
(rule_set
(selectors (tag_name))
(block
(rule_set (selectors (class_selector (nesting_selector) (class_name))) (block))
(rule_set (selectors (descendant_selector (nesting_selector) (tag_name))) (block))
(rule_set (selectors (child_selector (nesting_selector) (tag_name))) (block)))))
===========================
Sibling selectors
===========================
a.b ~ c.d {}
.e.f + .g.h {}
---
(stylesheet
(rule_set
(selectors (sibling_selector
(class_selector (tag_name) (class_name))
(class_selector (tag_name) (class_name))))
(block))
(rule_set
(selectors (adjacent_sibling_selector
(class_selector (class_selector (class_name)) (class_name))
(class_selector (class_selector (class_name)) (class_name))))
(block)))
===========================
The :not selector
===========================
a:not(:hover) {}
.b:not(c > .d) {}
---
(stylesheet
(rule_set
(selectors (pseudo_class_selector
(tag_name)
(class_name)
(arguments (pseudo_class_selector (class_name)))))
(block))
(rule_set
(selectors (pseudo_class_selector
(class_selector (class_name))
(class_name)
(arguments (child_selector (tag_name) (class_selector (class_name))))))
(block)))

@ -0,0 +1,159 @@
==============================
Import statements
==============================
@import url("fineprint.css") print;
@import url("bluish.css") speech;
@import 'custom.css';
@import url("chrome://communicator/skin/");
@import "common.css" screen;
---
(stylesheet
(import_statement (call_expression (function_name) (arguments (string_value))) (keyword_query))
(import_statement (call_expression (function_name) (arguments (string_value))) (keyword_query))
(import_statement (string_value))
(import_statement (call_expression (function_name) (arguments (string_value))))
(import_statement (string_value) (keyword_query)))
==============================
Namespace statements
==============================
/* Default namespace */
@namespace url(XML-namespace-URL);
@namespace "XML-namespace-URL";
@namespace url(http://www.w3.org/1999/xhtml);
@namespace svg url(http://www.w3.org/2000/svg);
/* Prefixed namespace */
@namespace prefix url(XML-namespace-URL);
@namespace prefix "XML-namespace-URL";
---
(stylesheet
(comment)
(namespace_statement (call_expression (function_name) (arguments (plain_value))))
(namespace_statement (string_value))
(namespace_statement (call_expression (function_name) (arguments (plain_value))))
(namespace_statement (namespace_name) (call_expression (function_name) (arguments (plain_value))))
(comment)
(namespace_statement (namespace_name) (call_expression (function_name) (arguments (plain_value))))
(namespace_statement (namespace_name) (string_value)))
==============================
Keyframes statements
==============================
@keyframes important1 {
from { margin-top: 50px; }
50% { margin-top: 150px !important; } /* ignored */
to { margin-top: 100px; }
}
---
(stylesheet
(keyframes_statement (keyframes_name) (keyframe_block_list
(keyframe_block (from) (block (declaration (property_name) (integer_value (unit)))))
(keyframe_block (integer_value (unit)) (block (declaration (property_name) (integer_value (unit)) (important))))
(comment)
(keyframe_block (to) (block (declaration (property_name) (integer_value (unit))))))))
==============================
Media statements
==============================
@media screen and (min-width: 30em) and (orientation: landscape) {}
@media (min-height: 680px), screen and (orientation: portrait) {}
@media not all and (monochrome) {}
@media only screen {}
---
(stylesheet
(media_statement
(binary_query
(binary_query
(keyword_query)
(feature_query (feature_name) (integer_value (unit))))
(feature_query (feature_name) (plain_value)))
(block))
(media_statement
(feature_query (feature_name) (integer_value (unit)))
(binary_query (keyword_query) (feature_query (feature_name) (plain_value)))
(block))
(media_statement
(binary_query (unary_query (keyword_query)) (parenthesized_query (keyword_query)))
(block))
(media_statement (unary_query (keyword_query)) (block)))
==============================
Supports statements
==============================
@supports (animation-name: test) {
div { animation-name: test; }
}
@supports (transform-style: preserve) or (-moz-transform-style: preserve) {}
@supports not ((text-align-last: justify) or (-moz-text-align-last: justify)) {}
@supports not selector(:matches(a, b)) {}
---
(stylesheet
(supports_statement
(feature_query (feature_name) (plain_value))
(block
(rule_set (selectors (tag_name)) (block
(declaration (property_name) (plain_value))))))
(supports_statement
(binary_query
(feature_query (feature_name) (plain_value))
(feature_query (feature_name) (plain_value)))
(block))
(supports_statement
(unary_query (parenthesized_query (binary_query
(feature_query (feature_name) (plain_value))
(feature_query (feature_name) (plain_value)))))
(block))
(supports_statement
(unary_query (selector_query (pseudo_class_selector
(class_name)
(arguments (tag_name) (tag_name)))))
(block)))
==============================
Charset statements
==============================
@charset "utf-8";
---
(stylesheet
(charset_statement (string_value)))
==============================
Other at-statements
==============================
@font-face {
font-family: "Open Sans";
src: url("/a") format("woff2"), url("/b/c") format("woff");
}
---
(stylesheet
(at_rule
(at_keyword)
(block
(declaration (property_name) (string_value))
(declaration (property_name)
(call_expression (function_name) (arguments (string_value)))
(call_expression (function_name) (arguments (string_value)))
(call_expression (function_name) (arguments (string_value)))
(call_expression (function_name) (arguments (string_value)))))))

@ -0,0 +1,15 @@
============================
Rule sets
============================
#some-id {
some-property: 5px;
}
---
(stylesheet
(rule_set
(selectors (id_selector (id_name)))
(block
(declaration (property_name) (integer_value (unit))))))

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,366 @@
module.exports = grammar({
name: 'css',
extras: $ => [
/\s/,
$.comment,
],
externals: $ => [
$._descendant_operator,
],
conflicts: $ => [
[$._selector, $.declaration],
],
inline: $ => [
$._top_level_item,
$._block_item,
],
rules: {
stylesheet: $ => repeat($._top_level_item),
_top_level_item: $ => choice(
$.declaration,
$.rule_set,
$.import_statement,
$.media_statement,
$.charset_statement,
$.namespace_statement,
$.keyframes_statement,
$.supports_statement,
$.at_rule
),
// Statements
import_statement: $ => seq(
'@import',
$._value,
sep(',', $._query),
';'
),
media_statement: $ => seq(
'@media',
sep1(',', $._query),
$.block
),
charset_statement: $ => seq(
'@charset',
$._value,
';'
),
namespace_statement: $ => seq(
'@namespace',
optional(alias($.identifier, $.namespace_name)),
choice($.string_value, $.call_expression),
';'
),
keyframes_statement: $ => seq(
choice(
'@keyframes',
alias(/@[-a-z]+keyframes/, $.at_keyword)
),
alias($.identifier, $.keyframes_name),
$.keyframe_block_list,
),
keyframe_block_list: $ => seq(
'{',
repeat($.keyframe_block),
'}'
),
keyframe_block: $ => seq(
choice($.from, $.to, $.integer_value),
$.block
),
from: $ => 'from',
to: $ => 'to',
supports_statement: $ => seq(
'@supports',
$._query,
$.block
),
at_rule: $ => seq(
$.at_keyword,
sep(',', $._query),
choice(';', $.block)
),
// Rule sets
rule_set: $ => seq(
$.selectors,
$.block
),
selectors: $ => sep1(',', $._selector),
block: $ => seq('{',
repeat($._block_item),
optional(alias($.last_declaration, $.declaration)),
'}'
),
_block_item: $ => choice(
$.declaration,
$.rule_set,
$.import_statement,
$.media_statement,
$.charset_statement,
$.namespace_statement,
$.keyframes_statement,
$.supports_statement,
$.at_rule
),
// Selectors
_selector: $ => choice(
$.universal_selector,
alias($.identifier, $.tag_name),
$.class_selector,
$.nesting_selector,
$.pseudo_class_selector,
$.pseudo_element_selector,
$.id_selector,
$.attribute_selector,
$.string_value,
$.child_selector,
$.descendant_selector,
$.sibling_selector,
$.adjacent_sibling_selector
),
nesting_selector: $ => '&',
universal_selector: $ => '*',
class_selector: $ => prec(1, seq(
optional($._selector),
'.',
alias($.identifier, $.class_name),
)),
pseudo_class_selector: $ => seq(
optional($._selector),
':',
alias($.identifier, $.class_name),
optional(alias($.pseudo_class_arguments, $.arguments))
),
pseudo_element_selector: $ => seq(
optional($._selector),
'::',
alias($.identifier, $.tag_name)
),
id_selector: $ => seq(
optional($._selector),
'#',
alias($.identifier, $.id_name)
),
attribute_selector: $ => seq(
optional($._selector),
'[',
alias($.identifier, $.attribute_name),
optional(seq(
choice('=', '~=', '^=', '|=', '*=', '$='),
$._value
)),
']'
),
child_selector: $ => prec.left(seq($._selector, '>', $._selector)),
descendant_selector: $ => prec.left(seq($._selector, $._descendant_operator, $._selector)),
sibling_selector: $ => prec.left(seq($._selector, '~', $._selector)),
adjacent_sibling_selector: $ => prec.left(seq($._selector, '+', $._selector)),
pseudo_class_arguments: $ => seq(
token.immediate('('),
sep(',', choice($._selector, repeat1($._value))),
')'
),
// Declarations
declaration: $ => seq(
alias($.identifier, $.property_name),
':',
$._value,
repeat(seq(
optional(','),
$._value
)),
optional($.important),
';'
),
last_declaration: $ => prec(1, seq(
alias($.identifier, $.property_name),
':',
$._value,
repeat(seq(
optional(','),
$._value
)),
optional($.important)
)),
important: $ => '!important',
// Media queries
_query: $ => choice(
alias($.identifier, $.keyword_query),
$.feature_query,
$.binary_query,
$.unary_query,
$.selector_query,
$.parenthesized_query
),
feature_query: $ => seq(
'(',
alias($.identifier, $.feature_name),
':',
repeat1($._value),
')'
),
parenthesized_query: $ => seq(
'(',
$._query,
')'
),
binary_query: $ => prec.left(seq(
$._query,
choice('and', 'or'),
$._query
)),
unary_query: $ => prec(1, seq(
choice('not', 'only'),
$._query
)),
selector_query: $ => seq(
'selector',
'(',
$._selector,
')'
),
// Property Values
_value: $ => prec(-1, choice(
alias($.identifier, $.plain_value),
$.plain_value,
$.color_value,
$.integer_value,
$.float_value,
$.string_value,
$.binary_expression,
$.parenthesized_value,
$.call_expression
)),
parenthesized_value: $ => seq(
'(',
$._value,
')'
),
color_value: $ => seq('#', token.immediate(/[0-9a-fA-F]{3,8}/)),
string_value: $ => token(choice(
seq("'", /([^'\n]|\\(.|\n))*/, "'"),
seq('"', /([^"\n]|\\(.|\n))*/, '"')
)),
integer_value: $ => seq(
token(seq(
optional(choice('+', '-')),
/\d+/
)),
optional($.unit)
),
float_value: $ => seq(
token(seq(
optional(choice('+', '-')),
/\d*/,
choice(
seq('.', /\d+/),
seq(/[eE]/, optional('-'), /\d+/),
seq('.', /\d+/, /[eE]/, optional('-'), /\d+/)
)
)),
optional($.unit)
),
unit: $ => token.immediate(/[a-zA-Z%]+/),
call_expression: $ => seq(
alias($.identifier, $.function_name),
$.arguments
),
binary_expression: $ => prec.left(seq(
$._value,
choice('+', '-', '*', '/'),
$._value
)),
arguments: $ => seq(
token.immediate('('),
sep(choice(',', ';'), repeat1($._value)),
')'
),
identifier: $ => /(--|-?[a-zA-Z_])[a-zA-Z0-9-_]*/,
at_keyword: $ => /@[a-zA-Z-_]+/,
comment: $ => token(seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/'
)),
plain_value: $ => token(seq(
repeat(choice(
/[-_]/,
/\/[^\*\s,;!{}()\[\]]/ // Slash not followed by a '*' (which would be a comment)
)),
/[a-zA-Z]/,
repeat(choice(
/[^/\s,;!{}()\[\]]/, // Not a slash, not a delimiter character
/\/[^\*\s,;!{}()\[\]]/ // Slash not followed by a '*' (which would be a comment)
))
))
}
})
function sep (separator, rule) {
return optional(sep1(separator, rule))
}
function sep1 (separator, rule) {
return seq(rule, repeat(seq(separator, rule)))
}

@ -0,0 +1,31 @@
{
"name": "tree-sitter-css",
"version": "0.19.0",
"description": "CSS grammar for tree-sitter",
"main": "bindings/node",
"keywords": [
"parser",
"lexer"
],
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"nan": "^2.14.1"
},
"devDependencies": {
"tree-sitter-cli": "^0.19.1"
},
"scripts": {
"test": "tree-sitter test && tree-sitter parse examples/*.css --quiet --time",
"test-windows": "tree-sitter test"
},
"tree-sitter": [
{
"scope": "source.css",
"file-types": [
"css"
],
"injection-regex": "^css$"
}
]
}

@ -0,0 +1,64 @@
(comment) @comment
(tag_name) @tag
(nesting_selector) @tag
(universal_selector) @tag
"~" @operator
">" @operator
"+" @operator
"-" @operator
"*" @operator
"/" @operator
"=" @operator
"^=" @operator
"|=" @operator
"~=" @operator
"$=" @operator
"*=" @operator
"and" @operator
"or" @operator
"not" @operator
"only" @operator
(attribute_selector (plain_value) @string)
(pseudo_element_selector (tag_name) @attribute)
(pseudo_class_selector (class_name) @attribute)
(class_name) @property
(id_name) @property
(namespace_name) @property
(property_name) @property
(feature_name) @property
(attribute_name) @attribute
(function_name) @function
((property_name) @variable
(#match? @variable "^--"))
((plain_value) @variable
(#match? @variable "^--"))
"@media" @keyword
"@import" @keyword
"@charset" @keyword
"@namespace" @keyword
"@supports" @keyword
"@keyframes" @keyword
(at_keyword) @keyword
(to) @keyword
(from) @keyword
(important) @keyword
(string_value) @string
(color_value) @string.special
(integer_value) @number
(float_value) @number
(unit) @type
"#" @punctuation.delimiter
"," @punctuation.delimiter
":" @punctuation.delimiter

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,36 @@
#include <tree_sitter/parser.h>
#include <wctype.h>
enum TokenType {
DESCENDANT_OP,
};
void *tree_sitter_css_external_scanner_create() { return NULL; }
void tree_sitter_css_external_scanner_destroy(void *p) {}
void tree_sitter_css_external_scanner_reset(void *p) {}
unsigned tree_sitter_css_external_scanner_serialize(void *p, char *buffer) { return 0; }
void tree_sitter_css_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
bool tree_sitter_css_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
if (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
while (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
}
if (
lexer->lookahead == '#' ||
lexer->lookahead == '.' ||
lexer->lookahead == '[' ||
lexer->lookahead == ':' ||
lexer->lookahead == '-' ||
iswalnum(lexer->lookahead)
) {
lexer->result_symbol = DESCENDANT_OP;
return true;
}
}
return false;
}

@ -0,0 +1,223 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char **symbol_names;
const char **field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
};
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
bool skip = false; \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_