1%{ 2/* 3 Licensed to the Apache Software Foundation (ASF) under one 4 or more contributor license agreements. See the NOTICE file 5 distributed with this work for additional information 6 regarding copyright ownership. The ASF licenses this file 7 to you under the Apache License, Version 2.0 (the 8 "License"); you may not use this file except in compliance 9 with the License. You may obtain a copy of the License at 10 11 https://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18*/ 19 20#include <boost/format.hpp> 21#include "Compiler.hh" 22#include "Exception.hh" 23 24#define YYLEX_PARAM ctx 25#define YYPARSE_PARAM ctx 26 27void yyerror(const char *str) 28{ 29 throw avro::Exception(boost::format("Parser error: %1%") % str); 30} 31 32extern void *lexer; 33extern int yylex(int *, void *); 34 35avro::CompilerContext &context(void *ctx) { 36 return *static_cast<avro::CompilerContext *>(ctx); 37}; 38 39%} 40 41%pure-parser 42%error-verbose 43 44%token AVRO_LEX_INT AVRO_LEX_LONG 45%token AVRO_LEX_FLOAT AVRO_LEX_DOUBLE 46%token AVRO_LEX_BOOL AVRO_LEX_NULL 47%token AVRO_LEX_BYTES AVRO_LEX_STRING 48%token AVRO_LEX_RECORD AVRO_LEX_ENUM AVRO_LEX_ARRAY AVRO_LEX_MAP AVRO_LEX_UNION AVRO_LEX_FIXED 49 50%token AVRO_LEX_METADATA 51 52%token AVRO_LEX_SYMBOLS AVRO_LEX_SYMBOLS_END 53%token AVRO_LEX_FIELDS AVRO_LEX_FIELDS_END AVRO_LEX_FIELD AVRO_LEX_FIELD_END 54 55%token AVRO_LEX_TYPE AVRO_LEX_ITEMS AVRO_LEX_VALUES 56 57// Tokens that output text: 58%token AVRO_LEX_OUTPUT_TEXT_BEGIN 59%token AVRO_LEX_NAME 60%token AVRO_LEX_NAMED_TYPE 61%token AVRO_LEX_FIELD_NAME 62%token AVRO_LEX_SYMBOL 63%token AVRO_LEX_SIZE 64%token AVRO_LEX_OUTPUT_TEXT_END 65 66%token AVRO_LEX_SIMPLE_TYPE 67 68%% 69 70avroschema: 71 simpleprimitive | object | union_t 72 ; 73 74primitive: 75 AVRO_LEX_INT { context(ctx).addType(avro::AVRO_INT); } 76 | 77 AVRO_LEX_LONG { context(ctx).addType(avro::AVRO_LONG); } 78 | 79 AVRO_LEX_FLOAT { context(ctx).addType(avro::AVRO_FLOAT); } 80 | 81 AVRO_LEX_DOUBLE { context(ctx).addType(avro::AVRO_DOUBLE); } 82 | 83 AVRO_LEX_BOOL { context(ctx).addType(avro::AVRO_BOOL); } 84 | 85 AVRO_LEX_NULL { context(ctx).addType(avro::AVRO_NULL); } 86 | 87 AVRO_LEX_BYTES { context(ctx).addType(avro::AVRO_BYTES); } 88 | 89 AVRO_LEX_STRING { context(ctx).addType(avro::AVRO_STRING); } 90 | 91 AVRO_LEX_NAMED_TYPE { context(ctx).addNamedType(); } 92 ; 93 94simpleprimitive: 95 AVRO_LEX_SIMPLE_TYPE { context(ctx).startType(); } primitive { context(ctx).stopType(); } 96 ; 97 98primitive_t: 99 AVRO_LEX_TYPE primitive 100 ; 101 102array_t: 103 AVRO_LEX_TYPE AVRO_LEX_ARRAY { context(ctx).addType(avro::AVRO_ARRAY); } 104 ; 105 106enum_t: 107 AVRO_LEX_TYPE AVRO_LEX_ENUM { context(ctx).addType(avro::AVRO_ENUM); } 108 ; 109 110fixed_t: 111 AVRO_LEX_TYPE AVRO_LEX_FIXED { context(ctx).addType(avro::AVRO_FIXED); } 112 ; 113 114map_t: 115 AVRO_LEX_TYPE AVRO_LEX_MAP { context(ctx).addType(avro::AVRO_MAP); } 116 ; 117 118record_t: 119 AVRO_LEX_TYPE AVRO_LEX_RECORD { context(ctx).addType(avro::AVRO_RECORD); } 120 ; 121 122type_attribute: 123 array_t | enum_t | fixed_t | map_t | record_t | primitive_t 124 ; 125 126union_t: 127 '[' { context(ctx).startType(); context(ctx).addType(avro::AVRO_UNION); context(ctx).setTypesAttribute(); } 128 unionlist 129 ']' { context(ctx).stopType(); } 130 ; 131 132object: 133 '{' { context(ctx).startType(); } 134 attributelist 135 '}' { context(ctx).stopType(); } 136 ; 137 138name_attribute: 139 AVRO_LEX_NAME { context(ctx).setNameAttribute(); } 140 ; 141 142size_attribute: 143 AVRO_LEX_SIZE { context(ctx).setSizeAttribute(); } 144 ; 145 146values_attribute: 147 AVRO_LEX_VALUES { context(ctx).setValuesAttribute(); } avroschema 148 ; 149 150fields_attribute: 151 AVRO_LEX_FIELDS { context(ctx).setFieldsAttribute(); } fieldslist AVRO_LEX_FIELDS_END 152 ; 153 154items_attribute: 155 AVRO_LEX_ITEMS { context(ctx).setItemsAttribute(); } avroschema 156 ; 157 158symbols_attribute: 159 AVRO_LEX_SYMBOLS symbollist AVRO_LEX_SYMBOLS_END 160 ; 161 162attribute: 163 type_attribute | name_attribute | fields_attribute | items_attribute | size_attribute | values_attribute | symbols_attribute | AVRO_LEX_METADATA 164 ; 165 166attributelist: 167 attribute | attributelist ',' attribute 168 ; 169 170symbol: 171 AVRO_LEX_SYMBOL { context(ctx).setSymbolsAttribute(); } 172 ; 173 174symbollist: 175 symbol | symbollist ',' symbol 176 ; 177 178fieldsetting: 179 fieldname | avroschema | AVRO_LEX_METADATA 180 ; 181 182fieldsettinglist: 183 fieldsetting | fieldsettinglist ',' fieldsetting 184 ; 185 186fields: 187 AVRO_LEX_FIELD fieldsettinglist AVRO_LEX_FIELD_END 188 ; 189 190fieldname: 191 AVRO_LEX_FIELD_NAME { context(ctx).textContainsFieldName(); } 192 ; 193 194fieldslist: 195 fields | fieldslist ',' fields 196 ; 197 198unionlist: 199 avroschema | unionlist ',' avroschema 200 ; 201