Skip to content

Commit

Permalink
libexpr: Rearrange lexer files so that yylex_init_extra can be found
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Jul 9, 2024
1 parent 0795bb4 commit 3800736
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
31 changes: 31 additions & 0 deletions src/libexpr/lexer-helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "lexer-tab.hh"
#include "lexer-helpers.hh"
#include "parser-tab.hh"

void nix::lexer::internal::initLoc(YYLTYPE * loc)
{
loc->first_line = loc->last_line = 0;
loc->first_column = loc->last_column = 0;
}

void nix::lexer::internal::adjustLoc(yyscan_t yyscanner, YYLTYPE * loc, const char * s, size_t len)
{
loc->stash();

LexerState & lexerState = *yyget_extra(yyscanner);

if (lexerState.docCommentDistance == 1) {
// Preceding token was a doc comment.
ParserLocation doc;
doc.first_column = lexerState.lastDocCommentLoc.first_column;
ParserLocation docEnd;
docEnd.first_column = lexerState.lastDocCommentLoc.last_column;
DocComment docComment{lexerState.at(doc), lexerState.at(docEnd)};
PosIdx locPos = lexerState.at(*loc);
lexerState.positionToDocComment.emplace(locPos, docComment);
}
lexerState.docCommentDistance++;

loc->first_column = loc->last_column;
loc->last_column += len;
}
9 changes: 9 additions & 0 deletions src/libexpr/lexer-helpers.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

namespace nix::lexer::internal {

void initLoc(YYLTYPE * loc);

void adjustLoc(yyscan_t yyscanner, YYLTYPE * loc, const char * s, size_t len);

} // namespace nix::lexer
43 changes: 9 additions & 34 deletions src/libexpr/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
%x INPATH_SLASH
%x PATH_START

%top {
#include "parser-tab.hh" // YYSTYPE
#include "parser-state.hh"
}

%{
#ifdef __clang__
Expand All @@ -22,48 +26,19 @@

#include "nixexpr.hh"
#include "parser-tab.hh"
#include "lexer-helpers.hh"

// !!! FIXME !!!
#define YY_EXTRA_TYPE ::nix::LexerState *
int yylex_init_extra ( YY_EXTRA_TYPE user_defined, yyscan_t* scanner);
YY_EXTRA_TYPE yyget_extra ( yyscan_t yyscanner );
#undef YY_EXTRA_TYPE
namespace nix {
struct LexerState;
}

using namespace nix;
using namespace nix::lexer::internal;

namespace nix {

#define CUR_POS state->at(*yylloc)

static void initLoc(YYLTYPE * loc)
{
loc->first_line = loc->last_line = 0;
loc->first_column = loc->last_column = 0;
}

static void adjustLoc(yyscan_t yyscanner, YYLTYPE * loc, const char * s, size_t len)
{
loc->stash();

LexerState & lexerState = *yyget_extra(yyscanner);

if (lexerState.docCommentDistance == 1) {
// Preceding token was a doc comment.
ParserLocation doc;
doc.first_column = lexerState.lastDocCommentLoc.first_column;
ParserLocation docEnd;
docEnd.first_column = lexerState.lastDocCommentLoc.last_column;
DocComment docComment{lexerState.at(doc), lexerState.at(docEnd)};
PosIdx locPos = lexerState.at(*loc);
lexerState.positionToDocComment.emplace(locPos, docComment);
}
lexerState.docCommentDistance++;

loc->first_column = loc->last_column;
loc->last_column += len;
}


// we make use of the fact that the parser receives a private copy of the input
// string and can munge around in it.
static StringToken unescapeStr(SymbolTable & symbols, char * s, size_t length)
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ sources = files(
'function-trace.cc',
'get-drvs.cc',
'json-to-value.cc',
'lexer-helpers.cc',
'nixexpr.cc',
'paths.cc',
'primops.cc',
Expand All @@ -165,6 +166,7 @@ headers = [config_h] + files(
'gc-small-vector.hh',
'get-drvs.hh',
'json-to-value.hh',
# internal: 'lexer-helpers.hh',
'nixexpr.hh',
'parser-state.hh',
'pos-idx.hh',
Expand Down

0 comments on commit 3800736

Please sign in to comment.