Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Update with %% grammar #38

Merged
merged 4 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ name = "snapshots"
path = "src/bin/snapshots.rs"

[patch.'crates-io']
gll = { git = "https://github.com/rust-lang/gll", rev = "bc72820905eac498c1fb1cc644e931d63e7d07e3" }
gll = { git = "https://github.com/rust-lang/gll", rev = "617ecfc58a554c92226ec276063c99a09678fb05" }
grammer = { git = "https://github.com/lykenware/grammer", rev = "eb47b51a9332c0e82d7c02d988e203d2a01f3654" }
12 changes: 6 additions & 6 deletions grammar/expr.lyg
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ ExprKind =
| Cast:{ expr:Expr "as" ty:Type }
| Field:{ base:Expr "." field:FieldName }
| Index:{ base:Expr "[" index:Expr "]" }
| Array:{ "[" attrs:InnerAttr* exprs:Expr* % "," ","? "]" }
| Array:{ "[" attrs:InnerAttr* exprs:Expr* %% "," "]" }
| Repeat:{ "[" attrs:InnerAttr* elem:Expr ";" count:Expr "]" }
| Tuple:{ "(" attrs:InnerAttr* exprs:Expr* % "," ","? ")" }
| Tuple:{ "(" attrs:InnerAttr* exprs:Expr* %% "," ")" }
| Path:QPath
| Call:{ callee:Expr "(" args:Expr* % "," ","? ")" }
| MethodCall:{ receiver:Expr "." method:PathSegment "(" args:Expr* % "," ","? ")" }
| Call:{ callee:Expr "(" args:Expr* %% "," ")" }
| MethodCall:{ receiver:Expr "." method:PathSegment "(" args:Expr* %% "," ")" }
| Struct:{ path:Path "{" attrs:InnerAttr* fields:StructExprFieldsAndBase "}" }
| Block:{ { label:Label ":" }? unsafety:"unsafe"? block:Block }
// ustable(async_await):
Expand All @@ -43,7 +43,7 @@ ExprKind =
// unstable(async_await):
asyncness:"async"?
by_val:"move"?
"|" args:ClosureArg* % "," ","? "|" { "->" ret_ty:Type }? body:Expr
"|" args:ClosureArg* %% "," "|" { "->" ret_ty:Type }? body:Expr
}
| MacroCall:MacroCall
;
Expand Down Expand Up @@ -102,7 +102,7 @@ FieldName =

// FIXME(eddyb) find a way to express this `A* B?` pattern better
StructExprFieldsAndBase =
| Fields:{ fields:StructExprField* % "," ","? }
| Fields:StructExprField* %% ","
| Base:{ ".." base:Expr }
| FieldsAndBase:{ fields:StructExprField+ % "," "," ".." base:Expr }
;
Expand Down
23 changes: 12 additions & 11 deletions grammar/generics.lyg
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Generics = "<" params:GenericParam* % "," ","? ">";
Generics = "<" params:GenericParam* %% "," ">";
GenericParam = attrs:OuterAttr* kind:GenericParamKind;
GenericParamKind =
| Lifetime:{ name:LIFETIME { ":" bounds:LifetimeBound* % "+" "+"? }? }
| Type:{ name:IDENT { ":" bounds:TypeBound* % "+" "+"? }? { "=" default:Type }? }
| Lifetime:{ name:LIFETIME { ":" bounds:LifetimeBound* %% "+" }? }
| Type:{ name:IDENT { ":" bounds:TypeBound* %% "+" }? { "=" default:Type }? }
;

ForAllBinder = "for" generics:Generics;

WhereClause = "where" bounds:WhereBound* % "," ","?;
WhereClause = "where" bounds:WhereBound* %% ",";
WhereBound =
| Lifetime:{ lt:LIFETIME ":" bounds:LifetimeBound* % "+" "+"? }
| Type:{ binder:ForAllBinder? ty:Type ":" bounds:TypeBound* % "+" "+"? }
| Lifetime:{ lt:LIFETIME ":" bounds:LifetimeBound* %% "+" }
| Type:{ binder:ForAllBinder? ty:Type ":" bounds:TypeBound* %% "+" }
// unstable(#20041):
| TypeEq:{ binder:ForAllBinder? left:Type { "=" | "==" } right:Type }
;
Expand All @@ -23,16 +23,17 @@ TypeBound =
;
TypeTraitBound = unbound:"?"? binder:ForAllBinder? path:Path;

