forked from mirrors/gecko-dev
This aligns us with Servo. It pulls a new `rand` crate version, but I'm removing one in https://phabricator.services.mozilla.com/D55351, so hopefully adds up :) Differential Revision: https://phabricator.services.mozilla.com/D55359 --HG-- rename : third_party/rust/cssparser-macros/Cargo.toml => third_party/rust/phf_macros/Cargo.toml rename : third_party/rust/cssparser-macros/Cargo.toml => third_party/rust/proc-macro-hack/Cargo.toml rename : third_party/rust/rand/.cargo-checksum.json => third_party/rust/rand-0.6.5/.cargo-checksum.json rename : third_party/rust/rand/CHANGELOG.md => third_party/rust/rand-0.6.5/CHANGELOG.md rename : third_party/rust/rand/Cargo.toml => third_party/rust/rand-0.6.5/Cargo.toml rename : third_party/rust/rand/README.md => third_party/rust/rand-0.6.5/README.md rename : third_party/rust/rand/benches/distributions.rs => third_party/rust/rand-0.6.5/benches/distributions.rs rename : third_party/rust/rand/benches/generators.rs => third_party/rust/rand-0.6.5/benches/generators.rs rename : third_party/rust/rand/benches/misc.rs => third_party/rust/rand-0.6.5/benches/misc.rs rename : third_party/rust/rand/benches/seq.rs => third_party/rust/rand-0.6.5/benches/seq.rs rename : third_party/rust/rand/build.rs => third_party/rust/rand-0.6.5/build.rs rename : third_party/rust/rand/examples/monte-carlo.rs => third_party/rust/rand-0.6.5/examples/monte-carlo.rs rename : third_party/rust/rand/examples/monty-hall.rs => third_party/rust/rand-0.6.5/examples/monty-hall.rs rename : third_party/rust/rand/src/deprecated.rs => third_party/rust/rand-0.6.5/src/deprecated.rs rename : third_party/rust/rand/src/distributions/bernoulli.rs => third_party/rust/rand-0.6.5/src/distributions/bernoulli.rs rename : third_party/rust/rand/src/distributions/binomial.rs => third_party/rust/rand-0.6.5/src/distributions/binomial.rs rename : third_party/rust/rand/src/distributions/cauchy.rs => third_party/rust/rand-0.6.5/src/distributions/cauchy.rs rename : third_party/rust/rand/src/distributions/dirichlet.rs => third_party/rust/rand-0.6.5/src/distributions/dirichlet.rs rename : third_party/rust/rand/src/distributions/exponential.rs => third_party/rust/rand-0.6.5/src/distributions/exponential.rs rename : third_party/rust/rand/src/distributions/float.rs => third_party/rust/rand-0.6.5/src/distributions/float.rs rename : third_party/rust/rand/src/distributions/gamma.rs => third_party/rust/rand-0.6.5/src/distributions/gamma.rs rename : third_party/rust/rand/src/distributions/integer.rs => third_party/rust/rand-0.6.5/src/distributions/integer.rs rename : third_party/rust/rand/src/distributions/mod.rs => third_party/rust/rand-0.6.5/src/distributions/mod.rs rename : third_party/rust/rand/src/distributions/normal.rs => third_party/rust/rand-0.6.5/src/distributions/normal.rs rename : third_party/rust/rand/src/distributions/other.rs => third_party/rust/rand-0.6.5/src/distributions/other.rs rename : third_party/rust/rand/src/distributions/pareto.rs => third_party/rust/rand-0.6.5/src/distributions/pareto.rs rename : third_party/rust/rand/src/distributions/poisson.rs => third_party/rust/rand-0.6.5/src/distributions/poisson.rs rename : third_party/rust/rand/src/distributions/triangular.rs => third_party/rust/rand-0.6.5/src/distributions/triangular.rs rename : third_party/rust/rand/src/distributions/uniform.rs => third_party/rust/rand-0.6.5/src/distributions/uniform.rs rename : third_party/rust/rand/src/distributions/unit_circle.rs => third_party/rust/rand-0.6.5/src/distributions/unit_circle.rs rename : third_party/rust/rand/src/distributions/unit_sphere.rs => third_party/rust/rand-0.6.5/src/distributions/unit_sphere.rs rename : third_party/rust/rand/src/distributions/utils.rs => third_party/rust/rand-0.6.5/src/distributions/utils.rs rename : third_party/rust/rand/src/distributions/weibull.rs => third_party/rust/rand-0.6.5/src/distributions/weibull.rs rename : third_party/rust/rand/src/distributions/weighted.rs => third_party/rust/rand-0.6.5/src/distributions/weighted.rs rename : third_party/rust/rand/src/lib.rs => third_party/rust/rand-0.6.5/src/lib.rs rename : third_party/rust/rand/src/prelude.rs => third_party/rust/rand-0.6.5/src/prelude.rs rename : third_party/rust/rand/src/prng/mod.rs => third_party/rust/rand-0.6.5/src/prng/mod.rs rename : third_party/rust/rand/src/rngs/adapter/mod.rs => third_party/rust/rand-0.6.5/src/rngs/adapter/mod.rs rename : third_party/rust/rand/src/rngs/adapter/read.rs => third_party/rust/rand-0.6.5/src/rngs/adapter/read.rs rename : third_party/rust/rand/src/rngs/adapter/reseeding.rs => third_party/rust/rand-0.6.5/src/rngs/adapter/reseeding.rs rename : third_party/rust/rand/src/rngs/entropy.rs => third_party/rust/rand-0.6.5/src/rngs/entropy.rs rename : third_party/rust/rand/src/rngs/mock.rs => third_party/rust/rand-0.6.5/src/rngs/mock.rs rename : third_party/rust/rand/src/rngs/mod.rs => third_party/rust/rand-0.6.5/src/rngs/mod.rs rename : third_party/rust/rand/src/rngs/small.rs => third_party/rust/rand-0.6.5/src/rngs/small.rs rename : third_party/rust/rand/src/rngs/std.rs => third_party/rust/rand-0.6.5/src/rngs/std.rs rename : third_party/rust/rand/src/rngs/thread.rs => third_party/rust/rand-0.6.5/src/rngs/thread.rs rename : third_party/rust/rand/src/seq/index.rs => third_party/rust/rand-0.6.5/src/seq/index.rs rename : third_party/rust/rand/src/seq/mod.rs => third_party/rust/rand-0.6.5/src/seq/mod.rs rename : third_party/rust/rand/tests/uniformity.rs => third_party/rust/rand-0.6.5/tests/uniformity.rs rename : third_party/rust/rand/src/distributions/weighted.rs => third_party/rust/rand/src/distributions/weighted/mod.rs rename : third_party/rust/rand_chacha/.cargo-checksum.json => third_party/rust/rand_chacha-0.1.1/.cargo-checksum.json rename : third_party/rust/rand_chacha/CHANGELOG.md => third_party/rust/rand_chacha-0.1.1/CHANGELOG.md rename : third_party/rust/rand_chacha/Cargo.toml => third_party/rust/rand_chacha-0.1.1/Cargo.toml rename : third_party/rust/rand_chacha/README.md => third_party/rust/rand_chacha-0.1.1/README.md rename : third_party/rust/rand_chacha/build.rs => third_party/rust/rand_chacha-0.1.1/build.rs rename : third_party/rust/rand_chacha/src/chacha.rs => third_party/rust/rand_chacha-0.1.1/src/chacha.rs rename : third_party/rust/rand_chacha/src/lib.rs => third_party/rust/rand_chacha-0.1.1/src/lib.rs rename : third_party/rust/rand_core/.cargo-checksum.json => third_party/rust/rand_core-0.4.0/.cargo-checksum.json rename : third_party/rust/rand_core/CHANGELOG.md => third_party/rust/rand_core-0.4.0/CHANGELOG.md rename : third_party/rust/rand_core/Cargo.toml => third_party/rust/rand_core-0.4.0/Cargo.toml rename : third_party/rust/rand_core/README.md => third_party/rust/rand_core-0.4.0/README.md rename : third_party/rust/rand_core/src/block.rs => third_party/rust/rand_core-0.4.0/src/block.rs rename : third_party/rust/rand_core/src/error.rs => third_party/rust/rand_core-0.4.0/src/error.rs rename : third_party/rust/rand_core/src/impls.rs => third_party/rust/rand_core-0.4.0/src/impls.rs rename : third_party/rust/rand_core/src/lib.rs => third_party/rust/rand_core-0.4.0/src/lib.rs rename : third_party/rust/rand_hc/.cargo-checksum.json => third_party/rust/rand_hc-0.1.0/.cargo-checksum.json rename : third_party/rust/rand_hc/CHANGELOG.md => third_party/rust/rand_hc-0.1.0/CHANGELOG.md rename : third_party/rust/rand_hc/Cargo.toml => third_party/rust/rand_hc-0.1.0/Cargo.toml rename : third_party/rust/rand_hc/README.md => third_party/rust/rand_hc-0.1.0/README.md rename : third_party/rust/rand_hc/src/hc128.rs => third_party/rust/rand_hc-0.1.0/src/hc128.rs rename : third_party/rust/rand_hc/src/lib.rs => third_party/rust/rand_hc-0.1.0/src/lib.rs rename : third_party/rust/rand_pcg/.cargo-checksum.json => third_party/rust/rand_pcg-0.1.2/.cargo-checksum.json rename : third_party/rust/rand_pcg/CHANGELOG.md => third_party/rust/rand_pcg-0.1.2/CHANGELOG.md rename : third_party/rust/rand_pcg/Cargo.toml => third_party/rust/rand_pcg-0.1.2/Cargo.toml rename : third_party/rust/rand_pcg/README.md => third_party/rust/rand_pcg-0.1.2/README.md rename : third_party/rust/rand_chacha/build.rs => third_party/rust/rand_pcg-0.1.2/build.rs rename : third_party/rust/rand_pcg/src/lib.rs => third_party/rust/rand_pcg-0.1.2/src/lib.rs rename : third_party/rust/rand_pcg/src/pcg128.rs => third_party/rust/rand_pcg-0.1.2/src/pcg128.rs rename : third_party/rust/rand_pcg/src/pcg64.rs => third_party/rust/rand_pcg-0.1.2/src/pcg64.rs rename : third_party/rust/rand_pcg/tests/lcg64xsh32.rs => third_party/rust/rand_pcg-0.1.2/tests/lcg64xsh32.rs rename : third_party/rust/rand_pcg/tests/mcg128xsl64.rs => third_party/rust/rand_pcg-0.1.2/tests/mcg128xsl64.rs extra : moz-landing-system : lando
209 lines
6 KiB
Rust
209 lines
6 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
use quote::{quote, ToTokens};
|
|
use std::fs::File;
|
|
use std::io::{Read, Write};
|
|
use std::path::Path;
|
|
use syn;
|
|
use syn::fold::Fold;
|
|
use syn::parse::{Parse, ParseStream, Result};
|
|
use syn::{parse_quote, Token};
|
|
|
|
use proc_macro2::{Span, TokenStream};
|
|
|
|
struct MatchByteParser {}
|
|
|
|
pub fn expand(from: &Path, to: &Path) {
|
|
let mut source = String::new();
|
|
File::open(from)
|
|
.unwrap()
|
|
.read_to_string(&mut source)
|
|
.unwrap();
|
|
let ast = syn::parse_file(&source).expect("Parsing rules.rs module");
|
|
let mut m = MatchByteParser {};
|
|
let ast = m.fold_file(ast);
|
|
|
|
let code = ast
|
|
.into_token_stream()
|
|
.to_string()
|
|
.replace("{ ", "{\n")
|
|
.replace(" }", "\n}");
|
|
File::create(to)
|
|
.unwrap()
|
|
.write_all(code.as_bytes())
|
|
.unwrap();
|
|
}
|
|
|
|
struct MatchByte {
|
|
expr: syn::Expr,
|
|
arms: Vec<syn::Arm>,
|
|
}
|
|
|
|
impl Parse for MatchByte {
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
|
Ok(MatchByte {
|
|
expr: {
|
|
let expr = input.parse()?;
|
|
input.parse::<Token![,]>()?;
|
|
expr
|
|
},
|
|
arms: {
|
|
let mut arms = Vec::new();
|
|
while !input.is_empty() {
|
|
arms.push(input.call(syn::Arm::parse)?);
|
|
}
|
|
arms
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
fn get_byte_from_expr_lit(expr: &Box<syn::Expr>) -> u8 {
|
|
match **expr {
|
|
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
|
|
if let syn::Lit::Byte(ref byte) = *lit {
|
|
byte.value()
|
|
} else {
|
|
panic!("Found a pattern that wasn't a byte")
|
|
}
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
/// 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
|
|
///
|
|
/// ```rust
|
|
/// match_byte! { tokenizer.next_byte_unchecked(),
|
|
/// b'a'..b'z' => { ... }
|
|
/// b'0'..b'9' => { ... }
|
|
/// b'\n' | b'\\' => { ... }
|
|
/// foo => { ... }
|
|
/// }
|
|
/// ```
|
|
///
|
|
fn expand_match_byte(body: &TokenStream) -> syn::Expr {
|
|
let match_byte: MatchByte = syn::parse2(body.clone()).unwrap();
|
|
let expr = match_byte.expr;
|
|
let mut cases = Vec::new();
|
|
let mut table = [0u8; 256];
|
|
let mut match_body = Vec::new();
|
|
let mut wildcard = None;
|
|
|
|
for (i, ref arm) in match_byte.arms.iter().enumerate() {
|
|
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);
|
|
|
|
cases.push(quote!(#name = #index));
|
|
let body = &arm.body;
|
|
match_body.push(quote!(Case::#name => { #body }))
|
|
}
|
|
let en = quote!(enum Case {
|
|
#(#cases),*
|
|
});
|
|
|
|
let mut table_content = Vec::new();
|
|
for entry in table.iter() {
|
|
let name: syn::Path = syn::parse_str(&format!("Case::Case{}", entry)).unwrap();
|
|
table_content.push(name);
|
|
}
|
|
let table = quote!(static __CASES: [Case; 256] = [#(#table_content),*];);
|
|
|
|
let expr = if let Some(binding) = wildcard {
|
|
quote!({ #en #table let #binding = #expr; match __CASES[#binding as usize] { #(#match_body),* }})
|
|
} else {
|
|
quote!({ #en #table match __CASES[#expr as usize] { #(#match_body),* }})
|
|
};
|
|
|
|
syn::parse2(expr.into()).unwrap()
|
|
}
|
|
|
|
impl Fold for MatchByteParser {
|
|
fn fold_stmt(&mut self, stmt: syn::Stmt) -> syn::Stmt {
|
|
match stmt {
|
|
syn::Stmt::Item(syn::Item::Macro(syn::ItemMacro { ref mac, .. })) => {
|
|
if mac.path == parse_quote!(match_byte) {
|
|
return syn::fold::fold_stmt(
|
|
self,
|
|
syn::Stmt::Expr(expand_match_byte(&mac.tokens)),
|
|
);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
syn::fold::fold_stmt(self, stmt)
|
|
}
|
|
|
|
fn fold_expr(&mut self, expr: syn::Expr) -> syn::Expr {
|
|
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.tokens));
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
syn::fold::fold_expr(self, expr)
|
|
}
|
|
}
|