diff --git a/nixd/include/nixd/Syntax/Nodes.h b/nixd/include/nixd/Syntax/Nodes.h index 202693d5e..6fe91c926 100644 --- a/nixd/include/nixd/Syntax/Nodes.h +++ b/nixd/include/nixd/Syntax/Nodes.h @@ -2,6 +2,7 @@ #pragma once #include "Range.h" +#include "value.hh" #include @@ -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; @@ -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 SubStrings; +}; + struct ConcatStrings : Node { std::vector SubStrings; }; diff --git a/nixd/lib/Syntax/Parser/Parser.y b/nixd/lib/Syntax/Parser/Parser.y index 23ba52357..7b8f96858 100644 --- a/nixd/lib/Syntax/Parser/Parser.y +++ b/nixd/lib/Syntax/Parser/Parser.y @@ -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; @@ -41,10 +42,11 @@ %type start expr expr_function expr_if expr_op expr_select expr_simple %type string_parts -%type string; +%type string ind_string; %type string_parts_interpolated %type string_parts_interp_expr %type identifier +%type ind_string_parts %type formals %type binds %type attrpath @@ -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 @@ -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 @@ -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