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