about summary refs log tree commit diff
path: root/third_party/nix/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-16T19·10+0100
committertazjin <mail@tazj.in>2020-07-16T19·53+0000
commitd579ceb7830406ae743aa3da274017d5e8a7bfa7 (patch)
tree05585b9c9bd0936550553a429bfe6f289957aa67 /third_party/nix/src
parentcb3d9675084f735c099c211edc4c8472f97a0578 (diff)
refactor(3p/nix/libexpr): Extract parser header out of Yacc file r/1321
First step (of many?) towards extracting all the inline code from the
Yacc file and keeping it somewhere more accessible instead.

Note that none of this code has previously been touched by a linter or
formatter, pretty much ever, so as it is extracted it also undergoes
similar changes to the whole codebase after the initial fork.

Change-Id: If3b7181f22e3b3fd8c58dfa9befa7ee2896ea06d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1217
Tested-by: BuildkiteCI
Reviewed-by: isomer <isomer@tvl.fyi>
Diffstat (limited to 'third_party/nix/src')
-rw-r--r--third_party/nix/src/libexpr/CMakeLists.txt1
-rw-r--r--third_party/nix/src/libexpr/parser.hh47
-rw-r--r--third_party/nix/src/libexpr/parser.y55
3 files changed, 49 insertions, 54 deletions
diff --git a/third_party/nix/src/libexpr/CMakeLists.txt b/third_party/nix/src/libexpr/CMakeLists.txt
index 02bedc46fd7f..d2e991657b1c 100644
--- a/third_party/nix/src/libexpr/CMakeLists.txt
+++ b/third_party/nix/src/libexpr/CMakeLists.txt
@@ -29,6 +29,7 @@ set(HEADER_FILES
     json-to-value.hh
     names.hh
     nixexpr.hh
+    parser.hh
     primops.hh
     symbol-table.hh
     value.hh
diff --git a/third_party/nix/src/libexpr/parser.hh b/third_party/nix/src/libexpr/parser.hh
new file mode 100644
index 000000000000..ae0e932147f5
--- /dev/null
+++ b/third_party/nix/src/libexpr/parser.hh
@@ -0,0 +1,47 @@
+// Parser utilities for use in parser.y
+#pragma once
+
+// TODO(tazjin): Audit these includes, they were in parser.y
+#include <optional>
+#include <variant>
+
+#include <glog/logging.h>
+
+#include "libexpr/eval.hh"
+#include "libexpr/nixexpr.hh"
+#include "libutil/util.hh"
+
+#define YY_DECL                                                               \
+  int yylex(YYSTYPE* yylval_param, YYLTYPE* yylloc_param, yyscan_t yyscanner, \
+            nix::ParseData* data)
+
+namespace nix {
+
+struct ParseData {
+  EvalState& state;
+  SymbolTable& symbols;
+  Expr* result;
+  Path basePath;
+  std::optional<Symbol> path;
+  std::string error;
+  Symbol sLetBody;
+
+  ParseData(EvalState& state)
+      : state(state),
+        symbols(state.symbols),
+        sLetBody(symbols.Create("<let-body>")){};
+};
+
+// TODO(tazjin): move dupAttr to anonymous namespace
+static void dupAttr(const AttrPath& attrPath, const Pos& pos,
+                    const Pos& prevPos) {
+  throw ParseError(format("attribute '%1%' at %2% already defined at %3%") %
+                   showAttrPath(attrPath) % pos % prevPos);
+}
+
+static void dupAttr(Symbol attr, const Pos& pos, const Pos& prevPos) {
+  throw ParseError(format("attribute '%1%' at %2% already defined at %3%") %
+                   attr % pos % prevPos);
+}
+
+}  // namespace nix
diff --git a/third_party/nix/src/libexpr/parser.y b/third_party/nix/src/libexpr/parser.y
index 2ea84fea66b9..9df9b3081a26 100644
--- a/third_party/nix/src/libexpr/parser.y
+++ b/third_party/nix/src/libexpr/parser.y
@@ -11,44 +11,7 @@
 %expect 1
 %expect-rr 1
 
-%code requires {
-
-#ifndef BISON_HEADER
-#define BISON_HEADER
-
-#include <optional>
-#include <variant>
-#include "libutil/util.hh"
-#include "libexpr/nixexpr.hh"
-#include "libexpr/eval.hh"
-#include <glog/logging.h>
-
-namespace nix {
-
-    struct ParseData
-    {
-        EvalState & state;
-        SymbolTable & symbols;
-        Expr* result;
-        Path basePath;
-        std::optional<Symbol> path;
-        std::string error;
-        Symbol sLetBody;
-        ParseData(EvalState & state)
-            : state(state)
-            , symbols(state.symbols)
-            , sLetBody(symbols.Create("<let-body>"))
-            { };
-    };
-
-}
-
-#define YY_DECL int yylex \
-    (YYSTYPE * yylval_param, YYLTYPE * yylloc_param, yyscan_t yyscanner, nix::ParseData * data)
-
-#endif
-
-}
+%code requires { #include "libexpr/parser.hh" }
 
 %{
 
@@ -59,24 +22,8 @@ YY_DECL;
 
 using namespace nix;
 
-
 namespace nix {
 
-
-static void dupAttr(const AttrPath & attrPath, const Pos & pos, const Pos & prevPos)
-{
-    throw ParseError(format("attribute '%1%' at %2% already defined at %3%")
-        % showAttrPath(attrPath) % pos % prevPos);
-}
-
-
-static void dupAttr(Symbol attr, const Pos & pos, const Pos & prevPos)
-{
-    throw ParseError(format("attribute '%1%' at %2% already defined at %3%")
-        % attr % pos % prevPos);
-}
-
-
 static void addAttr(ExprAttrs * attrs, AttrPath & attrPath,
     Expr * e, const Pos & pos)
 {