Skip to content

Commit

Permalink
nixd/Syntax: actions for ind_strings
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 16, 2023
1 parent 04b28ac commit ecbc5a6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
20 changes: 17 additions & 3 deletions nixd/include/nixd/Syntax/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "Range.h"
#include "value.hh"

#include <nix/nixexpr.hh>

Expand Down Expand Up @@ -64,12 +65,16 @@ struct Apply : Node {};
struct Simple : Node {};

struct Variable : Node {
Identifier ID;
Identifier *ID;
};

struct Int : Node {};
struct Int : Node {
nix::NixInt N;
};

struct Float : Node {};
struct Float : Node {
nix::NixFloat NF;
};

struct InterpExpr : Node {
Node *Body;
Expand All @@ -79,6 +84,15 @@ struct String : Node {
std::string S;
};

// String formals, but indented
struct IndString : Node {
std::string S;
};

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

struct ConcatStrings : Node {
std::vector<Node *> SubStrings;
};
Expand Down
53 changes: 44 additions & 9 deletions nixd/lib/Syntax/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
nixd::syntax::ConcatStrings *ConcatStrings;
nixd::syntax::String *String;
nixd::syntax::InterpExpr *InterpExpr;
nixd::syntax::IndStringParts *IndStringParts;

// Tokens
nixd::syntax::StringToken STR;
Expand All @@ -41,10 +42,11 @@

%type <Node> start expr expr_function expr_if expr_op expr_select expr_simple
%type <Node> string_parts
%type <String> string;
%type <String> string ind_string;
%type <ConcatStrings> string_parts_interpolated
%type <InterpExpr> string_parts_interp_expr
%type <Identifier> identifier
%type <IndStringParts> ind_string_parts
%type <Formals> formals
%type <Binds> binds
%type <AttrPath> attrpath
Expand Down Expand Up @@ -205,11 +207,30 @@ expr_select


expr_simple
: identifier
| INT
| FLOAT
| '"' string_parts '"'
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE
: identifier {
// Note: __curPos is actually not a "variable",
// but for now we treat as if it is,
// and convert it to ExprPos in the lowering process.
$$ = decorateNode(new Variable{
.ID = $1
}, yylloc, *Data);
}
| INT {
$$ = decorateNode(new Int {
.N = $1
}, yylloc, *Data);
}
| FLOAT {
$$ = decorateNode(new Float {
.NF = $1
}, yylloc, *Data);
}
| '"' string_parts '"' {
$$ = $2;
}
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
$$ = $2;
}
| path_start PATH_END
| path_start string_parts_interpolated PATH_END
| SPATH
Expand Down Expand Up @@ -256,9 +277,15 @@ path_start


ind_string_parts
: ind_string_parts IND_STR
| ind_string_parts string_parts_interp_expr
|
: ind_string_parts ind_string {
$$->SubStrings.emplace_back($2);
$$->Range = mkRange(yylloc, *Data);
}
| ind_string_parts string_parts_interp_expr {
$$->SubStrings.emplace_back($2);
$$->Range = mkRange(yylloc, *Data);
}
| { $$ = decorateNode(new IndStringParts, yylloc, *Data); }

// Nixd extension, nix uses the token directly
string
Expand All @@ -268,6 +295,14 @@ string
}, yylloc, *Data);
}

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

// Nixd extension
// nix: DOLLAR_CURLY expr '}'
string_parts_interp_expr
Expand Down

0 comments on commit ecbc5a6

Please sign in to comment.