Skip to content

Commit

Permalink
Auto merge of #257 - Eijebong:syn-1.0, r=jdm
Browse files Browse the repository at this point in the history
Update syn related dependencies to 1.0 and bump version

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/257)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Aug 15, 2019
2 parents 088093f + f37a09e commit 6c69040
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,7 @@ rust:
- nightly
- beta
- stable
- 1.30.0
- 1.31.0

script:
- cargo build --verbose
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.25.8"
version = "0.25.9"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down Expand Up @@ -31,9 +31,9 @@ smallvec = "0.6"

[build-dependencies]
autocfg = "0.1.4"
syn = { version = "0.15.12", features = ["extra-traits", "fold", "full"] }
quote = "0.6"
proc-macro2 = "0.4"
syn = { version = "1", features = ["extra-traits", "fold", "full"] }
quote = "1"
proc-macro2 = "1"

[features]
bench = []
Expand Down
94 changes: 51 additions & 43 deletions build/match_byte.rs
Expand Up @@ -72,6 +72,54 @@ fn get_byte_from_expr_lit(expr: &Box<syn::Expr>) -> u8 {
}
}

/// Parse a pattern and fill the table accordingly
fn parse_pat_to_table<'a>(pat: &'a syn::Pat, case_id: u8, wildcard: &mut Option<&'a syn::Ident>, table: &mut [u8; 256]) {
match pat {
&syn::Pat::Lit(syn::PatLit { ref expr, .. }) => {
let value = get_byte_from_expr_lit(expr);
if table[value as usize] == 0 {
table[value as usize] = case_id;
}
}
&syn::Pat::Range(syn::PatRange { ref lo, ref hi, .. }) => {
let lo = get_byte_from_expr_lit(lo);
let hi = get_byte_from_expr_lit(hi);
for value in lo..hi {
if table[value as usize] == 0 {
table[value as usize] = case_id;
}
}
if table[hi as usize] == 0 {
table[hi as usize] = case_id;
}
}
&syn::Pat::Wild(_) => {
for byte in table.iter_mut() {
if *byte == 0 {
*byte = case_id;
}
}
}
&syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
assert_eq!(*wildcard, None);
*wildcard = Some(ident);
for byte in table.iter_mut() {
if *byte == 0 {
*byte = case_id;
}
}
},
&syn::Pat::Or(syn::PatOr { ref cases, .. }) => {
for case in cases {
parse_pat_to_table(case, case_id, wildcard, table);
}
}
_ => {
panic!("Unexpected pattern: {:?}. Buggy code ?", pat);
}
}
}

/// Expand a TokenStream corresponding to the `match_byte` macro.
///
/// ## Example
Expand All @@ -97,48 +145,8 @@ fn expand_match_byte(body: &TokenStream) -> syn::Expr {
let case_id = i + 1;
let index = case_id as isize;
let name = syn::Ident::new(&format!("Case{}", case_id), Span::call_site());
parse_pat_to_table(&arm.pat, case_id as u8, &mut wildcard, &mut table);

for pat in &arm.pats {
match pat {
&syn::Pat::Lit(syn::PatLit { ref expr }) => {
let value = get_byte_from_expr_lit(expr);
if table[value as usize] == 0 {
table[value as usize] = case_id as u8;
}
}
&syn::Pat::Range(syn::PatRange { ref lo, ref hi, .. }) => {
let lo = get_byte_from_expr_lit(lo);
let hi = get_byte_from_expr_lit(hi);
for value in lo..hi {
if table[value as usize] == 0 {
table[value as usize] = case_id as u8;
}
}
if table[hi as usize] == 0 {
table[hi as usize] = case_id as u8;
}
}
&syn::Pat::Wild(_) => {
for byte in table.iter_mut() {
if *byte == 0 {
*byte = case_id as u8;
}
}
}
&syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
assert_eq!(wildcard, None);
wildcard = Some(ident);
for byte in table.iter_mut() {
if *byte == 0 {
*byte = case_id as u8;
}
}
}
_ => {
panic!("Unexpected pattern: {:?}. Buggy code ?", pat);
}
}
}
cases.push(quote!(#name = #index));
let body = &arm.body;
match_body.push(quote!(Case::#name => { #body }))
Expand Down Expand Up @@ -170,7 +178,7 @@ impl Fold for MatchByteParser {
if mac.path == parse_quote!(match_byte) {
return syn::fold::fold_stmt(
self,
syn::Stmt::Expr(expand_match_byte(&mac.tts)),
syn::Stmt::Expr(expand_match_byte(&mac.tokens)),
);
}
}
Expand All @@ -184,7 +192,7 @@ impl Fold for MatchByteParser {
match expr {
syn::Expr::Macro(syn::ExprMacro { ref mac, .. }) => {
if mac.path == parse_quote!(match_byte) {
return syn::fold::fold_expr(self, expand_match_byte(&mac.tts));
return syn::fold::fold_expr(self, expand_match_byte(&mac.tokens));
}
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions macros/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cssparser-macros"
version = "0.3.5"
version = "0.3.6"
authors = ["Simon Sapin <simon.sapin@exyr.org>"]
description = "Procedural macros for cssparser"
documentation = "https://docs.rs/cssparser-macros/"
Expand All @@ -14,6 +14,6 @@ proc-macro = true
[dependencies]
procedural-masquerade = {path = "../procedural-masquerade", version = "0.1"}
phf_codegen = "0.7"
quote = "0.6"
syn = {version = "0.15.12", features = ["full", "extra-traits"]}
proc-macro2 = "0.4"
quote = "1"
syn = {version = "1", features = ["full", "extra-traits"]}
proc-macro2 = "1"
7 changes: 6 additions & 1 deletion macros/lib.rs
Expand Up @@ -29,7 +29,12 @@ define_proc_macros! {
syn::Expr::Match(syn::ExprMatch { arms, .. }) => arms,
_ => panic!("expected a match expression, got {:?}", expr)
};
max_len(arms.into_iter().flat_map(|arm| arm.pats).filter_map(|pattern| {
max_len(arms.into_iter().flat_map(|ref arm| {
match arm.pat {
syn::Pat::Or(ref p) => p.cases.iter().cloned().collect(),
ref p => vec![p.clone()]
}
}).filter_map(|pattern| {
let expr = match pattern {
syn::Pat::Lit(expr) => expr,
syn::Pat::Wild(_) => return None,
Expand Down

0 comments on commit 6c69040

Please sign in to comment.