Skip to content

Commit 0ec50a7

Browse files
authored
Merge pull request #838 from jaytaph/css-syntax
Fixed css syntax where literals where parsed as tokens
2 parents e0daf88 + 1f4fca1 commit 0ec50a7

File tree

11 files changed

+9136
-8205
lines changed

11 files changed

+9136
-8205
lines changed

crates/gosub_css3/resources/definitions/definitions.json

Lines changed: 4524 additions & 4108 deletions
Large diffs are not rendered by default.

crates/gosub_css3/resources/definitions/definitions_at-rules.json

Lines changed: 186 additions & 191 deletions
Large diffs are not rendered by default.

crates/gosub_css3/resources/definitions/definitions_properties.json

Lines changed: 3415 additions & 3069 deletions
Large diffs are not rendered by default.

crates/gosub_css3/resources/definitions/definitions_selectors.json

Lines changed: 166 additions & 103 deletions
Large diffs are not rendered by default.

crates/gosub_css3/resources/definitions/definitions_values.json

Lines changed: 724 additions & 712 deletions
Large diffs are not rendered by default.

crates/gosub_css3/src/matcher/property_definitions.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::stylesheet::CssValue;
1212

1313
/// List of elements that are built-in data types in the CSS specification. These will be handled
1414
/// by the syntax matcher as built-in types.
15-
const BUILTIN_DATA_TYPES: [&str; 41] = [
15+
const BUILTIN_DATA_TYPES: [&str; 43] = [
1616
"absolute-size",
1717
"age",
1818
"angle",
@@ -54,6 +54,8 @@ const BUILTIN_DATA_TYPES: [&str; 41] = [
5454
"color()",
5555
"attr()", //TODO: this is not a builtin!
5656
"element()", //TODO: this is not a builtin!
57+
"dynamic-range-limit-mix()",
58+
"palette-mix()",
5759
];
5860

5961
/// A CSS property definition including its type and initial value and optional expanded values if it's a shorthand property
@@ -584,7 +586,12 @@ mod tests {
584586

585587
#[test]
586588
fn test_parse_definition_file() {
587-
assert_eq!(CSS_DEFINITIONS.len(), 620);
589+
// set the log level to WARN
590+
simple_logger::SimpleLogger::new()
591+
.with_level(log::LevelFilter::Warn)
592+
.init()
593+
.unwrap();
594+
assert_eq!(CSS_DEFINITIONS.len(), 656);
588595
}
589596

590597
#[test]

crates/gosub_css3/src/matcher/syntax.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use nom::character::complete::{alpha1, alphanumeric1, char, digit0, digit1, mult
77
use nom::combinator::{map, map_res, opt, recognize};
88
use nom::multi::{fold_many1, many0, many1, separated_list0, separated_list1};
99
use nom::number::complete::float;
10-
use nom::sequence::{delimited, pair, preceded, separated_pair};
10+
use nom::sequence::{delimited, pair, preceded, separated_pair, terminated};
1111
use nom::Err;
1212
use nom::IResult;
1313
use nom::Parser;
@@ -18,7 +18,7 @@ use crate::stylesheet::CssValue;
1818
// When debugging the parser, it's nice to have some additional information ready. This should maybe
1919
// be inside a cfg setting, but for now (un)commenting the appropriate line is good enough.
2020
macro_rules! debug_print {
21-
// ($($x:tt)*) => { println!($($x)*) }
21+
// ($($x:tt)*) => { println!($($x)*) };
2222
($($x:tt)*) => {{}};
2323
}
2424

@@ -341,8 +341,9 @@ fn parse_curly_braces_multiplier(input: &str) -> IResult<&str, SyntaxComponentMu
341341

342342
fn parse_comma_separated_multiplier(input: &str) -> IResult<&str, SyntaxComponentMultiplier> {
343343
let range = alt((
344-
separated_pair(ws(integer), ws(tag(",")), ws(integer)),
345-
map(ws(integer), |num| (num, num)),
344+
separated_pair(ws(integer), ws(tag(",")), ws(integer)), // "2,5"
345+
map(terminated(ws(integer), ws(tag(","))), |num| (num, u32::MAX)), // "3,"
346+
map(ws(integer), |num| (num, num)), // "3"
346347
));
347348

348349
let (input, minmax) = alt((
@@ -367,8 +368,8 @@ fn parse_multipliers(input: &str) -> IResult<&str, Vec<SyntaxComponentMultiplier
367368
map(tag("+"), |_| SyntaxComponentMultiplier::OneOrMore),
368369
map(tag("?"), |_| SyntaxComponentMultiplier::Optional),
369370
map(tag("!"), |_| SyntaxComponentMultiplier::AtLeastOneValue),
370-
parse_comma_separated_multiplier,
371-
parse_curly_braces_multiplier,
371+
parse_comma_separated_multiplier, // Deals with # and #{x,y}
372+
parse_curly_braces_multiplier, // Deals with {x,y}
372373
)))
373374
.parse(input)?;
374375

@@ -794,7 +795,7 @@ fn parse_component(input: &str) -> IResult<&str, SyntaxComponent> {
794795

795796
component.update_multipliers(multipliers.clone());
796797

797-
debug_print!("<- Parsed component_type: {:#?} {}", component, multipliers);
798+
debug_print!("<- Parsed component_type: {:#?} {:?}", component, multipliers);
798799

799800
Ok((input, component))
800801
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.css_cache
2+
.output

crates/gosub_css3/tools/generate_definitions/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package main
22

33
import (
44
"bytes"
5+
"cmp"
56
"encoding/json"
67
"generate_definitions/mdn"
78
"generate_definitions/utils"
89
"generate_definitions/webref"
910
"log"
1011
"os"
1112
"path"
13+
"slices"
1214
)
1315

1416
type ExportType int
@@ -21,7 +23,7 @@ const (
2123

2224
const (
2325
exportType = Both
24-
ResourcePath = "crates/gosub_css3/resources/definitions"
26+
ResourcePath = ".output/definitions"
2527
SingleFilePath = ResourcePath + "/definitions.json"
2628
MultiFileDir = ResourcePath
2729
MultiFilePrefix = "definitions_"
@@ -109,6 +111,20 @@ func main() {
109111
len(data.Properties), len(data.Values), len(data.AtRules), len(data.Selectors),
110112
)
111113

114+
// Sort elements, so that the output is deterministic and we have less issues with version control
115+
slices.SortFunc(data.Properties, func(a, b utils.Property) int {
116+
return cmp.Compare(a.Name, b.Name)
117+
})
118+
slices.SortFunc(data.Values, func(a, b utils.Value) int {
119+
return cmp.Compare(a.Name, b.Name)
120+
})
121+
slices.SortFunc(data.AtRules, func(a, b utils.AtRule) int {
122+
return cmp.Compare(a.Name, b.Name)
123+
})
124+
slices.SortFunc(data.Selectors, func(a, b utils.Selector) int {
125+
return cmp.Compare(a.Name, b.Name)
126+
})
127+
112128
switch exportType {
113129
case SingleFile:
114130
ExportSingleFile(&data)
@@ -140,7 +156,6 @@ func ExportMultiFile(data *utils.Data) {
140156
ExportData(data.Values, path.Join(MultiFileDir, MultiFilePrefix+"values.json"))
141157
ExportData(data.AtRules, path.Join(MultiFileDir, MultiFilePrefix+"at-rules.json"))
142158
ExportData(data.Selectors, path.Join(MultiFileDir, MultiFilePrefix+"selectors.json"))
143-
144159
}
145160

146161
func ExportData(data any, path string) {

crates/gosub_css3/tools/generate_definitions/utils/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const (
1010
REPO = "w3c/webref"
1111
LOCATION = "ed/css"
1212
PATCH_LOCATION = "ed/csspatches"
13-
CACHE_DIR = "crates/gosub_css3/resources/cache"
13+
CACHE_DIR = ".css_cache"
1414
CACHE_INDEX_FILE = CACHE_DIR + "/index/cache_index.json"
15-
CUSTOM_PATCH_DIR = "crates/gosub_css3/resources/patches"
15+
CUSTOM_PATCH_DIR = ".css_cache/patches"
1616
BRANCH = "curated"
1717
)
1818

0 commit comments

Comments
 (0)