Skip to content

Commit

Permalink
nixd/Syntax: actions for string_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 16, 2023
1 parent 9ceaa93 commit 04b28ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
12 changes: 10 additions & 2 deletions nixd/include/nixd/Syntax/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ struct Int : Node {};

struct Float : Node {};

struct StringParts : Node {};
struct InterpExpr : Node {
Node *Body;
};

struct StringPartsInterpolated : Node {};
struct String : Node {
std::string S;
};

struct ConcatStrings : Node {
std::vector<Node *> SubStrings;
};

struct List : Node {
std::vector<Node *> Elems;
Expand Down
59 changes: 49 additions & 10 deletions nixd/lib/Syntax/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
nixd::syntax::Binds *Binds;
nixd::syntax::AttrPath *AttrPath;
nixd::syntax::Call *Call;
nixd::syntax::ConcatStrings *ConcatStrings;
nixd::syntax::String *String;
nixd::syntax::InterpExpr *InterpExpr;

// Tokens
nixd::syntax::StringToken STR;
Expand All @@ -37,6 +40,10 @@


%type <Node> start expr expr_function expr_if expr_op expr_select expr_simple
%type <Node> string_parts
%type <String> string;
%type <ConcatStrings> string_parts_interpolated
%type <InterpExpr> string_parts_interp_expr
%type <Identifier> identifier
%type <Formals> formals
%type <Binds> binds
Expand Down Expand Up @@ -213,18 +220,34 @@ expr_simple
| '{' binds '}'
| '[' expr_list ']'


string_parts
: STR
| string_parts_interpolated
|
: string {
$$ = $1;
}
| string_parts_interpolated {
$$ = $1;
}


string_parts_interpolated
: string_parts_interpolated STR
| string_parts_interpolated DOLLAR_CURLY expr '}'
| DOLLAR_CURLY expr '}'
| STR DOLLAR_CURLY expr '}'
: string_parts_interpolated string {
$$->SubStrings.emplace_back($2);
$$->Range = mkRange(yylloc, *Data);
}
| string_parts_interpolated string_parts_interp_expr {
$$->SubStrings.emplace_back($2);
$$->Range = mkRange(yylloc, *Data);
}
| string_parts_interp_expr {
$$ = decorateNode(new ConcatStrings{
.SubStrings = {$1}
}, yylloc, *Data);
}
| string string_parts_interp_expr {
$$ = decorateNode(new ConcatStrings{
.SubStrings = {$1, $2}
}, yylloc, *Data);
}


path_start
Expand All @@ -234,9 +257,25 @@ path_start

ind_string_parts
: ind_string_parts IND_STR
| ind_string_parts DOLLAR_CURLY expr '}'
| ind_string_parts string_parts_interp_expr
|

// Nixd extension, nix uses the token directly
string
: STR {
$$ = decorateNode(new String{
.S = std::string($1)
}, yylloc, *Data);
}

// Nixd extension
// nix: DOLLAR_CURLY expr '}'
string_parts_interp_expr
: DOLLAR_CURLY expr '}' {
$$ = decorateNode(new InterpExpr{
.Body = $2
}, yylloc, *Data);
}

identifier
: ID {
Expand Down Expand Up @@ -275,7 +314,7 @@ attr

string_attr
: '"' string_parts '"'
| DOLLAR_CURLY expr '}'
| string_parts_interp_expr


expr_list
Expand Down

0 comments on commit 04b28ac

Please sign in to comment.