1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
%code requires {
/* pull in types to populate YYSTYPE: */
#include "src/parse/ast.h"
namespace re2c {
struct AstNode;
}
}
%{
#include <stdio.h>
#include <stdlib.h>
#include "src/parse/ast.h"
#include "src/util/attribute.h"
#include <stdint.h>
#include "lib/lex.h"
#pragma GCC diagnostic push
#include "src/util/nowarn_in_bison.h"
using namespace re2c;
%}
%define api.pure full
%define api.prefix {re2c_lib_}
%lex-param {const uint8_t*& pattern}
%lex-param {re2c::Ast& ast}
%parse-param {const uint8_t*& pattern}
%parse-param {re2c::Ast& ast}
%start regexp
%union {
const re2c::AstNode* regexp;
re2c::AstBounds bounds;
};
%{
extern "C" {
static int yylex(YYSTYPE* yylval, const uint8_t*& pattern, Ast& ast);
static void yyerror(const uint8_t* pattern, Ast&, const char* msg) RE2C_ATTR((noreturn));
}
%}
%token TOKEN_COUNT
%token TOKEN_ERROR
%token TOKEN_REGEXP
%type <regexp> TOKEN_REGEXP regexp expr term factor primary
%type <bounds> TOKEN_COUNT
%%
regexp: expr { regexp = $$; };
expr
: term
| expr '|' term { $$ = ast.alt($1, $3); }
;
term
: factor
| factor term { $$ = ast.cat($1, $2); } // in POSIX concatenation is right-associative
;
factor
: primary
| primary '*' { $$ = ast.iter($1, 0, Ast::MANY); }
| primary '+' { $$ = ast.iter($1, 1, Ast::MANY); }
| primary '?' { $$ = ast.iter($1, 0, 1); }
| primary TOKEN_COUNT { $$ = ast.iter($1, $2.min, $2.max); }
;
primary
: TOKEN_REGEXP
| '(' ')' { $$ = ast.cap(ast.nil(NOWHERE), CAPTURE); }
| '(' expr ')' { $$ = ast.cap($2, CAPTURE); }
;
%%
#pragma GCC diagnostic pop
extern "C" {
static void yyerror(const uint8_t* pattern, Ast&, const char* msg) {
fprintf(stderr, "%s (on regexp %s)", msg, pattern);
exit(1);
}
static int yylex(YYSTYPE* yylval, const uint8_t*& pattern, Ast& ast) {
return lex(yylval, pattern, ast);
}
}
namespace re2c {
const AstNode* parse(const char* pattern, Ast& ast) {
const uint8_t *p = reinterpret_cast<const uint8_t*>(pattern);
yyparse(p, ast);
return regexp;
}
} // namespace re2c
|