Skip to content

Commit

Permalink
nixd/Syntax: actions for formal/formals
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 17, 2023
1 parent dcbb181 commit bd08b2a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
6 changes: 3 additions & 3 deletions nixd/include/nixd/Syntax/Nodes.inc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifdef NODE
NODE(Identifier, { nix::Symbol Symbol; })
NODE(Formal, {
Identifier ID;
Identifier *ID;
/// The default argument.
Node *Default;
})
NODE(Formals, {
std::vector<Formal> Formals;
bool Elipsis;
std::vector<Formal *> Formals;
bool Ellipsis;
})
NODE(Function, {
Identifier *Arg;
Expand Down
43 changes: 37 additions & 6 deletions nixd/lib/Syntax/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
nixd::syntax::Identifier *Identifier;
nixd::syntax::If *If;
nixd::syntax::Formals *Formals;
nixd::syntax::Formal *Formal;
nixd::syntax::Binds *Binds;
nixd::syntax::AttrPath *AttrPath;
nixd::syntax::Call *Call;
Expand Down Expand Up @@ -50,6 +51,7 @@
%type <Identifier> identifier
%type <IndStringParts> ind_string_parts
%type <Formals> formals
%type <Formal> formal
%type <Binds> binds
%type <AttrPath> attrpath
%type <Call> expr_app
Expand Down Expand Up @@ -353,6 +355,7 @@ string_parts_interp_expr
}, yylloc, *Data);
}

// Nixd extension, nix uses the token directly
uri
: URI {
$$ = decorateNode(new nixd::syntax::URI {
Expand Down Expand Up @@ -406,15 +409,43 @@ expr_list


formals
: formal ',' formals
| formal
|
| ELLIPSIS
: formal ',' formals {
$$->Formals.emplace_back($1);
$$->Range = mkRange(yylloc, *Data);
}
| formal {
$$ = decorateNode(new Formals {
.Formals = {$1},
.Ellipsis = false
}, yylloc, *Data);
}
| {
$$ = decorateNode(new Formals {
.Formals = {},
.Ellipsis = false
}, yylloc, *Data);
}
| ELLIPSIS {
$$ = decorateNode(new Formals {
.Formals = {},
.Ellipsis = true
}, yylloc, *Data);
}


formal
: identifier
| identifier '?' expr
: identifier {
$$ = decorateNode(new Formal {
.ID = $1,
.Default = nullptr
}, yylloc, *Data);
}
| identifier '?' expr {
$$ = decorateNode(new Formal {
.ID = $1,
.Default = $3
}, yylloc, *Data);
}


%%

0 comments on commit bd08b2a

Please sign in to comment.