Skip to content

Commit

Permalink
Updated rust grammar to use our new treesitter-esque cross-sort form …
Browse files Browse the repository at this point in the history
…referencing
  • Loading branch information
green726 committed Aug 28, 2024
1 parent 46a600a commit 0ae2e28
Showing 1 changed file with 28 additions and 46 deletions.
74 changes: 28 additions & 46 deletions src/rust/Grammar.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ let c = (~p=Padding.none, s) => t(Label.const(~padding=p, s));
let abi = t(Id_lower);

//Path exps
let typ_atom = nt(Sort.of_str("Typ"));

let path_ident_segment =
alt([
t(Id_lower),
Expand All @@ -47,53 +45,21 @@ let path_ident_segment =
c("$crate"),
]);

let typ_path_fn_inputs =
seq([typ_atom, star(seq([c(","), typ_atom])), opt(c(","))]);
let typ_path_fn =
seq([
c("("),
opt(typ_path_fn_inputs),
c(")"),
opt(seq([c("->"), typ_atom])),
]);
let typ_path_segment =
seq([path_ident_segment, opt(seq([c("::"), typ_path_fn]))]);
let typ_path =
seq([
opt(c("::")),
typ_path_segment,
star(seq([c("::"), typ_path_segment])),
]);

let path_exp_segment = seq([path_ident_segment]);
let path_in_exp =
seq([
opt(c("::")),
path_exp_segment,
star(seq([c("::"), path_exp_segment])),
]);

let qualified_path_typ =
seq([
c("<"),
nt(Sort.of_str("Typ")),
opt(seq([c("as"), typ_path])),
c(">"),
]);

let qualified_path_in_exp =
seq([qualified_path_typ, plus(seq([c("::"), path_exp_segment]))]);
let path_exp = alt([path_in_exp, qualified_path_in_exp]);
module Filter = {
//Whitelisted strings
type t = list(string);
};

module type SORT = {
let atom: unit => Regex.t;
let atom: (~filter: Filter.t=?, unit) => Regex.t;

let sort: unit => Sort.t;
let tbl: unit => Prec.Table.t(Regex.t);
};

module rec Stat: SORT = {
let sort = () => Sort.of_str("Stat");
let atom = () => nt(sort());
let atom = (~filter as _: Filter.t=[], ()) => nt(sort());

let block_exp = seq([c("{"), plus(atom()), c("}")]);

Expand All @@ -111,7 +77,7 @@ module rec Stat: SORT = {
}
and Item: SORT = {
let sort = () => Sort.of_str("Item");
let atom = () => nt(sort());
let atom = (~filter as _: Filter.t=[], ()) => nt(sort());

[@warning "-32"]
let comma_sep = seq([atom(), star(seq([c(","), atom()]))]);
Expand Down Expand Up @@ -275,7 +241,7 @@ and Item: SORT = {
opt(c("unsafe")),
c("impl"),
opt(c("!")),
typ_path,
Typ.atom(~filter=["typ_path"], ()),
c("for"),
Typ.atom(),
]);
Expand Down Expand Up @@ -314,7 +280,7 @@ and Item: SORT = {
}
and Typ: SORT = {
let sort = () => Sort.of_str("Typ");
let atom = () => nt(sort());
let atom = (~filter as _: Filter.t=[], ()) => nt(sort());

let typ_path_fn_inputs =
seq([atom(), star(seq([c(","), atom()])), opt(c(","))]);
Expand Down Expand Up @@ -432,7 +398,7 @@ and Typ: SORT = {
}
and Exp: SORT = {
let sort = () => Sort.of_str("Exp");
let atom = () => nt(sort());
let atom = (~filter as _: Filter.t=[], ()) => nt(sort());

[@warning "-32"]
let comma_sep = seq([atom(), star(seq([c(","), atom()]))]);
Expand Down Expand Up @@ -546,7 +512,7 @@ and Exp: SORT = {
}
and Pat: SORT = {
let sort = () => Sort.of_str("Pat");
let atom = () => nt(sort());
let atom = (~filter as _: Filter.t=[], ()) => nt(sort());

let ident_pat =
seq([
Expand Down Expand Up @@ -575,6 +541,15 @@ and Pat: SORT = {
]),
struct_pat_et_cetera,
]);

let path_exp_segment = seq([path_ident_segment]);
let path_in_exp =
seq([
opt(c("::")),
path_exp_segment,
star(seq([c("::"), path_exp_segment])),
]);

let struct_pat =
seq([path_in_exp, c("{"), opt(struct_pat_elements), c("}")]);

Expand All @@ -598,6 +573,13 @@ and Pat: SORT = {
seq([atom(), star(seq([c(","), atom()])), opt(c(","))]);
let slice_pat = seq([c("["), opt(slice_pat_items), c("]")]);

let qualified_path_in_exp =
seq([
Typ.atom(~filter=["qualified_path_typ"], ()),
plus(seq([c("::"), path_exp_segment])),
]);

let path_exp = alt([path_in_exp, qualified_path_in_exp]);
let path_pat = path_exp;

let operand =
Expand Down

0 comments on commit 0ae2e28

Please sign in to comment.