Allow top-level declarations

This commit is contained in:
Max Brunsfeld 2018-10-30 15:54:37 -07:00
parent c63a7cd74a
commit 53d1095d4a
4 changed files with 3297 additions and 3232 deletions

View File

@ -229,3 +229,16 @@ a {
(integer_value (unit)) (integer_value (unit))
(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) (plain_value))
(declaration (property_name) (call_expression (function_name) (arguments (binary_expression (integer_value (unit)) (call_expression (function_name) (arguments (plain_value))))))))

View File

@ -10,6 +10,10 @@ module.exports = grammar({
$._descendant_operator, $._descendant_operator,
], ],
conflicts: $ => [
[$._selector, $.declaration],
],
inline: $ => [ inline: $ => [
$._top_level_item, $._top_level_item,
$._block_item, $._block_item,
@ -19,6 +23,7 @@ module.exports = grammar({
stylesheet: $ => repeat($._top_level_item), stylesheet: $ => repeat($._top_level_item),
_top_level_item: $ => choice( _top_level_item: $ => choice(
$.declaration,
$.rule_set, $.rule_set,
$.import_statement, $.import_statement,
$.media_statement, $.media_statement,
@ -109,7 +114,14 @@ module.exports = grammar({
_block_item: $ => choice( _block_item: $ => choice(
$.declaration, $.declaration,
$._top_level_item $.rule_set,
$.import_statement,
$.media_statement,
$.charset_statement,
$.namespace_statement,
$.keyframes_statement,
$.supports_statement,
$.at_rule
), ),
// Selectors // Selectors
@ -186,7 +198,7 @@ module.exports = grammar({
// Declarations // Declarations
declaration: $ => prec(1, seq( declaration: $ => seq(
alias($.identifier, $.property_name), alias($.identifier, $.property_name),
':', ':',
$._value, $._value,
@ -196,7 +208,7 @@ module.exports = grammar({
)), )),
optional($.important), optional($.important),
';' ';'
)), ),
last_declaration: $ => prec(1, seq( last_declaration: $ => prec(1, seq(
alias($.identifier, $.property_name), alias($.identifier, $.property_name),

147
src/grammar.json vendored
View File

@ -11,6 +11,10 @@
"_top_level_item": { "_top_level_item": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "declaration"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "rule_set" "name": "rule_set"
@ -439,7 +443,35 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_top_level_item" "name": "rule_set"
},
{
"type": "SYMBOL",
"name": "import_statement"
},
{
"type": "SYMBOL",
"name": "media_statement"
},
{
"type": "SYMBOL",
"name": "charset_statement"
},
{
"type": "SYMBOL",
"name": "namespace_statement"
},
{
"type": "SYMBOL",
"name": "keyframes_statement"
},
{
"type": "SYMBOL",
"name": "supports_statement"
},
{
"type": "SYMBOL",
"name": "at_rule"
} }
] ]
}, },
@ -892,70 +924,66 @@
] ]
}, },
"declaration": { "declaration": {
"type": "PREC", "type": "SEQ",
"value": 1, "members": [
"content": { {
"type": "SEQ", "type": "ALIAS",
"members": [ "content": {
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "identifier"
},
"named": true,
"value": "property_name"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "_value" "name": "identifier"
}, },
{ "named": true,
"type": "REPEAT", "value": "property_name"
"content": { },
"type": "SEQ", {
"members": [ "type": "STRING",
{ "value": ":"
"type": "CHOICE", },
"members": [ {
{ "type": "SYMBOL",
"type": "STRING", "name": "_value"
"value": "," },
}, {
{ "type": "REPEAT",
"type": "BLANK" "content": {
} "type": "SEQ",
]
},
{
"type": "SYMBOL",
"name": "_value"
}
]
}
},
{
"type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "CHOICE",
"name": "important" "members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}, },
{ {
"type": "BLANK" "type": "SYMBOL",
"name": "_value"
} }
] ]
},
{
"type": "STRING",
"value": ";"
} }
] },
} {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "important"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ";"
}
]
}, },
"last_declaration": { "last_declaration": {
"type": "PREC", "type": "PREC",
@ -1695,7 +1723,12 @@
"name": "comment" "name": "comment"
} }
], ],
"conflicts": [], "conflicts": [
[
"_selector",
"declaration"
]
],
"externals": [ "externals": [
{ {
"type": "SYMBOL", "type": "SYMBOL",

6351
src/parser.c vendored

File diff suppressed because it is too large Load Diff