1%{
2#include <stdlib.h>
3#include <stdio.h>
4#include <assert.h>
5
6#ifdef TEST_PARSER
7#include "../../../include/octopus/parser/ast.h"
8#else
9#include <octopus/parser/ast.h>
10#endif
11
12#include "y.tab.h"
13
14void yyerror(const char *);
15
16#define YYPARSE_PARAM data
17#define YYLEX_PARAM   ((struct oct_parser_state*)data)->scanner
18
19%}
20
21%define api.pure
22%error-verbose
23
24%union {
25    long long int integer;
26    double dl;
27    char* str;
28    char c;
29    struct ast_object* nPtr;
30};
31
32%token RBRACKET
33%token LBRACKET
34%token RCURLY
35%token LCURLY
36%token COLON
37%token COMMA
38%token GT
39%token GE
40%token LT
41%token LE
42%token EQ
43%token NE
44%token VARIABLE
45%token EUNEXPECTED
46%token END_OF_INPUT
47
48%token <integer> BOOL
49%token <dl> FLOAT
50%token <integer> NUMBER
51%token <str> IDENT
52%token <str> REGEX
53%token <str> STRING
54%token <c> SCAN
55
56%type <nPtr> value
57%type <nPtr> attribute
58%type <nPtr> attributes
59%type <nPtr> record
60%type <nPtr> constraint
61%type <nPtr> name
62
63%destructor { free_ast($$); } <nPtr>
64%destructor { free($$); } <str>
65
66%%
67program:
68      record                         { ((struct oct_parser_state*) data)->ast = $1; YYACCEPT;  }
69
70record:
71      name END_OF_INPUT              { $$ = ast_object($1, NULL); }
72    | name RCURLY LCURLY             { $$ = ast_object($1, NULL); }
73    | name RCURLY attributes LCURLY  { $$ = ast_object($1, $3); }
74
75name:
76      IDENT                          { $$ = ast_ident($1); }
77    | VARIABLE                       { $$ = ast_variable(); }
78    | SCAN                           { $$ = ast_scan($1); }
79    | REGEX                          { $$ = ast_constraints(constraint_REGEX, ast_string($1)); }
80
81attributes:
82      attribute                      { $$ = ast_attribute($1, NULL); }
83    | attribute COMMA attributes     { $$ = ast_attribute($1, $3); }
84
85attribute:
86      IDENT COLON value              { $$ = ast_pair(ast_ident($1), $3); }
87    | IDENT constraint               { $$ = ast_pair(ast_ident($1), $2); }
88
89constraint:
90      GT value                       { $$ = ast_constraints(constraint_GT, $2);  }
91    | GE value                       { $$ = ast_constraints(constraint_GE, $2); }
92    | LT value                       { $$ = ast_constraints(constraint_LT, $2); }
93    | LE value                       { $$ = ast_constraints(constraint_LE, $2); }
94    | EQ value                       { $$ = ast_constraints(constraint_EQ, $2); }
95    | NE value                       { $$ = ast_constraints(constraint_NE, $2); }
96    | COLON REGEX                    { $$ = ast_constraints(constraint_REGEX, ast_string($2)); }
97    | COLON VARIABLE                 { $$ = ast_variable(); }
98
99value:
100      IDENT                          { $$ = ast_ident($1); }
101    | STRING                         { $$ = ast_string($1); }
102    | NUMBER                         { $$ = ast_num($1); }
103    | BOOL                           { $$ = ast_boolean($1); }
104    | FLOAT                          { $$ = ast_floatingpoint($1); }
105    | SCAN                           { $$ = ast_scan($1); }
106%%
107
108void yyerror(const char *s)
109{
110#ifdef OCT_DEBUG
111    fprintf(stderr, "octopus_parser: %s\n", s);
112#endif
113}
114