refactor(3p/nix/libexpr): Move some code out of lexer.l

Moves a function that is not dependent on the generated code over to
parser.hh. This function also looks like it could be improved, but
that is left as an exercise for the reader.

Code that remains in lexer.l has been reformatted, while we're here.

Change-Id: I9c26bb4eed0772a720d0715029e8bc10ab16ac38
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1279
Tested-by: BuildkiteCI
Reviewed-by: Kane York <rikingcoding@gmail.com>
This commit is contained in:
Vincent Ambo 2020-07-19 02:26:42 +01:00 committed by tazjin
parent 9f5f71616a
commit 105ad71015
2 changed files with 52 additions and 51 deletions

View file

@ -14,23 +14,20 @@
%{
#include <boost/lexical_cast.hpp>
#include "libexpr/nixexpr.hh"
#include "generated/parser-tab.hh"
#include "libexpr/nixexpr.hh"
#include "libexpr/parser.hh"
using namespace nix;
namespace nix {
static void initLoc(YYLTYPE * loc)
{
static void initLoc(YYLTYPE* loc) {
loc->first_line = loc->last_line = 1;
loc->first_column = loc->last_column = 1;
}
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
{
static void adjustLoc(YYLTYPE* loc, const char* s, size_t len) {
loc->first_line = loc->last_line;
loc->first_column = loc->last_column;
@ -50,32 +47,6 @@ static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
}
}
static Expr * unescapeStr(SymbolTable & symbols, const char * s, size_t length)
{
std::string t;
t.reserve(length);
char c;
while ((c = *s++)) {
if (c == '\\') {
assert(*s);
c = *s++;
if (c == 'n') { t += '\n'; }
else if (c == 'r') { t += '\r'; }
else if (c == 't') { t += '\t'; }
else t += c;
}
else if (c == '\r') {
/* Normalise CR and CR/LF into LF. */
t += '\n';
if (*s == '\n') { s++; } /* cr/lf */
}
else t += c;
}
return new ExprString(symbols.Create(t));
}
}
#define YY_USER_INIT initLoc(yylloc)

View file

@ -19,7 +19,7 @@
namespace nix {
struct ParseData {
struct ParseData : public gc {
EvalState& state;
SymbolTable& symbols;
Expr* result;
@ -55,4 +55,34 @@ Expr* stripIndentation(const Pos& pos, SymbolTable& symbols,
Path resolveExprPath(Path path);
// implementations originally from lexer.l
static Expr* unescapeStr(SymbolTable& symbols, const char* s, size_t length) {
std::string t;
t.reserve(length);
char c;
while ((c = *s++)) {
if (c == '\\') {
assert(*s);
c = *s++;
if (c == 'n') {
t += '\n';
} else if (c == 'r') {
t += '\r';
} else if (c == 't') {
t += '\t';
} else
t += c;
} else if (c == '\r') {
/* Normalise CR and CR/LF into LF. */
t += '\n';
if (*s == '\n') {
s++;
} /* cr/lf */
} else
t += c;
}
return new ExprString(symbols.Create(t));
}
} // namespace nix