paul@0 | 1 | /* |
paul@0 | 2 | * Grammar for a simple interface description language. |
paul@28 | 3 | * |
paul@214 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@28 | 5 | * |
paul@28 | 6 | * This program is free software; you can redistribute it and/or |
paul@28 | 7 | * modify it under the terms of the GNU General Public License as |
paul@28 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@28 | 9 | * the License, or (at your option) any later version. |
paul@28 | 10 | * |
paul@28 | 11 | * This program is distributed in the hope that it will be useful, |
paul@28 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@28 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@28 | 14 | * GNU General Public License for more details. |
paul@28 | 15 | * |
paul@28 | 16 | * You should have received a copy of the GNU General Public License |
paul@28 | 17 | * along with this program; if not, write to the Free Software |
paul@28 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@28 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | %{ |
paul@0 | 23 | #include <stdio.h> |
paul@7 | 24 | #include <string.h> |
paul@48 | 25 | #include "program.h" |
paul@15 | 26 | #include "parser.h" |
paul@7 | 27 | |
paul@8 | 28 | /* Standard signatures. */ |
paul@8 | 29 | |
paul@3 | 30 | int yylex(void); |
paul@3 | 31 | void yyerror(const char *); |
paul@21 | 32 | |
paul@33 | 33 | /* State information, also reset by the function defined below. */ |
paul@21 | 34 | |
paul@33 | 35 | struct interface *last_interface = NULL; |
paul@214 | 36 | struct import *last_import = NULL; |
paul@51 | 37 | struct include *last_include = NULL; |
paul@214 | 38 | const char *output_dirname = NULL, *output_basename = NULL; |
paul@0 | 39 | %} |
paul@0 | 40 | |
paul@1 | 41 | %define parse.error verbose |
paul@1 | 42 | |
paul@3 | 43 | /* Define the identifier type employed in the scanner. */ |
paul@3 | 44 | |
paul@3 | 45 | %union { |
paul@38 | 46 | long num; |
paul@3 | 47 | char *str; |
paul@214 | 48 | struct import imp; |
paul@51 | 49 | struct include inc; |
paul@6 | 50 | struct interface iface; |
paul@214 | 51 | struct interface_ref iface_ref; |
paul@6 | 52 | struct signature sig; |
paul@51 | 53 | struct identifier ident; |
paul@6 | 54 | struct parameter param; |
paul@25 | 55 | struct attribute attr; |
paul@3 | 56 | } |
paul@3 | 57 | |
paul@21 | 58 | /* Associate identifiers and similar values with the string union member. */ |
paul@3 | 59 | |
paul@38 | 60 | %type <str> IDENTIFIER DECIMAL HEXADECIMAL |
paul@4 | 61 | %type <str> interface_name |
paul@4 | 62 | %type <str> qualifier operation |
paul@214 | 63 | %type <str> header PHEADER QHEADER |
paul@38 | 64 | %type <str> attr_param |
paul@4 | 65 | |
paul@30 | 66 | /* Specifiers and items have numeric values. */ |
paul@4 | 67 | |
paul@4 | 68 | %type <num> IN INOUT OUT |
paul@4 | 69 | %type <num> specifier |
paul@30 | 70 | %type <num> CAP FPAGE |
paul@30 | 71 | %type <num> item |
paul@4 | 72 | |
paul@18 | 73 | /* Associate rules with the appropriate union members. */ |
paul@4 | 74 | |
paul@214 | 75 | %type <imp> import |
paul@21 | 76 | %type <inc> include |
paul@25 | 77 | %type <iface> q_interface |
paul@4 | 78 | %type <iface> interface |
paul@214 | 79 | %type <iface_ref> interface_bases |
paul@25 | 80 | %type <sig> q_signature |
paul@4 | 81 | %type <sig> signature |
paul@51 | 82 | %type <sig> signatures |
paul@51 | 83 | %type <attr> attribute |
paul@51 | 84 | %type <attr> attributes |
paul@4 | 85 | %type <param> parameter |
paul@51 | 86 | %type <param> parameters |
paul@51 | 87 | %type <ident> attr_params |
paul@51 | 88 | %type <ident> identifiers |
paul@3 | 89 | |
paul@3 | 90 | /* Scanner token types. */ |
paul@3 | 91 | |
paul@214 | 92 | %token IMPORT INCLUDE PHEADER QHEADER |
paul@0 | 93 | %token INTERFACE |
paul@0 | 94 | %token BEGINSECTION ENDSECTION |
paul@0 | 95 | %token LPAR RPAR |
paul@25 | 96 | %token LBRACE RBRACE |
paul@0 | 97 | %token SEP TERM |
paul@0 | 98 | %token IN INOUT OUT |
paul@30 | 99 | %token CAP FPAGE |
paul@38 | 100 | %token IDENTIFIER DECIMAL HEXADECIMAL |
paul@214 | 101 | %token COMPOSES |
paul@0 | 102 | |
paul@3 | 103 | /* Starting rule. */ |
paul@3 | 104 | |
paul@1 | 105 | %start file |
paul@1 | 106 | |
paul@0 | 107 | %% |
paul@0 | 108 | |
paul@214 | 109 | /* Parse each file, with last_interface yielding the parsing products. */ |
paul@214 | 110 | |
paul@33 | 111 | file : statements |
paul@33 | 112 | ; |
paul@1 | 113 | |
paul@21 | 114 | statements : statement statements |
paul@3 | 115 | | %empty |
paul@1 | 116 | ; |
paul@1 | 117 | |
paul@214 | 118 | statement : import |
paul@214 | 119 | | include |
paul@25 | 120 | | q_interface |
paul@214 | 121 | { $1.output_dirname = output_dirname; $1.output_basename = output_basename; |
paul@214 | 122 | $1.tail = last_interface; last_interface = copy_interface($1); } |
paul@21 | 123 | ; |
paul@21 | 124 | |
paul@214 | 125 | import : IMPORT QHEADER TERM |
paul@214 | 126 | { $$.filename = $2; $$.tail = last_import; last_import = copy_import($$); } |
paul@214 | 127 | ; |
paul@214 | 128 | |
paul@214 | 129 | include : INCLUDE header |
paul@51 | 130 | { $$.filename = $2; $$.tail = last_include; last_include = copy_include($$); } |
paul@21 | 131 | ; |
paul@21 | 132 | |
paul@214 | 133 | header : PHEADER |
paul@214 | 134 | | QHEADER |
paul@214 | 135 | ; |
paul@214 | 136 | |
paul@214 | 137 | /* Interface qualified with or without attributes, either having a signature |
paul@214 | 138 | section or a list of base interfaces. */ |
paul@25 | 139 | |
paul@51 | 140 | q_interface : LBRACE attributes RBRACE interface |
paul@51 | 141 | { $4.attributes = copy_attribute($2); $$ = $4; } |
paul@25 | 142 | | interface |
paul@25 | 143 | { $$.attributes = NULL; } |
paul@25 | 144 | ; |
paul@25 | 145 | |
paul@10 | 146 | interface : INTERFACE interface_name BEGINSECTION signatures ENDSECTION TERM |
paul@214 | 147 | { $$.name = $2; $$.bases = NULL; $$.signatures = copy_signature($4); |
paul@214 | 148 | $$.imports = last_import; |
paul@214 | 149 | $$.includes = last_include; last_include = NULL; } |
paul@51 | 150 | | INTERFACE interface_name BEGINSECTION ENDSECTION TERM |
paul@214 | 151 | { $$.name = $2; $$.bases = NULL; $$.signatures = NULL; |
paul@214 | 152 | $$.imports = last_import; |
paul@214 | 153 | $$.includes = last_include; last_include = NULL; } |
paul@214 | 154 | | INTERFACE interface_name COMPOSES interface_bases TERM |
paul@214 | 155 | { $$.name = $2; $$.bases = copy_interface_ref($4); $$.signatures = NULL; |
paul@214 | 156 | $$.imports = last_import; |
paul@214 | 157 | $$.includes = last_include; last_include = NULL; } |
paul@0 | 158 | ; |
paul@0 | 159 | |
paul@4 | 160 | interface_name : IDENTIFIER ; |
paul@0 | 161 | |
paul@214 | 162 | interface_bases : interface_name SEP interface_bases |
paul@214 | 163 | { $$.name = $1; $$.iface = NULL; copy_tail(interface_ref, $$, $3); } |
paul@214 | 164 | | interface_name |
paul@214 | 165 | { $$.name = $1; $$.iface = NULL; $$.tail = NULL; } |
paul@214 | 166 | |
paul@51 | 167 | /* Signatures qualified with or without attributes. */ |
paul@25 | 168 | |
paul@51 | 169 | signatures : q_signature signatures |
paul@51 | 170 | { copy_tail(signature, $1, $2); $$ = $1; } |
paul@51 | 171 | | q_signature |
paul@51 | 172 | { $1.tail = NULL; $$ = $1; } |
paul@51 | 173 | ; |
paul@51 | 174 | |
paul@51 | 175 | q_signature : LBRACE attributes RBRACE signature |
paul@51 | 176 | { $4.attributes = copy_attribute($2); $$ = $4; } |
paul@51 | 177 | | signature |
paul@51 | 178 | { $1.attributes = NULL; $$ = $1; } |
paul@25 | 179 | ; |
paul@25 | 180 | |
paul@51 | 181 | signature : qualifier operation LPAR RPAR TERM |
paul@51 | 182 | { $$.qualifier = $1; $$.operation = $2; $$.parameters = NULL; } |
paul@51 | 183 | | qualifier operation LPAR parameters RPAR TERM |
paul@51 | 184 | { $$.qualifier = $1; $$.operation = $2; $$.parameters = copy_parameter($4); } |
paul@25 | 185 | ; |
paul@25 | 186 | |
paul@51 | 187 | qualifier : IDENTIFIER ; |
paul@51 | 188 | |
paul@51 | 189 | operation : IDENTIFIER ; |
paul@51 | 190 | |
paul@51 | 191 | /* Attributes with or without parameters. */ |
paul@51 | 192 | |
paul@51 | 193 | attributes : attribute SEP attributes |
paul@51 | 194 | { copy_tail(attribute, $1, $3); $$ = $1; } |
paul@51 | 195 | | attribute |
paul@51 | 196 | { $1.tail = NULL; $$ = $1; } |
paul@51 | 197 | ; |
paul@25 | 198 | |
paul@25 | 199 | attribute : IDENTIFIER LPAR attr_params RPAR |
paul@51 | 200 | { $$.attribute = $1; $$.identifiers = copy_identifier($3); } |
paul@25 | 201 | | IDENTIFIER |
paul@51 | 202 | { $$.attribute = $1; $$.identifiers = NULL; } |
paul@25 | 203 | ; |
paul@25 | 204 | |
paul@38 | 205 | attr_params : attr_param SEP attr_params |
paul@51 | 206 | { $$.identifier = $1; copy_tail(identifier, $$, $3); } |
paul@38 | 207 | | attr_param |
paul@25 | 208 | { $$.identifier = $1; $$.tail = NULL; } |
paul@25 | 209 | ; |
paul@25 | 210 | |
paul@38 | 211 | attr_param : IDENTIFIER |
paul@38 | 212 | | DECIMAL |
paul@38 | 213 | | HEXADECIMAL |
paul@38 | 214 | ; |
paul@38 | 215 | |
paul@51 | 216 | /* Signature parameters. */ |
paul@25 | 217 | |
paul@51 | 218 | parameters : parameter SEP parameters |
paul@51 | 219 | { copy_tail(parameter, $1, $3); $$ = $1; } |
paul@51 | 220 | | parameter |
paul@51 | 221 | { $1.tail = NULL; $$ = $1; } |
paul@3 | 222 | ; |
paul@3 | 223 | |
paul@30 | 224 | parameter : specifier item identifiers |
paul@57 | 225 | { $$.specifier = $1; $$.cls = $2; $$.identifiers = copy_identifier($3); } |
paul@30 | 226 | | specifier identifiers |
paul@57 | 227 | { $$.specifier = $1; $$.cls = WORD_CLASS; $$.identifiers = copy_identifier($2); } |
paul@18 | 228 | ; |
paul@0 | 229 | |
paul@18 | 230 | specifier : IN | INOUT | OUT ; |
paul@0 | 231 | |
paul@30 | 232 | item : CAP | FPAGE ; |
paul@30 | 233 | |
paul@25 | 234 | identifiers : IDENTIFIER |
paul@25 | 235 | { $$.identifier = $1; $$.tail = NULL; } |
paul@25 | 236 | | IDENTIFIER identifiers |
paul@51 | 237 | { $$.identifier = $1; copy_tail(identifier, $$, $2); } |
paul@25 | 238 | ; |
paul@25 | 239 | |
paul@0 | 240 | %% |
paul@0 | 241 | |
paul@7 | 242 | void *copy(void *value) |
paul@7 | 243 | { |
paul@25 | 244 | union YYSTYPE *v; |
paul@25 | 245 | |
paul@25 | 246 | if (value == NULL) |
paul@25 | 247 | return value; |
paul@25 | 248 | |
paul@25 | 249 | v = (union YYSTYPE *) malloc(sizeof(union YYSTYPE)); |
paul@7 | 250 | memcpy(v, value, sizeof(union YYSTYPE)); |
paul@7 | 251 | return v; |
paul@7 | 252 | } |
paul@7 | 253 | |
paul@214 | 254 | void reset(const char *dirname, const char *basename) |
paul@33 | 255 | { |
paul@33 | 256 | last_interface = NULL; |
paul@214 | 257 | last_import = NULL; |
paul@33 | 258 | last_include = NULL; |
paul@214 | 259 | output_dirname = dirname; |
paul@214 | 260 | output_basename = basename; |
paul@33 | 261 | } |
paul@33 | 262 | |
paul@3 | 263 | void yyerror(const char *message) |
paul@0 | 264 | { |
paul@0 | 265 | printf("%s\n", message); |
paul@0 | 266 | } |