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

Commit 9758b24

Browse files
authored
Update with %% grammar (#38)
Update with %% grammar
2 parents 9d1984d + cf6094e commit 9758b24

File tree

98 files changed

+2681
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2681
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ name = "snapshots"
3333
path = "src/bin/snapshots.rs"
3434

3535
[patch.'crates-io']
36-
gll = { git = "https://github.com/rust-lang/gll", rev = "bc72820905eac498c1fb1cc644e931d63e7d07e3" }
36+
gll = { git = "https://github.com/rust-lang/gll", rev = "617ecfc58a554c92226ec276063c99a09678fb05" }
3737
grammer = { git = "https://github.com/lykenware/grammer", rev = "eb47b51a9332c0e82d7c02d988e203d2a01f3654" }

grammar/expr.lyg

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ ExprKind =
1515
| Cast:{ expr:Expr "as" ty:Type }
1616
| Field:{ base:Expr "." field:FieldName }
1717
| Index:{ base:Expr "[" index:Expr "]" }
18-
| Array:{ "[" attrs:InnerAttr* exprs:Expr* % "," ","? "]" }
18+
| Array:{ "[" attrs:InnerAttr* exprs:Expr* %% "," "]" }
1919
| Repeat:{ "[" attrs:InnerAttr* elem:Expr ";" count:Expr "]" }
20-
| Tuple:{ "(" attrs:InnerAttr* exprs:Expr* % "," ","? ")" }
20+
| Tuple:{ "(" attrs:InnerAttr* exprs:Expr* %% "," ")" }
2121
| Path:QPath
22-
| Call:{ callee:Expr "(" args:Expr* % "," ","? ")" }
23-
| MethodCall:{ receiver:Expr "." method:PathSegment "(" args:Expr* % "," ","? ")" }
22+
| Call:{ callee:Expr "(" args:Expr* %% "," ")" }
23+
| MethodCall:{ receiver:Expr "." method:PathSegment "(" args:Expr* %% "," ")" }
2424
| Struct:{ path:Path "{" attrs:InnerAttr* fields:StructExprFieldsAndBase "}" }
2525
| Block:{ { label:Label ":" }? unsafety:"unsafe"? block:Block }
2626
// ustable(async_await):
@@ -43,7 +43,7 @@ ExprKind =
4343
// unstable(async_await):
4444
asyncness:"async"?
4545
by_val:"move"?
46-
"|" args:ClosureArg* % "," ","? "|" { "->" ret_ty:Type }? body:Expr
46+
"|" args:ClosureArg* %% "," "|" { "->" ret_ty:Type }? body:Expr
4747
}
4848
| MacroCall:MacroCall
4949
;
@@ -102,7 +102,7 @@ FieldName =
102102

103103
// FIXME(eddyb) find a way to express this `A* B?` pattern better
104104
StructExprFieldsAndBase =
105-
| Fields:{ fields:StructExprField* % "," ","? }
105+
| Fields:StructExprField* %% ","
106106
| Base:{ ".." base:Expr }
107107
| FieldsAndBase:{ fields:StructExprField+ % "," "," ".." base:Expr }
108108
;

grammar/generics.lyg

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
Generics = "<" params:GenericParam* % "," ","? ">";
1+
Generics = "<" params:GenericParam* %% "," ">";
22
GenericParam = attrs:OuterAttr* kind:GenericParamKind;
33
GenericParamKind =
4-
| Lifetime:{ name:LIFETIME { ":" bounds:LifetimeBound* % "+" "+"? }? }
5-
| Type:{ name:IDENT { ":" bounds:TypeBound* % "+" "+"? }? { "=" default:Type }? }
4+
| Lifetime:{ name:LIFETIME { ":" bounds:LifetimeBound* %% "+" }? }
5+
| Type:{ name:IDENT { ":" bounds:TypeBound* %% "+" }? { "=" default:Type }? }
66
;
77

88
ForAllBinder = "for" generics:Generics;
99

10-
WhereClause = "where" bounds:WhereBound* % "," ","?;
10+
WhereClause = "where" bounds:WhereBound* %% ",";
1111
WhereBound =
12-
| Lifetime:{ lt:LIFETIME ":" bounds:LifetimeBound* % "+" "+"? }
13-
| Type:{ binder:ForAllBinder? ty:Type ":" bounds:TypeBound* % "+" "+"? }
12+
| Lifetime:{ lt:LIFETIME ":" bounds:LifetimeBound* %% "+" }
13+
| Type:{ binder:ForAllBinder? ty:Type ":" bounds:TypeBound* %% "+" }
1414
// unstable(#20041):
1515
| TypeEq:{ binder:ForAllBinder? left:Type { "=" | "==" } right:Type }
1616
;
@@ -23,16 +23,17 @@ TypeBound =
2323
;
2424
TypeTraitBound = unbound:"?"? binder:ForAllBinder? path:Path;
2525

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

3132
// FIXME(eddyb) find a way to express this `A* B*` pattern better
3233
AngleBracketGenericArgsAndBindings =
33-
| Args:GenericArg+ % ","
34-
| Bindings:TypeBinding+ % ","
35-
| ArgsAndBindings:{ args:GenericArg+ % "," "," bindings:TypeBinding+ % "," }
34+
| Args:GenericArg+ %% ","
35+
| Bindings:TypeBinding+ %% ","
36+
| ArgsAndBindings:{ args:GenericArg+ % "," "," bindings:TypeBinding+ %% "," }
3637
;
3738

3839
GenericArg =

