Вот сейчас по Ъ-way

This commit is contained in:
Kolan Sh 2011-06-27 14:43:41 +04:00
parent ba528486cb
commit b175af9da4
4 changed files with 82 additions and 52 deletions

View File

@ -10,21 +10,8 @@
/* unnecessary includes */
#include <stdio.h>
enum tex_parse_err {
TEX_PARSING_NOERROR = 0,
TEX_PARSING_ERROR_UNDEF,
TEX_PARSING_ERROR_STACKOVERFLOW
};
char * const TEX_PARSING_ERROR_UNDEF_STR = "unknown parsing error";
char * const TEX_PARSING_ERROR_STACKOVERFLOW_STR = "parser stack overflow";
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error)
{
char *tag = NULL;/*,
*brpar = NULL;*/
size_t i = 0;
if (!len)
len = strlen(table_source);
@ -33,6 +20,8 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
IN_UNDEF = 0, // undefined place
IN_COMMENT, // any comment
IN_TAG, // any tag
IN_TAG_UNKNOWN, // unknown tag
IN_SPACE, // space, \tag { for example
IN_TAGPARM, // in \tag{} curly braces
IN_TAG_BEGIN,
IN_TAG_CLINE,
@ -50,14 +39,16 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
char *tag = NULL;
size_t i = 0;
size_t tex_level = 0;
for (i = 0; i < len; i++) {
/* stack checking */
if (tex_level + 1 == MAX_TEX_STACK_LEVEL) {
error->code = TEX_PARSING_ERROR_STACKOVERFLOW;
error->message = TEX_PARSING_ERROR_STACKOVERFLOW_STR;
error->code = (int)i;
error->message = "stack overflow";
return error->code;
}
@ -75,8 +66,8 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
where_stack[++tex_level] = IN_COMMENT;
} else {
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
error->code = (int)i;
error->message = "unexpected symbol (IN_UDEF)";
return error->code;
}
@ -92,12 +83,12 @@ 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 == '\\')) { // newline tag
} else if (!tag && c == '\\') { // newline tag
where_stack[tex_level--] = IN_UNDEF;
} else if (!tag) {
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
error->code = (int)i;
error->message = "empty tag (IN_TAG)";
return error->code;
} else if (!strcmp(tag, TEX_TAG_BEGIN)) {
@ -155,32 +146,37 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
printf("\\%s\n", tag);
tag = zfree_null(tag);
} else if (c == '%') {
printf("\\%s\n", tag);
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG_UNKNOWN;
where_stack[++tex_level] = IN_COMMENT;
} else if (c == '{' || c == '[') {
printf("\\%s\n", tag);
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG_UNKNOWN;
where_stack[++tex_level] = IN_TAGPARM;
} else if (isspace(c)) { // \tag { for example
printf("\\%s\n", tag);
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG_UNKNOWN;
where_stack[++tex_level] = IN_SPACE;
} else { // unknown tag
printf("\\%s\n", tag);
tag = zfree_null(tag);
} else if (c == '{' || c == '[') {
where_stack[++tex_level] = IN_TAGPARM;
} else if (isspace(c)) {
} else if (c == '{' || c == '[') {
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else if (!tag) {
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
return error->code;
} else {
where_stack[tex_level] = IN_TAG_UNKNOWN;
}
break;
case IN_SPACE:
if (isspace(c)) {
} else {
where_stack[tex_level--] = IN_UNDEF;
}
break;
case IN_TAGPARM:
@ -194,18 +190,25 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
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 (isprint(c)) {
//~ brpar = zalloc_append8_str(brpar, c); // ???????
} else {
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
error->code = (int)i;
error->message = "unexpected symbol (IN_TAGPARM)";
return error->code;
}
break;
case IN_TAG_BEGIN:
if (c == '{' || c == '[') { // tag params
case IN_TAG_UNKNOWN:
if (isspace(c)) {
} else if (c == '{' || c == '[') { // tag params
where_stack[++tex_level] = IN_TAGPARM;
} else if (c == '\\') { // new tag
@ -216,10 +219,34 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
}
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else {
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_UNKNOWN)";
return error->code;
}
break;
case IN_TAG_BEGIN:
if (isspace(c)) {
} else 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;
if (FALSE) { // longtable
}
} else if (isspace(c)) {
where_stack[++tex_level] = IN_SPACE;
} else {
error->code = (int)i;
error->message = "unexpected symbol (IN_TAG_BEGIN)";
return error->code;
}
break;
@ -273,8 +300,8 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break;
default:
error->code = TEX_PARSING_ERROR_UNDEF;
error->message = TEX_PARSING_ERROR_UNDEF_STR;
error->code = (int)i;
error->message = "unknown error";
return error->code;
break;
}

View File

@ -18,7 +18,7 @@ long_line_column=72
[files]
current_page=1
FILE_NAME_0=1029;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_test.c;0
FILE_NAME_1=2132;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table.c;0
FILE_NAME_1=1575;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table.c;0
FILE_NAME_2=119;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table.h;0
FILE_NAME_3=1762;C;0;16;1;1;1;/home/kolan/projects/include/zalloc_ext.h;0
FILE_NAME_4=634;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_tags.h;0

View File

@ -53,6 +53,8 @@ struct table_s {
struct row_s *rows;
};
/* LaTeX table parser
*/
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error);
#endif

View File

@ -31,10 +31,11 @@ int main(int argc, char *argv[])
struct xerror_s error;
struct table_s table;
memset(&table, 0, sizeof(struct table_s));
memset(&error, 0, sizeof(struct xerror_s));
/* Коммент по-русски */
int result = parse_table(tex_buf, (size_t)stat_buf.st_size, &table, &error);
if (result) {
printf("Parsing error: %s\n", error.message);
printf("Parsing error at %d, symb=%c: %s\n", error.code + 1, tex_buf[error.code], error.message);
}
tex_buf = xfree_null(tex_buf);