// Generic args of a path segment.
GenericArgs =
| AngleBracket:{ "<" args_and_bindings:AngleBracketGenericArgsAndBindings? ","? ">" }
| Paren:{ "(" inputs:Type* % "," ","? ")" { "->" output:Type }? }
| AngleBracket:{ "<" args_and_bindings:AngleBracketGenericArgsAndBindings? ">" }
| Paren:{ "(" inputs:Type* %% "," ")" { "->" output:Type }? }
;

// FIXME(eddyb) find a way to express this `A* B*` pattern better
AngleBracketGenericArgsAndBindings =
| Args:GenericArg+ % ","
| Bindings:TypeBinding+ % ","
| ArgsAndBindings:{ args:GenericArg+ % "," "," bindings:TypeBinding+ % "," }
| Args:GenericArg+ %% ","
| Bindings:TypeBinding+ %% ","
| ArgsAndBindings:{ args:GenericArg+ % "," "," bindings:TypeBinding+ %% "," }
;

GenericArg =
Expand Down
30 changes: 15 additions & 15 deletions grammar/item.lyg
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ ItemKind =
| Fn:{ header:FnHeader "fn" decl:FnDecl body:Block }
| TypeAlias:{ "type" name:IDENT generics:Generics? where_clause:WhereClause? "=" ty:Type ";" }
// unstable(existential_type):
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* % "+" "+"? ";" }
| Enum:{ "enum" name:IDENT generics:Generics? where_clause:WhereClause? "{" variants:EnumVariant* % "," ","? "}" }
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* %% "+" ";" }
| Enum:{ "enum" name:IDENT generics:Generics? where_clause:WhereClause? "{" variants:EnumVariant* %% "," "}" }
| Struct:{ "struct" name:IDENT generics:Generics? body:StructBody }
| Union:{ "union" name:IDENT generics:Generics? where_clause:WhereClause? "{" fields:RecordField* % "," ","? "}" }
| Union:{ "union" name:IDENT generics:Generics? where_clause:WhereClause? "{" fields:RecordField* %% "," "}" }
| Trait:{
unsafety:"unsafe"?
// unstable(optin_builtin_traits):
auto:"auto"?
"trait" name:IDENT generics:Generics?
{ ":" superbounds:TypeBound* % "+" "+"? }?
{ ":" superbounds:TypeBound* %% "+" }?
where_clause:WhereClause? "{" trait_items:TraitItem* "}"
}
// unstable(trait_alias):
| TraitAlias:{
"trait" name:IDENT generics:Generics?
{ ":" superbounds:TypeBound* % "+" "+"? }?
where_clause:WhereClause? "=" bounds:TypeBound* % "+" "+"? ";"
{ ":" superbounds:TypeBound* %% "+" }?
where_clause:WhereClause? "=" bounds:TypeBound* %% "+" ";"
}
| Impl:{
// unstable(specialization):
Expand All @@ -43,7 +43,7 @@ ItemKind =

UseTree =
| Glob:{ prefix:UseTreePrefix? "*" }
| Nested:{ prefix:UseTreePrefix? "{" children:UseTree* % "," ","? "}" }
| Nested:{ prefix:UseTreePrefix? "{" children:UseTree* %% "," "}" }
| Simple:{ path:Path { "as" rename:IDENT }? }
;
UseTreePrefix =
Expand All @@ -65,7 +65,7 @@ TraitItem = attrs:OuterAttr* kind:TraitItemKind;
TraitItemKind =
| Const:{ "const" name:IDENT ":" ty:Type { "=" default:Expr }? ";" }
| Fn:{ header:FnHeader "fn" decl:FnDecl { default_body:Block | ";" } }
| Type:{ "type" name:IDENT generics:Generics? { ":" bounds:TypeBound* % "+" "+"? }? where_clause:WhereClause? { "=" default:Type }? ";" }
| Type:{ "type" name:IDENT generics:Generics? { ":" bounds:TypeBound* %% "+" }? where_clause:WhereClause? { "=" default:Type }? ";" }
| MacroCall:ItemMacroCall
;

Expand All @@ -80,16 +80,16 @@ ImplItemKind =
| Fn:{ header:FnHeader "fn" decl:FnDecl body:Block }
| Type:{ "type" name:IDENT generics:Generics? where_clause:WhereClause? "=" ty:Type ";" }
// unstable(existential_type):
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* % "+" "+"? ";" }
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* %% "+" ";" }
| MacroCall:ItemMacroCall
;

