Second tex_parser() successful pass :)
This commit is contained in:
parent
a0e8963fb2
commit
ba84015079
|
@ -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)
|
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;
|
size_t i = 0;
|
||||||
|
|
||||||
if (!len)
|
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_TAG, // any tag
|
||||||
IN_CRLBR, // parameter in curly bracket
|
IN_CRLBR, // parameter in curly bracket
|
||||||
IN_SQRBR, // parameter in square 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_FORMULA, // in $...$
|
||||||
IN_LONGTABLE, // in tag
|
|
||||||
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
|
} where_stack[MAX_TEX_STACK_LEVEL] = {IN_UNDEF};
|
||||||
|
|
||||||
size_t tex_level = 0;
|
size_t tex_level = 0;
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
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];
|
char c = table_source[i];
|
||||||
|
|
||||||
switch (where_stack[tex_level]) {
|
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 == '\\') {
|
if (c == '\\') {
|
||||||
tag = zfree_null(tag);
|
tag = zfree_null(tag);
|
||||||
where_stack[tex_level] = IN_TAG;
|
where_stack[tex_level] = IN_TAG;
|
||||||
|
|
||||||
} else if (c == '%') {
|
} else if (c == '%') {
|
||||||
where_stack[++tex_level] = IN_COMMENT;
|
where_stack[++tex_level] = IN_COMMENT;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
error->code = -1;
|
error->code = -1;
|
||||||
error->message = PARSING_ERROR;
|
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);
|
zalloc_append8_str(tag, c);
|
||||||
|
|
||||||
} else {
|
} 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)) {
|
} 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;
|
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:
|
default:
|
||||||
error->code = -1;
|
error->code = -1;
|
||||||
error->message = PARSING_ERROR;
|
error->message = PARSING_ERROR;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "tex_table.h"
|
#include "tex_table.h"
|
||||||
#include "zalloc.h"
|
#include "zalloc.h"
|
||||||
|
@ -29,6 +30,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
struct xerror_s error;
|
struct xerror_s error;
|
||||||
struct table_s table;
|
struct table_s table;
|
||||||
|
memset(&table, 0, sizeof(struct table_s));
|
||||||
/* Коммент по-русски */
|
/* Коммент по-русски */
|
||||||
parse_table(tex_buf, (size_t)stat_buf.st_size, &table, &error);
|
parse_table(tex_buf, (size_t)stat_buf.st_size, &table, &error);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue