-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use hdx_ast::css::values::{ | ||
Expr, Shorthand, TextWrapValue, WhiteSpaceCollapseValue, WhiteSpaceShorthand, | ||
WhiteSpaceTrimValue, | ||
}; | ||
use hdx_lexer::Kind; | ||
|
||
use crate::{atom, diagnostics, Atomizable, Parse, Parser, Result, Spanned}; | ||
|
||
impl<'a> Parse<'a> for WhiteSpaceTrimValue { | ||
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> { | ||
let span = parser.cur().span; | ||
let mut inner = false; | ||
let mut after = false; | ||
let mut before = false; | ||
loop { | ||
match parser.cur().kind { | ||
Kind::Ident => match parser.cur_atom_lower().unwrap() { | ||
atom!("none") => { | ||
parser.advance(); | ||
return Ok(Self::None.spanned(span)); | ||
} | ||
atom!("discard-inner") => { | ||
parser.advance(); | ||
inner = true; | ||
} | ||
atom!("discard-after") => { | ||
parser.advance(); | ||
after = true; | ||
} | ||
atom!("discard-before") => { | ||
parser.advance(); | ||
before = true; | ||
} | ||
_ => break, | ||
}, | ||
_ => break, | ||
} | ||
if inner && after && before { | ||
break; | ||
} | ||
} | ||
Ok(Self::Discard { inner, after, before }.spanned(span.up_to(&parser.cur().span))) | ||
} | ||
} | ||
|
||
impl<'a> Parse<'a> for WhiteSpaceShorthand<'a> { | ||
fn parse(parser: &mut Parser<'a>) -> Result<Spanned<Self>> { | ||
let span = parser.cur().span; | ||
if parser.at(Kind::Ident) { | ||
match parser.cur_atom_lower().unwrap() { | ||
//normal | pre | nowrap | pre-wrap | pre-line | ||
atom!("normal") => { | ||
parser.advance(); | ||
return Ok(Self::Normal.spanned(span)); | ||
} | ||
atom!("pre") => { | ||
parser.advance(); | ||
return Ok(Self::Pre.spanned(span)); | ||
} | ||
atom!("nowrap") => { | ||
parser.advance(); | ||
return Ok(Self::Nowrap.spanned(span)); | ||
} | ||
atom!("pre-wrap") => { | ||
parser.advance(); | ||
return Ok(Self::PreWrap.spanned(span)); | ||
} | ||
atom!("pre-line") => { | ||
parser.advance(); | ||
return Ok(Self::PreLine.spanned(span)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
let mut collapse = Shorthand::Implicit; | ||
let mut wrap = Shorthand::Implicit; | ||
let mut trim = Shorthand::Implicit; | ||
loop { | ||
match parser.cur().kind { | ||
Kind::Semicolon | Kind::Comma | Kind::Eof => { | ||
break; | ||
} | ||
Kind::Ident => { | ||
let ident = parser.cur_atom_lower().unwrap(); | ||
if collapse.is_implicit() | ||
&& WhiteSpaceCollapseValue::from_atom(ident.clone()).is_some() | ||
{ | ||
let node = Expr::<WhiteSpaceCollapseValue>::parse(parser)?; | ||
collapse = Shorthand::Explicit(parser.boxup(node)); | ||
} else if wrap.is_implicit() | ||
&& TextWrapValue::from_atom(ident.clone()).is_some() | ||
{ | ||
let node = Expr::<TextWrapValue>::parse(parser)?; | ||
wrap = Shorthand::Explicit(parser.boxup(node)); | ||
} else if trim.is_implicit() | ||
&& matches!( | ||
ident, | ||
atom!("none") | ||
| atom!("discard-inner") | atom!("discard-after") | ||
| atom!("discard-before") | ||
) { | ||
let node = Expr::<WhiteSpaceTrimValue>::parse(parser)?; | ||
trim = Shorthand::Explicit(parser.boxup(node)); | ||
} else { | ||
Err(diagnostics::UnexpectedIdent(ident.clone(), parser.cur().span))? | ||
} | ||
} | ||
k => { | ||
let checkpoint = parser.checkpoint(); | ||
if collapse.is_implicit() { | ||
let node = Expr::<WhiteSpaceCollapseValue>::parse(parser); | ||
match node { | ||
Ok(node) => { | ||
collapse = Shorthand::Explicit(parser.boxup(node)); | ||
continue; | ||
} | ||
Err(_) => parser.rewind(checkpoint), | ||
} | ||
} | ||
let checkpoint = parser.checkpoint(); | ||
if wrap.is_implicit() { | ||
let node = Expr::<TextWrapValue>::parse(parser); | ||
match node { | ||
Ok(node) => { | ||
wrap = Shorthand::Explicit(parser.boxup(node)); | ||
continue; | ||
} | ||
Err(_) => parser.rewind(checkpoint), | ||
} | ||
} | ||
let checkpoint = parser.checkpoint(); | ||
if trim.is_implicit() { | ||
let node = Expr::<WhiteSpaceTrimValue>::parse(parser); | ||
match node { | ||
Ok(node) => { | ||
trim = Shorthand::Explicit(parser.boxup(node)); | ||
continue; | ||
} | ||
Err(_) => parser.rewind(checkpoint), | ||
} | ||
} | ||
Err(diagnostics::Unexpected(k, parser.cur().span))? | ||
} | ||
} | ||
if collapse.is_explicit() && wrap.is_explicit() && trim.is_explicit() { | ||
break; | ||
} | ||
} | ||
Ok(Self::Expanded { collapse, wrap, trim }.spanned(span.up_to(&parser.cur().span))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use hdx_ast::css::values::{text::*, Shorthand}; | ||
|
||
use crate::{CssWriter, Result, WriteCss}; | ||
|
||
impl<'a> WriteCss<'a> for WhiteSpaceTrimValue { | ||
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result { | ||
match self { | ||
Self::None => sink.write_str("none"), | ||
Self::Discard { inner, after, before } => { | ||
if *inner { | ||
sink.write_str("discard-inner")?; | ||
if *before { | ||
sink.write_char(' ')?; | ||
} | ||
} | ||
if *before { | ||
sink.write_str("discard-before")?; | ||
if *after { | ||
sink.write_char(' ')?; | ||
} | ||
} | ||
if *after { | ||
sink.write_str("discard-after")?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a> WriteCss<'a> for WhiteSpaceShorthand<'a> { | ||
fn write_css<W: CssWriter>(&self, sink: &mut W) -> Result { | ||
match self { | ||
Self::Normal => sink.write_str("normal")?, | ||
Self::Pre => sink.write_str("pre")?, | ||
Self::Nowrap => sink.write_str("nowrap")?, | ||
Self::PreWrap => sink.write_str("pre-wrap")?, | ||
Self::PreLine => sink.write_str("pre-line")?, | ||
Self::Expanded { collapse, trim, wrap } => { | ||
if let Shorthand::Explicit(value) = collapse { | ||
value.write_css(sink)?; | ||
if trim.is_explicit() { | ||
sink.write_char(' ')?; | ||
} | ||
} | ||
if let Shorthand::Explicit(value) = trim { | ||
value.write_css(sink)?; | ||
if wrap.is_explicit() { | ||
sink.write_char(' ')?; | ||
} | ||
} | ||
if let Shorthand::Explicit(value) = wrap { | ||
value.write_css(sink)?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |