1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

decouple parser and EvalState

there's no reason the parser itself should be doing semantic analysis
like bindVars. split this bit apart (retaining the previous name in
EvalState) and have the parser really do *only* parsing, decoupled from
EvalState.
This commit is contained in:
pennae 2024-01-15 16:52:18 +01:00
parent e1aa585964
commit b596cc9e79
4 changed files with 53 additions and 22 deletions

View file

@ -32,6 +32,19 @@
#define YY_DECL int yylex \
(YYSTYPE * yylval_param, YYLTYPE * yylloc_param, yyscan_t yyscanner, nix::ParserState * state)
namespace nix {
Expr * parseExprFromBuf(
char * text,
size_t length,
Pos::Origin origin,
const SourcePath & basePath,
SymbolTable & symbols,
PosTable & positions,
const ref<InputAccessor> rootFS);
}
#endif
}
@ -52,7 +65,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParserState * state, const char *
{
throw ParseError({
.msg = hintfmt(error),
.errPos = state->state.positions[state->at(*loc)]
.errPos = state->positions[state->at(*loc)]
});
}
@ -141,7 +154,7 @@ expr_function
{ if (!$2->dynamicAttrs.empty())
throw ParseError({
.msg = hintfmt("dynamic attributes not allowed in let"),
.errPos = state->state.positions[CUR_POS]
.errPos = state->positions[CUR_POS]
});
$$ = new ExprLet($2, $4);
}
@ -231,7 +244,7 @@ expr_simple
if (noURLLiterals)
throw ParseError({
.msg = hintfmt("URL literals are disabled"),
.errPos = state->state.positions[CUR_POS]
.errPos = state->positions[CUR_POS]
});
$$ = new ExprString(std::string($1));
}
@ -271,7 +284,7 @@ path_start
/* add back in the trailing '/' to the first segment */
if ($1.p[$1.l-1] == '/' && $1.l > 1)
path += "/";
$$ = new ExprPath(ref<InputAccessor>(state->state.rootFS), std::move(path));
$$ = new ExprPath(ref<InputAccessor>(state->rootFS), std::move(path));
}
| HPATH {
if (evalSettings.pureEval) {
@ -281,7 +294,7 @@ path_start
);
}
Path path(getHome() + std::string($1.p + 1, $1.l - 1));
$$ = new ExprPath(ref<InputAccessor>(state->state.rootFS), std::move(path));
$$ = new ExprPath(ref<InputAccessor>(state->rootFS), std::move(path));
}
;
@ -327,7 +340,7 @@ attrs
} else
throw ParseError({
.msg = hintfmt("dynamic attributes not allowed in inherit"),
.errPos = state->state.positions[state->at(@2)]
.errPos = state->positions[state->at(@2)]
});
}
| { $$ = new AttrPath; }
@ -394,19 +407,22 @@ formal
namespace nix {
Expr * EvalState::parse(
Expr * parseExprFromBuf(
char * text,
size_t length,
Pos::Origin origin,
const SourcePath & basePath,
std::shared_ptr<StaticEnv> & staticEnv)
SymbolTable & symbols,
PosTable & positions,
const ref<InputAccessor> rootFS)
{
yyscan_t scanner;
ParserState state {
.state = *this,
.symbols = symbols,
.positions = positions,
.basePath = basePath,
.origin = {origin},
.rootFS = rootFS,
};
yylex_init(&scanner);
@ -415,8 +431,6 @@ Expr * EvalState::parse(
yy_scan_buffer(text, length, scanner);
yyparse(scanner, &state);
state.result->bindVars(*this, staticEnv);
return state.result;
}