code simplified and works correct

This commit is contained in:
Kolan Sh 2011-06-28 16:16:35 +04:00
parent 4364c55c16
commit 46be388497
1 changed files with 88 additions and 268 deletions

View File

@ -8,8 +8,8 @@
#include "c_const.h"
/* only for debug */
//~ #define __TEX_TABLE_DEBUG 0
//~ #include <stdio.h>
#define __TEX_TABLE_DEBUG 0
#include <stdio.h>
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error)
@ -38,7 +38,6 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
IN_TAG_MULTICOLUMN,
IN_TAG_MULTIROW,
IN_TAG_TABULARNEWLINE,
IN_TAG_SLASH,
IN_FORMULA, // in $...$
IN_LONGTABLE, // in longtable
@ -62,6 +61,7 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
#ifdef __TEX_TABLE_DEBUG
printf("tex_level = %lu, c = %c\n", tex_level, c);
#endif
/* looking at where_stack and encountered character */
switch (where_stack[tex_level]) {
case IN_UNDEF:
@ -94,7 +94,7 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
if (isalnum(c)) {
tag = zalloc_append8_str(tag, c);
} else if (!tag && (c == '\\' || c == ',' || c == '%')) { // newline tag or \,
} else if (!tag && (c == '\\' || c == ',' || c == '%')) {
where_stack[tex_level--] = IN_UNDEF;
} else if (!tag) {
@ -103,110 +103,50 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
return error->code;
} else {
if (!strcmp(tag, TEX_TAG_BEGIN)) {
if (!strcmp(tag, TEX_TAG_BEGIN))
where_stack[tex_level] = IN_TAG_BEGIN;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_CLINE)) {
else if (!strcmp(tag, TEX_TAG_CLINE))
where_stack[tex_level] = IN_TAG_CLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_END)) {
else if (!strcmp(tag, TEX_TAG_END))
where_stack[tex_level] = IN_TAG_END;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_ENDFOOT)) {
else if (!strcmp(tag, TEX_TAG_ENDFOOT))
where_stack[tex_level] = IN_TAG_ENDFOOT;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_ENDHEAD)) {
else if (!strcmp(tag, TEX_TAG_ENDHEAD))
where_stack[tex_level] = IN_TAG_ENDHEAD;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_HLINE)) {
else if (!strcmp(tag, TEX_TAG_HLINE))
where_stack[tex_level] = IN_TAG_HLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_HSPACE)) {
else if (!strcmp(tag, TEX_TAG_HSPACE))
where_stack[tex_level] = IN_TAG_HSPACE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_MULTICOLUMN)) {
else if (!strcmp(tag, TEX_TAG_MULTICOLUMN))
where_stack[tex_level] = IN_TAG_MULTICOLUMN;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_MULTIROW)) {
else if (!strcmp(tag, TEX_TAG_MULTIROW))
where_stack[tex_level] = IN_TAG_MULTIROW;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_TABULARNEWLINE)) {
else if (!strcmp(tag, TEX_TAG_TABULARNEWLINE))
where_stack[tex_level] = IN_TAG_TABULARNEWLINE;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else if (!strcmp(tag, TEX_TAG_SLASH)) {
where_stack[tex_level] = IN_TAG_SLASH;
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
} else {
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
else
where_stack[tex_level] = IN_TAG_UNKNOWN;
}
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
if (isspace(c)) { // \tag { for example
#ifdef __TEX_TABLE_DEBUG
printf("\\%s\n", tag);
#endif
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG_UNKNOWN;
where_stack[++tex_level] = IN_SPACE;
} else if (c == '\\' || c == '}'
|| c == ']' || c == '{'
|| c == '[' || c == '%')
i--;
i--;
}
break;
case IN_SPACE:
if (isspace(c)) {
} else {
if (!isspace(c)) {
where_stack[tex_level--] = IN_UNDEF;
i--;
}
@ -239,215 +179,95 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break;
case IN_TAG_UNKNOWN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_BEGIN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#') {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_BEGIN)";
return error->code;
}
break;
case IN_TAG_CLINE:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_END:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_ENDFOOT:
break;
case IN_TAG_ENDHEAD:
break;
case IN_TAG_HLINE:
where_stack[tex_level--] = IN_UNDEF;
break;
case IN_TAG_HSPACE:
break;
case IN_TAG_MULTICOLUMN:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#' || c == '&' || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_MULTICOLUMN)";
return error->code;
}
break;
case IN_TAG_MULTIROW:
if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_MULTIROW)";
return error->code;
}
break;
case IN_TAG_TABULARNEWLINE:
if (c == '{' || c == '[') { // tag params
switch(where_stack[tex_level]) {
case IN_TAG_UNKNOWN:
break;
case IN_TAG_BEGIN:
break;
case IN_TAG_CLINE:
break;
case IN_TAG_END:
break;
case IN_TAG_ENDFOOT:
break;
case IN_TAG_ENDHEAD:
break;
case IN_TAG_HLINE:
break;
case IN_TAG_HSPACE:
break;
case IN_TAG_MULTICOLUMN:
break;
case IN_TAG_MULTIROW:
break;
case IN_TAG_TABULARNEWLINE:
break;
default:
error->code = (int)i;
error->message = "unknown error (IN_TAG_))";
return error->code;
break;
}
if (c == '{' || c == '[')
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else if (c == '}' || c == ']') {
else if (c == '}' || c == ']') {
where_stack[tex_level--] = IN_UNDEF;
i--;
} else if (isalnum(c) || c == '#') {
} else if (c == '\\') {
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (isspace(c))
where_stack[++tex_level] = IN_SPACE;
else if (c == '%')
where_stack[++tex_level] = IN_COMMENT;
else if (isgraph(c) || c < 0) {
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_TABULARNEWLINE)";
error->message = "unexpected symbol (IN_TAG_)";
return error->code;
}
break;
case IN_TAG_SLASH:
break;
case IN_FORMULA:
break;