Second tex_parser() successful pass :)

This commit is contained in:
Kolan Sh 2011-06-20 20:07:17 +04:00
parent a0e8963fb2
commit ba84015079
2 changed files with 52 additions and 3 deletions

View File

@ -12,7 +12,8 @@ char * const PARSING_ERROR = "Error parsing";
int parse_table(const char *table_source, size_t len, struct table_s *table, struct xerror_s *error)
{
char *tag = NULL;
char *tag = NULL,
*brpar = NULL;
size_t i = 0;
if (!len)
@ -25,13 +26,25 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
IN_TAG, // any tag
IN_CRLBR, // parameter in curly bracket
IN_SQRBR, // parameter in square bracket
IN_TAG_BEGIN, // in \begin tag
IN_TAGPARM, // in \tag{} curly braces
IN_LONGTABLE, // in longtable
IN_FORMULA, // in $...$
IN_LONGTABLE, // in tag
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
size_t tex_level = 0;
for (i = 0; i < len; i++) {
/* stack checking */
if (tex_level + 1 == MAX_TEX_STACK_LEVEL) {
error->code = -1;
error->message = PARSING_ERROR;
return -1;
}
/* read one charecter from input stream */
char c = table_source[i];
switch (where_stack[tex_level]) {
@ -39,8 +52,10 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
if (c == '\\') {
tag = zfree_null(tag);
where_stack[tex_level] = IN_TAG;
} else if (c == '%') {
where_stack[++tex_level] = IN_COMMENT;
} else {
error->code = -1;
error->message = PARSING_ERROR;
@ -60,7 +75,10 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
zalloc_append8_str(tag, c);
} else {
if (!strcmp(tag, TEX_TAG_BEGIN)) {
if (!tag) {
} else if (!strcmp(tag, TEX_TAG_BEGIN)) {
where_stack[tex_level++] = IN_TAG_BEGIN;
} else if (!strcmp(tag, TEX_TAG_CLINE)) {
@ -86,6 +104,35 @@ int parse_table(const char *table_source, size_t len, struct table_s *table, str
}
break;
case IN_TAG_BEGIN:
if (c == '{') {
where_stack[++tex_level] = IN_TAGPARM;
} else if (isspace(c)) {
} else {
error->code = -1;
error->message = PARSING_ERROR;
return -1;
}
break;
case IN_TAGPARM:
if (c == '}') {
where_stack[tex_level--] = IN_UNDEF;
} else if (isgraph(c)) {
zalloc_append8_str(brpar, c);
} else if (isspace(c)) {
} else {
error->code = -1;
error->message = PARSING_ERROR;
return -1;
}
break;
default:
error->code = -1;
error->message = PARSING_ERROR;

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "tex_table.h"
#include "zalloc.h"
@ -29,6 +30,7 @@ int main(int argc, char *argv[])
struct xerror_s error;
struct table_s table;
memset(&table, 0, sizeof(struct table_s));
/* Коммент по-русски */
parse_table(tex_buf, (size_t)stat_buf.st_size, &table, &error);