grammar/item.lyg

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ ItemKind =
1111
| Fn:{ header:FnHeader "fn" decl:FnDecl body:Block }
1212
| TypeAlias:{ "type" name:IDENT generics:Generics? where_clause:WhereClause? "=" ty:Type ";" }
1313
// unstable(existential_type):
14-
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* % "+" "+"? ";" }
15-
| Enum:{ "enum" name:IDENT generics:Generics? where_clause:WhereClause? "{" variants:EnumVariant* % "," ","? "}" }
14+
| ExistentialType:{ "existential" "type" name:IDENT generics:Generics? where_clause:WhereClause? ":" bounds:TypeBound* %% "+" ";" }
15+
| Enum:{ "enum" name:IDENT generics:Generics? where_clause:WhereClause? "{" variants:EnumVariant* %% "," "}" }
1616
| Struct:{ "struct" name:IDENT generics:Generics? body:StructBody }
17-
| Union:{ "union" name:IDENT generics:Generics? where_clause:WhereClause? "{" fields:RecordField* % "," ","? "}" }
17+
| Union:{ "union" name:IDENT generics:Generics? where_clause:WhereClause? "{" fields:RecordField* %% "," "}" }
1818
| Trait:{
1919
unsafety:"unsafe"?
2020
// unstable(optin_builtin_traits):
2121
auto:"auto"?
2222
"trait" name:IDENT generics:Generics?
23-
{ ":" superbounds:TypeBound* % "+" "+"? }?
23+
{ ":" superbounds:TypeBound* %% "+" }?
2424
where_clause:WhereClause? "{" trait_items:TraitItem* "}"
2525
}
2626
// unstable(trait_alias):
2727
| TraitAlias:{
2828
"trait" name:IDENT generics:Generics?
29-
{ ":" superbounds:TypeBound* % "+" "+"? }?
30-
where_clause:WhereClause? "=" bounds:TypeBound* % "+" "+"? ";"
29+
{ ":" superbounds:TypeBound* %% "+" }?
30+
where_clause:WhereClause? "=" bounds:TypeBound* %% "+" ";"
3131
}
3232
| Impl:{
3333
// unstable(specialization):
@@ -43,7 +43,7 @@ ItemKind =
4343

4444
UseTree =
4545
| Glob:{ prefix:UseTreePrefix? "*" }
46-
| Nested:{ prefix:UseTreePrefix? "{" children:UseTree* % "," ","? "}" }
46+
| Nested:{ prefix:UseTreePrefix? "{" children:UseTree* %% "," "}" }
4747
| Simple:{ path:Path { "as" rename:IDENT }? }
4848
;
4949
UseTreePrefix =
@@ -65,7 +65,7 @@ TraitItem = attrs:OuterAttr* kind:TraitItemKind;
6565
TraitItemKind =
6666
| Const:{ "const" name:IDENT ":" ty:Type { "=" default:Expr }? ";" }
6767
| Fn:{ header:FnHeader "fn" decl:FnDecl { default_body:Block | ";" } }
68-
| Type:{ "type" name:IDENT generics:Generics? { ":" bounds:TypeBound* % "+" "+"? }? where_clause:WhereClause? { "=" default:Type }? ";" }
68+
| Type:{ "type" name:IDENT generics:Generics? { ":" bounds:TypeBound* %% "+" }? where_clause:WhereClause? { "=" default:Type }? ";" }
6969
| MacroCall:ItemMacroCall
7070
;
7171

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

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

9090
// FIXME(eddyb) find a way to express this `A* B?` pattern better
9191
FnArgs =
92-
| Regular:FnArg+ % ","
92+
| Regular:FnArg+ %% ","
9393
| Variadic:"..."
9494
| RegularAndVariadic:{ args:FnArg+ % "," "," "..." }
9595
;
@@ -102,16 +102,16 @@ FnArg =
102102
EnumVariant = attrs:OuterAttr* name:IDENT kind:EnumVariantKind { "=" discr:Expr }?;
103103
EnumVariantKind =
104104
| Unit:{}
105-
| Tuple:{ "(" fields:TupleField* % "," ","? ")" }
106-
| Record:{ "{" fields:RecordField* % "," ","? "}" }
105+
| Tuple:{ "(" fields:TupleField* %% "," ")" }
106+
| Record:{ "{" fields:RecordField* %% "," "}" }
107107
;
108108

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

117117
TupleField = attrs:OuterAttr* vis:Vis? ty:Type;

grammar/pat.lyg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Pat =
99
| Ref:{ "&" mutable:"mut"? pat:Pat }
1010
// unstable(box_patterns):
1111
| Box:{ "box" pat:Pat }
12-
| Slice:{ "[" elems:SlicePatElem* % "," ","? "]" }
13-
| Tuple:{ "(" fields:TuplePatField* % "," ","? ")" }
12+
| Slice:{ "[" elems:SlicePatElem* %% "," "]" }
13+
| Tuple:{ "(" fields:TuplePatField* %% "," ")" }
1414
| Path:QPath
15-
| TupleStruct:{ path:Path "(" fields:TuplePatField* % "," ","? ")" }
15+
| TupleStruct:{ path:Path "(" fields:TuplePatField* %% "," ")" }
1616
| Struct:{ path:Path "{" fields:StructPatFieldsAndEllipsis "}" }
1717
| MacroCall:MacroCall
1818
;
@@ -36,7 +36,7 @@ TuplePatField =
3636

3737
// FIXME(eddyb) find a way to express this `A* B?` pattern better
3838
StructPatFieldsAndEllipsis =
39-
| Fields:{ fields:StructPatField* % "," ","? }
39+
| Fields:StructPatField* %% ","
4040
| Ellipsis:{ ".." }
4141
| FieldsAndEllipsis:{ fields:StructPatField+ % "," "," ".." }
4242
;

grammar/type.lyg

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ Type =
66
| Ref:{ "&" lt:LIFETIME? mutable:"mut"? pointee:Type }
77
| Slice:{ "[" elem:Type "]" }
88
| Array:{ "[" elem:Type ";" len:Expr "]" }
9-
| Tuple:{ "(" tys:Type* % "," ","? ")" }
9+
| Tuple:{ "(" tys:Type* %% "," ")" }
1010
| Path:QPath
1111
| FnPtr:{
1212
binder:ForAllBinder? unsafety:"unsafe"? { "extern" abi:Abi }?
13-
"fn" "(" inputs:FnSigInputs? ","? ")" { "->" ret_ty:Type }?
13+
"fn" "(" inputs:FnSigInputs? ")" { "->" ret_ty:Type }?
1414
}
15-
| ImplTrait:{ "impl" bounds:TypeBound+ % "+" "+"? }
16-
| DynTrait:{ "dyn"? bounds:TypeBound+ % "+" "+"? }
15+
| ImplTrait:{ "impl" bounds:TypeBound+ %% "+" }
16+
| DynTrait:{ "dyn"? bounds:TypeBound+ %% "+" }
1717
// unstable(not exposed to users):
1818
| TypeOf:{ "typeof" "(" expr:Expr ")" }
1919
| MacroCall:MacroCall
2020
;
2121

2222
FnSigInputs =
23-
| Regular:FnSigInput+ % ","
23+
| Regular:FnSigInput+ %% ","
2424
| Variadic:"..."
2525
| RegularAndVariadic:{ inputs:FnSigInput+ % "," "," "..." }
2626
;

src/bin/coverage.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ fn report_file_result(
139139
"(missing location information; \
140140
set `RUSTFLAGS='--cfg procmacro2_semver_exempt'`)"
141141
);
142+
142143
}
143-
eprintln!("Expected: {:?}", error.expected);
144+
145+
// HACK(eddyb) this is inefficient - `expected` should be already
146+
// sorted for us, so this is a temporary workaround.
147+
let mut expected = error.expected.clone();
148+
expected.sort_by_cached_key(|x| format!("{:?}", x));
149+
150+
eprintln!("Expected: {:?}", expected);
144151
}
145152
(Err(Error::Lex(e)), _) => eprintln!("FAIL ({:?})", e),
146153
}

src/bin/snapshots.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ use {
99
walkdir::WalkDir,
1010
};
1111

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

1624
macro_rules! snapshot {
1725
($production:ident, $src:expr) => {{
1826
let tts = $src
1927
.parse::<proc_macro2::TokenStream>()
2028
.expect("tokenization");
21-
to_debug_str(&parse::$production::parse(tts))
29+
parse_result_to_str(parse::$production::parse(tts))
2230
}};
2331
}
2432

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
created: "2019-06-02T15:47:35.179319Z"
3+
creator: insta@0.8.1
4+
source: src/bin/snapshots.rs
5+
expression: forest
6+
---
7+
Ok(
8+
GenericArgs::AngleBracket {
9+
args_and_bindings: AngleBracketGenericArgsAndBindings::ArgsAndBindings {
10+
args: [
11+
GenericArg::Lifetime | GenericArg::Type(
12+
Type::DynTrait {
13+
bounds: [
14+
TypeBound::Outlives,
15+
],
16+
},
17+
),
18+
GenericArg::Type(
19+
Type::Path(
20+
QPath::Unqualified(
21+
Path {
22+
path: RelativePath {
23+
segments: [
24+
PathSegment {
25+
ident: _,
26+
},
27+
],
28+
},
29+
},
30+
),
31+
) | Type::DynTrait {
32+
bounds: [
33+
TypeBound::Trait(
34+
TypeTraitBound {
35+
path: Path {
36+
path: RelativePath {
37+
segments: [
38+
PathSegment {
39+
ident: _,
40+
},
41+
],
42+
},
43+
},
44+
},
45+
),
46+
],
47+
},
48+
),
49+
],
50+
bindings: [
51+
TypeBinding {
52+
name: _,
53+
ty: Type::Path(
54+
QPath::Unqualified(
55+
Path {
56+
path: RelativePath {
57+
segments: [
58+
PathSegment {
59+
ident: _,
60+
},
61+
],
62+
},
63+
},
64+
),
65+
) | Type::DynTrait {
66+
bounds: [
67+
TypeBound::Trait(
68+
TypeTraitBound {
69+
path: Path {
70+
path: RelativePath {
71+
segments: [
72+
PathSegment {
73+
ident: _,
74+
},
75+
],
76+
},
77+
},
78+
},
79+
),
80+
],
81+
},
82+
},
83+
],
84+
},
85+
},
86+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
created: "2019-08-24T22:44:34.221417271Z"
3+
creator: insta@0.7.4
4+
source: src/bin/snapshots.rs
5+
expression: forest
6+
---
7+
Err(
8+
ParseError {
9+
at: Span,
10+
expected: [
11+
[
12+
Ident(
13+
None,
14+
),
15+
],
16+
[
17+
Punct {
18+
ch: Some(
19+
'>',
20+
),
21+
joint: None,
22+
},
23+
],
24+
],
25+
},
26+
)

0 commit comments

Comments
 (0)