FnHeader = constness:"const"? unsafety:"unsafe"? asyncness:"async"? { "extern" abi:Abi }?;
FnDecl = name:IDENT generics:Generics? "(" args:FnArgs? ","? ")" { "->" ret_ty:Type }? where_clause:WhereClause?;
FnDecl = name:IDENT generics:Generics? "(" args:FnArgs? ")" { "->" ret_ty:Type }? where_clause:WhereClause?;

// FIXME(eddyb) find a way to express this `A* B?` pattern better
FnArgs =
| Regular:FnArg+ % ","
| Regular:FnArg+ %% ","
| Variadic:"..."
| RegularAndVariadic:{ args:FnArg+ % "," "," "..." }
;
Expand All @@ -102,16 +102,16 @@ FnArg =
EnumVariant = attrs:OuterAttr* name:IDENT kind:EnumVariantKind { "=" discr:Expr }?;
EnumVariantKind =
| Unit:{}
| Tuple:{ "(" fields:TupleField* % "," ","? ")" }
| Record:{ "{" fields:RecordField* % "," ","? "}" }
| Tuple:{ "(" fields:TupleField* %% "," ")" }
| Record:{ "{" fields:RecordField* %% "," "}" }
;

// FIXME(eddyb) could maybe be shared more with `EnumVariantKind`?
// The problem is the semicolons for `Unit` and `Tuple`, and the where clauses.
StructBody =
| Unit:{ where_clause:WhereClause? ";" }
| Tuple:{ "(" fields:TupleField* % "," ","? ")" where_clause:WhereClause? ";" }
| Record:{ where_clause:WhereClause? "{" fields:RecordField* % "," ","? "}" }
| Tuple:{ "(" fields:TupleField* %% "," ")" where_clause:WhereClause? ";" }
| Record:{ where_clause:WhereClause? "{" fields:RecordField* %% "," "}" }
;

TupleField = attrs:OuterAttr* vis:Vis? ty:Type;
Expand Down
8 changes: 4 additions & 4 deletions grammar/pat.lyg
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Pat =
| Ref:{ "&" mutable:"mut"? pat:Pat }
// unstable(box_patterns):
| Box:{ "box" pat:Pat }
| Slice:{ "[" elems:SlicePatElem* % "," ","? "]" }
| Tuple:{ "(" fields:TuplePatField* % "," ","? ")" }
| Slice:{ "[" elems:SlicePatElem* %% "," "]" }
| Tuple:{ "(" fields:TuplePatField* %% "," ")" }
| Path:QPath
| TupleStruct:{ path:Path "(" fields:TuplePatField* % "," ","? ")" }
| TupleStruct:{ path:Path "(" fields:TuplePatField* %% "," ")" }
| Struct:{ path:Path "{" fields:StructPatFieldsAndEllipsis "}" }
| MacroCall:MacroCall
;
Expand All @@ -36,7 +36,7 @@ TuplePatField =

// FIXME(eddyb) find a way to express this `A* B?` pattern better
StructPatFieldsAndEllipsis =
| Fields:{ fields:StructPatField* % "," ","? }
| Fields:StructPatField* %% ","
| Ellipsis:{ ".." }
| FieldsAndEllipsis:{ fields:StructPatField+ % "," "," ".." }
;
Expand Down
10 changes: 5 additions & 5 deletions grammar/type.lyg
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ Type =
| Ref:{ "&" lt:LIFETIME? mutable:"mut"? pointee:Type }
| Slice:{ "[" elem:Type "]" }
| Array:{ "[" elem:Type ";" len:Expr "]" }
| Tuple:{ "(" tys:Type* % "," ","? ")" }
| Tuple:{ "(" tys:Type* %% "," ")" }
| Path:QPath
| FnPtr:{
binder:ForAllBinder? unsafety:"unsafe"? { "extern" abi:Abi }?
"fn" "(" inputs:FnSigInputs? ","? ")" { "->" ret_ty:Type }?
"fn" "(" inputs:FnSigInputs? ")" { "->" ret_ty:Type }?
}
| ImplTrait:{ "impl" bounds:TypeBound+ % "+" "+"? }
| DynTrait:{ "dyn"? bounds:TypeBound+ % "+" "+"? }
| ImplTrait:{ "impl" bounds:TypeBound+ %% "+" }
| DynTrait:{ "dyn"? bounds:TypeBound+ %% "+" }
// unstable(not exposed to users):
| TypeOf:{ "typeof" "(" expr:Expr ")" }
| MacroCall:MacroCall
;

