home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / refer / label.cc < prev    next >
C/C++ Source or Header  |  1993-12-13  |  45KB  |  1,594 lines

  1. #ifndef lint
  2. static char yysccsid[] = "@(#)yaccpar    1.8 (Berkeley) 01/20/90";
  3. #endif
  4. #define YYBYACC 1
  5. #line 22 "/u/jjc/groff/refer/label.y"
  6.  
  7. #include "refer.h"
  8. #include "refid.h"
  9. #include "ref.h"
  10. #include "token.h"
  11.  
  12. int yylex();
  13. void yyerror(const char *);
  14. int yyparse();
  15.  
  16. static const char *format_serial(char c, int n);
  17.  
  18. struct label_info {
  19.   int start;
  20.   int length;
  21.   int count;
  22.   int total;
  23.   label_info(const string &);
  24. };
  25.  
  26. label_info *lookup_label(const string &label);
  27.  
  28. struct expression {
  29.   enum {
  30.     /* Does the tentative label depend on the reference?*/
  31.     CONTAINS_VARIABLE = 01, 
  32.     CONTAINS_STAR = 02,
  33.     CONTAINS_FORMAT = 04,
  34.     CONTAINS_AT = 010
  35.   };
  36.   virtual ~expression() { }
  37.   virtual void evaluate(int, const reference &, string &,
  38.             substring_position &) = 0;
  39.   virtual unsigned analyze() { return 0; }
  40. };
  41.  
  42. class at_expr : public expression {
  43. public:
  44.   at_expr() { }
  45.   void evaluate(int, const reference &, string &, substring_position &);
  46.   unsigned analyze() { return CONTAINS_VARIABLE|CONTAINS_AT; }
  47. };
  48.  
  49. class format_expr : public expression {
  50.   char type;
  51.   int width;
  52.   int first_number;
  53. public:
  54.   format_expr(char c, int w = 0, int f = 1)
  55.     : type(c), width(w), first_number(f) { }
  56.   void evaluate(int, const reference &, string &, substring_position &);
  57.   unsigned analyze() { return CONTAINS_FORMAT; }
  58. };
  59.  
  60. class field_expr : public expression {
  61.   int number;
  62.   char name;
  63. public:
  64.   field_expr(char nm, int num) : name(nm), number(num) { }
  65.   void evaluate(int, const reference &, string &, substring_position &);
  66.   unsigned analyze() { return CONTAINS_VARIABLE; }
  67. };
  68.  
  69. class literal_expr : public expression {
  70.   string s;
  71. public:
  72.   literal_expr(const char *ptr, int len) : s(ptr, len) { }
  73.   void evaluate(int, const reference &, string &, substring_position &);
  74. };
  75.  
  76. class unary_expr : public expression {
  77. protected:
  78.   expression *expr;
  79. public:
  80.   unary_expr(expression *e) : expr(e) { }
  81.   ~unary_expr() { delete expr; }
  82.   void evaluate(int, const reference &, string &, substring_position &) = 0;
  83.   unsigned analyze() { return expr ? expr->analyze() : 0; }
  84. };
  85.  
  86. /* This caches the analysis of an expression.*/
  87.  
  88. class analyzed_expr : public unary_expr {
  89.   unsigned flags;
  90. public:
  91.   analyzed_expr(expression *);
  92.   void evaluate(int, const reference &, string &, substring_position &);
  93.   unsigned analyze() { return flags; }
  94. };
  95.  
  96. class star_expr : public unary_expr {
  97. public:
  98.   star_expr(expression *e) : unary_expr(e) { }
  99.   void evaluate(int, const reference &, string &, substring_position &);
  100.   unsigned analyze() {
  101.     return ((expr ? (expr->analyze() & ~CONTAINS_VARIABLE) : 0)
  102.         | CONTAINS_STAR);
  103.   }
  104. };
  105.  
  106. typedef void map_func(const char *, const char *, string &);
  107.  
  108. class map_expr : public unary_expr {
  109.   map_func *func;
  110. public:
  111.   map_expr(expression *e, map_func *f) : unary_expr(e), func(f) { }
  112.   void evaluate(int, const reference &, string &, substring_position &);
  113. };
  114.   
  115. typedef const char *extractor_func(const char *, const char *, const char **);
  116.  
  117. class extractor_expr : public unary_expr {
  118.   int part;
  119.   extractor_func *func;
  120. public:
  121.   enum { BEFORE = +1, MATCH = 0, AFTER = -1 };
  122.   extractor_expr(expression *e, extractor_func *f, int pt)
  123.     : unary_expr(e), func(f), part(pt) { }
  124.   void evaluate(int, const reference &, string &, substring_position &);
  125. };
  126.  
  127. class truncate_expr : public unary_expr {
  128.   int n;
  129. public:
  130.   truncate_expr(expression *e, int i) : n(i), unary_expr(e) { } 
  131.   void evaluate(int, const reference &, string &, substring_position &);
  132. };
  133.  
  134. class separator_expr : public unary_expr {
  135. public:
  136.   separator_expr(expression *e) : unary_expr(e) { }
  137.   void evaluate(int, const reference &, string &, substring_position &);
  138. };
  139.  
  140. class binary_expr : public expression {
  141. protected:
  142.   expression *expr1;
  143.   expression *expr2;
  144. public:
  145.   binary_expr(expression *e1, expression *e2) : expr1(e1), expr2(e2) { }
  146.   ~binary_expr() { delete expr1; delete expr2; }
  147.   void evaluate(int, const reference &, string &, substring_position &) = 0;
  148.   unsigned analyze() {
  149.     return (expr1 ? expr1->analyze() : 0) | (expr2 ? expr2->analyze() : 0);
  150.   }
  151. };
  152.  
  153. class alternative_expr : public binary_expr {
  154. public:
  155.   alternative_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
  156.   void evaluate(int, const reference &, string &, substring_position &);
  157. };
  158.  
  159. class list_expr : public binary_expr {
  160. public:
  161.   list_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
  162.   void evaluate(int, const reference &, string &, substring_position &);
  163. };
  164.  
  165. class substitute_expr : public binary_expr {
  166. public:
  167.   substitute_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
  168.   void evaluate(int, const reference &, string &, substring_position &);
  169. };
  170.  
  171. class ternary_expr : public expression {
  172. protected:
  173.   expression *expr1;
  174.   expression *expr2;
  175.   expression *expr3;
  176. public:
  177.   ternary_expr(expression *e1, expression *e2, expression *e3)
  178.     : expr1(e1), expr2(e2), expr3(e3) { }
  179.   ~ternary_expr() { delete expr1; delete expr2; delete expr3; }
  180.   void evaluate(int, const reference &, string &, substring_position &) = 0;
  181.   unsigned analyze() {
  182.     return ((expr1 ? expr1->analyze() : 0)
  183.         | (expr2 ? expr2->analyze() : 0)
  184.         | (expr3 ? expr3->analyze() : 0));
  185.   }
  186. };
  187.  
  188. class conditional_expr : public ternary_expr {
  189. public:
  190.   conditional_expr(expression *e1, expression *e2, expression *e3)
  191.     : ternary_expr(e1, e2, e3) { }
  192.   void evaluate(int, const reference &, string &, substring_position &);
  193. };
  194.  
  195. static expression *parsed_label = 0;
  196. static expression *parsed_date_label = 0;
  197. static expression *parsed_short_label = 0;
  198.  
  199. static expression *parse_result;
  200.  
  201. string literals;
  202.  
  203. #line 221 "/u/jjc/groff/refer/label.y"
  204. typedef union {
  205.   int num;
  206.   expression *expr;
  207.   struct { int ndigits; int val; } dig;
  208.   struct { int start; int len; } str;
  209. } YYSTYPE;
  210. #line 211 "y.tab.c"
  211. #define TOKEN_LETTER 257
  212. #define TOKEN_LITERAL 258
  213. #define TOKEN_DIGIT 259
  214. #define YYERRCODE 256
  215. short yylhs[] = {                                        -1,
  216.     0,    1,    1,    6,    6,    2,    2,    2,    3,    3,
  217.     5,    5,    4,    4,    4,    4,    4,    4,    4,    4,
  218.     4,    4,    4,    4,    9,    9,    7,    7,    8,    8,
  219.    10,   10,   10,
  220. };
  221. short yylen[] = {                                         2,
  222.     1,    1,    5,    0,    1,    1,    3,    3,    1,    2,
  223.     1,    3,    1,    1,    1,    2,    2,    2,    5,    3,
  224.     3,    2,    3,    3,    0,    1,    1,    2,    1,    2,
  225.     0,    1,    1,
  226. };
  227. short yydefred[] = {                                      0,
  228.     0,   14,   13,    0,    0,    0,    0,    5,    0,    0,
  229.     0,    0,    1,   27,    0,   17,   29,    0,    0,    0,
  230.     0,    0,    0,    0,    0,    0,    0,   22,    0,   28,
  231.    30,   23,   24,    0,    0,    0,   32,   33,    0,    0,
  232.     0,    0,    0,    0,    3,    0,   19,
  233. };
  234. short yydgoto[] = {                                       7,
  235.     8,    9,   10,   11,   12,   13,   15,   18,   47,   39,
  236. };
  237. short yysindex[] = {                                    -32,
  238.  -257,    0,    0, -240,  -32,  -32,    0,    0,  -18,  -32,
  239.   -36, -114,    0,    0, -246,    0,    0, -241,  -14,  -39,
  240.   -32,  -32,  -32, -114,  -21, -257, -257,    0,  -32,    0,
  241.     0,    0,    0,  -25,  -32,  -32,    0,    0, -223, -246,
  242.  -246,  -36,  -32, -257,    0, -246,    0,
  243. };
  244. short yyrindex[] = {                                     35,
  245.     1,    0,    0,    0,   -5,   -4,    0,    0,   14,  208,
  246.   159,  224,    0,    0,   11,    0,    0,   40,    0,    0,
  247.     2,    0,    0,  253, -220,    0,    0,    0,    0,    0,
  248.     0,    0,    0,    0,  263,  281,    0,    0,    0,   50,
  249.   105,  214,    0,  115,    0,  149,    0,
  250. };
  251. short yygindex[] = {                                      0,
  252.    19,    0,    7,   37,  -10,   10,  -23,    0,    0,    0,
  253. };
  254. #define YYTABLESIZE 511
  255. short yytable[] = {                                      24,
  256.    15,   14,   40,   41,    4,   28,   26,    5,   27,   25,
  257.    16,   29,   30,    2,   19,   20,   16,   31,   17,   23,
  258.    46,   37,   33,   38,   24,   24,   32,    6,   35,   36,
  259.    34,    3,   43,   44,    4,    4,   31,   15,   15,   18,
  260.    15,   15,   15,   15,   21,   15,   15,   16,   16,   20,
  261.    16,   16,   16,   16,    2,   16,   16,    4,   15,    4,
  262.    15,   45,   15,   15,   15,   42,    0,    0,   16,    0,
  263.    16,    2,   16,   16,   16,    2,   18,   18,    0,   18,
  264.    18,   18,   18,    0,   18,   18,   20,   20,    0,   20,
  265.    20,   20,   20,    0,   20,   20,    0,   18,    0,   18,
  266.     0,   18,   18,   18,   21,   22,    0,   20,    0,   20,
  267.     0,   20,   20,   20,   25,    0,    0,    0,    0,    0,
  268.     0,    0,    0,    0,   15,    0,   15,    0,    0,    0,
  269.     0,    0,    0,    0,   16,    0,   16,    0,    0,    0,
  270.     0,   21,   21,    0,   21,   21,   21,   21,   26,   21,
  271.    21,   25,   25,    0,   25,   25,   25,   25,   11,   25,
  272.    25,    0,   21,   18,   21,   18,   21,   21,   21,    0,
  273.     0,    0,   25,   20,   25,   20,   25,   25,   25,    0,
  274.     0,    0,    0,    0,    0,   26,   26,    0,   26,   26,
  275.    26,   26,    0,   26,   26,   11,   11,    0,   11,   11,
  276.     0,    0,    0,    0,    0,    0,   26,    6,   26,    0,
  277.    26,   26,   26,   12,    0,    0,   11,    0,   11,    0,
  278.    11,   11,   11,    9,    1,    2,    0,    0,   21,    0,
  279.    21,    0,    0,    0,    0,    0,    0,    0,   25,    0,
  280.    25,    0,    0,    0,    0,    6,    0,    0,    6,    0,
  281.    12,   12,   10,   12,   12,    0,    0,   15,   15,    0,
  282.     9,    9,    7,    9,    9,    6,    0,   16,   16,    6,
  283.     6,   12,   26,   12,   26,   12,   12,   12,    0,    0,
  284.     8,    9,   11,    9,   11,    9,    9,    9,    0,   10,
  285.    10,    0,   10,   10,    0,    0,   18,   18,    0,    0,
  286.     7,    0,    0,    7,    0,    0,   20,   20,    0,    0,
  287.    10,    0,   10,    0,   10,   10,   10,    0,    8,    0,
  288.     7,    8,    0,    0,    7,    7,    0,    0,    0,    0,
  289.     0,    6,    0,    0,    0,    0,    0,   12,    8,   12,
  290.     0,    0,    8,    8,    0,    0,    0,    9,    0,    0,
  291.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  292.     0,   21,   21,    0,    0,    0,    0,    0,    0,    0,
  293.     0,   25,   25,    0,    0,    0,   10,    0,    0,    0,
  294.     0,    0,    0,    0,    0,    0,    7,    0,    0,    0,
  295.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  296.     0,    0,    0,    0,    8,   26,   26,    0,    0,    0,
  297.     0,    0,    0,    0,    0,   11,   11,    0,    0,    0,
  298.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  299.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  300.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  301.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  302.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  303.    12,   12,    0,    0,    0,    0,    0,    0,    0,    0,
  304.     9,    9,    0,    0,    0,    0,    0,    0,    0,    0,
  305.     0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  306.     0,    0,    0,    0,    0,    0,    0,    0,    0,   10,
  307.    10,
  308. };
  309. short yycheck[] = {                                      10,
  310.     0,  259,   26,   27,   37,   42,   43,   40,   45,   46,
  311.     0,  126,  259,    0,    5,    6,  257,  259,  259,   38,
  312.    44,   43,   62,   45,   35,   36,   41,   60,   22,   23,
  313.    21,   64,   58,  257,    0,   41,  257,   37,   38,    0,
  314.    40,   41,   42,   43,   63,   45,   46,   37,   38,    0,
  315.    40,   41,   42,   43,   41,   45,   46,   62,   58,   58,
  316.    60,   43,   62,   63,   64,   29,   -1,   -1,   58,   -1,
  317.    60,   58,   62,   63,   64,   62,   37,   38,   -1,   40,
  318.    41,   42,   43,   -1,   45,   46,   37,   38,   -1,   40,
  319.    41,   42,   43,   -1,   45,   46,   -1,   58,   -1,   60,
  320.    -1,   62,   63,   64,    0,  124,   -1,   58,   -1,   60,
  321.    -1,   62,   63,   64,    0,   -1,   -1,   -1,   -1,   -1,
  322.    -1,   -1,   -1,   -1,  124,   -1,  126,   -1,   -1,   -1,
  323.    -1,   -1,   -1,   -1,  124,   -1,  126,   -1,   -1,   -1,
  324.    -1,   37,   38,   -1,   40,   41,   42,   43,    0,   45,
  325.    46,   37,   38,   -1,   40,   41,   42,   43,    0,   45,
  326.    46,   -1,   58,  124,   60,  126,   62,   63,   64,   -1,
  327.    -1,   -1,   58,  124,   60,  126,   62,   63,   64,   -1,
  328.    -1,   -1,   -1,   -1,   -1,   37,   38,   -1,   40,   41,
  329.    42,   43,   -1,   45,   46,   37,   38,   -1,   40,   41,
  330.    -1,   -1,   -1,   -1,   -1,   -1,   58,    0,   60,   -1,
  331.    62,   63,   64,    0,   -1,   -1,   58,   -1,   60,   -1,
  332.    62,   63,   64,    0,  257,  258,   -1,   -1,  124,   -1,
  333.   126,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  124,   -1,
  334.   126,   -1,   -1,   -1,   -1,   38,   -1,   -1,   41,   -1,
  335.    37,   38,    0,   40,   41,   -1,   -1,  257,  258,   -1,
  336.    37,   38,    0,   40,   41,   58,   -1,  257,  258,   62,
  337.    63,   58,  124,   60,  126,   62,   63,   64,   -1,   -1,
  338.     0,   58,  124,   60,  126,   62,   63,   64,   -1,   37,
  339.    38,   -1,   40,   41,   -1,   -1,  257,  258,   -1,   -1,
  340.    38,   -1,   -1,   41,   -1,   -1,  257,  258,   -1,   -1,
  341.    58,   -1,   60,   -1,   62,   63,   64,   -1,   38,   -1,
  342.    58,   41,   -1,   -1,   62,   63,   -1,   -1,   -1,   -1,
  343.    -1,  124,   -1,   -1,   -1,   -1,   -1,  124,   58,  126,
  344.    -1,   -1,   62,   63,   -1,   -1,   -1,  124,   -1,   -1,
  345.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  346.    -1,  257,  258,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  347.    -1,  257,  258,   -1,   -1,   -1,  124,   -1,   -1,   -1,
  348.    -1,   -1,   -1,   -1,   -1,   -1,  124,   -1,   -1,   -1,
  349.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  350.    -1,   -1,   -1,   -1,  124,  257,  258,   -1,   -1,   -1,
  351.    -1,   -1,   -1,   -1,   -1,  257,  258,   -1,   -1,   -1,
  352.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  353.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  354.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  355.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  356.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  357.   257,  258,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  358.   257,  258,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  359.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
  360.    -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  257,
  361.   258,
  362. };
  363. #define YYFINAL 7
  364. #ifndef YYDEBUG
  365. #define YYDEBUG 0
  366. #endif
  367. #define YYMAXTOKEN 259
  368. #if YYDEBUG
  369. char *yyname[] = {
  370. "end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  371. 0,0,0,"'%'","'&'",0,"'('","')'","'*'","'+'",0,"'-'","'.'",0,0,0,0,0,0,0,0,0,0,0,
  372. "':'",0,"'<'",0,"'>'","'?'","'@'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  373. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'|'",0,
  374. "'~'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  375. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  376. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  377. 0,0,0,0,0,0,0,0,0,0,0,0,0,"TOKEN_LETTER","TOKEN_LITERAL","TOKEN_DIGIT",
  378. };
  379. char *yyrule[] = {
  380. "$accept : expr",
  381. "expr : optional_conditional",
  382. "conditional : alternative",
  383. "conditional : alternative '?' optional_conditional ':' conditional",
  384. "optional_conditional :",
  385. "optional_conditional : conditional",
  386. "alternative : list",
  387. "alternative : alternative '|' list",
  388. "alternative : alternative '&' list",
  389. "list : substitute",
  390. "list : list substitute",
  391. "substitute : string",
  392. "substitute : substitute '~' string",
  393. "string : '@'",
  394. "string : TOKEN_LITERAL",
  395. "string : TOKEN_LETTER",
  396. "string : TOKEN_LETTER number",
  397. "string : '%' TOKEN_LETTER",
  398. "string : '%' digits",
  399. "string : string '.' flag TOKEN_LETTER optional_number",
  400. "string : string '+' number",
  401. "string : string '-' number",
  402. "string : string '*'",
  403. "string : '(' optional_conditional ')'",
  404. "string : '<' optional_conditional '>'",
  405. "optional_number :",
  406. "optional_number : number",
  407. "number : TOKEN_DIGIT",
  408. "number : number TOKEN_DIGIT",
  409. "digits : TOKEN_DIGIT",
  410. "digits : digits TOKEN_DIGIT",
  411. "flag :",
  412. "flag : '+'",
  413. "flag : '-'",
  414. };
  415. #endif
  416. #define yyclearin (yychar=(-1))
  417. #define yyerrok (yyerrflag=0)
  418. #ifdef YYSTACKSIZE
  419. #ifndef YYMAXDEPTH
  420. #define YYMAXDEPTH YYSTACKSIZE
  421. #endif
  422. #else
  423. #ifdef YYMAXDEPTH
  424. #define YYSTACKSIZE YYMAXDEPTH
  425. #else
  426. #define YYSTACKSIZE 500
  427. #define YYMAXDEPTH 500
  428. #endif
  429. #endif
  430. int yydebug;
  431. int yynerrs;
  432. int yyerrflag;
  433. int yychar;
  434. short *yyssp;
  435. YYSTYPE *yyvsp;
  436. YYSTYPE yyval;
  437. YYSTYPE yylval;
  438. short yyss[YYSTACKSIZE];
  439. YYSTYPE yyvs[YYSTACKSIZE];
  440. #define yystacksize YYSTACKSIZE
  441. #line 397 "/u/jjc/groff/refer/label.y"
  442.  
  443. /* bison defines const to be empty unless __STDC__ is defined, which it
  444. isn't under cfront */
  445.  
  446. #ifdef const
  447. #undef const
  448. #endif
  449.  
  450. const char *spec_ptr;
  451. const char *spec_end;
  452. const char *spec_cur;
  453.  
  454. int yylex()
  455. {
  456.   while (spec_ptr < spec_end && csspace(*spec_ptr))
  457.     spec_ptr++;
  458.   spec_cur = spec_ptr;
  459.   if (spec_ptr >= spec_end)
  460.     return 0;
  461.   unsigned char c = *spec_ptr++;
  462.   if (csalpha(c)) {
  463.     yylval.num = c;
  464.     return TOKEN_LETTER;
  465.   }
  466.   if (csdigit(c)) {
  467.     yylval.num = c - '0';
  468.     return TOKEN_DIGIT;
  469.   }
  470.   if (c == '\'') {
  471.     yylval.str.start = literals.length();
  472.     for (; spec_ptr < spec_end; spec_ptr++) {
  473.       if (*spec_ptr == '\'') {
  474.     if (++spec_ptr < spec_end && *spec_ptr == '\'')
  475.       literals += '\'';
  476.     else {
  477.       yylval.str.len = literals.length() - yylval.str.start;
  478.       return TOKEN_LITERAL;
  479.     }
  480.       }
  481.       else
  482.     literals += *spec_ptr;
  483.     }
  484.     yylval.str.len = literals.length() - yylval.str.start;
  485.     return TOKEN_LITERAL;
  486.   }
  487.   return c;
  488. }
  489.  
  490. int set_label_spec(const char *label_spec)
  491. {
  492.   spec_cur = spec_ptr = label_spec;
  493.   spec_end = strchr(label_spec, '\0');
  494.   literals.clear();
  495.   if (yyparse())
  496.     return 0;
  497.   delete parsed_label;
  498.   parsed_label = parse_result;
  499.   return 1;
  500. }
  501.  
  502. int set_date_label_spec(const char *label_spec)
  503. {
  504.   spec_cur = spec_ptr = label_spec;
  505.   spec_end = strchr(label_spec, '\0');
  506.   literals.clear();
  507.   if (yyparse())
  508.     return 0;
  509.   delete parsed_date_label;
  510.   parsed_date_label = parse_result;
  511.   return 1;
  512. }
  513.  
  514. int set_short_label_spec(const char *label_spec)
  515. {
  516.   spec_cur = spec_ptr = label_spec;
  517.   spec_end = strchr(label_spec, '\0');
  518.   literals.clear();
  519.   if (yyparse())
  520.     return 0;
  521.   delete parsed_short_label;
  522.   parsed_short_label = parse_result;
  523.   return 1;
  524. }
  525.  
  526. void yyerror(const char *message)
  527. {
  528.   if (spec_cur < spec_end)
  529.     command_error("label specification %1 before `%2'", message, spec_cur);
  530.   else
  531.     command_error("label specification %1 at end of string",
  532.           message, spec_cur);
  533. }
  534.  
  535. void at_expr::evaluate(int tentative, const reference &ref,
  536.                string &result, substring_position &)
  537. {
  538.   if (tentative)
  539.     ref.canonicalize_authors(result);
  540.   else {
  541.     const char *end, *start = ref.get_authors(&end);
  542.     if (start)
  543.       result.append(start, end - start);
  544.   }
  545. }
  546.  
  547. void format_expr::evaluate(int tentative, const reference &ref,
  548.                string &result, substring_position &)
  549. {
  550.   if (tentative)
  551.     return;
  552.   const label_info *lp = ref.get_label_ptr();
  553.   int num = lp == 0 ? ref.get_number() : lp->count;
  554.   if (type != '0')
  555.     result += format_serial(type, num + 1);
  556.   else {
  557.     const char *ptr = itoa(num + first_number);
  558.     int pad = width - strlen(ptr);
  559.     while (--pad >= 0)
  560.       result += '0';
  561.     result += ptr;
  562.   }
  563. }
  564.  
  565. static const char *format_serial(char c, int n)
  566. {
  567.   assert(n > 0);
  568.   static char buf[128]; // more than enough.
  569.   switch (c) {
  570.   case 'i':
  571.   case 'I':
  572.     {
  573.       char *p = buf;
  574.       // troff uses z and w to represent 10000 and 5000 in Roman
  575.       // numerals; I can find no historical basis for this usage
  576.       const char *s = c == 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
  577.       if (n >= 40000)
  578.     return itoa(n);
  579.       while (n >= 10000) {
  580.     *p++ = s[0];
  581.     n -= 10000;
  582.       }
  583.       for (int i = 1000; i > 0; i /= 10, s += 2) {
  584.     int m = n/i;
  585.     n -= m*i;
  586.     switch (m) {
  587.     case 3:
  588.       *p++ = s[2];
  589.       /* falls through */
  590.     case 2:
  591.       *p++ = s[2];
  592.       /* falls through */
  593.     case 1:
  594.       *p++ = s[2];
  595.       break;
  596.     case 4:
  597.       *p++ = s[2];
  598.       *p++ = s[1];
  599.       break;
  600.     case 8:
  601.       *p++ = s[1];
  602.       *p++ = s[2];
  603.       *p++ = s[2];
  604.       *p++ = s[2];
  605.       break;
  606.     case 7:
  607.       *p++ = s[1];
  608.       *p++ = s[2];
  609.       *p++ = s[2];
  610.       break;
  611.     case 6:
  612.       *p++ = s[1];
  613.       *p++ = s[2];
  614.       break;
  615.     case 5:
  616.       *p++ = s[1];
  617.       break;
  618.     case 9:
  619.       *p++ = s[2];
  620.       *p++ = s[0];
  621.     }
  622.       }
  623.       *p = 0;
  624.       break;
  625.     }
  626.   case 'a':
  627.   case 'A':
  628.     {
  629.       char *p = buf;
  630.       // this is derived from troff/reg.c
  631.       while (n > 0) {
  632.     int d = n % 26;
  633.     if (d == 0)
  634.       d = 26;
  635.     n -= d;
  636.     n /= 26;
  637.     *p++ = c + d - 1;    // ASCII dependent
  638.       }
  639.       *p-- = 0;
  640.       // Reverse it.
  641.       char *q = buf;
  642.       while (q < p) {
  643.     char temp = *q;
  644.     *q = *p;
  645.     *p = temp;
  646.     --p;
  647.     ++q;
  648.       }
  649.       break;
  650.     }
  651.   default:
  652.     assert(0);
  653.   }
  654.   return buf;
  655. }
  656.  
  657. void field_expr::evaluate(int, const reference &ref,
  658.               string &result, substring_position &)
  659. {
  660.   const char *end;
  661.   const char *start = ref.get_field(name, &end);
  662.   if (start) {
  663.     start = nth_field(number, start, &end);
  664.     if (start)
  665.       result.append(start, end - start);
  666.   }
  667. }
  668.  
  669. void literal_expr::evaluate(int, const reference &,
  670.                 string &result, substring_position &)
  671. {
  672.   result += s;
  673. }
  674.  
  675. analyzed_expr::analyzed_expr(expression *e)
  676. : unary_expr(e), flags(e ? e->analyze() : 0)
  677. {
  678. }
  679.  
  680. void analyzed_expr::evaluate(int tentative, const reference &ref,
  681.                  string &result, substring_position &pos)
  682. {
  683.   if (expr)
  684.     expr->evaluate(tentative, ref, result, pos);
  685. }
  686.  
  687. void star_expr::evaluate(int tentative, const reference &ref,
  688.              string &result, substring_position &pos)
  689. {
  690.   const label_info *lp = ref.get_label_ptr();
  691.   if (!tentative
  692.       && (lp == 0 || lp->total > 1)
  693.       && expr)
  694.     expr->evaluate(tentative, ref, result, pos);
  695. }
  696.  
  697. void separator_expr::evaluate(int tentative, const reference &ref,
  698.                   string &result, substring_position &pos)
  699. {
  700.   int start_length = result.length();
  701.   int is_first = pos.start < 0;
  702.   if (expr)
  703.     expr->evaluate(tentative, ref, result, pos);
  704.   if (is_first) {
  705.     pos.start = start_length;
  706.     pos.length = result.length() - start_length;
  707.   }
  708. }
  709.  
  710. void map_expr::evaluate(int tentative, const reference &ref,
  711.             string &result, substring_position &)
  712. {
  713.   if (expr) {
  714.     string temp;
  715.     substring_position temp_pos;
  716.     expr->evaluate(tentative, ref, temp, temp_pos);
  717.     (*func)(temp.contents(), temp.contents() + temp.length(), result);
  718.   }
  719. }
  720.  
  721. void extractor_expr::evaluate(int tentative, const reference &ref,
  722.                   string &result, substring_position &)
  723. {
  724.   if (expr) {
  725.     string temp;
  726.     substring_position temp_pos;
  727.     expr->evaluate(tentative, ref, temp, temp_pos);
  728.     const char *end, *start = (*func)(temp.contents(),
  729.                       temp.contents() + temp.length(),
  730.                       &end);
  731.     switch (part) {
  732.     case BEFORE:
  733.       if (start)
  734.     result.append(temp.contents(), start - temp.contents());
  735.       else
  736.     result += temp;
  737.       break;
  738.     case MATCH:
  739.       if (start)
  740.     result.append(start, end - start);
  741.       break;
  742.     case AFTER:
  743.       if (start)
  744.     result.append(end, temp.contents() + temp.length() - end);
  745.       break;
  746.     default:
  747.       assert(0);
  748.     }
  749.   }
  750. }
  751.  
  752. static void first_part(int len, const char *ptr, const char *end,
  753.               string &result)
  754. {
  755.   for (;;) {
  756.     const char *token_start = ptr;
  757.     if (!get_token(&ptr, end))
  758.       break;
  759.     const token_info *ti = lookup_token(token_start, ptr);
  760.     int counts = ti->sortify_non_empty(token_start, ptr);
  761.     if (counts && --len < 0)
  762.       break;
  763.     if (counts || ti->is_accent())
  764.       result.append(token_start, ptr - token_start);
  765.   }
  766. }
  767.  
  768. static void last_part(int len, const char *ptr, const char *end,
  769.               string &result)
  770. {
  771.   const char *start = ptr;
  772.   int count = 0;
  773.   for (;;) {
  774.     const char *token_start = ptr;
  775.     if (!get_token(&ptr, end))
  776.       break;
  777.     const token_info *ti = lookup_token(token_start, ptr);
  778.     if (ti->sortify_non_empty(token_start, ptr))
  779.       count++;
  780.   }
  781.   ptr = start;
  782.   int skip = count - len;
  783.   if (skip > 0) {
  784.     for (;;) {
  785.       const char *token_start = ptr;
  786.       if (!get_token(&ptr, end))
  787.     assert(0);
  788.       const token_info *ti = lookup_token(token_start, ptr);
  789.       if (ti->sortify_non_empty(token_start, ptr) && --skip < 0) {
  790.     ptr = token_start;
  791.     break;
  792.       }
  793.     }
  794.   }
  795.   first_part(len, ptr, end, result);
  796. }
  797.  
  798. void truncate_expr::evaluate(int tentative, const reference &ref,
  799.                  string &result, substring_position &)
  800. {
  801.   if (expr) {
  802.     string temp;
  803.     substring_position temp_pos;
  804.     expr->evaluate(tentative, ref, temp, temp_pos);
  805.     const char *start = temp.contents();
  806.     const char *end = start + temp.length();
  807.     if (n > 0)
  808.       first_part(n, start, end, result);
  809.     else if (n < 0)
  810.       last_part(-n, start, end, result);
  811.   }
  812. }
  813.  
  814. void alternative_expr::evaluate(int tentative, const reference &ref,
  815.                 string &result, substring_position &pos)
  816. {
  817.   int start_length = result.length();
  818.   if (expr1)
  819.     expr1->evaluate(tentative, ref, result, pos);
  820.   if (result.length() == start_length && expr2)
  821.     expr2->evaluate(tentative, ref, result, pos);
  822. }
  823.  
  824. void list_expr::evaluate(int tentative, const reference &ref,
  825.              string &result, substring_position &pos)
  826. {
  827.   if (expr1)
  828.     expr1->evaluate(tentative, ref, result, pos);
  829.   if (expr2)
  830.     expr2->evaluate(tentative, ref, result, pos);
  831. }
  832.  
  833. void substitute_expr::evaluate(int tentative, const reference &ref,
  834.                    string &result, substring_position &pos)
  835. {
  836.   int start_length = result.length();
  837.   if (expr1)
  838.     expr1->evaluate(tentative, ref, result, pos);
  839.   if (result.length() > start_length && result[result.length() - 1] == '-') {
  840.     // ought to see if pos covers the -
  841.     result.set_length(result.length() - 1);
  842.     if (expr2)
  843.       expr2->evaluate(tentative, ref, result, pos);
  844.   }
  845. }
  846.  
  847. void conditional_expr::evaluate(int tentative, const reference &ref,
  848.                 string &result, substring_position &pos)
  849. {
  850.   string temp;
  851.   substring_position temp_pos;
  852.   if (expr1)
  853.     expr1->evaluate(tentative, ref, temp, temp_pos);
  854.   if (temp.length() > 0) {
  855.     if (expr2)
  856.       expr2->evaluate(tentative, ref, result, pos);
  857.   }
  858.   else {
  859.     if (expr3)
  860.       expr3->evaluate(tentative, ref, result, pos);
  861.   }
  862. }
  863.  
  864. void reference::pre_compute_label()
  865. {
  866.   if (parsed_label != 0
  867.       && (parsed_label->analyze() & expression::CONTAINS_VARIABLE)) {
  868.     label.clear();
  869.     substring_position temp_pos;
  870.     parsed_label->evaluate(1, *this, label, temp_pos);
  871.     label_ptr = lookup_label(label);
  872.   }
  873. }
  874.  
  875. void reference::compute_label()
  876. {
  877.   label.clear();
  878.   if (parsed_label)
  879.     parsed_label->evaluate(0, *this, label, separator_pos);
  880.   if (short_label_flag && parsed_short_label)
  881.     parsed_short_label->evaluate(0, *this, short_label, short_separator_pos);
  882.   if (date_as_label) {
  883.     string new_date;
  884.     if (parsed_date_label) {
  885.       substring_position temp_pos;
  886.       parsed_date_label->evaluate(0, *this, new_date, temp_pos);
  887.     }
  888.     set_date(new_date);
  889.   }
  890.   if (label_ptr)
  891.     label_ptr->count += 1;
  892. }
  893.  
  894. void reference::immediate_compute_label()
  895. {
  896.   if (label_ptr)
  897.     label_ptr->total = 2;    // force use of disambiguator
  898.   compute_label();
  899. }
  900.  
  901. int reference::merge_labels(reference **v, int n, label_type type,
  902.                 string &result)
  903. {
  904.   if (abbreviate_label_ranges)
  905.     return merge_labels_by_number(v, n, type, result);
  906.   else
  907.     return merge_labels_by_parts(v, n, type, result);
  908. }
  909.  
  910. int reference::merge_labels_by_number(reference **v, int n, label_type type,
  911.                       string &result)
  912. {
  913.   if (n <= 1)
  914.     return 0;
  915.   int num = get_number();
  916.   // Only merge three or more labels.
  917.   if (v[0]->get_number() != num + 1
  918.       || v[1]->get_number() != num + 2)
  919.     return 0;
  920.   for (int i = 2; i < n; i++)
  921.     if (v[i]->get_number() != num + i + 1)
  922.       break;
  923.   result = get_label(type);
  924.   result += label_range_indicator;
  925.   result += v[i - 1]->get_label(type);
  926.   return i;
  927. }
  928.  
  929. const substring_position &reference::get_separator_pos(label_type type) const
  930. {
  931.   if (type == SHORT_LABEL && short_label_flag)
  932.     return short_separator_pos;
  933.   else
  934.     return separator_pos;
  935. }
  936.  
  937. const string &reference::get_label(label_type type) const
  938. {
  939.   if (type == SHORT_LABEL && short_label_flag)
  940.     return short_label; 
  941.   else
  942.     return label;
  943. }
  944.  
  945. int reference::merge_labels_by_parts(reference **v, int n, label_type type,
  946.                      string &result)
  947. {
  948.   if (n <= 0)
  949.     return 0;
  950.   const string &lb = get_label(type);
  951.   const substring_position &sp = get_separator_pos(type);
  952.   if (sp.start < 0
  953.       || sp.start != v[0]->get_separator_pos(type).start 
  954.       || memcmp(lb.contents(), v[0]->get_label(type).contents(),
  955.         sp.start) != 0)
  956.     return 0;
  957.   result = lb;
  958.   int i = 0;
  959.   do {
  960.     result += separate_label_second_parts;
  961.     const substring_position &s = v[i]->get_separator_pos(type);
  962.     int sep_end_pos = s.start + s.length;
  963.     result.append(v[i]->get_label(type).contents() + sep_end_pos,
  964.           v[i]->get_label(type).length() - sep_end_pos);
  965.   } while (++i < n
  966.        && sp.start == v[i]->get_separator_pos(type).start
  967.        && memcmp(lb.contents(), v[i]->get_label(type).contents(),
  968.              sp.start) == 0);
  969.   return i;
  970. }
  971.  
  972. string label_pool;
  973.  
  974. label_info::label_info(const string &s)
  975. : count(0), total(1), length(s.length()), start(label_pool.length())
  976. {
  977.   label_pool += s;
  978. }
  979.  
  980. static label_info **label_table = 0;
  981. static int label_table_size = 0;
  982. static int label_table_used = 0;
  983.  
  984. label_info *lookup_label(const string &label)
  985. {
  986.   if (label_table == 0) {
  987.     label_table = new label_info *[17];
  988.     label_table_size = 17;
  989.     for (int i = 0; i < 17; i++)
  990.       label_table[i] = 0;
  991.   }
  992.   unsigned h = hash_string(label.contents(), label.length()) % label_table_size;
  993.   for (label_info **ptr = label_table + h;
  994.        *ptr != 0;
  995.        (ptr == label_table)
  996.        ? (ptr = label_table + label_table_size - 1)
  997.        : ptr--)
  998.     if ((*ptr)->length == label.length()
  999.     && memcmp(label_pool.contents() + (*ptr)->start, label.contents(),
  1000.           label.length()) == 0) {
  1001.       (*ptr)->total += 1;
  1002.       return *ptr;
  1003.     }
  1004.   label_info *result = *ptr = new label_info(label);
  1005.   if (++label_table_used * 2 > label_table_size) {
  1006.     // Rehash the table.
  1007.     label_info **old_table = label_table;
  1008.     int old_size = label_table_size;
  1009.     label_table_size = next_size(label_table_size);
  1010.     label_table = new label_info *[label_table_size];
  1011.     int i;
  1012.     for (i = 0; i < label_table_size; i++)
  1013.       label_table[i] = 0;
  1014.     for (i = 0; i < old_size; i++)
  1015.       if (old_table[i]) {
  1016.     unsigned h = hash_string(label_pool.contents() + old_table[i]->start,
  1017.                  old_table[i]->length);
  1018.     for (label_info **p = label_table + (h % label_table_size);
  1019.          *p != 0;
  1020.          (p == label_table)
  1021.          ? (p = label_table + label_table_size - 1)
  1022.          : --p)
  1023.         ;
  1024.     *p = old_table[i];
  1025.     }
  1026.     a_delete old_table;
  1027.   }
  1028.   return result;
  1029. }
  1030.  
  1031. void clear_labels()
  1032. {
  1033.   for (int i = 0; i < label_table_size; i++) {
  1034.     delete label_table[i];
  1035.     label_table[i] = 0;
  1036.   }
  1037.   label_table_used = 0;
  1038.   label_pool.clear();
  1039. }
  1040.  
  1041. static void consider_authors(reference **start, reference **end, int i);
  1042.  
  1043. void compute_labels(reference **v, int n)
  1044. {
  1045.   if (parsed_label
  1046.       && (parsed_label->analyze() & expression::CONTAINS_AT)
  1047.       && sort_fields.length() >= 2
  1048.       && sort_fields[0] == 'A'
  1049.       && sort_fields[1] == '+')
  1050.     consider_authors(v, v + n, 0);
  1051.   for (int i = 0; i < n; i++)
  1052.     v[i]->compute_label();
  1053. }
  1054.  
  1055.  
  1056. /* A reference with a list of authors <A0,A1,...,AN> _needs_ author i
  1057. where 0 <= i <= N if there exists a reference with a list of authors
  1058. <B0,B1,...,BM> such that <A0,A1,...,AN> != <B0,B1,...,BM> and M >= i
  1059. and Aj = Bj for 0 <= j < i. In this case if we can't say ``A0,
  1060. A1,...,A(i-1) et al'' because this would match both <A0,A1,...,AN> and
  1061. <B0,B1,...,BM>.  If a reference needs author i we only have to call
  1062. need_author(j) for some j >= i such that the reference also needs
  1063. author j. */
  1064.  
  1065. /* This function handles 2 tasks:
  1066. determine which authors are needed (cannot be elided with et al.);
  1067. determine which authors can have only last names in the labels.
  1068.  
  1069. References >= start and < end have the same first i author names.
  1070. Also they're sorted by A+. */
  1071.  
  1072. static void consider_authors(reference **start, reference **end, int i)
  1073. {
  1074.   if (start >= end)
  1075.     return;
  1076.   reference **p = start;
  1077.   if (i >= (*p)->get_nauthors()) {
  1078.     for (++p; p < end && i >= (*p)->get_nauthors(); p++)
  1079.       ;
  1080.     if (p < end && i > 0) {
  1081.       // If we have an author list <A B C> and an author list <A B C D>,
  1082.       // then both lists need C.
  1083.       for (reference **q = start; q < end; q++)
  1084.     (*q)->need_author(i - 1);
  1085.     }
  1086.     start = p;
  1087.   }
  1088.   while (p < end) {
  1089.     reference **last_name_start = p;
  1090.     reference **name_start = p;
  1091.     for (++p;
  1092.      p < end && i < (*p)->get_nauthors()
  1093.      && same_author_last_name(**last_name_start, **p, i);
  1094.      p++) {
  1095.       if (!same_author_name(**name_start, **p, i)) {
  1096.     consider_authors(name_start, p, i + 1);
  1097.     name_start = p;
  1098.       }
  1099.     }
  1100.     consider_authors(name_start, p, i + 1);
  1101.     if (last_name_start == name_start) {
  1102.       for (reference **q = last_name_start; q < p; q++)
  1103.     (*q)->set_last_name_unambiguous(i);
  1104.     }
  1105.     // If we have an author list <A B C D> and <A B C E>, then the lists
  1106.     // need author D and E respectively.
  1107.     if (name_start > start || p < end) {
  1108.       for (reference **q = last_name_start; q < p; q++)
  1109.     (*q)->need_author(i);
  1110.     }
  1111.   }
  1112. }
  1113.  
  1114. int same_author_last_name(const reference &r1, const reference &r2, int n)
  1115. {
  1116.   const char *ae1;
  1117.   const char *as1 = r1.get_sort_field(0, n, 0, &ae1);
  1118.   assert(as1 != 0);
  1119.   const char *ae2;
  1120.   const char *as2 = r2.get_sort_field(0, n, 0, &ae2);
  1121.   assert(as2 != 0);
  1122.   return ae1 - as1 == ae2 - as2 && memcmp(as1, as2, ae1 - as1) == 0;
  1123. }
  1124.  
  1125. int same_author_name(const reference &r1, const reference &r2, int n)
  1126. {
  1127.   const char *ae1;
  1128.   const char *as1 = r1.get_sort_field(0, n, -1, &ae1);
  1129.   assert(as1 != 0);
  1130.   const char *ae2;
  1131.   const char *as2 = r2.get_sort_field(0, n, -1, &ae2);
  1132.   assert(as2 != 0);
  1133.   return ae1 - as1 == ae2 - as2 && memcmp(as1, as2, ae1 - as1) == 0;
  1134. }
  1135.  
  1136.  
  1137. void int_set::set(int i)
  1138. {
  1139.   assert(i >= 0);
  1140.   int bytei = i >> 3;
  1141.   if (bytei >= v.length()) {
  1142.     int old_length = v.length();
  1143.     v.set_length(bytei + 1);
  1144.     for (int j = old_length; j <= bytei; j++)
  1145.       v[j] = 0;
  1146.   }
  1147.   v[bytei] |= 1 << (i & 7);
  1148. }
  1149.  
  1150. int int_set::get(int i) const
  1151. {
  1152.   assert(i >= 0);
  1153.   int bytei = i >> 3;
  1154.   return bytei >= v.length() ? 0 : (v[bytei] & (1 << (i & 7))) != 0;
  1155. }
  1156.  
  1157. void reference::set_last_name_unambiguous(int i)
  1158. {
  1159.   last_name_unambiguous.set(i);
  1160. }
  1161.  
  1162. void reference::need_author(int n)
  1163. {
  1164.   if (n > last_needed_author)
  1165.     last_needed_author = n;
  1166. }
  1167.  
  1168. const char *reference::get_authors(const char **end) const
  1169. {
  1170.   if (!computed_authors) {
  1171.     ((reference *)this)->computed_authors = 1;
  1172.     string &result = ((reference *)this)->authors;
  1173.     int na = get_nauthors();
  1174.     result.clear();
  1175.     for (int i = 0; i < na; i++) {
  1176.       if (last_name_unambiguous.get(i)) {
  1177.     const char *e, *start = get_author_last_name(i, &e);
  1178.     assert(start != 0);
  1179.     result.append(start, e - start);
  1180.       }
  1181.       else {
  1182.     const char *e, *start = get_author(i, &e);
  1183.     assert(start != 0);
  1184.     result.append(start, e - start);
  1185.       }
  1186.       if (i == last_needed_author
  1187.       && et_al.length() > 0
  1188.       && et_al_min_elide > 0
  1189.       && last_needed_author + et_al_min_elide < na
  1190.       && na >= et_al_min_total) {
  1191.     result += et_al;
  1192.     break;
  1193.       }
  1194.       if (i < na - 1) {
  1195.     if (na == 2)
  1196.       result += join_authors_exactly_two;
  1197.     else if (i < na - 2)
  1198.       result += join_authors_default;
  1199.     else
  1200.       result += join_authors_last_two;
  1201.       }
  1202.     }
  1203.   }
  1204.   const char *start = authors.contents();
  1205.   *end = start + authors.length();
  1206.   return start;
  1207. }
  1208.  
  1209. int reference::get_nauthors() const
  1210. {
  1211.   if (nauthors < 0) {
  1212.     const char *dummy;
  1213.     for (int na = 0; get_author(na, &dummy) != 0; na++)
  1214.       ;
  1215.     ((reference *)this)->nauthors = na;
  1216.   }
  1217.   return nauthors;
  1218. }
  1219. #line 1220 "y.tab.c"
  1220. #define YYABORT goto yyabort
  1221. #define YYACCEPT goto yyaccept
  1222. #define YYERROR goto yyerrlab
  1223. int
  1224. yyparse()
  1225. {
  1226.     register int yym, yyn, yystate;
  1227. #if YYDEBUG
  1228.     register char *yys;
  1229.     extern char *getenv();
  1230.  
  1231.     if (yys = getenv("YYDEBUG"))
  1232.     {
  1233.         yyn = *yys;
  1234.         if (yyn >= '0' && yyn <= '9')
  1235.             yydebug = yyn - '0';
  1236.     }
  1237. #endif
  1238.  
  1239.     yynerrs = 0;
  1240.     yyerrflag = 0;
  1241.     yychar = (-1);
  1242.  
  1243.     yyssp = yyss;
  1244.     yyvsp = yyvs;
  1245.     *yyssp = yystate = 0;
  1246.  
  1247. yyloop:
  1248.     if (yyn = yydefred[yystate]) goto yyreduce;
  1249.     if (yychar < 0)
  1250.     {
  1251.         if ((yychar = yylex()) < 0) yychar = 0;
  1252. #if YYDEBUG
  1253.         if (yydebug)
  1254.         {
  1255.             yys = 0;
  1256.             if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  1257.             if (!yys) yys = "illegal-symbol";
  1258.             printf("yydebug: state %d, reading %d (%s)\n", yystate,
  1259.                     yychar, yys);
  1260.         }
  1261. #endif
  1262.     }
  1263.     if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
  1264.             yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
  1265.     {
  1266. #if YYDEBUG
  1267.         if (yydebug)
  1268.             printf("yydebug: state %d, shifting to state %d\n",
  1269.                     yystate, yytable[yyn]);
  1270. #endif
  1271.         if (yyssp >= yyss + yystacksize - 1)
  1272.         {
  1273.             goto yyoverflow;
  1274.         }
  1275.         *++yyssp = yystate = yytable[yyn];
  1276.         *++yyvsp = yylval;
  1277.         yychar = (-1);
  1278.         if (yyerrflag > 0)  --yyerrflag;
  1279.         goto yyloop;
  1280.     }
  1281.     if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
  1282.             yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
  1283.     {
  1284.         yyn = yytable[yyn];
  1285.         goto yyreduce;
  1286.     }
  1287.     if (yyerrflag) goto yyinrecovery;
  1288. #ifdef lint
  1289.     goto yynewerror;
  1290. #endif
  1291. yynewerror:
  1292.     yyerror("syntax error");
  1293. #ifdef lint
  1294.     goto yyerrlab;
  1295. #endif
  1296. yyerrlab:
  1297.     ++yynerrs;
  1298. yyinrecovery:
  1299.     if (yyerrflag < 3)
  1300.     {
  1301.         yyerrflag = 3;
  1302.         for (;;)
  1303.         {
  1304.             if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
  1305.                     yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
  1306.             {
  1307. #if YYDEBUG
  1308.                 if (yydebug)
  1309.                     printf("yydebug: state %d, error recovery shifting\
  1310.  to state %d\n", *yyssp, yytable[yyn]);
  1311. #endif
  1312.                 if (yyssp >= yyss + yystacksize - 1)
  1313.                 {
  1314.                     goto yyoverflow;
  1315.                 }
  1316.                 *++yyssp = yystate = yytable[yyn];
  1317.                 *++yyvsp = yylval;
  1318.                 goto yyloop;
  1319.             }
  1320.             else
  1321.             {
  1322. #if YYDEBUG
  1323.                 if (yydebug)
  1324.                     printf("yydebug: error recovery discarding state %d\n",
  1325.                             *yyssp);
  1326. #endif
  1327.                 if (yyssp <= yyss) goto yyabort;
  1328.                 --yyssp;
  1329.                 --yyvsp;
  1330.             }
  1331.         }
  1332.     }
  1333.     else
  1334.     {
  1335.         if (yychar == 0) goto yyabort;
  1336. #if YYDEBUG
  1337.         if (yydebug)
  1338.         {
  1339.             yys = 0;
  1340.             if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  1341.             if (!yys) yys = "illegal-symbol";
  1342.             printf("yydebug: state %d, error recovery discards token %d (%s)\n",
  1343.                     yystate, yychar, yys);
  1344.         }
  1345. #endif
  1346.         yychar = (-1);
  1347.         goto yyloop;
  1348.     }
  1349. yyreduce:
  1350. #if YYDEBUG
  1351.     if (yydebug)
  1352.         printf("yydebug: state %d, reducing by rule %d (%s)\n",
  1353.                 yystate, yyn, yyrule[yyn]);
  1354. #endif
  1355.     yym = yylen[yyn];
  1356.     yyval = yyvsp[1-yym];
  1357.     switch (yyn)
  1358.     {
  1359. case 1:
  1360. #line 250 "/u/jjc/groff/refer/label.y"
  1361. { parse_result = (yyvsp[0].expr ? new analyzed_expr(yyvsp[0].expr) : 0); }
  1362. break;
  1363. case 2:
  1364. #line 255 "/u/jjc/groff/refer/label.y"
  1365. { yyval.expr = yyvsp[0].expr; }
  1366. break;
  1367. case 3:
  1368. #line 257 "/u/jjc/groff/refer/label.y"
  1369. { yyval.expr = new conditional_expr(yyvsp[-4].expr, yyvsp[-2].expr, yyvsp[0].expr); }
  1370. break;
  1371. case 4:
  1372. #line 262 "/u/jjc/groff/refer/label.y"
  1373. { yyval.expr = 0; }
  1374. break;
  1375. case 5:
  1376. #line 264 "/u/jjc/groff/refer/label.y"
  1377. { yyval.expr = yyvsp[0].expr; }
  1378. break;
  1379. case 6:
  1380. #line 269 "/u/jjc/groff/refer/label.y"
  1381. { yyval.expr = yyvsp[0].expr; }
  1382. break;
  1383. case 7:
  1384. #line 271 "/u/jjc/groff/refer/label.y"
  1385. { yyval.expr = new alternative_expr(yyvsp[-2].expr, yyvsp[0].expr); }
  1386. break;
  1387. case 8:
  1388. #line 273 "/u/jjc/groff/refer/label.y"
  1389. { yyval.expr = new conditional_expr(yyvsp[-2].expr, yyvsp[0].expr, 0); }
  1390. break;
  1391. case 9:
  1392. #line 278 "/u/jjc/groff/refer/label.y"
  1393. { yyval.expr = yyvsp[0].expr; }
  1394. break;
  1395. case 10:
  1396. #line 280 "/u/jjc/groff/refer/label.y"
  1397. { yyval.expr = new list_expr(yyvsp[-1].expr, yyvsp[0].expr); }
  1398. break;
  1399. case 11:
  1400. #line 285 "/u/jjc/groff/refer/label.y"
  1401. { yyval.expr = yyvsp[0].expr; }
  1402. break;
  1403. case 12:
  1404. #line 287 "/u/jjc/groff/refer/label.y"
  1405. { yyval.expr = new substitute_expr(yyvsp[-2].expr, yyvsp[0].expr); }
  1406. break;
  1407. case 13:
  1408. #line 292 "/u/jjc/groff/refer/label.y"
  1409. { yyval.expr = new at_expr; }
  1410. break;
  1411. case 14:
  1412. #line 294 "/u/jjc/groff/refer/label.y"
  1413. {
  1414.           yyval.expr = new literal_expr(literals.contents() + yyvsp[0].str.start,
  1415.                     yyvsp[0].str.len);
  1416.         }
  1417. break;
  1418. case 15:
  1419. #line 299 "/u/jjc/groff/refer/label.y"
  1420. { yyval.expr = new field_expr(yyvsp[0].num, 0); }
  1421. break;
  1422. case 16:
  1423. #line 301 "/u/jjc/groff/refer/label.y"
  1424. { yyval.expr = new field_expr(yyvsp[-1].num, yyvsp[0].num - 1); }
  1425. break;
  1426. case 17:
  1427. #line 303 "/u/jjc/groff/refer/label.y"
  1428. {
  1429.           switch (yyvsp[0].num) {
  1430.           case 'I':
  1431.           case 'i':
  1432.           case 'A':
  1433.           case 'a':
  1434.             yyval.expr = new format_expr(yyvsp[0].num);
  1435.             break;
  1436.           default:
  1437.             command_error("unrecognized format `%1'", char(yyvsp[0].num));
  1438.             yyval.expr = new format_expr('a');
  1439.             break;
  1440.           }
  1441.         }
  1442. break;
  1443. case 18:
  1444. #line 319 "/u/jjc/groff/refer/label.y"
  1445. {
  1446.           yyval.expr = new format_expr('0', yyvsp[0].dig.ndigits, yyvsp[0].dig.val);
  1447.         }
  1448. break;
  1449. case 19:
  1450. #line 323 "/u/jjc/groff/refer/label.y"
  1451. {
  1452.           switch (yyvsp[-1].num) {
  1453.           case 'l':
  1454.             yyval.expr = new map_expr(yyvsp[-4].expr, lowercase);
  1455.             break;
  1456.           case 'u':
  1457.             yyval.expr = new map_expr(yyvsp[-4].expr, uppercase);
  1458.             break;
  1459.           case 'c':
  1460.             yyval.expr = new map_expr(yyvsp[-4].expr, capitalize);
  1461.             break;
  1462.           case 'r':
  1463.             yyval.expr = new map_expr(yyvsp[-4].expr, reverse_name);
  1464.             break;
  1465.           case 'a':
  1466.             yyval.expr = new map_expr(yyvsp[-4].expr, abbreviate_name);
  1467.             break;
  1468.           case 'y':
  1469.             yyval.expr = new extractor_expr(yyvsp[-4].expr, find_year, yyvsp[-2].num);
  1470.             break;
  1471.           case 'n':
  1472.             yyval.expr = new extractor_expr(yyvsp[-4].expr, find_last_name, yyvsp[-2].num);
  1473.             break;
  1474.           default:
  1475.             yyval.expr = yyvsp[-4].expr;
  1476.             command_error("unknown function `%1'", char(yyvsp[-1].num));
  1477.             break;
  1478.           }
  1479.         }
  1480. break;
  1481. case 20:
  1482. #line 354 "/u/jjc/groff/refer/label.y"
  1483. { yyval.expr = new truncate_expr(yyvsp[-2].expr, yyvsp[0].num); }
  1484. break;
  1485. case 21:
  1486. #line 356 "/u/jjc/groff/refer/label.y"
  1487. { yyval.expr = new truncate_expr(yyvsp[-2].expr, -yyvsp[0].num); }
  1488. break;
  1489. case 22:
  1490. #line 358 "/u/jjc/groff/refer/label.y"
  1491. { yyval.expr = new star_expr(yyvsp[-1].expr); }
  1492. break;
  1493. case 23:
  1494. #line 360 "/u/jjc/groff/refer/label.y"
  1495. { yyval.expr = yyvsp[-1].expr; }
  1496. break;
  1497. case 24:
  1498. #line 362 "/u/jjc/groff/refer/label.y"
  1499. { yyval.expr = new separator_expr(yyvsp[-1].expr); }
  1500. break;
  1501. case 25:
  1502. #line 367 "/u/jjc/groff/refer/label.y"
  1503. { yyval.num = -1; }
  1504. break;
  1505. case 26:
  1506. #line 369 "/u/jjc/groff/refer/label.y"
  1507. { yyval.num = yyvsp[0].num; }
  1508. break;
  1509. case 27:
  1510. #line 374 "/u/jjc/groff/refer/label.y"
  1511. { yyval.num = yyvsp[0].num; }
  1512. break;
  1513. case 28:
  1514. #line 376 "/u/jjc/groff/refer/label.y"
  1515. { yyval.num = yyvsp[-1].num*10 + yyvsp[0].num; }
  1516. break;
  1517. case 29:
  1518. #line 381 "/u/jjc/groff/refer/label.y"
  1519. { yyval.dig.ndigits = 1; yyval.dig.val = yyvsp[0].num; }
  1520. break;
  1521. case 30:
  1522. #line 383 "/u/jjc/groff/refer/label.y"
  1523. { yyval.dig.ndigits = yyvsp[-1].dig.ndigits + 1; yyval.dig.val = yyvsp[-1].dig.val*10 + yyvsp[0].num; }
  1524. break;
  1525. case 31:
  1526. #line 389 "/u/jjc/groff/refer/label.y"
  1527. { yyval.num = 0; }
  1528. break;
  1529. case 32:
  1530. #line 391 "/u/jjc/groff/refer/label.y"
  1531. { yyval.num = 1; }
  1532. break;
  1533. case 33:
  1534. #line 393 "/u/jjc/groff/refer/label.y"
  1535. { yyval.num = -1; }
  1536. break;
  1537. #line 1538 "y.tab.c"
  1538.     }
  1539.     yyssp -= yym;
  1540.     yystate = *yyssp;
  1541.     yyvsp -= yym;
  1542.     yym = yylhs[yyn];
  1543.     if (yystate == 0 && yym == 0)
  1544.     {
  1545. #if YYDEBUG
  1546.         if (yydebug)
  1547.             printf("yydebug: after reduction, shifting from state 0 to\
  1548.  state %d\n", YYFINAL);
  1549. #endif
  1550.         yystate = YYFINAL;
  1551.         *++yyssp = YYFINAL;
  1552.         *++yyvsp = yyval;
  1553.         if (yychar < 0)
  1554.         {
  1555.             if ((yychar = yylex()) < 0) yychar = 0;
  1556. #if YYDEBUG
  1557.             if (yydebug)
  1558.             {
  1559.                 yys = 0;
  1560.                 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
  1561.                 if (!yys) yys = "illegal-symbol";
  1562.                 printf("yydebug: state %d, reading %d (%s)\n",
  1563.                         YYFINAL, yychar, yys);
  1564.             }
  1565. #endif
  1566.         }
  1567.         if (yychar == 0) goto yyaccept;
  1568.         goto yyloop;
  1569.     }
  1570.     if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
  1571.             yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
  1572.         yystate = yytable[yyn];
  1573.     else
  1574.         yystate = yydgoto[yym];
  1575. #if YYDEBUG
  1576.     if (yydebug)
  1577.         printf("yydebug: after reduction, shifting from state %d \
  1578. to state %d\n", *yyssp, yystate);
  1579. #endif
  1580.     if (yyssp >= yyss + yystacksize - 1)
  1581.     {
  1582.         goto yyoverflow;
  1583.     }
  1584.     *++yyssp = yystate;
  1585.     *++yyvsp = yyval;
  1586.     goto yyloop;
  1587. yyoverflow:
  1588.     yyerror("yacc stack overflow");
  1589. yyabort:
  1590.     return (1);
  1591. yyaccept:
  1592.     return (0);
  1593. }
  1594.