Skip to content

Commit b9f5324

Browse files
committed
libexpr: split yacc prologue & epilogue (NFC)
This is an NFC PR that splits epilogue & prologue from parser. As mentioned in #8812, we can add static checking tools & auto formatting for these files, but if it is written .y directly, the clang parser cannot understand the code is actually "C++".
1 parent 13a9090 commit b9f5324

File tree

8 files changed

+904
-920
lines changed

8 files changed

+904
-920
lines changed

src/libexpr/lexer.l renamed to src/libexpr/lexer/lexer.l

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,98 +15,7 @@
1515

1616

1717
%{
18-
#ifdef __clang__
19-
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
20-
#endif
21-
22-
#include <boost/lexical_cast.hpp>
23-
24-
#include "nixexpr.hh"
25-
#include "parser-tab.hh"
26-
27-
using namespace nix;
28-
29-
namespace nix {
30-
31-
static inline PosIdx makeCurPos(const YYLTYPE & loc, ParseData * data)
32-
{
33-
return data->state.positions.add(data->origin, loc.first_line, loc.first_column);
34-
}
35-
36-
#define CUR_POS makeCurPos(*yylloc, data)
37-
38-
// backup to recover from yyless(0)
39-
thread_local YYLTYPE prev_yylloc;
40-
41-
static void initLoc(YYLTYPE * loc)
42-
{
43-
loc->first_line = loc->last_line = 1;
44-
loc->first_column = loc->last_column = 1;
45-
}
46-
47-
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
48-
{
49-
prev_yylloc = *loc;
50-
51-
loc->first_line = loc->last_line;
52-
loc->first_column = loc->last_column;
53-
54-
for (size_t i = 0; i < len; i++) {
55-
switch (*s++) {
56-
case '\r':
57-
if (*s == '\n') { /* cr/lf */
58-
i++;
59-
s++;
60-
}
61-
/* fall through */
62-
case '\n':
63-
++loc->last_line;
64-
loc->last_column = 1;
65-
break;
66-
default:
67-
++loc->last_column;
68-
}
69-
}
70-
}
71-
72-
73-
// we make use of the fact that the parser receives a private copy of the input
74-
// string and can munge around in it.
75-
static StringToken unescapeStr(SymbolTable & symbols, char * s, size_t length)
76-
{
77-
char * result = s;
78-
char * t = s;
79-
char c;
80-
// the input string is terminated with *two* NULs, so we can safely take
81-
// *one* character after the one being checked against.
82-
while ((c = *s++)) {
83-
if (c == '\\') {
84-
c = *s++;
85-
if (c == 'n') *t = '\n';
86-
else if (c == 'r') *t = '\r';
87-
else if (c == 't') *t = '\t';
88-
else *t = c;
89-
}
90-
else if (c == '\r') {
91-
/* Normalise CR and CR/LF into LF. */
92-
*t = '\n';
93-
if (*s == '\n') s++; /* cr/lf */
94-
}
95-
else *t = c;
96-
t++;
97-
}
98-
return {result, size_t(t - result)};
99-
}
100-
101-
102-
}
103-
104-
#define YY_USER_INIT initLoc(yylloc)
105-
#define YY_USER_ACTION adjustLoc(yylloc, yytext, yyleng);
106-
107-
#define PUSH_STATE(state) yy_push_state(state, yyscanner)
108-
#define POP_STATE() yy_pop_state(yyscanner)
109-
18+
#include "lexer/prologue.inc"
11019
%}
11120

11221

src/libexpr/lexer/prologue.inc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#ifdef __clang__
2+
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
3+
#endif
4+
5+
#include <boost/lexical_cast.hpp>
6+
7+
#include "nixexpr.hh"
8+
#include "parser-tab.hh"
9+
10+
using namespace nix;
11+
12+
namespace nix {
13+
14+
static inline PosIdx makeCurPos(const YYLTYPE & loc, ParseData * data)
15+
{
16+
return data->state.positions.add(data->origin, loc.first_line, loc.first_column);
17+
}
18+
19+
#define CUR_POS makeCurPos(*yylloc, data)
20+
21+
// backup to recover from yyless(0)
22+
thread_local YYLTYPE prev_yylloc;
23+
24+
static void initLoc(YYLTYPE * loc)
25+
{
26+
loc->first_line = loc->last_line = 1;
27+
loc->first_column = loc->last_column = 1;
28+
}
29+
30+
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
31+
{
32+
prev_yylloc = *loc;
33+
34+
loc->first_line = loc->last_line;
35+
loc->first_column = loc->last_column;
36+
37+
for (size_t i = 0; i < len; i++) {
38+
switch (*s++) {
39+
case '\r':
40+
if (*s == '\n') { /* cr/lf */
41+
i++;
42+
s++;
43+
}
44+
/* fall through */
45+
case '\n':
46+
++loc->last_line;
47+
loc->last_column = 1;
48+
break;
49+
default:
50+
++loc->last_column;
51+
}
52+
}
53+
}
54+
55+
// we make use of the fact that the parser receives a private copy of the input
56+
// string and can munge around in it.
57+
static StringToken unescapeStr(SymbolTable & symbols, char * s, size_t length)
58+
{
59+
char * result = s;
60+
char * t = s;
61+
char c;
62+
// the input string is terminated with *two* NULs, so we can safely take
63+
// *one* character after the one being checked against.
64+
while ((c = *s++)) {
65+
if (c == '\\') {
66+
c = *s++;
67+
if (c == 'n')
68+
*t = '\n';
69+
else if (c == 'r')
70+
*t = '\r';
71+
else if (c == 't')
72+
*t = '\t';
73+
else
74+
*t = c;
75+
} else if (c == '\r') {
76+
/* Normalise CR and CR/LF into LF. */
77+
*t = '\n';
78+
if (*s == '\n')
79+
s++; /* cr/lf */
80+
} else
81+
*t = c;
82+
t++;
83+
}
84+
return {result, size_t(t - result)};
85+
}
86+
87+
}
88+
89+
#define YY_USER_INIT initLoc(yylloc)
90+
#define YY_USER_ACTION adjustLoc(yylloc, yytext, yyleng);
91+
92+
#define PUSH_STATE(state) yy_push_state(state, yyscanner)
93+
#define POP_STATE() yy_pop_state(yyscanner)

src/libexpr/local.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ libexpr_LDFLAGS_PROPAGATED = $(BDW_GC_LIBS)
2828

2929
libexpr_ORDER_AFTER := $(d)/parser-tab.cc $(d)/parser-tab.hh $(d)/lexer-tab.cc $(d)/lexer-tab.hh
3030

31-
$(d)/parser-tab.cc $(d)/parser-tab.hh: $(d)/parser.y
31+
$(d)/parser-tab.cc $(d)/parser-tab.hh: $(d)/parser/parser.y $(d)/parser/prologue.inc $(d)/parser/epilogue.inc
3232
$(trace-gen) bison -v -o $(libexpr_DIR)/parser-tab.cc $< -d
3333

34-
$(d)/lexer-tab.cc $(d)/lexer-tab.hh: $(d)/lexer.l
34+
$(d)/lexer-tab.cc $(d)/lexer-tab.hh: $(d)/lexer/lexer.l $(d)/lexer/prologue.inc
3535
$(trace-gen) flex --outfile $(libexpr_DIR)/lexer-tab.cc --header-file=$(libexpr_DIR)/lexer-tab.hh $<
3636

3737
clean-files += $(d)/parser-tab.cc $(d)/parser-tab.hh $(d)/lexer-tab.cc $(d)/lexer-tab.hh

0 commit comments

Comments
 (0)