Skip to content

Commit

Permalink
get 960gs working again
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Feb 25, 2024
1 parent aae7c27 commit 31e214f
Show file tree
Hide file tree
Showing 53 changed files with 1,052 additions and 215 deletions.
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/hdx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hdx_atom = { workspace = true }
hdx_derive = { workspace = true }

clap = { workspace = true, features = ["derive", "cargo"] }
miette = { workspace = true }

# Use OXC Allocator until https://github.com/fitzgen/bumpalo/pull/210 is resolved
oxc_allocator = { workspace = true }
Expand All @@ -28,5 +29,6 @@ serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

[features]
default = []
serde = ["dep:serde", "dep:serde_json", "hdx_lexer/serde"]
default = ["fancy"]
serde = ["dep:serde", "dep:serde_json", "hdx_lexer/serde"]
fancy = ["hdx_ast/fancy", "hdx_parser/fancy", "miette/fancy"]
21 changes: 13 additions & 8 deletions crates/hdx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use hdx_ast::css::StyleSheet;
use hdx_writer::{BaseCssWriter, WriteCss};
use miette::{NamedSource, GraphicalReportHandler, GraphicalTheme};
use oxc_allocator::Allocator;

#[derive(Debug, Parser)]
Expand All @@ -25,14 +26,11 @@ fn main() {
todo!("Can't handle multiple files yet")
}

let source_text = std::fs::read_to_string(args.input.first().unwrap()).unwrap();
let file_name = args.input.first().unwrap();
let source_text = std::fs::read_to_string(file_name).unwrap();
let allocator = Allocator::default();
let result = hdx_parser::Parser::new(
&allocator,
source_text.as_str(),
hdx_parser::Features::default(),
)
.parse_with::<StyleSheet>();
let result = hdx_parser::Parser::new(&allocator, source_text.as_str(), hdx_parser::Features::default())
.parse_with::<StyleSheet>();
{
let start = std::time::Instant::now();
let mut str = String::new();
Expand All @@ -46,7 +44,14 @@ fn main() {
eprintln!("Slurped up CSS in {:?}! Neat!", start.elapsed());
}
} else {
eprintln!("{:?}", result.errors);
let handler = GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor());
for err in result.errors {
let mut report = String::new();
let named = NamedSource::new(file_name, source_text.clone());
let err = err.with_source_code(named);
handler.render_report(&mut report, err.as_ref()).unwrap();
println!("{}", report);
}
}
}
}
1 change: 1 addition & 0 deletions crates/hdx_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ serde_json = { workspace = true }
[features]
default = []
serde = ["dep:serde", "dep:serde_json", "hdx_lexer/serde"]
fancy = ["miette/fancy-no-backtrace"]
96 changes: 88 additions & 8 deletions crates/hdx_ast/src/css/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ impl<'a> Parse<'a> for Custom<'a> {
}
}

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct Computed<'a> {
pub value: Box<'a, Spanned<ComponentValues<'a>>>,
// pub value_like: Spanned<values::ValueLike<'a>>,
}

impl<'a> WriteCss<'a> for Computed<'a> {
fn write_css<W: CssWriter>(&self, sink: &mut W) -> WriterResult {
self.value.write_css(sink)
}
}

impl<'a> Parse<'a> for Computed<'a> {
fn parse(parser: &mut Parser<'a>) -> ParserResult<Spanned<Self>> {
let span = parser.span();
parser.set(State::StopOnSemicolon);
let value = ComponentValues::parse(parser)?;
parser.unset(State::StopOnSemicolon);
Ok(Self { value: parser.boxup(value) }.spanned(span.end(parser.pos())))
}
}

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct Unknown<'a> {
Expand Down Expand Up @@ -75,6 +98,37 @@ impl<'a> WriteCss<'a> for StyleProperty<'a> {
}
}

#[inline]
fn is_computed_token(token: Token) -> bool {
match token {
Token::Function(atom) => match atom.to_ascii_lowercase() {
atom!("var")
| atom!("calc")
| atom!("min")
| atom!("max")
| atom!("clamp")
| atom!("round")
| atom!("mod")
| atom!("rem")
| atom!("sin")
| atom!("cos")
| atom!("tan")
| atom!("asin")
| atom!("atan")
| atom!("atan2")
| atom!("pow")
| atom!("sqrt")
| atom!("hypot")
| atom!("log")
| atom!("exp")
| atom!("abs")
| atom!("sign") => true,
_ => false,
},
_ => false,
}
}

