parent
06c2d10255
commit
fd3d224067
|
@ -7,13 +7,13 @@ use sailfish::TemplateOnce;
|
||||||
#[template(path = "include.stpl")]
|
#[template(path = "include.stpl")]
|
||||||
struct Include {
|
struct Include {
|
||||||
title: String,
|
title: String,
|
||||||
name: String
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let ctx = Include {
|
let ctx = Include {
|
||||||
title: "Website".to_owned(),
|
title: "Website".to_owned(),
|
||||||
name: "Hanako".to_owned()
|
name: "Hanako".to_owned(),
|
||||||
};
|
};
|
||||||
println!("{}", ctx.render_once().unwrap());
|
println!("{}", ctx.render_once().unwrap());
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,13 +88,18 @@ struct Include<'a> {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_include() {
|
fn test_include() {
|
||||||
assert_render("include", Include { strs: &["foo", "bar"] });
|
assert_render(
|
||||||
|
"include",
|
||||||
|
Include {
|
||||||
|
strs: &["foo", "bar"],
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(TemplateOnce)]
|
#[derive(TemplateOnce)]
|
||||||
#[template(path = "big-table.stpl")]
|
#[template(path = "big-table.stpl")]
|
||||||
struct BigTable {
|
struct BigTable {
|
||||||
table: Vec<Vec<usize>>
|
table: Vec<Vec<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -151,7 +156,12 @@ struct RmWhitespace<'a, 'b> {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rm_whitespace() {
|
fn test_rm_whitespace() {
|
||||||
assert_render("rm_whitespace", RmWhitespace { messages: &["foo", "bar"] });
|
assert_render(
|
||||||
|
"rm_whitespace",
|
||||||
|
RmWhitespace {
|
||||||
|
messages: &["foo", "bar"],
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
|
|
@ -8,12 +8,12 @@ use crate::error::*;
|
||||||
use crate::optimizer::Optimizer;
|
use crate::optimizer::Optimizer;
|
||||||
use crate::parser::Parser;
|
use crate::parser::Parser;
|
||||||
use crate::resolver::Resolver;
|
use crate::resolver::Resolver;
|
||||||
use crate::translator::{Translator, TranslatedSource};
|
use crate::translator::{TranslatedSource, Translator};
|
||||||
use crate::util::{read_to_string, rustfmt_block};
|
use crate::util::{read_to_string, rustfmt_block};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Compiler {
|
pub struct Compiler {
|
||||||
config: Config
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compiler {
|
impl Compiler {
|
||||||
|
@ -35,7 +35,12 @@ impl Compiler {
|
||||||
translator.translate(stream)
|
translator.translate(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_file(&self, template_dir: &Path, input: &Path, output: &Path) -> Result<(), Error> {
|
pub fn compile_file(
|
||||||
|
&self,
|
||||||
|
template_dir: &Path,
|
||||||
|
input: &Path,
|
||||||
|
output: &Path,
|
||||||
|
) -> Result<(), Error> {
|
||||||
// TODO: introduce cache system
|
// TODO: introduce cache system
|
||||||
|
|
||||||
let input = input.canonicalize()?;
|
let input = input.canonicalize()?;
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub struct Config {
|
||||||
pub cache_dir: PathBuf,
|
pub cache_dir: PathBuf,
|
||||||
pub rm_whitespace: bool,
|
pub rm_whitespace: bool,
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub _non_exhaustive: ()
|
pub _non_exhaustive: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
@ -17,7 +17,7 @@ impl Default for Config {
|
||||||
escape: true,
|
escape: true,
|
||||||
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
|
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
|
||||||
rm_whitespace: false,
|
rm_whitespace: false,
|
||||||
_non_exhaustive: ()
|
_non_exhaustive: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ use syn::visit_mut::VisitMut;
|
||||||
use syn::{Block, Expr, ExprMacro, Ident, LitStr, Stmt, Token};
|
use syn::{Block, Expr, ExprMacro, Ident, LitStr, Stmt, Token};
|
||||||
|
|
||||||
struct RenderTextMacroArgument {
|
struct RenderTextMacroArgument {
|
||||||
|
#[allow(dead_code)]
|
||||||
context: Ident,
|
context: Ident,
|
||||||
arg: LitStr,
|
arg: LitStr,
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,6 +103,7 @@ pub struct ParseStream<'a> {
|
||||||
delimiter: char,
|
delimiter: char,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl<'a> ParseStream<'a> {
|
impl<'a> ParseStream<'a> {
|
||||||
/// Returns an empty `ParseStream` containing no tokens
|
/// Returns an empty `ParseStream` containing no tokens
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
@ -220,7 +221,7 @@ impl<'a> ParseStream<'a> {
|
||||||
let end = self
|
let end = self
|
||||||
.source
|
.source
|
||||||
.find(&*self.block_delimiter.0)
|
.find(&*self.block_delimiter.0)
|
||||||
.unwrap_or(self.source.len());
|
.unwrap_or_else(|| self.source.len());
|
||||||
let token = Token {
|
let token = Token {
|
||||||
content: self.take_n(end),
|
content: self.take_n(end),
|
||||||
offset,
|
offset,
|
||||||
|
|
|
@ -6,8 +6,8 @@ use syn::parse::{Parse, ParseStream, Result as ParseResult};
|
||||||
use syn::punctuated::Punctuated;
|
use syn::punctuated::Punctuated;
|
||||||
use syn::{Fields, Ident, ItemStruct, Lifetime, LitBool, LitChar, LitStr, Token};
|
use syn::{Fields, Ident, ItemStruct, Lifetime, LitBool, LitChar, LitStr, Token};
|
||||||
|
|
||||||
use crate::config::Config;
|
|
||||||
use crate::compiler::Compiler;
|
use crate::compiler::Compiler;
|
||||||
|
use crate::config::Config;
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
|
|
||||||
enum GenericParamName {
|
enum GenericParamName {
|
||||||
|
|
|
@ -23,13 +23,6 @@ macro_rules! return_if_some {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn empty_block() -> Block {
|
|
||||||
Block {
|
|
||||||
brace_token: Default::default(),
|
|
||||||
stmts: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ResolverImpl<'s, 'h> {
|
struct ResolverImpl<'s, 'h> {
|
||||||
template_dir: &'s Path,
|
template_dir: &'s Path,
|
||||||
path_stack: Vec<PathBuf>,
|
path_stack: Vec<PathBuf>,
|
||||||
|
@ -66,8 +59,9 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> {
|
||||||
|
|
||||||
// resolve include! for rust file
|
// resolve include! for rust file
|
||||||
if arg.ends_with(".rs") {
|
if arg.ends_with(".rs") {
|
||||||
if !arg.starts_with("/") {
|
if !arg.starts_with('/') {
|
||||||
let absolute_path = self.path_stack.last().unwrap().parent().unwrap().join(arg);
|
let absolute_path =
|
||||||
|
self.path_stack.last().unwrap().parent().unwrap().join(arg);
|
||||||
let absolute_path_str = absolute_path.to_string_lossy();
|
let absolute_path_str = absolute_path.to_string_lossy();
|
||||||
em.mac.tokens = quote! { include!(#absolute_path_str) };
|
em.mac.tokens = quote! { include!(#absolute_path_str) };
|
||||||
}
|
}
|
||||||
|
@ -81,7 +75,12 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> {
|
||||||
self.template_dir.join(&arg[1..])
|
self.template_dir.join(&arg[1..])
|
||||||
} else {
|
} else {
|
||||||
// relative include
|
// relative include
|
||||||
self.path_stack.last().unwrap().parent().unwrap().join(arg.clone())
|
self.path_stack
|
||||||
|
.last()
|
||||||
|
.unwrap()
|
||||||
|
.parent()
|
||||||
|
.unwrap()
|
||||||
|
.join(arg.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
// parse and translate the child template
|
// parse and translate the child template
|
||||||
|
@ -135,13 +134,18 @@ impl<'h> Resolver<'h> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn resolve(&self, template_dir: &Path, input_file: &Path, ast: &mut Block) -> Result<(), Error> {
|
pub fn resolve(
|
||||||
|
&self,
|
||||||
|
template_dir: &Path,
|
||||||
|
input_file: &Path,
|
||||||
|
ast: &mut Block,
|
||||||
|
) -> Result<(), Error> {
|
||||||
let mut child = ResolverImpl {
|
let mut child = ResolverImpl {
|
||||||
template_dir: template_dir,
|
template_dir,
|
||||||
path_stack: vec![input_file.to_owned()],
|
path_stack: vec![input_file.to_owned()],
|
||||||
deps: Vec::new(),
|
deps: Vec::new(),
|
||||||
error: None,
|
error: None,
|
||||||
include_handler: Arc::clone(&self.include_handler)
|
include_handler: Arc::clone(&self.include_handler),
|
||||||
};
|
};
|
||||||
child.visit_block_mut(ast);
|
child.visit_block_mut(ast);
|
||||||
|
|
||||||
|
|
|
@ -18,10 +18,10 @@ pub struct SourceMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceMap {
|
impl SourceMap {
|
||||||
#[inline]
|
// #[inline]
|
||||||
pub fn entries(&self) -> &[SourceMapEntry] {
|
// pub fn entries(&self) -> &[SourceMapEntry] {
|
||||||
&*self.entries
|
// &*self.entries
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn reverse_mapping(&self, offset: usize) -> Option<usize> {
|
pub fn reverse_mapping(&self, offset: usize) -> Option<usize> {
|
||||||
// find entry which satisfies entry.new <= offset < entry.new + entry.length
|
// find entry which satisfies entry.new <= offset < entry.new + entry.length
|
||||||
|
|
|
@ -58,9 +58,20 @@ impl Buffer {
|
||||||
self.capacity
|
self.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Force the length of buffer to `new_len`
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// - `new_len` must be less than or equal to `capacity()`
|
||||||
|
/// - The elements at `old_len..new_len` must be initialized
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn set_len(&mut self, new: usize) {
|
pub unsafe fn set_len(&mut self, new_len: usize) {
|
||||||
self.len = new;
|
self.len = new_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reserve(&mut self, size: usize) {
|
pub fn reserve(&mut self, size: usize) {
|
||||||
|
@ -145,7 +156,7 @@ impl From<String> for Buffer {
|
||||||
Buffer {
|
Buffer {
|
||||||
data: other.as_mut_ptr(),
|
data: other.as_mut_ptr(),
|
||||||
len: other.len(),
|
len: other.len(),
|
||||||
capacity: other.capacity()
|
capacity: other.capacity(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Buffer::new()
|
Buffer::new()
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::cast_ptr_alignment)]
|
||||||
|
|
||||||
#[cfg(target_arch = "x86")]
|
#[cfg(target_arch = "x86")]
|
||||||
use std::arch::x86::*;
|
use std::arch::x86::*;
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::cast_ptr_alignment)]
|
||||||
|
|
||||||
use super::naive;
|
use super::naive;
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "16")]
|
#[cfg(target_pointer_width = "16")]
|
||||||
|
@ -13,8 +15,8 @@ const USIZE_ALIGN: usize = USIZE_BYTES - 1;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn contains_zero_byte(x: usize) -> bool {
|
fn contains_zero_byte(x: usize) -> bool {
|
||||||
const LO_U64: u64 = 0x0101010101010101;
|
const LO_U64: u64 = 0x0101_0101_0101_0101;
|
||||||
const HI_U64: u64 = 0x8080808080808080;
|
const HI_U64: u64 = 0x8080_8080_8080_8080;
|
||||||
const LO_USIZE: usize = LO_U64 as usize;
|
const LO_USIZE: usize = LO_U64 as usize;
|
||||||
const HI_USIZE: usize = HI_U64 as usize;
|
const HI_USIZE: usize = HI_U64 as usize;
|
||||||
|
|
||||||
|
@ -23,10 +25,10 @@ fn contains_zero_byte(x: usize) -> bool {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn contains_key(x: usize) -> bool {
|
fn contains_key(x: usize) -> bool {
|
||||||
const INDEPENDENTS1: usize = 0x0404040404040404_u64 as usize;
|
const INDEPENDENTS1: usize = 0x0404_0404_0404_0404_u64 as usize;
|
||||||
const INDEPENDENTS2: usize = 0x0202020202020202_u64 as usize;
|
const INDEPENDENTS2: usize = 0x0202_0202_0202_0202_u64 as usize;
|
||||||
const KEY1: usize = 0x2626262626262626_u64 as usize;
|
const KEY1: usize = 0x2626_2626_2626_2626_u64 as usize;
|
||||||
const KEY2: usize = 0x3e3e3e3e3e3e3e3e_u64 as usize;
|
const KEY2: usize = 0x3e3e_3e3e_3e3e_3e3e_u64 as usize;
|
||||||
|
|
||||||
let y1 = x | INDEPENDENTS1;
|
let y1 = x | INDEPENDENTS1;
|
||||||
let y2 = x | INDEPENDENTS2;
|
let y2 = x | INDEPENDENTS2;
|
||||||
|
|
|
@ -22,7 +22,7 @@ static ESCAPE_LUT: [u8; 256] = [
|
||||||
9, 9, 9, 9,
|
9, 9, 9, 9,
|
||||||
];
|
];
|
||||||
|
|
||||||
const ESCAPED: [&'static str; 4] = [""", "&", "<", ">"];
|
const ESCAPED: [&str; 4] = [""", "&", "<", ">"];
|
||||||
const ESCAPED_LEN: usize = 4;
|
const ESCAPED_LEN: usize = 4;
|
||||||
|
|
||||||
/// write the escaped contents with custom function
|
/// write the escaped contents with custom function
|
||||||
|
|
|
@ -42,5 +42,5 @@ pub(super) unsafe fn proceed<F: FnMut(&str)>(
|
||||||
|
|
||||||
debug_assert_eq!(ptr, end_ptr);
|
debug_assert_eq!(ptr, end_ptr);
|
||||||
debug_assert!(start_ptr <= ptr);
|
debug_assert!(start_ptr <= ptr);
|
||||||
return start_ptr;
|
start_ptr
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::cast_ptr_alignment)]
|
||||||
|
|
||||||
#[cfg(target_arch = "x86")]
|
#[cfg(target_arch = "x86")]
|
||||||
use std::arch::x86::*;
|
use std::arch::x86::*;
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
|
|
Loading…
Reference in New Issue