Skip to content
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
4 changes: 2 additions & 2 deletions handrolled_parser/parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1927,13 +1927,13 @@ fn State::parse_range_pattern(self : Self) -> @syntax.Pattern {
self.skip()
let rhs = self.parse_simple_pattern()
let loc = self.loc_start_with(spos)
Range(lhs~, rhs~, inclusive=true, loc~)
Range(lhs~, rhs~, kind=Inclusive, loc~)
}
RANGE_EXCLUSIVE => {
self.skip()
let rhs = self.parse_simple_pattern()
let loc = self.loc_start_with(spos)
Range(lhs~, rhs~, inclusive=false, loc~)
Range(lhs~, rhs~, kind=Exclusive, loc~)
}
_ => lhs
}
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"moonbitlang/x": "0.4.37"
},
"bin-deps": {
"moonbitlang/yacc": "0.7.5"
"moonbitlang/yacc": "0.7.7"
},
"readme": "README.md",
"repository": "https://github.com/moonbitlang/parser",
Expand Down
11 changes: 9 additions & 2 deletions syntax/ast.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
///|
pub(all) enum RangeKind {
Inclusive
Exclusive
InclusiveMissingEqual
}

///|
pub(all) enum Visibility {
Default
Expand Down Expand Up @@ -690,8 +697,8 @@ pub(all) enum Pattern {
Record(fields~ : @list.List[FieldPat], is_closed~ : Bool, loc~ : Location)
/// `{ "k1": e1, "k2": e2, .. }`
Map(elems~ : @list.List[MapPatElem], is_closed~ : Bool, loc~ : Location)
/// `p1..=p2` or `p1..<p2` if inclusive is false.
Range(lhs~ : Pattern, rhs~ : Pattern, inclusive~ : Bool, loc~ : Location)
/// `p1..=p2` or `p1..<p2`
Range(lhs~ : Pattern, rhs~ : Pattern, kind~ : RangeKind, loc~ : Location)
SpecialConstr(
binder~ : Binder,
args~ : @list.List[ConstrPatArg],
Expand Down
4 changes: 2 additions & 2 deletions syntax/ast_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ test "pattern to_json" {
@syntax.Range(
lhs=any_pattern,
rhs=constant_pattern,
inclusive=true,
kind=Inclusive,
loc=dummy_loc,
),
content={
Expand All @@ -1712,7 +1712,7 @@ test "pattern to_json" {
"c": { "type": "Constant::Int", "0": "42" },
"loc": null,
},
"inclusive": true,
"kind": { "type": "RangeKind::Inclusive" },
"loc": null,
},
)
Expand Down
12 changes: 10 additions & 2 deletions syntax/ast_to_json.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
///|
let null : Json = Json::null()

///|
pub impl ToJson for RangeKind with to_json(self) -> Json {
match self {
Inclusive | InclusiveMissingEqual => { "type": "RangeKind::Inclusive" }
Exclusive => { "type": "RangeKind::Exclusive" }
}
}

///|
pub impl ToJson for Label with to_json(self) {
{ "type": "Label", "name": self.name, "loc": self.loc.to_json() }
Expand Down Expand Up @@ -695,12 +703,12 @@ pub impl ToJson for Pattern with to_json(self) {
"is_closed": is_closed,
"loc": loc,
}
Range(lhs~, rhs~, inclusive~, loc~) =>
Range(lhs~, rhs~, kind~, loc~) =>
{
"type": "Pattern::Range",
"lhs": lhs,
"rhs": rhs,
"inclusive": inclusive,
"kind": kind,
"loc": loc,
}
SpecialConstr(binder~, args~, loc~) =>
Expand Down
31 changes: 24 additions & 7 deletions syntax/iter_visitor.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ pub(open) trait IterVisitor {
// MapPatElem
visit_MapPatElem(Self, MapPatElem) -> Unit = _

// RangeKind
visit_RangeKind(Self, RangeKind) -> Unit = _

// Pattern
visit_Pattern(Self, Pattern) -> Unit = _
visit_Pattern_Alias(Self, pat~ : Pattern, alias_~ : Binder, loc~ : Location) -> Unit = _
Expand Down Expand Up @@ -308,7 +311,7 @@ pub(open) trait IterVisitor {
Self,
lhs~ : Pattern,
rhs~ : Pattern,
inclusive~ : Bool,
kind~ : RangeKind,
loc~ : Location,
) -> Unit = _
visit_Pattern_SpecialConstr(
Expand Down Expand Up @@ -1983,6 +1986,11 @@ impl IterVisitor with visit_MapPatElem(env, map_pat_elem) {
IterVisitorBase::visit_MapPatElem(env, map_pat_elem)
}

///|
impl IterVisitor with visit_RangeKind(env, kind) {
IterVisitorBase::visit_RangeKind(env, kind)
}

///|
impl IterVisitor with visit_Pattern(env, pattern) {
IterVisitorBase::visit_Pattern(env, pattern)
Expand Down Expand Up @@ -2044,8 +2052,8 @@ impl IterVisitor with visit_Pattern_Map(env, elems~, is_closed~, loc~) {
}

///|
impl IterVisitor with visit_Pattern_Range(env, lhs~, rhs~, inclusive~, loc~) {
IterVisitorBase::visit_Pattern_Range(env, lhs~, rhs~, inclusive~, loc~)
impl IterVisitor with visit_Pattern_Range(env, lhs~, rhs~, kind~, loc~) {
IterVisitorBase::visit_Pattern_Range(env, lhs~, rhs~, kind~, loc~)
}

///|
Expand Down Expand Up @@ -3373,6 +3381,15 @@ pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_MapPatEl
env.0.visit_Pattern(map_pat_elem.pat)
}

///|
pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_RangeKind(
env,
kind,
) {
ignore(env)
ignore(kind)
}

///|
pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_Pattern(
env,
Expand All @@ -3394,8 +3411,8 @@ pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_Pattern(
env.0.visit_Pattern_Record(fields~, is_closed~, loc~)
Map(elems~, is_closed~, loc~) =>
env.0.visit_Pattern_Map(elems~, is_closed~, loc~)
Range(lhs~, rhs~, inclusive~, loc~) =>
env.0.visit_Pattern_Range(lhs~, rhs~, inclusive~, loc~)
Range(lhs~, rhs~, kind~, loc~) =>
env.0.visit_Pattern_Range(lhs~, rhs~, kind~, loc~)
SpecialConstr(binder~, args~, loc~) =>
env.0.visit_Pattern_SpecialConstr(binder~, args~, loc~)
}
Expand Down Expand Up @@ -3526,13 +3543,13 @@ pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_Pattern_
env,
lhs~,
rhs~,
inclusive~,
kind~,
loc~,
) {
ignore(inclusive)
ignore(loc)
env.0.visit_Pattern(lhs)
env.0.visit_Pattern(rhs)
env.0.visit_RangeKind(kind)
}

///|
Expand Down
32 changes: 25 additions & 7 deletions syntax/map_visitor.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ pub(open) trait MapVisitor {
// MapPatElem
visit_MapPatElem(Self, MapPatElem) -> MapPatElem = _

// RangeKind
visit_RangeKind(Self, RangeKind) -> RangeKind = _

// Pattern
visit_Pattern(Self, Pattern) -> Pattern = _
visit_Pattern_Alias(Self, pat~ : Pattern, alias_~ : Binder, loc~ : Location) -> Pattern = _
Expand Down Expand Up @@ -308,7 +311,7 @@ pub(open) trait MapVisitor {
Self,
lhs~ : Pattern,
rhs~ : Pattern,
inclusive~ : Bool,
kind~ : RangeKind,
loc~ : Location,
) -> Pattern = _
visit_Pattern_SpecialConstr(
Expand Down Expand Up @@ -1983,6 +1986,11 @@ impl MapVisitor with visit_MapPatElem(env, map_pat_elem) {
MapVisitorBase::visit_MapPatElem(env, map_pat_elem)
}

///|
impl MapVisitor with visit_RangeKind(env, kind) {
MapVisitorBase::visit_RangeKind(env, kind)
}

///|
impl MapVisitor with visit_Pattern(env, pattern) {
MapVisitorBase::visit_Pattern(env, pattern)
Expand Down Expand Up @@ -2044,8 +2052,8 @@ impl MapVisitor with visit_Pattern_Map(env, elems~, is_closed~, loc~) {
}

///|
impl MapVisitor with visit_Pattern_Range(env, lhs~, rhs~, inclusive~, loc~) {
MapVisitorBase::visit_Pattern_Range(env, lhs~, rhs~, inclusive~, loc~)
impl MapVisitor with visit_Pattern_Range(env, lhs~, rhs~, kind~, loc~) {
MapVisitorBase::visit_Pattern_Range(env, lhs~, rhs~, kind~, loc~)
}

///|
Expand Down Expand Up @@ -3440,6 +3448,15 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_MapPatElem(
MapPatElem::{ key, pat, match_absent, key_loc, loc }
}

///|
pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_RangeKind(
env,
kind,
) {
ignore(env)
kind
}

///|
pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Pattern(
env,
Expand All @@ -3461,8 +3478,8 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Pattern(
env.0.visit_Pattern_Record(fields~, is_closed~, loc~)
Map(elems~, is_closed~, loc~) =>
env.0.visit_Pattern_Map(elems~, is_closed~, loc~)
Range(lhs~, rhs~, inclusive~, loc~) =>
env.0.visit_Pattern_Range(lhs~, rhs~, inclusive~, loc~)
Range(lhs~, rhs~, kind~, loc~) =>
env.0.visit_Pattern_Range(lhs~, rhs~, kind~, loc~)
SpecialConstr(binder~, args~, loc~) =>
env.0.visit_Pattern_SpecialConstr(binder~, args~, loc~)
}
Expand Down Expand Up @@ -3591,12 +3608,13 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Pattern_Ran
env,
lhs~,
rhs~,
inclusive~,
kind~,
loc~,
) {
let lhs = env.0.visit_Pattern(lhs)
let rhs = env.0.visit_Pattern(rhs)
Range(lhs~, rhs~, inclusive~, loc~)
let kind = env.0.visit_RangeKind(kind)
Range(lhs~, rhs~, kind~, loc~)
}

///|
Expand Down
2 changes: 1 addition & 1 deletion syntax/map_visitor_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test "Pattern" {
let range_pattern = @syntax.Range(
lhs=any_pattern,
rhs=constant_pattern,
inclusive=true,
kind=Inclusive,
loc=dummy_loc,
)
let constant_pattern = @syntax.Expr::Constant(c=int_constant, loc=dummy_loc)
Expand Down
15 changes: 12 additions & 3 deletions syntax/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,19 @@ pub(all) enum Pattern {
Var(Binder)
Record(fields~ : @list.List[FieldPat], is_closed~ : Bool, loc~ : @basic.Location)
Map(elems~ : @list.List[MapPatElem], is_closed~ : Bool, loc~ : @basic.Location)
Range(lhs~ : Pattern, rhs~ : Pattern, inclusive~ : Bool, loc~ : @basic.Location)
Range(lhs~ : Pattern, rhs~ : Pattern, kind~ : RangeKind, loc~ : @basic.Location)
SpecialConstr(binder~ : Binder, args~ : @list.List[ConstrPatArg], loc~ : @basic.Location)
}
pub fn Pattern::loc(Self) -> @basic.Location
pub impl ToJson for Pattern

pub(all) enum RangeKind {
Inclusive
Exclusive
InclusiveMissingEqual
}
pub impl ToJson for RangeKind

pub(all) enum SpreadableElem {
Regular(Expr)
Spread(expr~ : Expr, loc~ : @basic.Location)
Expand Down Expand Up @@ -799,6 +806,7 @@ pub(open) trait IterVisitor {
visit_FieldPat(Self, FieldPat) -> Unit = _
visit_ConstrPatArg(Self, ConstrPatArg) -> Unit = _
visit_MapPatElem(Self, MapPatElem) -> Unit = _
visit_RangeKind(Self, RangeKind) -> Unit = _
visit_Pattern(Self, Pattern) -> Unit = _
visit_Pattern_Alias(Self, pat~ : Pattern, alias_~ : Binder, loc~ : @basic.Location) -> Unit = _
visit_Pattern_Any(Self, loc~ : @basic.Location) -> Unit = _
Expand All @@ -811,7 +819,7 @@ pub(open) trait IterVisitor {
visit_Pattern_Var(Self, Binder) -> Unit = _
visit_Pattern_Record(Self, fields~ : @list.List[FieldPat], is_closed~ : Bool, loc~ : @basic.Location) -> Unit = _
visit_Pattern_Map(Self, elems~ : @list.List[MapPatElem], is_closed~ : Bool, loc~ : @basic.Location) -> Unit = _
visit_Pattern_Range(Self, lhs~ : Pattern, rhs~ : Pattern, inclusive~ : Bool, loc~ : @basic.Location) -> Unit = _
visit_Pattern_Range(Self, lhs~ : Pattern, rhs~ : Pattern, kind~ : RangeKind, loc~ : @basic.Location) -> Unit = _
visit_Pattern_SpecialConstr(Self, binder~ : Binder, args~ : @list.List[ConstrPatArg], loc~ : @basic.Location) -> Unit = _
visit_LocalTypeDecl(Self, LocalTypeDecl) -> Unit = _
visit_DerivingDirective(Self, DerivingDirective) -> Unit = _
Expand Down Expand Up @@ -1012,6 +1020,7 @@ pub(open) trait MapVisitor {
visit_FieldPat(Self, FieldPat) -> FieldPat = _
visit_ConstrPatArg(Self, ConstrPatArg) -> ConstrPatArg = _
visit_MapPatElem(Self, MapPatElem) -> MapPatElem = _
visit_RangeKind(Self, RangeKind) -> RangeKind = _
visit_Pattern(Self, Pattern) -> Pattern = _
visit_Pattern_Alias(Self, pat~ : Pattern, alias_~ : Binder, loc~ : @basic.Location) -> Pattern = _
visit_Pattern_Any(Self, loc~ : @basic.Location) -> Pattern = _
Expand All @@ -1024,7 +1033,7 @@ pub(open) trait MapVisitor {
visit_Pattern_Var(Self, Binder) -> Pattern = _
visit_Pattern_Record(Self, fields~ : @list.List[FieldPat], is_closed~ : Bool, loc~ : @basic.Location) -> Pattern = _
visit_Pattern_Map(Self, elems~ : @list.List[MapPatElem], is_closed~ : Bool, loc~ : @basic.Location) -> Pattern = _
visit_Pattern_Range(Self, lhs~ : Pattern, rhs~ : Pattern, inclusive~ : Bool, loc~ : @basic.Location) -> Pattern = _
visit_Pattern_Range(Self, lhs~ : Pattern, rhs~ : Pattern, kind~ : RangeKind, loc~ : @basic.Location) -> Pattern = _
visit_Pattern_SpecialConstr(Self, binder~ : Binder, args~ : @list.List[ConstrPatArg], loc~ : @basic.Location) -> Pattern = _
visit_LocalTypeDecl(Self, LocalTypeDecl) -> LocalTypeDecl = _
visit_DerivingDirective(Self, DerivingDirective) -> DerivingDirective = _
Expand Down
8 changes: 4 additions & 4 deletions yacc_parser/parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22163,7 +22163,7 @@ fn yy_action_741(
}

// file:///./parser.mbty
// 1875| simple_pattern "..<" simple_pattern { Range(lhs = $1, rhs = $3, inclusive = false, loc = mk_loc($sloc)) }
// 1875| simple_pattern "..<" simple_pattern { Range(lhs = $1, rhs = $3, kind=Exclusive, loc = mk_loc($sloc)) }

///|
fn yy_action_742(
Expand All @@ -22184,7 +22184,7 @@ fn yy_action_742(
Range(
lhs=_dollar1,
rhs=_dollar3,
inclusive=false,
kind=Exclusive,
loc=mk_loc((_symbol_start_pos, _end_pos)),
)
},
Expand Down Expand Up @@ -22308,7 +22308,7 @@ fn yy_action_745(
}

// file:///./parser.mbty
// 1876| simple_pattern "..=" simple_pattern { Range(lhs = $1, rhs = $3, inclusive = true, loc = mk_loc($sloc)) }
// 1876| simple_pattern "..=" simple_pattern { Range(lhs = $1, rhs = $3, kind=Inclusive, loc = mk_loc($sloc)) }

///|
fn yy_action_746(
Expand All @@ -22329,7 +22329,7 @@ fn yy_action_746(
Range(
lhs=_dollar1,
rhs=_dollar3,
inclusive=true,
kind=Inclusive,
loc=mk_loc((_symbol_start_pos, _end_pos)),
)
},
Expand Down
Loading