]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_parser.C
Small parser bug fixed
[lyx.git] / src / mathed / math_parser.C
index 1ae93cb3694bc77f6a72de40137eb3e547e65bff..bdba0035a3742131f65136fc95727dc072106fa1 100644 (file)
@@ -72,7 +72,6 @@ enum lexcode_enum {
 lexcode_enum lexcode[256];  
 
 
-
 char const * latex_special_chars = "#$%&_{}";
 
 
@@ -82,8 +81,8 @@ void mathed_parse(MathArray & data, unsigned flags);
 
 namespace {
 
-const char LM_TK_OPEN  = '{';
-const char LM_TK_CLOSE = '}';
+const unsigned char LM_TK_OPEN  = '{';
+const unsigned char LM_TK_CLOSE = '}';
 
 enum {
        FLAG_BRACE      = 1 << 0,  //  A { needed              //}
@@ -109,10 +108,9 @@ union {
 
 
 string yytext;
-
 int yylineno;
 istream * yyis;
-bool yy_mtextmode = false;
+MathTextCodes yyvarcode;
 
 
 
@@ -152,14 +150,12 @@ void mathPrintError(string const & msg)
 void LexInitCodes()
 {
        for (int i = 0; i <= 255; ++i) {
-               if (isalpha(i))
-                       lexcode[i] = LexAlpha;
-               else if (isdigit(i))
+               if (isdigit(i))
                        lexcode[i] = LexDigit;
                else if (isspace(i))
                        lexcode[i] = LexSpace;
                else
-                       lexcode[i] = LexNone;
+                       lexcode[i] = LexAlpha;
        }
        
        lexcode['\t'] = lexcode['\f'] = lexcode[' '] = LexSpace;
@@ -186,10 +182,10 @@ void LexInitCodes()
 }
 
 
-char LexGetArg(char lf, bool accept_spaces = false)
+unsigned char LexGetArg(unsigned char lf, bool accept_spaces = false)
 {
        while (yyis->good()) {
-               char c;
+               unsigned char c;
                yyis->get(c);
                if (c > ' ') {
                        if (!lf) 
@@ -201,7 +197,7 @@ char LexGetArg(char lf, bool accept_spaces = false)
                        break;
                }
        }
-       char rg = 0;
+       unsigned char rg = 0;
        if (lf == '{') rg = '}';
        if (lf == '[') rg = ']';
        if (lf == '(') rg = ')';
@@ -212,7 +208,7 @@ char LexGetArg(char lf, bool accept_spaces = false)
        yytext.erase();
        int bcnt = 1;
        do {
-               char c;
+               unsigned char c;
                yyis->get(c);
                if (c == lf) ++bcnt;
                if (c == rg) --bcnt;
@@ -231,11 +227,13 @@ int yylex()
        if (!init_done) LexInitCodes();
        
        while (yyis->good()) {
-               char c;
+               unsigned char c;
                yyis->get(c);
+               lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl;
+               lyxerr << "              code: " << lexcode['ΓΌ'] << endl;
                
-               if (yy_mtextmode && c == ' ') {
-                       yylval.i= ' ';
+               if (yyvarcode == LM_TC_TEXTRM && c == ' ') {
+                       yylval.i = ' ';
                        return LM_TK_ALPHA;
                } else if (lexcode[c] == LexNewLine) {
                        ++yylineno; 
@@ -342,7 +340,7 @@ void setAccent(int ac)
 }
 
 
-MathInset * doAccent(byte c, MathTextCodes t)
+MathInset * doAccent(unsigned char c, MathTextCodes t)
 {
        MathInset * ac = 0;
        
@@ -382,7 +380,7 @@ void do_insert(MathArray & dat, MathInset * m)
                dat.push_back(m);
 }
 
-void do_insert(MathArray & dat, byte ch, MathTextCodes fcode)
+void do_insert(MathArray & dat, unsigned char ch, MathTextCodes fcode)
 {
        if (accent) 
                dat.push_back(doAccent(ch, fcode));
@@ -403,7 +401,7 @@ void handle_frac(MathArray & dat, MathInsetTypes t)
 MathScriptInset * lastScriptInset(MathArray & array)
 {
        MathInset * p = array.back_inset();
-       if (!p || p->GetType() != LM_OT_SCRIPT) {
+       if (!p || !p->isScriptInset()) {
                p = new MathScriptInset;
                array.push_back(p);
        }
@@ -417,12 +415,16 @@ MathScriptInset * lastScriptInset(MathArray & array)
 static bool   curr_num;
 static string curr_label;
 
-void mathed_parse_lines(MathInset * inset, int col)
+void mathed_parse_lines(MathInset * inset, int col, bool numbered, bool outmost)
 {
+       // save global variables
+       bool   saved_num   = curr_num;
+       string saved_label = curr_label;
+
        MathGridInset * p = static_cast<MathGridInset *>(inset);
-       while (1) {
+       for (int row = 0; true; ++row) {
                // reset global variables
-               curr_num   = true;
+               curr_num   = numbered;
                curr_label = string();
 
                // reading a row
@@ -431,6 +433,12 @@ void mathed_parse_lines(MathInset * inset, int col)
                        mathed_parse(p->cell(idx), FLAG_AMPERSAND);
                mathed_parse(p->cell(idx), FLAG_NEWLINE | FLAG_END);
 
+               if (outmost) {
+                       MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
+                       m->numbered(row, curr_num);
+                       m->label(row, curr_label);
+               }
+
                // Hack!
                // no newline
                if (yylval.i != -1)
@@ -438,6 +446,10 @@ void mathed_parse_lines(MathInset * inset, int col)
 
                p->appendRow();
        }
+
+       // restore global variables
+       curr_num   = saved_num;
+       curr_label = saved_label;
 }
 
 
@@ -452,7 +464,7 @@ MathInset * mathed_parse()
                        string name = yytext.substr(1);
                        
                        int na = 0; 
-                       char const c = yyis->peek();
+                       unsigned char const c = yyis->peek();
                        if (c == '[') {
                                LexGetArg('[');
                                na = atoi(yytext.c_str());
@@ -474,17 +486,27 @@ MathInset * mathed_parse()
                        switch (typ) {
 
                                case LM_OT_SIMPLE: {
+                                       curr_num   = latex_mathenv[i].numbered;
+                                       curr_label = string();
                                        mathed_parse(p->cell(0), 0);
+                                       MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
+                                       m->numbered(0, curr_num);
+                                       m->label(0, curr_label);
                                        break;
                                }
 
                                case LM_OT_EQUATION: {
+                                       curr_num   = latex_mathenv[i].numbered;
+                                       curr_label = string();
                                        mathed_parse(p->cell(0), FLAG_END);
+                                       MathMatrixInset * m = static_cast<MathMatrixInset *>(p);
+                                       m->numbered(0, curr_num);
+                                       m->label(0, curr_label);
                                        break;
                                }
 
                                case LM_OT_EQNARRAY: {
-                                       mathed_parse_lines(p, 3);
+                                       mathed_parse_lines(p, 3, latex_mathenv[i].numbered, true);
                                        break;
                                }
 
@@ -492,7 +514,7 @@ MathInset * mathed_parse()
                                        LexGetArg('{');
                                        //int c = atoi(yytext.c_str());
                                        lyxerr << "LM_OT_ALIGNAT: not implemented\n";
-                                       mathed_parse_lines(p, 2);
+                                       mathed_parse_lines(p, 2, latex_mathenv[i].numbered, true);
                                        lyxerr << "LM_OT_ALIGNAT: par: " << *p << "\n";
                                        break;
                                }
@@ -503,10 +525,6 @@ MathInset * mathed_parse()
 
                        p->SetName(latex_mathenv[i].basename);
 
-/*
-                       curr_num = latex_mathenv[i].numbered;
-                       p->numbered(p->nrows()-1, curr_num);
-*/
                        break;
                }
                
@@ -524,7 +542,7 @@ void mathed_parse(MathArray & array, unsigned flags)
        int  tprev = 0;
        bool panic = false;
        static int plevel = -1;
-       MathTextCodes varcode = LM_TC_VAR;
+       yyvarcode = LM_TC_VAR;
        
        int brace = 0;
        int acc_brace = 0;
@@ -548,7 +566,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                switch (t) {
                        
                case LM_TK_ALPHA:
-                       do_insert(array, yylval.i, varcode);
+                       do_insert(array, yylval.i, yyvarcode);
                        break;
 
                case LM_TK_ARGUMENT:
@@ -592,8 +610,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                                break;
                        }
                        if (flags & FLAG_BRACE_FONT) {
-                               varcode = LM_TC_VAR;
-                               yy_mtextmode = false;
+                               yyvarcode = LM_TC_VAR;
                                flags &= ~FLAG_BRACE_FONT;
                                break;
                        }
@@ -607,7 +624,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                case '[':
                        if (flags & FLAG_BRACK_ARG) {
                                flags &= ~FLAG_BRACK_ARG;
-                               char const rg = LexGetArg('[');
+                               unsigned char const rg = LexGetArg('[');
                                if (rg != ']') {
                                        mathPrintError("Expected ']'");
                                        panic = true;
@@ -649,7 +666,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                {
                        MathScriptInset * p = lastScriptInset(array);
                        if (p) 
-                               p->SetLimits(bool(yylval.l->id));
+                               p->limits(yylval.l->id ? 1 : -1);
                        break;
                }
                
@@ -716,7 +733,7 @@ void mathed_parse(MathArray & array, unsigned flags)
 
                case LM_TK_SQRT:
                {           
-                       char c;
+                       unsigned char c;
                        yyis->get(c);
                        if (c == '[') {
                                MathRootInset * rt = new MathRootInset;
@@ -765,7 +782,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                        break;
                
                case LM_TK_FONT:
-                       yy_mtextmode = (yylval.l->id == LM_TC_TEXTRM);
+                       yyvarcode = static_cast<MathTextCodes>(yylval.l->id);
                        flags |= (FLAG_BRACE | FLAG_BRACE_FONT);
                        break;
 
@@ -816,6 +833,8 @@ void mathed_parse(MathArray & array, unsigned flags)
                                        lyxerr << "reading cell " << i << " '" << m->cell(i) << "'\n";
                                }
                                do_insert(array, m);
+                               lyxerr << "macro: " << *m << "\n";
+                               m->Metrics(LM_ST_TEXT);
                        } else
                                do_insert(array, new MathFuncInset(yytext, LM_OT_UNDEF));
                        break;
@@ -831,7 +850,7 @@ void mathed_parse(MathArray & array, unsigned flags)
 
                        if (typ == LM_OT_MATRIX) {
                                string valign = "\0";
-                               char rg = LexGetArg(0);
+                               unsigned char rg = LexGetArg(0);
                                if (rg == ']') {
                                        valign = yytext;
                                        rg = LexGetArg('{');
@@ -843,7 +862,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                                mm->valign(valign[0]);
                                mm->halign(halign);
 
-                               mathed_parse_lines(mm, halign.size());
+                               mathed_parse_lines(mm, halign.size(), latex_mathenv[i].numbered, false);
                                do_insert(array, mm);
                                //lyxerr << "read matrix " << *mm << "\n";      
                                break;
@@ -858,7 +877,7 @@ void mathed_parse(MathArray & array, unsigned flags)
                
                case LM_TK_LABEL:
                {       
-                       char const rg = LexGetArg('\0', true);
+                       unsigned char const rg = LexGetArg('\0', true);
                        if (rg != '}') {
                                mathPrintError("Expected '{'");
                                // debug info