FnSigInputs =
| Regular:FnSigInput+ % ","
| Regular:FnSigInput+ %% ","
| Variadic:"..."
| RegularAndVariadic:{ inputs:FnSigInput+ % "," "," "..." }
;
Expand Down
9 changes: 8 additions & 1 deletion src/bin/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ fn report_file_result(
"(missing location information; \
set `RUSTFLAGS='--cfg procmacro2_semver_exempt'`)"
);

}
eprintln!("Expected: {:?}", error.expected);

// HACK(eddyb) this is inefficient - `expected` should be already
// sorted for us, so this is a temporary workaround.
let mut expected = error.expected.clone();
expected.sort_by_cached_key(|x| format!("{:?}", x));

eprintln!("Expected: {:?}", expected);
}
(Err(Error::Lex(e)), _) => eprintln!("FAIL ({:?})", e),
}
Expand Down
14 changes: 11 additions & 3 deletions src/bin/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ use {
walkdir::WalkDir,
};

fn to_debug_str(debug: &dyn Debug) -> String {
format!("{:#?}", debug)
fn parse_result_to_str<T: Debug>(
mut result: Result<T, gll::parser::ParseError<proc_macro2::Span>>,
) -> String {
if let Err(error) = &mut result {
// HACK(eddyb) this is inefficient - `expected` should be already
// sorted for us, so this is a temporary workaround.
error.expected.sort_by_cached_key(|x| format!("{:?}", x));
}

format!("{:#?}", result)
}

macro_rules! snapshot {
($production:ident, $src:expr) => {{
let tts = $src
.parse::<proc_macro2::TokenStream>()
.expect("tokenization");
to_debug_str(&parse::$production::parse(tts))
parse_result_to_str(parse::$production::parse(tts))
}};
}

Expand Down
86 changes: 86 additions & 0 deletions src/bin/snapshots/snapshots__GenericArgs.all.input.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
created: "2019-06-02T15:47:35.179319Z"
creator: [email protected]
source: src/bin/snapshots.rs
expression: forest
---
Ok(
GenericArgs::AngleBracket {
args_and_bindings: AngleBracketGenericArgsAndBindings::ArgsAndBindings {
args: [
GenericArg::Lifetime | GenericArg::Type(
Type::DynTrait {
bounds: [
TypeBound::Outlives,
],
},
),
GenericArg::Type(
Type::Path(
QPath::Unqualified(
Path {
path: RelativePath {
segments: [
PathSegment {
ident: _,
},
],
},
},
),
) | Type::DynTrait {
bounds: [
TypeBound::Trait(
TypeTraitBound {
path: Path {
path: RelativePath {
segments: [
PathSegment {
ident: _,
},
],
},
},
},
),
],
},
),
],
bindings: [
TypeBinding {
name: _,
ty: Type::Path(
QPath::Unqualified(
Path {
path: RelativePath {
segments: [
PathSegment {
ident: _,
},
],
},
},
),
) | Type::DynTrait {
bounds: [
TypeBound::Trait(
TypeTraitBound {
path: Path {
path: RelativePath {
segments: [
PathSegment {
ident: _,
},
],
},
},
},
),
],
},
},
],
},
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
created: "2019-08-24T22:44:34.221417271Z"
creator: [email protected]
source: src/bin/snapshots.rs
expression: forest
---
Err(
ParseError {
at: Span,
expected: [
[
Ident(
None,
),
],
[
Punct {
ch: Some(
'>',
),
joint: None,
},
],
],
},
)
Loading