Skip to content

Commit

Permalink
parser.y: GLR -> LALR
Browse files Browse the repository at this point in the history
  • Loading branch information
rhendric committed Jul 22, 2024
1 parent 758b92d commit edc83e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/libexpr/parser-state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct StringToken
operator std::string_view() const { return {p, l}; }
};

// This type must be trivially copyable; see YYLTYPE_IS_TRIVIAL in parser.y.
struct ParserLocation
{
int beginOffset;
Expand Down
17 changes: 13 additions & 4 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%glr-parser
%define api.location.type { ::nix::ParserLocation }
%define api.pure
%locations
%define parse.error verbose
Expand All @@ -9,7 +9,6 @@
%lex-param { void * scanner }
%lex-param { nix::ParserState * state }
%expect 0
%expect-rr 0

%code requires {

Expand All @@ -27,7 +26,17 @@
#include "eval-settings.hh"
#include "parser-state.hh"

#define YYLTYPE ::nix::ParserLocation
// Bison seems to have difficulty growing the parser stack when using C++ with
// a custom location type. This undocumented macro tells Bison that our
// location type is "trivially copyable" in C++-ese, so it is safe to use the
// same memcpy macro it uses to grow the stack that it uses with its own
// default location type. Without this, we get "error: memory exhausted" when
// parsing some large Nix files. Our other options are to increase the initial
// stack size (200 by default) to be as large as we ever want to support (so
// that growing the stack is unnecessary), or redefine the stack-relocation
// macro ourselves (which is also undocumented).
#define YYLTYPE_IS_TRIVIAL 1

#define YY_DECL int yylex \
(YYSTYPE * yylval_param, YYLTYPE * yylloc_param, yyscan_t yyscanner, nix::ParserState * state)

Expand Down Expand Up @@ -77,7 +86,7 @@ YY_DECL;

using namespace nix;

#define CUR_POS state->at(*yylocp)
#define CUR_POS state->at(yyloc)


void yyerror(YYLTYPE * loc, yyscan_t scanner, ParserState * state, const char * error)
Expand Down

0 comments on commit edc83e7

Please sign in to comment.