Этот парсер будет болванкой для парсеров таблиц, полей, рисунков и проч.

This commit is contained in:
Kolan Sh 2011-06-29 11:09:41 +04:00
parent 4dfff10806
commit b533ed5ba8
4 changed files with 11 additions and 66 deletions

View File

@ -8,17 +8,17 @@
#include "c_const.h" #include "c_const.h"
/* only for debug */ /* only for debug */
#define __TEX_TABLE_DEBUG 0 #define __TEX_PARSER_DEBUG 0
#include <stdio.h> #include <stdio.h>
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error) int tex_parse(const char *source, size_t len, struct xerror_s *error)
{ {
error->code = 0; error->code = 0;
error->message = NULL; error->message = NULL;
if (!len) if (!len)
len = strlen(table_source); len = strlen(source);
enum where_e enum where_e
{ {
@ -39,7 +39,6 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
IN_TAG_MULTIROW, IN_TAG_MULTIROW,
IN_TAG_TABULARNEWLINE, IN_TAG_TABULARNEWLINE,
IN_FORMULA, // in $...$ IN_FORMULA, // in $...$
IN_LONGTABLE, // in longtable
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF}; } where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
@ -57,8 +56,8 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
} }
/* read one character from input stream */ /* read one character from input stream */
char c = table_source[i]; char c = source[i];
#ifdef __TEX_TABLE_DEBUG #ifdef __TEX_PARSER_DEBUG
printf("tex_level = %lu, c = %c\n", tex_level, c); printf("tex_level = %lu, c = %c\n", tex_level, c);
#endif #endif
@ -138,7 +137,7 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
else else
where_stack[tex_level] = IN_TAG_UNKNOWN; where_stack[tex_level] = IN_TAG_UNKNOWN;
#ifdef __TEX_TABLE_DEBUG #ifdef __TEX_PARSER_DEBUG
printf("\\%s\n", tag); printf("\\%s\n", tag);
#endif #endif
tag = zfree_null(tag); tag = zfree_null(tag);
@ -274,10 +273,6 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
break; break;
case IN_LONGTABLE:
break;
default: default:
error->code = (int)i; error->code = (int)i;
error->message = "unknown error"; error->message = "unknown error";
@ -286,7 +281,5 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
} }
} }
table->id = "Drossel1.Table1";
return 0; return 0;
} }

View File

@ -7,54 +7,8 @@
#define MAX_TEX_STACK_LEVEL 128 #define MAX_TEX_STACK_LEVEL 128
/* Ячейка. /* LaTeX parser
* Если встречается ячейка с \multicolumn{n}, то
* следующие n-1 ячеек в таблице пустые и при компиляции
* к ним не добавляется символ &
*/ */
struct cell_s { int tex_parse(const char *source, size_t len, struct xerror_s *error);
char *before;
char *request;
char *after;
};
/* Строка.
* Заканчивается тегом "\tabularnewline"
*/
struct row_s {
char *before;
struct cell_s *cells;
char *after;
};
/* \hline\multicolumn{8}{|c|}{Сводный протокол результатов испытания двигателя ТВ7-117В \textnumero{}}\tabularnewline\multicolumn{8}{|c|}{Стенд \textnumero{}18 ~~ ИУД СИК ~ Дата: ~~~~~~~~ г. Время:~~~~~~~~~ Рн = ~~~ мм рт ст~~ tвх = ~~~ $\,^{\circ}\mbox{C}$}\tabularnewline\multicolumn{8}{|c|}{Характеристика ~~~~~~~ Акт сдачи, контрольные точки}\tabularnewline\hline\endhead
*/
struct header_s {
char *before;
struct row_s *rows;
char *after;
};
/* \hline\multicolumn{2}{|>{\centering}m{0.11\linewidth}|}{\centering{}Составил} & \multicolumn{2}{c|}{Инженер по испытаниям} & Начальник БТК & Начальник участка УИД СИК & \multicolumn{2}{c|}{Представитель заказчика}\tabularnewline\hline\multicolumn{2}{|>{\centering}p{0.11\linewidth}|}{\centering{}\textit{Подпись}} & \multicolumn{2}{c|}{\textit{Подпись}} & \centering{}\textit{Подпись} & \centering{}\textit{Подпись} & \multicolumn{2}{c|}{\textit{Подпись}}\tabularnewline\multicolumn{2}{|>{\centering}p{0.11\linewidth}|}{\textit{фамилия}} & \multicolumn{2}{c|}{\textit{фамилия}} & \multicolumn{1}{c|}{\textit{фамилия}} & \textit{фамилия} & \multicolumn{2}{c|}{\textit{фамилия}}\tabularnewline\hline\endfoot
*/
struct footer_s {
char *before;
struct row_s *rows;
char *after;
};
struct table_s {
size_t document_offset; // offset in the LaTeX document
size_t document_size; // size in the LaTeX document
char *id; // "Drossel1.Table1"
char **column_scheme; // "|>{\centering}m{0.1\paperwidth}"
struct header_s header;
struct footer_s footer;
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 #endif

View File

@ -13,4 +13,4 @@ const char * const TEX_TAG_MULTIROW = "multirow";
const char * const TEX_TAG_TABULARNEWLINE = "tabularnewline"; const char * const TEX_TAG_TABULARNEWLINE = "tabularnewline";
const char * const TEX_TAG_SLASH = "\\"; const char * const TEX_TAG_SLASH = "\\";
#endif // TEX_TABLE_TAGS_H #endif

View File

@ -13,7 +13,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) if (argc != 2)
xerrx(-1, "Usage: ./tex_table_test /path/to/table.tex", NULL); xerrx(-1, "Usage: ./tex_parser_test /path/to/example.tex", NULL);
FILE *tex_file = fopen(argv[1], "rb"); FILE *tex_file = fopen(argv[1], "rb");
if (!tex_file) if (!tex_file)
@ -30,14 +30,12 @@ int main(int argc, char *argv[])
tex_buf[stat_buf.st_size] = 0; tex_buf[stat_buf.st_size] = 0;
struct xerror_s error; struct xerror_s error;
struct table_s table;
memset(&table, 0, sizeof(struct table_s));
memset(&error, 0, sizeof(struct xerror_s)); memset(&error, 0, sizeof(struct xerror_s));
//~ setlocale(LC_ALL, "ru_RU.KOI8-R"); //~ setlocale(LC_ALL, "ru_RU.KOI8-R");
/* Коммент по-русски */ /* Коммент по-русски */
int result = parse_table(tex_buf, (size_t)stat_buf.st_size, &table, &error); int result = tex_parse(tex_buf, (size_t)stat_buf.st_size, &error);
if (result) { if (result) {
printf("Parsing error at %d, symb=%c: %s\n", error.code + 1, tex_buf[error.code], error.message); printf("Parsing error at %d, symb=%c: %s\n", error.code + 1, tex_buf[error.code], error.message);
} }