xref: /openbsd-src/gnu/gcc/intl/plural-exp.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Expression parsing for plural form selection.
2*404b540aSrobert    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3*404b540aSrobert    Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
4*404b540aSrobert 
5*404b540aSrobert    This program is free software; you can redistribute it and/or modify it
6*404b540aSrobert    under the terms of the GNU Library General Public License as published
7*404b540aSrobert    by the Free Software Foundation; either version 2, or (at your option)
8*404b540aSrobert    any later version.
9*404b540aSrobert 
10*404b540aSrobert    This program is distributed in the hope that it will be useful,
11*404b540aSrobert    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*404b540aSrobert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*404b540aSrobert    Library General Public License for more details.
14*404b540aSrobert 
15*404b540aSrobert    You should have received a copy of the GNU Library General Public
16*404b540aSrobert    License along with this program; if not, write to the Free Software
17*404b540aSrobert    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
18*404b540aSrobert    USA.  */
19*404b540aSrobert 
20*404b540aSrobert #ifdef HAVE_CONFIG_H
21*404b540aSrobert # include <config.h>
22*404b540aSrobert #endif
23*404b540aSrobert 
24*404b540aSrobert #include <ctype.h>
25*404b540aSrobert #include <stdlib.h>
26*404b540aSrobert #include <string.h>
27*404b540aSrobert 
28*404b540aSrobert #include "plural-exp.h"
29*404b540aSrobert 
30*404b540aSrobert #if (defined __GNUC__ && !defined __APPLE_CC__) \
31*404b540aSrobert     || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
32*404b540aSrobert 
33*404b540aSrobert /* These structs are the constant expression for the germanic plural
34*404b540aSrobert    form determination.  It represents the expression  "n != 1".  */
35*404b540aSrobert static const struct expression plvar =
36*404b540aSrobert {
37*404b540aSrobert   .nargs = 0,
38*404b540aSrobert   .operation = var,
39*404b540aSrobert };
40*404b540aSrobert static const struct expression plone =
41*404b540aSrobert {
42*404b540aSrobert   .nargs = 0,
43*404b540aSrobert   .operation = num,
44*404b540aSrobert   .val =
45*404b540aSrobert   {
46*404b540aSrobert     .num = 1
47*404b540aSrobert   }
48*404b540aSrobert };
49*404b540aSrobert struct expression GERMANIC_PLURAL =
50*404b540aSrobert {
51*404b540aSrobert   .nargs = 2,
52*404b540aSrobert   .operation = not_equal,
53*404b540aSrobert   .val =
54*404b540aSrobert   {
55*404b540aSrobert     .args =
56*404b540aSrobert     {
57*404b540aSrobert       [0] = (struct expression *) &plvar,
58*404b540aSrobert       [1] = (struct expression *) &plone
59*404b540aSrobert     }
60*404b540aSrobert   }
61*404b540aSrobert };
62*404b540aSrobert 
63*404b540aSrobert # define INIT_GERMANIC_PLURAL()
64*404b540aSrobert 
65*404b540aSrobert #else
66*404b540aSrobert 
67*404b540aSrobert /* For compilers without support for ISO C 99 struct/union initializers:
68*404b540aSrobert    Initialization at run-time.  */
69*404b540aSrobert 
70*404b540aSrobert static struct expression plvar;
71*404b540aSrobert static struct expression plone;
72*404b540aSrobert struct expression GERMANIC_PLURAL;
73*404b540aSrobert 
74*404b540aSrobert static void
init_germanic_plural()75*404b540aSrobert init_germanic_plural ()
76*404b540aSrobert {
77*404b540aSrobert   if (plone.val.num == 0)
78*404b540aSrobert     {
79*404b540aSrobert       plvar.nargs = 0;
80*404b540aSrobert       plvar.operation = var;
81*404b540aSrobert 
82*404b540aSrobert       plone.nargs = 0;
83*404b540aSrobert       plone.operation = num;
84*404b540aSrobert       plone.val.num = 1;
85*404b540aSrobert 
86*404b540aSrobert       GERMANIC_PLURAL.nargs = 2;
87*404b540aSrobert       GERMANIC_PLURAL.operation = not_equal;
88*404b540aSrobert       GERMANIC_PLURAL.val.args[0] = &plvar;
89*404b540aSrobert       GERMANIC_PLURAL.val.args[1] = &plone;
90*404b540aSrobert     }
91*404b540aSrobert }
92*404b540aSrobert 
93*404b540aSrobert # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
94*404b540aSrobert 
95*404b540aSrobert #endif
96*404b540aSrobert 
97*404b540aSrobert void
98*404b540aSrobert internal_function
EXTRACT_PLURAL_EXPRESSION(nullentry,pluralp,npluralsp)99*404b540aSrobert EXTRACT_PLURAL_EXPRESSION (nullentry, pluralp, npluralsp)
100*404b540aSrobert      const char *nullentry;
101*404b540aSrobert      struct expression **pluralp;
102*404b540aSrobert      unsigned long int *npluralsp;
103*404b540aSrobert {
104*404b540aSrobert   if (nullentry != NULL)
105*404b540aSrobert     {
106*404b540aSrobert       const char *plural;
107*404b540aSrobert       const char *nplurals;
108*404b540aSrobert 
109*404b540aSrobert       plural = strstr (nullentry, "plural=");
110*404b540aSrobert       nplurals = strstr (nullentry, "nplurals=");
111*404b540aSrobert       if (plural == NULL || nplurals == NULL)
112*404b540aSrobert 	goto no_plural;
113*404b540aSrobert       else
114*404b540aSrobert 	{
115*404b540aSrobert 	  char *endp;
116*404b540aSrobert 	  unsigned long int n;
117*404b540aSrobert 	  struct parse_args args;
118*404b540aSrobert 
119*404b540aSrobert 	  /* First get the number.  */
120*404b540aSrobert 	  nplurals += 9;
121*404b540aSrobert 	  while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
122*404b540aSrobert 	    ++nplurals;
123*404b540aSrobert 	  if (!(*nplurals >= '0' && *nplurals <= '9'))
124*404b540aSrobert 	    goto no_plural;
125*404b540aSrobert #if defined HAVE_STRTOUL || defined _LIBC
126*404b540aSrobert 	  n = strtoul (nplurals, &endp, 10);
127*404b540aSrobert #else
128*404b540aSrobert 	  for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
129*404b540aSrobert 	    n = n * 10 + (*endp - '0');
130*404b540aSrobert #endif
131*404b540aSrobert 	  if (nplurals == endp)
132*404b540aSrobert 	    goto no_plural;
133*404b540aSrobert 	  *npluralsp = n;
134*404b540aSrobert 
135*404b540aSrobert 	  /* Due to the restrictions bison imposes onto the interface of the
136*404b540aSrobert 	     scanner function we have to put the input string and the result
137*404b540aSrobert 	     passed up from the parser into the same structure which address
138*404b540aSrobert 	     is passed down to the parser.  */
139*404b540aSrobert 	  plural += 7;
140*404b540aSrobert 	  args.cp = plural;
141*404b540aSrobert 	  if (PLURAL_PARSE (&args) != 0)
142*404b540aSrobert 	    goto no_plural;
143*404b540aSrobert 	  *pluralp = args.res;
144*404b540aSrobert 	}
145*404b540aSrobert     }
146*404b540aSrobert   else
147*404b540aSrobert     {
148*404b540aSrobert       /* By default we are using the Germanic form: singular form only
149*404b540aSrobert          for `one', the plural form otherwise.  Yes, this is also what
150*404b540aSrobert          English is using since English is a Germanic language.  */
151*404b540aSrobert     no_plural:
152*404b540aSrobert       INIT_GERMANIC_PLURAL ();
153*404b540aSrobert       *pluralp = &GERMANIC_PLURAL;
154*404b540aSrobert       *npluralsp = 2;
155*404b540aSrobert     }
156*404b540aSrobert }
157