home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / math_parser.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  19KB  |  777 lines

  1. /*
  2.  *  File:        math_parser.C
  3.  *  Purpose:     Parser for mathed
  4.  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  5.  *  Created:     January 1996
  6.  *  Description: Parse LaTeX2e math mode code.
  7.  *
  8.  *  Dependencies: Xlib, XForms
  9.  *
  10.  *  Copyright: (c) 1996, Alejandro Aguilar Sierra
  11.  *
  12.  *   Version: 0.8beta.
  13.  *
  14.  *   You are free to use and modify this code under the terms of
  15.  *   the GNU General Public Licence version 2 or later.
  16.  */
  17.  
  18. #include <config.h>
  19. #include <stdlib.h>
  20.  
  21. #ifdef __GNUG__
  22. #pragma implementation "math_parser.h"
  23. #endif
  24.  
  25. #include "math_parser.h"
  26. #include "math_iter.h"
  27. #include "math_inset.h"
  28. #include "math_macro.h"
  29. #include "error.h"
  30.  
  31. enum {
  32.     FLAG_BRACE      = 1,    //  A { needed
  33.     FLAG_BRACE_ARG  = 2,    //  Next { is argument
  34.     FLAG_BRACE_OPT  = 4,    //  Optional {
  35.     FLAG_BRACE_LAST = 8,    //  Last } ends the parsing process
  36.     FLAG_BRACK_ARG  = 16,    //  Optional [
  37.     FLAG_RIGHT    = 32,      //  Next right ends the parsing process
  38.     FLAG_END    = 64,      //  Next end ends the parsing process
  39.     FLAG_BRACE_FONT = 128    //  Next } closes a font 
  40. };
  41.  
  42. YYSTYPE yylval;
  43.  
  44.  
  45. static short mathed_env = LM_EN_INTEXT;
  46.  
  47. char *mathed_label = NULL;
  48.  
  49. char const *latex_mathenv[] = { 
  50.    "math", 
  51.    "displaymath", 
  52.    "equation", 
  53.    "eqnarray*",
  54.    "eqnarray",
  55.    "array"
  56. };
  57.  
  58.  
  59. char const *latex_mathspace[] = {
  60.    "!", ",", ":", ";", "quad", "qquad"
  61. };
  62.    
  63. char const *latex_special_chars = "#$%&_{}";
  64.         
  65. // These are lexical codes, not semantic
  66. enum lexcode_enum {
  67.    LexNone,
  68.    LexESC,
  69.    LexAlpha,
  70.    LexDigit,
  71.    LexBOP,         // Binary operators or relations
  72.    LexMathSpace,
  73.    LexOpen,
  74.    LexClose,
  75.    LexComment,
  76.    LexArgument,
  77.    LexSpace,
  78.    LexNewLine,
  79.    LexOther,
  80.    LexSelf
  81. };
  82.  
  83. static lexcode_enum lexcode[256];  
  84. static char yytext[256];
  85. static int yylineno;
  86. static FILE *yyin;
  87. static bool yy_mtextmode=false;
  88.         
  89. inline
  90. char *strnew(char const* s)
  91. {
  92.     char *s1 = new char[strlen(s)+1]; // this leaks when not delete[]'ed
  93.     strcpy(s1, s);
  94.     return s1;
  95. }
  96.  
  97.  
  98. static void mathPrintError(char const *msg) 
  99. {
  100.     lyxerr.print(LString("Line ~")
  101.          + int(yylineno)
  102.          + LString(": Math parse error: ")
  103.          + msg); 
  104. }
  105.  
  106.  
  107. static void LexInitCodes()
  108. {
  109.    int i;
  110.    
  111.    for (i=0;  i<=255; i++) lexcode[i] = LexNone; 
  112.    for (i='A'; i<='Z'; i++) lexcode[i] = LexAlpha;
  113.    for (i='a'; i<='z'; i++) lexcode[i] = LexAlpha;
  114.    for (i='0'; i<='9'; i++) lexcode[i] = LexDigit;
  115.    lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
  116.    lexcode['\n'] = LexNewLine;
  117.    lexcode['%'] = LexComment;
  118.    lexcode['#'] = LexArgument;
  119.    lexcode['+'] = lexcode['-'] = lexcode['*'] = lexcode['/'] = 
  120.    lexcode['<'] = lexcode['>'] = lexcode['='] = LexBOP;
  121.    
  122.    lexcode['!'] = lexcode[','] = lexcode[':'] = lexcode[';'] = LexMathSpace;
  123.    lexcode['('] = lexcode[')'] = lexcode['|'] = lexcode['.'] = lexcode['?'] = LexOther; 
  124.    lexcode['\'']= LexAlpha;
  125.    
  126.    lexcode['['] = lexcode[']'] = lexcode['^'] = lexcode['_'] = 
  127.    lexcode['&'] = LexSelf;  
  128.    
  129.    lexcode['\\'] = LexESC;
  130.    lexcode['{'] = LexOpen;
  131.    lexcode['}'] = LexClose;
  132. }
  133.  
  134. static char LexGetArg(char lf)
  135. {
  136.    char c, rg, *p = &yytext[0];
  137.    int bcnt =1;
  138.    
  139.    while (!feof(yyin)) {
  140.       c = getc(yyin); 
  141.       if (c>' ') {
  142.      if (!lf) lf = c; else
  143.      if (c!=lf)
  144.        fprintf(stderr, "Math parse error: unexpected '%c'\n", c);
  145.      break;
  146.       }
  147.    }
  148.    rg = (lf=='{') ? '}': ((lf=='[') ? ']': ((lf=='(') ? ')': 0));
  149.    if (!rg) {
  150.       fprintf(stderr, "Math parse error: unknown bracket '%c'\n", lf);
  151.       return '\0';
  152.    } 
  153.    do {
  154.       c = getc(yyin); 
  155.       if (c==lf) bcnt++;
  156.       if (c==rg) bcnt--;
  157.       if (c>' ' && bcnt>0) *(p++) = c;
  158.    } while (bcnt>0 && !feof(yyin));
  159.    *p = '\0';
  160.    return rg;
  161. }
  162.  
  163. static int yylex(void)
  164. {
  165.    static int init_done = 0;
  166.    char c;
  167.    
  168.    if (!init_done) LexInitCodes();
  169.    
  170.    while (!feof(yyin)) { 
  171.       c = getc(yyin);
  172.        
  173.       if (yy_mtextmode && c==' ') {
  174.       yylval.i=' ';
  175.       return LM_TK_ALPHA;
  176.       }
  177.        
  178.        if (lexcode[c]==LexNewLine) {
  179.        yylineno++; 
  180.        continue;
  181.        }
  182.      
  183.       if (lexcode[c]==LexComment) 
  184.     do c = getc(yyin); while (c!='\n' % !feof(yyin));  // eat comments
  185.     
  186.       if (lexcode[c]==LexDigit || lexcode[c]==LexOther || lexcode[c]==LexMathSpace) 
  187.         { yylval.i= c; return LM_TK_STR; }
  188.       if (lexcode[c]==LexAlpha) { yylval.i=c; return LM_TK_ALPHA; }
  189.       if (lexcode[c]==LexBOP)   { yylval.i=c; return LM_TK_BOP; }
  190.       if (lexcode[c]==LexSelf)  { return c; }   
  191.       if (lexcode[c]==LexArgument)   { 
  192.       c = getc(yyin);
  193.       yylval.i = c - '0';
  194.       return LM_TK_ARGUMENT; 
  195.       }
  196.       if (lexcode[c]==LexOpen)   { return LM_TK_OPEN; }
  197.       if (lexcode[c]==LexClose)   { return LM_TK_CLOSE; }
  198.       
  199.       if (lexcode[c]==LexESC)   {
  200.      c = getc(yyin);
  201.      if (c=='\\')    { return LM_TK_NEWLINE; }
  202.      if (c=='(')    { yylval.i = LM_EN_INTEXT; return LM_TK_BEGIN; }
  203.      if (c==')')    { yylval.i = LM_EN_INTEXT; return LM_TK_END; }
  204.      if (c=='[')    { yylval.i = LM_EN_DISPLAY; return LM_TK_BEGIN; }
  205.      if (c==']')    { yylval.i = LM_EN_DISPLAY; return LM_TK_END; }
  206.      if (strchr(latex_special_chars, c)) {
  207.          yylval.i = c;
  208.          return LM_TK_SPECIAL;
  209.      }  
  210.      if (lexcode[c]==LexMathSpace) {
  211.         int i;
  212.         for (i=0; i<4 && c!=latex_mathspace[i][0]; i++);
  213.         yylval.i = (i<4) ? i: 0; 
  214.         return LM_TK_SPACE; 
  215.      }
  216.      if (lexcode[c]==LexAlpha) {
  217.         char* p = &yytext[0];
  218.         while (lexcode[c]==LexAlpha) {
  219.            *p = c;
  220.            c = getc(yyin);
  221.            p++;
  222.         }
  223.         *p = '\0';
  224.         if (!feof(yyin)) ungetc(c, yyin);
  225.         latexkeys *l = in_word_set (yytext, strlen(yytext));
  226.         if (l) {
  227.            if (l->token==LM_TK_BEGIN || l->token==LM_TK_END) { 
  228.           int i;
  229.           LexGetArg('{');
  230. //          for (i=0; i<5 && strncmp(yytext, latex_mathenv[i],
  231. //                strlen(latex_mathenv[i])); i++);
  232.           
  233.           for (i=0; i<6 && strcmp(yytext, latex_mathenv[i]); i++);
  234.           yylval.i = i;
  235.            } else
  236.            if (l->token==LM_TK_SPACE) 
  237.          yylval.i = l->id;
  238.            else
  239.          yylval.l = l;
  240.            return l->token;
  241.         } else { 
  242.            yylval.s = yytext;
  243.            return LM_TK_UNDEF;
  244.         }
  245.      }
  246.       }
  247.    }
  248.    return 0;
  249. }
  250.  
  251. int parse_align(char *hor, char *)
  252. {    
  253.    char *c;
  254.  
  255.    int nc = 0;
  256.    for (c=hor; c && *c>' '; c++) nc++;
  257.    return nc;
  258. }
  259.    
  260. LyxArrayBase *mathed_parse(unsigned flags, LyxArrayBase *array, MathParInset **mtx)
  261. {
  262.    int t = yylex();
  263.    bool panic = false;
  264.    static int plevel = -1;
  265.    static int size = LM_ST_TEXT;
  266.    MathedTextCodes varcode = LM_TC_VAR;
  267.    MathedInset* binset = 0;
  268.    static MathMacroTemplate *macro=0;
  269.    
  270.    int brace = 0;
  271.    int acc_code = 0;
  272.    int acc_brace = 0;
  273.    MathParInset *mt = (mtx) ? *mtx: 0;//(MathParInset*)NULL;
  274.     MathedRowSt *crow = (mt) ? mt->getRowSt(): 0;
  275.  
  276.    plevel++;
  277.    if (!array) array = new LyxArrayBase;
  278.    MathedIter data(array);
  279.    while (t) {
  280.       if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
  281.      if ((flags & FLAG_BRACK_ARG) && t=='[') {
  282.      }
  283.      else {
  284.          mathPrintError("Expected {. Maybe you forgot to enclose an argument in {}");
  285.         panic = true;
  286.         break;
  287.      }
  288.       }
  289.     MathedInsetTypes fractype = LM_OT_FRAC;
  290.     switch (t) {
  291.     case LM_TK_ALPHA:
  292.       {
  293.      if (acc_code) {
  294.          data.Insert(new MathAccentInset(yylval.i, varcode, acc_code));
  295.          acc_code = 0;
  296.      } else
  297.         data.Insert (yylval.i, varcode);  //LM_TC_VAR);
  298.      break;
  299.       }
  300.     case LM_TK_ARGUMENT:
  301.       {
  302.       if (macro) {
  303.           data.Insert(macro->getMacroPar(yylval.i-1), LM_TC_INSET);
  304.       } 
  305.       break;
  306.       } 
  307.     case LM_TK_NEWCOMMAND:
  308.       {
  309.       int na = 0; 
  310.  
  311.       LexGetArg('{');
  312.       // This name lives until quitting, for that reason
  313.       // I didn't care on deleting explicitly. Later I will.
  314.       char const *name = strnew(&yytext[1]);
  315.       // ugly trick to be removed soon (lyx3)
  316.       char c = getc(yyin);
  317.       ungetc(c, yyin);
  318.       if (c=='[') {
  319.           LexGetArg('[');
  320.           na = atoi(yytext);
  321.       }  
  322.       macro = new MathMacroTemplate(name, na);
  323.       flags = FLAG_BRACE|FLAG_BRACE_LAST;
  324.       *mtx = macro;
  325.       macro->SetData(array);
  326.       break;
  327.       }
  328.     case LM_TK_SPECIAL:
  329.       {      
  330.       data.Insert (yylval.i, LM_TC_SPECIAL);
  331.       break;
  332.       }
  333.     case LM_TK_STR:
  334.       {      
  335.       if (acc_code) {
  336.           data.Insert(new MathAccentInset(yylval.i, LM_TC_CONST, acc_code));
  337.           acc_code = 0;
  338.       } else
  339.         data.Insert (yylval.i, LM_TC_CONST);
  340.       break;
  341.       }
  342.     case LM_TK_OPEN:
  343.       {
  344.     brace++;
  345.     if  (acc_code && !acc_brace) {
  346.         acc_brace = brace;
  347.         break;
  348.     }
  349.     if (flags & FLAG_BRACE_OPT) {
  350.        flags &= ~FLAG_BRACE_OPT;
  351.        flags |= FLAG_BRACE;
  352.     }
  353.           
  354.     if (flags & FLAG_BRACE)
  355.       flags &= ~FLAG_BRACE;
  356.      else {
  357.         data.Insert ('{', LM_TC_TEX);
  358.      }
  359.     break;
  360.       }
  361.     case LM_TK_CLOSE:
  362.       {
  363.      brace--;     
  364.      if (brace < 0) {
  365.         mathPrintError("Unmatching braces");
  366.         panic = true;
  367.         break;
  368.      }
  369.      if (acc_brace && brace==acc_brace-1) {
  370.          acc_brace = 0;
  371.          break;
  372.      }
  373.      if (flags & FLAG_BRACE_FONT) {
  374.         varcode = LM_TC_VAR;
  375.         yy_mtextmode = false;
  376.         flags &= ~FLAG_BRACE_FONT;
  377.         break;
  378.      }
  379.      if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
  380.         plevel--;
  381.         return array;
  382.      } else {
  383.         data.Insert ('}', LM_TC_TEX);
  384.      }
  385.      break;
  386.       }
  387.  
  388.     case '[':
  389.       {
  390.      if (flags & FLAG_BRACK_ARG) {
  391.        flags &= ~FLAG_BRACK_ARG;
  392.        char rg=LexGetArg('[');
  393.        if (rg!=']') {
  394.           mathPrintError("Expected ']'");
  395.           panic = true;
  396.           break;
  397.        }       
  398. //       if (arg) strcpy(arg, yytext);
  399.     } else
  400.       data.Insert ('[');
  401.     break;
  402.       }
  403.     case ']':
  404.       {
  405.     data.Insert (']');
  406.     break;
  407.       }
  408.  
  409.     case '^':
  410.       {  
  411.      MathParInset *p = new MathParInset(size, "", LM_OT_SCRIPT);
  412.      LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, NULL);
  413.      p->SetData(ar);
  414. //     fprintf(stderr, "UP[%d]", p->GetStyle());
  415.      data.Insert (p, LM_TC_UP);
  416.      break;
  417.       }
  418.     case '_':
  419.       {
  420.      MathParInset *p = new MathParInset(size, "", LM_OT_SCRIPT);
  421.      LyxArrayBase * ar = mathed_parse(FLAG_BRACE_OPT|FLAG_BRACE_LAST, NULL);
  422.      p->SetData(ar);
  423.      data.Insert (p, LM_TC_DOWN);
  424.      break;
  425.       }
  426.  
  427.     case LM_TK_LIMIT:
  428.       {
  429.      if (binset) {
  430.         binset->SetLimits((bool)(yylval.l->id));
  431.         binset = NULL;
  432.      }
  433.      break;
  434.       }
  435.       
  436.     case '&':    // Tab
  437.       {
  438.      if ((flags & FLAG_END) && mt && data.getCol()<mt->GetColumns()-1) {
  439.          data.setNumCols(mt->GetColumns());
  440.          data.Insert('T', LM_TC_TAB);
  441.      } else 
  442.         mathPrintError("Unexpected tab");
  443.       // debug info
  444.       fprintf(stderr, "%d %d\n", data.getCol(), mt->GetColumns());
  445.     break;
  446.       }
  447.     case LM_TK_NEWLINE:
  448.       {
  449.       if (mt && (flags & FLAG_END)) {
  450.           if (mt->Permit(LMPF_ALLOW_CR)) {
  451.           if (crow) {
  452.               crow->setNext(new MathedRowSt(mt->GetColumns()+1)); // this leaks
  453.               crow = crow->getNext();
  454.           }
  455.           data.Insert('K', LM_TC_CR);
  456.           } else 
  457.         mathPrintError("Unexpected newline");
  458.       }
  459.       break;
  460.       }
  461.     case LM_TK_BIGSYM:  
  462.       {
  463.      binset = new MathBigopInset(yylval.l->name,yylval.l->id);
  464.      data.Insert(binset);    
  465.      break;
  466.       }
  467.     case LM_TK_SYM:
  468.       {
  469.      if (yylval.l->id < 256) {
  470.         MathedTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
  471.         if (acc_code) {
  472. //        fprintf(stderr, "accd[%d %d %x]", acc_code, LM_not, yylval.l->id);
  473.         data.Insert(new MathAccentInset(yylval.l->id, tc, acc_code));
  474.         acc_code = 0;
  475.         } else
  476.         data.Insert (yylval.l->id, tc);
  477.      } else {
  478.         MathFuncInset *bg = new MathFuncInset(yylval.l->name);
  479.          if (acc_code) {
  480.          data.Insert(new MathAccentInset(bg, acc_code));
  481.          acc_code = 0;
  482.          } else
  483.          data.Insert(bg, true);    
  484.      }
  485.      break;
  486.       }
  487.     case LM_TK_BOP:
  488.       {
  489.      if (acc_code) {
  490.           data.Insert(new MathAccentInset(yylval.i, LM_TC_BOP, acc_code));
  491.           acc_code = 0;
  492.       } else
  493.         data.Insert (yylval.i, LM_TC_BOP);
  494.      break;
  495.       }
  496.     case LM_TK_STY:
  497.       {
  498.       if (mt) {
  499.           mt->UserSetSize(yylval.l->id);
  500.       }
  501.       break; 
  502.       }
  503.     case LM_TK_SPACE:
  504.       {
  505.      if (yylval.i>=0) {
  506.         MathSpaceInset *sp = new MathSpaceInset(yylval.i);
  507.         data.Insert(sp);
  508.      }
  509.      break;
  510.       }       
  511.     case LM_TK_DOTS:
  512.       {
  513.      MathDotsInset *p = new MathDotsInset(yylval.l->name, yylval.l->id);
  514.      data.Insert(p);
  515.      break;
  516.       }     
  517.     case LM_TK_STACK:
  518.        fractype = LM_OT_STACKREL;
  519.     case LM_TK_FRAC:
  520.       {
  521.      MathFracInset *fc = new MathFracInset(fractype);
  522.      LyxArrayBase* num = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
  523.      LyxArrayBase* den = mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST);
  524.      fc->SetData(num, den);
  525.      data.Insert(fc, LM_TC_ACTIVE_INSET);
  526.      break;
  527.       }
  528.     case LM_TK_SQRT:
  529.       {
  530.      MathSqrtInset *sq = new MathSqrtInset(size);
  531.      sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST, 0, (MathParInset **)&sq));
  532.      data.Insert(sq, LM_TC_ACTIVE_INSET);
  533.      break;
  534.       }
  535.        
  536.     case LM_TK_LEFT:
  537.       {
  538.      int lfd, rgd;
  539.      lfd=yylex();
  540.      if (lfd==LM_TK_SYM || lfd==LM_TK_STR || lfd==LM_TK_BOP|| lfd==LM_TK_SPECIAL)
  541.        lfd = (lfd==LM_TK_SYM) ? yylval.l->id: yylval.i;
  542. //     fprintf(stderr, "L[%d %c]", lfd, lfd);
  543.      LyxArrayBase* a = mathed_parse(FLAG_RIGHT);
  544.      rgd=yylex();
  545. //     fprintf(stderr, "R[%d]", rgd);
  546.      if (rgd==LM_TK_SYM || rgd==LM_TK_STR || rgd==LM_TK_BOP || rgd==LM_TK_SPECIAL)
  547.        rgd = (rgd==LM_TK_SYM) ? yylval.l->id: yylval.i;     
  548.      MathDelimInset *dl = new MathDelimInset(lfd, rgd);
  549.      dl->SetData(a);
  550.      data.Insert(dl, LM_TC_ACTIVE_INSET);
  551. //     fprintf(stderr, "RL[%d %d]", lfd, rgd);
  552.        break;
  553.       }
  554.     case LM_TK_RIGHT:
  555.       {
  556.      if (flags & FLAG_RIGHT) { 
  557.         plevel--;
  558.         return array;
  559.      } else {
  560.         mathPrintError("Unmatched right delimiter");
  561. //        panic = true;
  562.      }
  563.      break;
  564.       }
  565.  
  566.     case LM_TK_FONT:
  567.       {
  568.      varcode = (MathedTextCodes)yylval.l->id;
  569.       yy_mtextmode = (bool)(varcode==LM_TC_TEXTRM);
  570.      flags |= (FLAG_BRACE|FLAG_BRACE_FONT);
  571.     break;
  572.       }
  573.     case LM_TK_WIDE:
  574.       {  
  575.      MathDecorationInset *sq = new MathDecorationInset(yylval.l->id, size);
  576.      sq->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
  577.      data.Insert(sq, LM_TC_ACTIVE_INSET);
  578.      break;
  579.       }
  580.       
  581.     case LM_TK_ACCENT: acc_code = yylval.l->id; break;
  582.       
  583.     case LM_TK_NONUM:
  584.       {
  585.       if (crow)
  586.         crow->setNumbered(false);
  587.       break;
  588.       }
  589.  
  590.     case LM_TK_PMOD:
  591.     case LM_TK_FUNC:
  592.       {
  593.       MathedInset *bg = new MathFuncInset(yylval.l->name); 
  594.       if (acc_code) {
  595.           data.Insert(t);
  596.           acc_code = 0;
  597.       } else
  598.         data.Insert(bg);
  599.       break;
  600.       }
  601.     case LM_TK_FUNCLIM:
  602.       {
  603.      data.Insert(new MathFuncInset(yylval.l->name, LM_OT_FUNCLIM));
  604.      break;
  605.       }
  606.     case LM_TK_UNDEF:
  607.       {
  608.       
  609.        MathMacro* p = 
  610.      MathMacroTable::mathMTable.getMacro(yylval.s);
  611.        if (p) {
  612.        data.Insert(p, p->getTCode());
  613.        for (int i=0; p->setArgumentIdx(i); i++)
  614.          p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
  615.        }
  616.        else {
  617.        MathedInset *q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
  618.        if (acc_code) {
  619.            data.Insert(new MathAccentInset(q, acc_code));
  620.            acc_code = 0;
  621.        } else {
  622.            data.Insert(q);
  623.        }
  624.        }
  625.      break;
  626.       }
  627.     case LM_TK_END:
  628.       {
  629.          if (mathed_env != yylval.i && yylval.i!=LM_EN_ARRAY)
  630.        mathPrintError("Unmatched environment");
  631.       // debug info
  632.       fprintf(stderr, "[%d]\n", yylval.i);
  633.      plevel--;
  634.      if (mt) { // && (flags & FLAG_END)) {
  635.         mt->SetData(array);
  636.         array = NULL;
  637.      }
  638.      return array;
  639.      break;
  640.       }
  641.     case LM_TK_BEGIN:
  642.       {
  643.      if (yylval.i==LM_EN_ARRAY) {
  644.         char ar[120], ar2[8];
  645.         ar[0] = ar2[0] = '\0'; 
  646.             char rg=LexGetArg(0);
  647.         if (rg==']') {
  648.            strcpy(ar2, yytext);
  649.            rg = LexGetArg('{');
  650.         }
  651.         strcpy(ar, yytext);
  652.         int nc = parse_align(ar, ar2);
  653.         MathParInset* mm = new MathMatrixInset(nc, 0);
  654.         mm->SetAlign(ar2[0], ar);
  655.                data.Insert(mm, LM_TC_ACTIVE_INSET);
  656.             mathed_parse(FLAG_END, mm->GetData(), &mm);
  657.      } else
  658.      if (yylval.i>=LM_EN_INTEXT && yylval.i<=LM_EN_EQNARRAY) {
  659.          if (plevel!=0) {
  660.          mathPrintError("Misplaced environment");
  661.          break;
  662.          }
  663.          if (!mt) {
  664.          mathPrintError("NULL paragraph.");
  665.          panic = true;
  666.          }
  667.          
  668.          mathed_env = yylval.i;
  669.          if (mathed_env>=LM_EN_DISPLAY) {
  670.          size = LM_ST_DISPLAY;
  671.          if (mathed_env>LM_EN_EQUATION) {
  672.              mt = new MathMatrixInset(3, -1);
  673.              mt->SetAlign(' ', "rcl");
  674.              if (mtx) *mtx = mt;
  675.              flags |= FLAG_END;
  676. //             data.Insert(' ', LM_TC_TAB);
  677. //             data.Insert(' ', LM_TC_TAB);
  678. //             data.Reset();
  679.          }
  680.          mt->SetStyle(size);
  681.          mt->SetType(mathed_env);
  682.          crow = mt->getRowSt();
  683.          }
  684.                                  
  685. #ifdef DEBUG
  686.            fprintf(stderr, "MATH BEGIN[%d]\n", mathed_env);
  687. #endif
  688.      } else {
  689. //         fprintf(stderr, "MATHCRO[%s]",yytext);
  690.          MathMacro* p = 
  691.            MathMacroTable::mathMTable.getMacro(yytext);
  692.          if (p) {
  693.          data.Insert(p, p->getTCode());
  694.          p->setArgumentIdx(0);
  695.          mathed_parse(FLAG_END, p->GetData(), (MathParInset**)&p);
  696. //         for (int i=0; p->setArgumentIdx(i); i++)
  697. //           p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
  698.          } else 
  699.            mathPrintError("Unrecognized environment");
  700.      }
  701.      break;
  702.       }
  703.        
  704.     case LM_TK_MACRO:
  705.      { 
  706.       MathedInset* p = 
  707.         MathMacroTable::mathMTable.getMacro(yylval.l->name);
  708.       if (p)
  709.         data.Insert(p, ((MathMacro*)p)->getTCode());
  710.       break;
  711.       }
  712.        
  713.      case LM_TK_LABEL:
  714.        {       
  715.       char rg = LexGetArg('\0');
  716.       if (rg != '}') {
  717.          mathPrintError("Expected '{'");
  718.           // debug info
  719.           fprintf(stderr, "[%s]\n", yytext); fflush(stderr);
  720.           panic = true;
  721.          break;
  722.       } 
  723.       if (crow) {
  724.           // This is removed by crow's destructor. Bad design? yes, this 
  725.           // will be changed after 0.12
  726.           crow->setLabel(strnew(yytext));
  727.       }
  728.       else {
  729.           // where is this math_label free'ed?
  730.               // Supposedly in ~formula, another bad hack,
  731.               // give me some time please.
  732.           mathed_label = strnew(yytext);
  733.       }
  734. #ifdef DEBUG
  735.       fprintf(stderr, "Label[%d]\n", mathed_label);
  736. #endif
  737.       break;
  738.     } 
  739.      default:
  740.        mathPrintError("Unrecognized token");
  741.        // debug info
  742.        fprintf(stderr, "[%d %s]\n", t, yytext);
  743.        break;
  744.     }
  745.     if (panic) {
  746.        fprintf(stderr, " Math Panic, expect problems!\n"); 
  747.        //   Search for the end command. 
  748.        do t = yylex (); while (t != LM_TK_END && t);
  749.     } else
  750.      t = yylex ();
  751.    
  752.    if ((flags & FLAG_BRACE_OPT)/* && t!='^' && t!='_'*/) {
  753.         flags &= ~FLAG_BRACE_OPT;
  754.        //data.Insert (LM_TC_CLOSE);
  755.        break;
  756.     }
  757.    }
  758.    plevel--;
  759.    return array;
  760. }
  761.  
  762.  
  763. void mathed_parser_file(FILE* file, int lineno)
  764. {
  765.     yyin = file;
  766.     yylineno = lineno;
  767.     if (!MathMacroTable::built)
  768.     MathMacroTable::mathMTable.builtinMacros();
  769. }
  770.  
  771.  
  772. int mathed_parser_lineno()
  773. {
  774.     return yylineno;
  775. }
  776.  
  777.