From: Bruno Haible Date: Fri, 8 Nov 2024 14:54:46 +0000 (+0100) Subject: Refactor. X-Git-Tag: v0.23~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55f9683ca4ab9f2fa028a1b473ef69394940843d;p=thirdparty%2Fgettext.git Refactor. * gettext-tools/src/cldr-plural.y: Untabify. (struct cldr_plural_parse_args): New type, moved here from gettext-tools/src/cldr-plural-exp.h. (cldr_plural_parse): New function, moved here from gettext-tools/src/cldr-plural-exp.c. * gettext-tools/src/cldr-plural-exp.h: Reorder declarations. (struct cldr_plural_parse_args): Remove declaration. * gettext-tools/src/cldr-plural-exp.c: Don't include cldr-plural.h. Reorder functions. (cldr_plural_parse): Remove function. --- diff --git a/gettext-tools/src/cldr-plural-exp.c b/gettext-tools/src/cldr-plural-exp.c index 60d3d0baf..32f9fd2a2 100644 --- a/gettext-tools/src/cldr-plural-exp.c +++ b/gettext-tools/src/cldr-plural-exp.c @@ -1,5 +1,5 @@ -/* Unicode CLDR plural rule parser and converter - Copyright (C) 2015, 2018-2020 Free Software Foundation, Inc. +/* Unicode CLDR plural rule parser and converter. + Copyright (C) 2015-2024 Free Software Foundation, Inc. This file was written by Daiki Ueno , 2015. @@ -28,7 +28,6 @@ #include "xalloc.h" #include "cldr-plural-exp.h" -#include "cldr-plural.h" /* The grammar of Unicode CLDR plural rules is defined at: https://unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax @@ -62,6 +61,14 @@ cldr_plural_range_list_free (struct cldr_plural_range_list_ty *ranges) free (ranges); } +void +cldr_plural_relation_free (struct cldr_plural_relation_ty *relation) +{ + free (relation->expression); + cldr_plural_range_list_free (relation->ranges); + free (relation); +} + void cldr_plural_condition_free (struct cldr_plural_condition_ty *condition) { @@ -76,14 +83,6 @@ cldr_plural_condition_free (struct cldr_plural_condition_ty *condition) free (condition); } -void -cldr_plural_relation_free (struct cldr_plural_relation_ty *relation) -{ - free (relation->expression); - cldr_plural_range_list_free (relation->ranges); - free (relation); -} - static void cldr_plural_rule_free (struct cldr_plural_rule_ty *rule) { @@ -101,23 +100,6 @@ cldr_plural_rule_list_free (struct cldr_plural_rule_list_ty *rules) free (rules); } -struct cldr_plural_rule_list_ty * -cldr_plural_parse (const char *input) -{ - struct cldr_plural_parse_args arg; - - memset (&arg, 0, sizeof (struct cldr_plural_parse_args)); - arg.cp = input; - arg.cp_end = input + strlen (input); - arg.result = XMALLOC (struct cldr_plural_rule_list_ty); - memset (arg.result, 0, sizeof (struct cldr_plural_rule_list_ty)); - - if (yyparse (&arg) != 0) - return NULL; - - return arg.result; -} - #define OPERAND_ZERO_P(o) \ (((o)->type == CLDR_PLURAL_OPERAND_INTEGER \ && (o)->value.ival == 0) \ diff --git a/gettext-tools/src/cldr-plural-exp.h b/gettext-tools/src/cldr-plural-exp.h index 2e5d82296..fe1d3d85f 100644 --- a/gettext-tools/src/cldr-plural-exp.h +++ b/gettext-tools/src/cldr-plural-exp.h @@ -1,5 +1,5 @@ -/* Unicode CLDR plural rule parser and converter - Copyright (C) 2015, 2018 Free Software Foundation, Inc. +/* Unicode CLDR plural rule parser and converter. + Copyright (C) 2015-2024 Free Software Foundation, Inc. This file was written by Daiki Ueno , 2015. @@ -112,29 +112,26 @@ struct cldr_plural_rule_list_ty size_t nitems_max; }; -struct cldr_plural_parse_args -{ - const char *cp; - const char *cp_end; - struct cldr_plural_rule_list_ty *result; -}; +/* Defined in cldr-plural-exp.c. */ extern void cldr_plural_range_free (struct cldr_plural_range_ty *range); extern void cldr_plural_range_list_free (struct cldr_plural_range_list_ty *ranges); extern void -cldr_plural_condition_free (struct cldr_plural_condition_ty *condition); -extern void cldr_plural_relation_free (struct cldr_plural_relation_ty *relation); - -extern struct cldr_plural_rule_list_ty * -cldr_plural_parse (const char *input); +extern void +cldr_plural_condition_free (struct cldr_plural_condition_ty *condition); extern void cldr_plural_rule_list_free (struct cldr_plural_rule_list_ty *rules); extern void cldr_plural_rule_list_print (struct cldr_plural_rule_list_ty *rules, FILE *fp); +/* Defined in cldr-plural.y. */ + +extern struct cldr_plural_rule_list_ty * +cldr_plural_parse (const char *input); + #ifdef __cplusplus } #endif diff --git a/gettext-tools/src/cldr-plural.y b/gettext-tools/src/cldr-plural.y index 05c1b56ec..ad6d5f82a 100644 --- a/gettext-tools/src/cldr-plural.y +++ b/gettext-tools/src/cldr-plural.y @@ -1,4 +1,4 @@ -/* Unicode CLDR plural rule parser and converter +/* Unicode CLDR plural rule parser and converter. Copyright (C) 2015-2024 Free Software Foundation, Inc. This file was written by Daiki Ueno , 2015. @@ -30,11 +30,6 @@ #include "string-buffer.h" #include "cldr-plural-exp.h" -#include "cldr-plural.h" - -/* Prototypes for local functions. */ -static int yylex (YYSTYPE *lval, struct cldr_plural_parse_args *arg); -static void yyerror (struct cldr_plural_parse_args *arg, const char *str); /* Allocation of expressions. */ @@ -119,6 +114,25 @@ new_range (struct cldr_plural_operand_ty *start, result->end = end; return result; } + +/* Internal state of the Bison-generated parser. */ + +struct cldr_plural_parse_args +{ + /* The lifetime of cp, cp_end is limited to the cldr_plural_parse + invocation. */ + const char *cp; + const char *cp_end; + + struct cldr_plural_rule_list_ty *result; +}; + +#include "cldr-plural.h" + +/* Prototypes for local functions, that must come after the rules. */ +static int yylex (YYSTYPE *lval, struct cldr_plural_parse_args *arg); +static void yyerror (struct cldr_plural_parse_args *arg, const char *str); + %} %require "3.0" @@ -272,17 +286,19 @@ sample_ellipsis: %empty ; sample_range: DECIMAL - { free ($1); } + { free ($1); } | DECIMAL '~' DECIMAL { free ($1); free ($3); } | INTEGER { free ($1); } | INTEGER '~' INTEGER - { free ($1); free ($3); } + { free ($1); free ($3); } ; %% +/* Functions invoked by the Bison-generated parser. */ + static int yylex (YYSTYPE *lval, struct cldr_plural_parse_args *arg) { @@ -464,3 +480,22 @@ yyerror (struct cldr_plural_parse_args *arg, char const *s) { fprintf (stderr, "%s\n", s); } + +/* Entry point to the parser. */ + +struct cldr_plural_rule_list_ty * +cldr_plural_parse (const char *input) +{ + struct cldr_plural_parse_args arg; + + memset (&arg, 0, sizeof (struct cldr_plural_parse_args)); + arg.cp = input; + arg.cp_end = input + strlen (input); + arg.result = XMALLOC (struct cldr_plural_rule_list_ty); + memset (arg.result, 0, sizeof (struct cldr_plural_rule_list_ty)); + + if (yyparse (&arg) != 0) + return NULL; + + return arg.result; +}