macro_rules! properties {
( $(
$name: ident$(<$a: lifetime>)?: $atom: pat,
Expand All @@ -88,6 +142,7 @@ macro_rules! properties {
Revert,
RevertLayer,
Custom(Box<'a, Spanned<Custom<'a>>>),
Computed(Box<'a, Spanned<Computed<'a>>>),
Unknown(Box<'a, Spanned<Unknown<'a>>>),
$(
$name(Box<'a, Spanned<values::$name$(<$a>)?>>),
Expand All @@ -104,6 +159,7 @@ macro_rules! properties {
Self::RevertLayer => sink.write_str("revert-layer")?,
Self::Custom(v) => v.write_css(sink)?,
Self::Unknown(v) => v.write_css(sink)?,
Self::Computed(v) => v.write_css(sink)?,
$(
Self::$name(v) => v.write_css(sink)?,
)+
Expand Down Expand Up @@ -147,13 +203,36 @@ macro_rules! properties {
StyleValue::RevertLayer
},
_ => {
let parsed = values::$name::parse(parser)?;
StyleValue::$name(parser.boxup(parsed))
let checkpoint = parser.checkpoint();
if let Ok(value) = values::$name::parse(parser) {
StyleValue::$name(parser.boxup(value))
} else if is_computed_token(parser.cur()) {
parser.rewind(checkpoint);
let value = Computed::parse(parser)?;
StyleValue::Computed(parser.boxup(value))
} else {
parser.rewind(checkpoint);
unexpected!(parser)
}
}
},
_ => {
let parsed = values::$name::parse(parser)?;
StyleValue::$name(parser.boxup(parsed))
if is_computed_token(parser.cur()) {
let value = Computed::parse(parser)?;
StyleValue::Computed(parser.boxup(value))
} else {
let checkpoint = parser.checkpoint();
if let Ok(value) = values::$name::parse(parser) {
StyleValue::$name(parser.boxup(value))
} else if is_computed_token(parser.cur()) {
parser.rewind(checkpoint);
let value = Computed::parse(parser)?;
StyleValue::Computed(parser.boxup(value))
} else {
parser.rewind(checkpoint);
unexpected!(parser)
}
}
}
};
(name, value)
Expand Down Expand Up @@ -1276,10 +1355,10 @@ properties! {
Zoom: atom!("zoom"),

// Webkit NonStandards
WebkitTextSizeAdjust: atom!("-webkit-text-size-adjust"),
WebkitTextDecoration: atom!("-webkit-text-decoration"),
WebkitTapHighlightColor: atom!("-webkit-tap-highlight-color"),
WebkitTextDecorationSkipInk: atom!("-webkit-text-decoration-skip-ink"),
// WebkitTextSizeAdjust: atom!("-webkit-text-size-adjust"),
// WebkitTextDecoration: atom!("-webkit-text-decoration"),
// WebkitTapHighlightColor: atom!("-webkit-tap-highlight-color"),
// WebkitTextDecorationSkipInk: atom!("-webkit-text-decoration-skip-ink"),
}

#[cfg(test)]
Expand All @@ -1300,5 +1379,6 @@ mod tests {
fn test_writes() {
let allocator = Allocator::default();
test_write::<StyleProperty>(&allocator, "width: 1px", "width:1px");
test_write::<StyleProperty>(&allocator, "width: min(1px, 2px)", "width:min(1px, 2px)");
}
}
1 change: 1 addition & 0 deletions crates/hdx_ast/src/css/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct Selector<'a> {

impl<'a> Parse<'a> for Selector<'a> {
fn parse(parser: &mut Parser<'a>) -> ParserResult<Spanned<Self>> {
discard!(parser, Token::Whitespace);
let span = parser.span();
let mut components: Vec<'a, Spanned<Component>> = parser.new_vec();
loop {
Expand Down
1 change: 0 additions & 1 deletion crates/hdx_ast/src/css/values/box/margin.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/hdx_ast/src/css/values/box/margin_bottom.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/hdx_ast/src/css/values/box/margin_left.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/hdx_ast/src/css/values/box/margin_right.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/hdx_ast/src/css/values/box/margin_top.rs

This file was deleted.

7 changes: 5 additions & 2 deletions crates/hdx_ast/src/css/values/box/margin_trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use hdx_writer::{CssWriter, Result as WriterResult, WriteCss};
#[cfg(feature = "serde")]
use serde::Serialize;

use crate::{bitmask, Atomizable};
use crate::{bitmask, Atomizable, Value};

// https://drafts.csswg.org/css-box-4/#propdef-margin-trim
#[derive(Atomizable)]
#[derive(Atomizable, Default)]
#[bitmask(u8)]
#[cfg_attr(feature = "serde", derive(Serialize), serde())]
pub enum MarginTrim {
#[default]
None,
Block,
Inline,
Expand All @@ -21,6 +22,8 @@ pub enum MarginTrim {
InlineEnd,
}

impl Value for MarginTrim {}

impl<'a> Parse<'a> for MarginTrim {
fn parse(parser: &mut Parser<'a>) -> ParserResult<Spanned<Self>> {
let span = parser.span();
Expand Down
Loading

0 comments on commit 31e214f

Please sign in to comment.