Metaprograms are programs that construct and deconstruct (i.e., inspect or analyze) other programs. It has been shown that these tasks are greatly facilitated by the use of quasiquotes, which present the concrete object syntax instead of unwieldy underlying data structures. We argue that two main flavors of quasiquotes have existed so far: Lisp-style quasiquotes can analyze code but may produce programs that contain type mismatches and unbound variables, while MetaML-style quasiquotes rely on static typing to guarantee generated programs are well-formed, but they can only generate code which is then viewed as a black box. In this paper, we show how to combine the advantages of both flavors into a unified framework. Drawing from previous formal work on staging calculi and contextual modal type theory (CMTT), we allow the construction and evaluation of code fragments ensuring that generated programs are well-typed and well-scoped, and we additionally allows the safe deconstruction of code fragments, a combination unseen in previous work. We formalize our approach as λ{}, a multi-stage calculus with code pattern matching and rewriting, and prove its soundness. We also present its realization in Squid, a metaprogramming framework implemented as a Scala macro library. Supporting the usefulness of our approach, we introduce speculative rewrite rules, a novel code transformation technique that makes decisive use of rewriting and CMTT.