Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'rust'
Browse files Browse the repository at this point in the history
# Conflicts:
#	syntex_syntax/src/parse/parser.rs
  • Loading branch information
erickt committed Feb 18, 2016
2 parents acb0bb0 + 76a01a4 commit 6283a53
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 247 deletions.
57 changes: 31 additions & 26 deletions syntex_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

// The Rust abstract syntax tree.

pub use self::Pat_::*;
pub use self::StructFieldKind::*;
pub use self::TyParamBound::*;
pub use self::UnsafeSource::*;
Expand Down Expand Up @@ -521,7 +520,7 @@ pub struct Block {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Pat {
pub id: NodeId,
pub node: Pat_,
pub node: PatKind,
pub span: Span,
}

Expand Down Expand Up @@ -552,47 +551,53 @@ pub enum BindingMode {
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Pat_ {
pub enum PatKind {
/// Represents a wildcard pattern (`_`)
PatWild,
Wild,

/// A PatIdent may either be a new bound variable,
/// or a nullary enum (in which case the third field
/// is None).
/// A `PatKind::Ident` may either be a new bound variable,
/// or a unit struct/variant pattern, or a const pattern (in the last two cases
/// the third field must be `None`).
///
/// In the nullary enum case, the parser can't determine
/// In the unit or const pattern case, the parser can't determine
/// which it is. The resolver determines this, and
/// records this pattern's NodeId in an auxiliary
/// set (of "PatIdents that refer to nullary enums")
PatIdent(BindingMode, SpannedIdent, Option<P<Pat>>),
/// records this pattern's `NodeId` in an auxiliary
/// set (of "PatIdents that refer to unit patterns or constants").
Ident(BindingMode, SpannedIdent, Option<P<Pat>>),

/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
/// The `bool` is `true` in the presence of a `..`.
Struct(Path, Vec<Spanned<FieldPat>>, bool),

/// A tuple struct/variant pattern `Variant(x, y, z)`.
/// "None" means a `Variant(..)` pattern where we don't bind the fields to names.
PatEnum(Path, Option<Vec<P<Pat>>>),
TupleStruct(Path, Option<Vec<P<Pat>>>),

/// A path pattern.
/// Such pattern can be resolved to a unit struct/variant or a constant.
Path(Path),

/// An associated const named using the qualified path `<T>::CONST` or
/// `<T as Trait>::CONST`. Associated consts from inherent impls can be
/// referred to as simply `T::CONST`, in which case they will end up as
/// PatEnum, and the resolver will have to sort that out.
PatQPath(QSelf, Path),
/// PatKind::Enum, and the resolver will have to sort that out.
QPath(QSelf, Path),

/// Destructuring of a struct, e.g. `Foo {x, y, ..}`
/// The `bool` is `true` in the presence of a `..`
PatStruct(Path, Vec<Spanned<FieldPat>>, bool),
/// A tuple pattern `(a, b)`
PatTup(Vec<P<Pat>>),
Tup(Vec<P<Pat>>),
/// A `box` pattern
PatBox(P<Pat>),
Box(P<Pat>),
/// A reference pattern, e.g. `&mut (a, b)`
PatRegion(P<Pat>, Mutability),
Ref(P<Pat>, Mutability),
/// A literal
PatLit(P<Expr>),
Lit(P<Expr>),
/// A range pattern, e.g. `1...2`
PatRange(P<Expr>, P<Expr>),
Range(P<Expr>, P<Expr>),
/// `[a, b, ..i, y, z]` is represented as:
/// `PatVec(box [a, b], Some(i), box [y, z])`
PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
/// `PatKind::Vec(box [a, b], Some(i), box [y, z])`
Vec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
/// A macro pattern; pre-expansion
PatMac(Mac),
Mac(Mac),
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand Down Expand Up @@ -1609,7 +1614,7 @@ impl Arg {
}),
pat: P(Pat {
id: DUMMY_NODE_ID,
node: PatIdent(BindingMode::ByValue(mutability), path, None),
node: PatKind::Ident(BindingMode::ByValue(mutability), path, None),
span: span
}),
id: DUMMY_NODE_ID
Expand Down
4 changes: 2 additions & 2 deletions syntex_syntax/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn ident_to_pat(id: NodeId, s: Span, i: Ident) -> P<Pat> {
let spanned = codemap::Spanned{ span: s, node: i };
P(Pat {
id: id,
node: PatIdent(BindingMode::ByValue(Mutability::Immutable), spanned, None),
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), spanned, None),
span: s
})
}
Expand Down Expand Up @@ -348,7 +348,7 @@ pub fn compute_id_range_for_fn_body(fk: FnKind,
/// and false otherwise.
pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
match pat.node {
ast::PatIdent(..) => true,
PatKind::Ident(..) => true,
_ => false,
}
}
Expand Down
6 changes: 3 additions & 3 deletions syntex_syntax/src/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
pub use self::SyntaxExtension::*;

use ast;
use ast::Name;
use ast::{Name, PatKind};
use codemap;
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
use errors::DiagnosticBuilder;
Expand Down Expand Up @@ -307,7 +307,7 @@ impl MacResult for MacEager {
return Some(P(ast::Pat {
id: ast::DUMMY_NODE_ID,
span: e.span,
node: ast::PatLit(e),
node: PatKind::Lit(e),
}));
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ impl DummyResult {
pub fn raw_pat(sp: Span) -> ast::Pat {
ast::Pat {
id: ast::DUMMY_NODE_ID,
node: ast::PatWild,
node: PatKind::Wild,
span: sp,
}
}
Expand Down
22 changes: 13 additions & 9 deletions syntex_syntax/src/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use abi::Abi;
use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp};
use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
use attr;
use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
use ext::base::ExtCtxt;
Expand Down Expand Up @@ -166,7 +166,7 @@ pub trait AstBuilder {
fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;

fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
fn pat_wild(&self, span: Span) -> P<ast::Pat>;
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
Expand Down Expand Up @@ -805,14 +805,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
}


fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
}
fn pat_wild(&self, span: Span) -> P<ast::Pat> {
self.pat(span, ast::PatWild)
self.pat(span, PatKind::Wild)
}
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
self.pat(span, ast::PatLit(expr))
self.pat(span, PatKind::Lit(expr))
}
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
Expand All @@ -823,20 +823,24 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
span: Span,
ident: ast::Ident,
bm: ast::BindingMode) -> P<ast::Pat> {
let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
self.pat(span, pat)
}
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
let pat = ast::PatEnum(path, Some(subpats));
let pat = if subpats.is_empty() {
PatKind::Path(path)
} else {
PatKind::TupleStruct(path, Some(subpats))
};
self.pat(span, pat)
}
fn pat_struct(&self, span: Span,
path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
let pat = ast::PatStruct(path, field_pats, false);
let pat = PatKind::Struct(path, field_pats, false);
self.pat(span, pat)
}
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
self.pat(span, ast::PatTup(pats))
self.pat(span, PatKind::Tup(pats))
}

fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
Expand Down
24 changes: 12 additions & 12 deletions syntex_syntax/src/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{Block, Crate, DeclKind, PatMac};
use ast::{Block, Crate, DeclKind, PatKind};
use ast::{Local, Ident, Mac_, Name};
use ast::{MacStmtStyle, Mrk, Stmt, StmtKind, ItemKind};
use ast::TokenTree;
Expand Down Expand Up @@ -678,7 +678,7 @@ fn rename_in_scope<X, F>(pats: Vec<P<ast::Pat>>,
(f(&mut rename_fld, fld, x), rewritten_pats)
}

/// A visitor that extracts the PatIdent (binding) paths
/// A visitor that extracts the PatKind::Ident (binding) paths
/// from a given thingy and puts them in a mutable
/// array
#[derive(Clone)]
Expand All @@ -689,9 +689,9 @@ struct PatIdentFinder {
impl<'v> Visitor<'v> for PatIdentFinder {
fn visit_pat(&mut self, pattern: &ast::Pat) {
match *pattern {
ast::Pat { id: _, node: ast::PatIdent(_, ref path1, ref inner), span: _ } => {
ast::Pat { id: _, node: PatKind::Ident(_, ref path1, ref inner), span: _ } => {
self.ident_accumulator.push(path1.node);
// visit optional subpattern of PatIdent:
// visit optional subpattern of PatKind::Ident:
if let Some(ref subpat) = *inner {
self.visit_pat(subpat)
}
Expand All @@ -702,14 +702,14 @@ impl<'v> Visitor<'v> for PatIdentFinder {
}
}

/// find the PatIdent paths in a pattern
/// find the PatKind::Ident paths in a pattern
fn pattern_bindings(pat: &ast::Pat) -> Vec<ast::Ident> {
let mut name_finder = PatIdentFinder{ident_accumulator:Vec::new()};
name_finder.visit_pat(pat);
name_finder.ident_accumulator
}

/// find the PatIdent paths in a
/// find the PatKind::Ident paths in a
fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> {
let mut pat_idents = PatIdentFinder{ident_accumulator:Vec::new()};
for arg in &fn_decl.inputs {
Expand Down Expand Up @@ -758,12 +758,12 @@ pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {

fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
match p.node {
PatMac(_) => {}
PatKind::Mac(_) => {}
_ => return noop_fold_pat(p, fld)
}
p.clone().map(|ast::Pat {node, span, ..}| {
let (pth, tts) = match node {
PatMac(mac) => (mac.node.path, mac.node.tts),
PatKind::Mac(mac) => (mac.node.path, mac.node.tts),
_ => unreachable!()
};
if pth.segments.len() > 1 {
Expand Down Expand Up @@ -855,7 +855,7 @@ impl<'a> Folder for IdentRenamer<'a> {
}

/// A tree-folder that applies every rename in its list to
/// the idents that are in PatIdent patterns. This is more narrowly
/// the idents that are in PatKind::Ident patterns. This is more narrowly
/// focused than IdentRenamer, and is needed for FnDecl,
/// where we want to rename the args but not the fn name or the generics etc.
pub struct PatIdentRenamer<'a> {
Expand All @@ -865,16 +865,16 @@ pub struct PatIdentRenamer<'a> {
impl<'a> Folder for PatIdentRenamer<'a> {
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
match pat.node {
ast::PatIdent(..) => {},
PatKind::Ident(..) => {},
_ => return noop_fold_pat(pat, self)
}

pat.map(|ast::Pat {id, node, span}| match node {
ast::PatIdent(binding_mode, Spanned{span: sp, node: ident}, sub) => {
PatKind::Ident(binding_mode, Spanned{span: sp, node: ident}, sub) => {
let new_ident = Ident::new(ident.name,
mtwt::apply_renames(self.renames, ident.ctxt));
let new_node =
ast::PatIdent(binding_mode,
PatKind::Ident(binding_mode,
Spanned{span: self.new_span(sp), node: new_ident},
sub.map(|p| self.fold_pat(p)));
ast::Pat {
Expand Down
8 changes: 4 additions & 4 deletions syntex_syntax/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use self::AttributeType::*;
use self::AttributeGate::*;

use abi::Abi;
use ast::NodeId;
use ast::{NodeId, PatKind};
use ast;
use attr;
use attr::AttrMetaMethods;
Expand Down Expand Up @@ -1005,19 +1005,19 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {

fn visit_pat(&mut self, pattern: &ast::Pat) {
match pattern.node {
ast::PatVec(_, Some(_), ref last) if !last.is_empty() => {
PatKind::Vec(_, Some(_), ref last) if !last.is_empty() => {
self.gate_feature("advanced_slice_patterns",
pattern.span,
"multiple-element slice matches anywhere \
but at the end of a slice (e.g. \
`[0, ..xs, 0]`) are experimental")
}
ast::PatVec(..) => {
PatKind::Vec(..) => {
self.gate_feature("slice_patterns",
pattern.span,
"slice pattern syntax is experimental");
}
ast::PatBox(..) => {
PatKind::Box(..) => {
self.gate_feature("box_patterns",
pattern.span,
"box pattern syntax is experimental");
Expand Down
Loading

0 comments on commit 6283a53

Please sign in to comment.