Skip to content

Instantly share code, notes, and snippets.

@stedolan
Created October 23, 2012 18:44
Show Gist options
  • Save stedolan/3940705 to your computer and use it in GitHub Desktop.
Save stedolan/3940705 to your computer and use it in GitHub Desktop.
parser.y forward declaration hack
diff --git a/parser.y b/parser.y
index 0effe07..ed05534 100644
--- a/parser.y
+++ b/parser.y
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>
#include "compile.h"
+
+struct lexer_param;
%}
%code requires {
#include "locfile.h"
@@ -32,11 +34,11 @@
%parse-param {block* answer}
%parse-param {int* errors}
%parse-param {struct locfile* locations}
-%parse-param {yyscan_t lexer}
+%parse-param {struct lexer_param* lexer_param_ptr}
%lex-param {block* answer}
%lex-param {int* errors}
%lex-param {struct locfile* locations}
-%lex-param {yyscan_t lexer}
+%lex-param {struct lexer_param* lexer_param_ptr}
%token INVALID_CHARACTER
@@ -84,21 +86,25 @@
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString FuncDef FuncDefs String
%{
#include "lexer.gen.h"
-#define FAIL(loc, msg) \
- do { \
- location l = loc; \
- yyerror(&l, answer, errors, locations, lexer, msg); \
- /*YYERROR*/; \
+struct lexer_param {
+ yyscan_t lexer;
+};
+#define FAIL(loc, msg) \
+ do { \
+ location l = loc; \
+ yyerror(&l, answer, errors, locations, lexer_param_ptr, msg); \
+ /*YYERROR*/; \
} while (0)
void yyerror(YYLTYPE* loc, block* answer, int* errors,
- struct locfile* locations, yyscan_t lexer, const char *s){
+ struct locfile* locations, struct lexer_param* lexer_param_ptr, const char *s){
(*errors)++;
locfile_locate(locations, *loc, "error: %s", s);
}
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
- struct locfile* locations, yyscan_t lexer) {
+ struct locfile* locations, struct lexer_param* lexer_param_ptr) {
+ yyscan_t lexer = lexer_param_ptr->lexer;
while (1) {
int tok = jq_yylex(yylval, yylloc, lexer);
if (tok == INVALID_CHARACTER) {
@@ -435,15 +441,15 @@ MkDictPair
%%
int jq_parse(struct locfile* locations, block* answer) {
- yyscan_t scanner;
+ struct lexer_param scanner;
YY_BUFFER_STATE buf;
- jq_yylex_init_extra(0, &scanner);
- buf = jq_yy_scan_bytes(locations->data, locations->length, scanner);
+ jq_yylex_init_extra(0, &scanner.lexer);
+ buf = jq_yy_scan_bytes(locations->data, locations->length, scanner.lexer);
int errors = 0;
*answer = gen_noop();
- yyparse(answer, &errors, locations, scanner);
- jq_yy_delete_buffer(buf, scanner);
- jq_yylex_destroy(scanner);
+ yyparse(answer, &errors, locations, &scanner);
+ jq_yy_delete_buffer(buf, scanner.lexer);
+ jq_yylex_destroy(scanner.lexer);
if (errors > 0) {
block_free(*answer);
*answer = gen_noop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment