| 1 |
%{ |
| 2 |
/* |
| 3 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 4 |
* This file implements the vowel rule parser. |
| 5 |
* |
| 6 |
* Copyright (C) 2015-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify it |
| 9 |
* under the terms of the GNU General Public License as published by the Free |
| 10 |
* Software Foundation, either version 3 of the License, or (at your option) |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 |
* more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along with |
| 19 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <assert.h> |
| 26 |
#include "bool.h" |
| 27 |
#include "struct.h" |
| 28 |
#include "unicode.h" |
| 29 |
#include "messages.h" |
| 30 |
|
| 31 |
#include "vowel.h" |
| 32 |
|
| 33 |
#include "vowel-rules.h" |
| 34 |
#include "vowel-rules-l.h" |
| 35 |
|
| 36 |
/* NOTE: This parser might allocate a new value for language; this value MUST |
| 37 |
* BE FREED after the parser returns (if the value of the language pointer |
| 38 |
* changes, then free the pointer). This parser DOES free the language |
| 39 |
* pointer before changing it, if status points to RFPS_ALIASED. */ |
| 40 |
|
| 41 |
/* uncomment it if you want to have an interactive shell to understand the |
| 42 |
* details on how bison works for a certain input */ |
| 43 |
/*int gregorio_vowel_rulefile_debug=1;*/ |
| 44 |
|
| 45 |
static void gregorio_vowel_rulefile_error(const char *const filename, |
| 46 |
char **const language __attribute__((__unused__)), |
| 47 |
rulefile_parse_status *const status __attribute__((__unused__)), |
| 48 |
const char *const error_str) |
| 49 |
{ |
| 50 |
gregorio_messagef("gregorio_vowel_rulefile_parse", VERBOSITY_ERROR, 0, |
| 51 |
_("%s: %s"), filename, error_str); |
| 52 |
} |
| 53 |
|
| 54 |
/* this returns false until the language *after* the desired language */ |
| 55 |
static __inline bool match_language(char **language, |
| 56 |
rulefile_parse_status *status, char *const name) |
| 57 |
{ |
| 58 |
if (*status == RFPS_FOUND) { |
| 59 |
free(name); |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
if (strcmp(*language, name) == 0) { |
| 64 |
*status = RFPS_FOUND; |
| 65 |
} |
| 66 |
|
| 67 |
free(name); |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
static __inline void alias(char **const language, |
| 72 |
rulefile_parse_status *const status, char *const name, |
| 73 |
char *const target) |
| 74 |
{ |
| 75 |
if (strcmp(*language, name) == 0) { |
| 76 |
gregorio_messagef("alias", VERBOSITY_INFO, 0, _("Aliasing %s to %s"), |
| 77 |
name, target); |
| 78 |
if (*status == RFPS_ALIASED) { |
| 79 |
free(*language); |
| 80 |
} |
| 81 |
*language = target; |
| 82 |
*status = RFPS_ALIASED; |
| 83 |
} else { |
| 84 |
free(target); |
| 85 |
} |
| 86 |
free(name); |
| 87 |
} |
| 88 |
|
| 89 |
static __inline void add(const rulefile_parse_status *const status, |
| 90 |
void (*const fn)(const char *), char *const value) |
| 91 |
{ |
| 92 |
if (*status == RFPS_FOUND) { |
| 93 |
fn(value); |
| 94 |
} |
| 95 |
free(value); |
| 96 |
} |
| 97 |
|
| 98 |
#define _MATCH(NAME) if (match_language(language, status, NAME)) YYACCEPT |
| 99 |
#define _ALIAS(NAME, TARGET) alias(language, status, NAME, TARGET) |
| 100 |
#define _ADD(TABLE, CHARS) add(status, gregorio_##TABLE##_table_add, CHARS) |
| 101 |
|
| 102 |
%} |
| 103 |
|
| 104 |
%name-prefix "gregorio_vowel_rulefile_" |
| 105 |
%parse-param { const char *const filename } |
| 106 |
%parse-param { char **language } |
| 107 |
%parse-param { rulefile_parse_status *const status } |
| 108 |
|
| 109 |
%token LANGUAGE VOWEL PREFIX SUFFIX SECONDARY ALIAS SEMICOLON TO |
| 110 |
%token NAME CHARACTERS INVALID |
| 111 |
|
| 112 |
%% |
| 113 |
|
| 114 |
rules |
| 115 |
: |
| 116 |
| rules rule |
| 117 |
; |
| 118 |
|
| 119 |
rule |
| 120 |
: LANGUAGE NAME SEMICOLON { _MATCH($2); } |
| 121 |
| ALIAS NAME TO NAME SEMICOLON { _ALIAS($2, $4); } |
| 122 |
| VOWEL vowels SEMICOLON |
| 123 |
| PREFIX prefixes SEMICOLON |
| 124 |
| SUFFIX suffixes SEMICOLON |
| 125 |
| SECONDARY secondaries SEMICOLON |
| 126 |
; |
| 127 |
|
| 128 |
vowels |
| 129 |
: |
| 130 |
| vowels CHARACTERS { _ADD(vowel, $2); } |
| 131 |
; |
| 132 |
|
| 133 |
prefixes |
| 134 |
: |
| 135 |
| prefixes CHARACTERS { _ADD(prefix, $2); } |
| 136 |
; |
| 137 |
|
| 138 |
suffixes |
| 139 |
: |
| 140 |
| suffixes CHARACTERS { _ADD(suffix, $2); } |
| 141 |
; |
| 142 |
|
| 143 |
secondaries |
| 144 |
: |
| 145 |
| secondaries CHARACTERS { _ADD(secondary, $2); } |
| 146 |
; |