source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Flip eqn into using parsed nodes.
@ 2011-07-21 11:34 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2011-07-21 11:34 UTC (permalink / raw)
  To: source

Log Message:
-----------
Flip eqn into using parsed nodes.  I've temporarily disabled printing
these in the front-ends except for -Ttree, which will display the parsed
tree.

While here, fix that quoted strings aren't scanned for replacement parts.

Modified Files:
--------------
    mdocml:
        eqn.7
        eqn.c
        man_html.c
        man_term.c
        mandoc.h
        mdoc_html.c
        mdoc_term.c
        tree.c

Revision Data
-------------
Index: eqn.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/eqn.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -Leqn.c -Leqn.c -u -p -r1.12 -r1.13
--- eqn.c
+++ eqn.c
@@ -44,6 +44,7 @@ enum	eqnpartt {
 	EQN__MAX
 };
 
+static	void		 eqn_box_free(struct eqn_box *);
 static	struct eqn_def	*eqn_def_find(struct eqn_node *, 
 				const char *, size_t);
 static	int		 eqn_do_define(struct eqn_node *);
@@ -51,7 +52,7 @@ static	int		 eqn_do_ign2(struct eqn_node
 static	int		 eqn_do_undef(struct eqn_node *);
 static	const char	*eqn_nexttok(struct eqn_node *, size_t *);
 static	const char	*eqn_next(struct eqn_node *, char, size_t *);
-static	int		 eqn_box(struct eqn_node *);
+static	int		 eqn_box(struct eqn_node *, struct eqn_box *);
 
 static	const struct eqnpart eqnparts[EQN__MAX] = {
 	{ "define", 6, eqn_do_define }, /* EQN_DEFINE */
@@ -116,29 +117,33 @@ eqn_alloc(int pos, int line, struct mpar
 enum rofferr
 eqn_end(struct eqn_node *ep)
 {
-	int		 c;
+	struct eqn_box	*root;
 
-	/*
-	 * Validate the expression.
-	 * Use the grammar found in the literature.
-	 */
+	ep->eqn.root = root = 
+		mandoc_calloc(1, sizeof(struct eqn_box));
+	root->type = EQN_ROOT;
 
 	if (0 == ep->sz)
 		return(ROFF_IGN);
 
-	while (1 == (c = eqn_box(ep)))
-		/* Keep parsing. */ ;
+	/*
+	 * Validate the expression.
+	 * Use the grammar found in the literature.
+	 */
 
-	return(c < 0 ? ROFF_IGN : ROFF_EQN);
+	return(eqn_box(ep, root) < 0 ? ROFF_IGN : ROFF_EQN);
 }
 
 static int
-eqn_box(struct eqn_node *ep)
+eqn_box(struct eqn_node *ep, struct eqn_box *last)
 {
 	size_t		 sz;
 	const char	*start;
-	int		 i;
+	int		 i, nextc;
+	struct eqn_box	*bp;
 
+	nextc = 1;
+again:
 	if (NULL == (start = eqn_nexttok(ep, &sz)))
 		return(0);
 
@@ -150,18 +155,24 @@ eqn_box(struct eqn_node *ep)
 		if ( ! (*eqnparts[i].fp)(ep))
 			return(-1);
 
-		return(1);
+		goto again;
 	} 
 
-	ep->eqn.data = mandoc_realloc
-		(ep->eqn.data, ep->eqn.sz + sz + 1);
+	bp = mandoc_calloc(1, sizeof(struct eqn_box));
+	bp->type = EQN_TEXT;
 
-	if (0 == ep->eqn.sz)
-		*ep->eqn.data = '\0';
+	if (nextc)
+		last->child = bp;
+	else
+		last->next = bp;
 
-	ep->eqn.sz += sz;
-	strlcat(ep->eqn.data, start, ep->eqn.sz + 1);
-	return(1);
+	bp->text = mandoc_malloc(sz + 1);
+	*bp->text = '\0';
+	strlcat(bp->text, start, sz + 1);
+
+	last = bp;
+	nextc = 0;
+	goto again;
 }
 
 void
@@ -169,7 +180,7 @@ eqn_free(struct eqn_node *p)
 {
 	int		 i;
 
-	free(p->eqn.data);
+	eqn_box_free(p->eqn.root);
 
 	for (i = 0; i < (int)p->defsz; i++) {
 		free(p->defs[i].key);
@@ -181,6 +192,19 @@ eqn_free(struct eqn_node *p)
 	free(p);
 }
 
+static void
+eqn_box_free(struct eqn_box *bp)
+{
+
+	if (bp->child)
+		eqn_box_free(bp->child);
+	if (bp->next)
+		eqn_box_free(bp->next);
+
+	free(bp->text);
+	free(bp);
+}
+
 static const char *
 eqn_nexttok(struct eqn_node *ep, size_t *sz)
 {
@@ -199,6 +223,17 @@ eqn_next(struct eqn_node *ep, char quote
 	if (NULL == sz)
 		sz = &ssz;
 
+	lim = 0;
+	sv = ep->cur;
+again:
+	/* Prevent self-definitions. */
+
+	if (lim >= EQN_NEST_MAX) {
+		EQN_MSG(MANDOCERR_EQNNEST, ep);
+		return(NULL);
+	}
+
+	ep->cur = sv;
 	start = &ep->data[(int)ep->cur];
 	q = 0;
 
@@ -210,16 +245,6 @@ eqn_next(struct eqn_node *ep, char quote
 		q = 1;
 	}
 
-	lim = 0;
-
-	sv = ep->cur;
-again:
-	if (lim >= EQN_NEST_MAX) {
-		EQN_MSG(MANDOCERR_EQNNEST, ep);
-		return(NULL);
-	}
-
-	ep->cur = sv;
 	start = &ep->data[(int)ep->cur];
 	next = q ? strchr(start, quote) : strchr(start, ' ');
 
@@ -238,6 +263,11 @@ again:
 		ep->cur += *sz;
 	}
 
+	/* Quotes aren't expanded for values. */
+
+	if (q)
+		return(start);
+
 	if (NULL != (def = eqn_def_find(ep, start, *sz))) {
 		diff = def->valsz - *sz;
 
@@ -322,9 +352,12 @@ eqn_do_define(struct eqn_node *ep)
 	}
 
 	def->valsz = sz;
-	def->val = mandoc_realloc(ep->defs[i].val, sz + 1);
+	def->val = mandoc_realloc(def->val, sz + 1);
 	memcpy(def->val, start, sz);
 	def->val[(int)sz] = '\0';
+
+	/*fprintf(stderr, "Defining: [%s], [%s]\n", 
+			def->key, def->val);*/
 	return(1);
 }
 
Index: man_term.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man_term.c,v
retrieving revision 1.113
retrieving revision 1.114
diff -Lman_term.c -Lman_term.c -u -p -r1.113 -r1.114
--- man_term.c
+++ man_term.c
@@ -905,7 +905,7 @@ print_man_node(DECL_ARGS)
 			p->flags |= TERMP_SENTENCE;
 		return;
 	case (MAN_EQN):
-		term_word(p, n->eqn->data);
+		/*term_word(p, n->eqn->data);*/
 		return;
 	case (MAN_TBL):
 		/*
Index: man_html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man_html.c,v
retrieving revision 1.78
retrieving revision 1.79
diff -Lman_html.c -Lman_html.c -u -p -r1.78 -r1.79
--- man_html.c
+++ man_html.c
@@ -230,8 +230,8 @@ print_man_node(MAN_ARGS)
 		return;
 	case (MAN_EQN):
 		PAIR_CLASS_INIT(&tag, "eqn");
-		print_otag(h, TAG_SPAN, 1, &tag);
-		print_text(h, n->eqn->data);
+		/*print_otag(h, TAG_SPAN, 1, &tag);
+		print_text(h, n->eqn->data);*/
 		break;
 	case (MAN_TBL):
 		/*
Index: mdoc_html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_html.c,v
retrieving revision 1.172
retrieving revision 1.173
diff -Lmdoc_html.c -Lmdoc_html.c -u -p -r1.172 -r1.173
--- mdoc_html.c
+++ mdoc_html.c
@@ -435,8 +435,8 @@ print_mdoc_node(MDOC_ARGS)
 		return;
 	case (MDOC_EQN):
 		PAIR_CLASS_INIT(&tag, "eqn");
-		print_otag(h, TAG_SPAN, 1, &tag);
-		print_text(h, n->eqn->data);
+		/*print_otag(h, TAG_SPAN, 1, &tag);
+		print_text(h, n->eqn->data);*/
 		break;
 	case (MDOC_TBL):
 		/*
Index: mdoc_term.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_term.c,v
retrieving revision 1.231
retrieving revision 1.232
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.231 -r1.232
--- mdoc_term.c
+++ mdoc_term.c
@@ -351,7 +351,7 @@ print_mdoc_node(DECL_ARGS)
 			p->flags |= TERMP_NOSPACE;
 		break;
 	case (MDOC_EQN):
-		term_word(p, n->eqn->data);
+		/*term_word(p, n->eqn->data);*/
 		break;
 	case (MDOC_TBL):
 		term_tbl(p, n->span);
Index: mandoc.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandoc.h,v
retrieving revision 1.82
retrieving revision 1.83
diff -Lmandoc.h -Lmandoc.h -u -p -r1.82 -r1.83
--- mandoc.h
+++ mandoc.h
@@ -277,9 +277,20 @@ struct	tbl_span {
 	struct tbl_span	 *next;
 };
 
+enum	eqn_boxt {
+	EQN_ROOT,
+	EQN_TEXT
+};
+
+struct	eqn_box {
+	enum eqn_boxt	  type;
+	struct eqn_box	 *child;
+	struct eqn_box	 *next;
+	char		 *text;
+};
+
 struct	eqn {
-	size_t		  sz;
-	char		 *data;
+	struct eqn_box	 *root;
 	int		  ln; /* invocation line */
 	int		  pos; /* invocation position */
 };
Index: eqn.7
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/eqn.7,v
retrieving revision 1.5
retrieving revision 1.6
diff -Leqn.7 -Leqn.7 -u -p -r1.5 -r1.6
--- eqn.7
+++ eqn.7
@@ -73,6 +73,7 @@ Data in TEXT form is a non-empty sequenc
 non-empty quoted string.
 Unless within a quoted string, white-space (and enclosing literal quote
 pairs) is thrown away.
+Quoted strings are not scanned for replacement definitions.
 .Pp
 The following control statements are available:
 .Bl -tag -width Ds
Index: tree.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/tree.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -Ltree.c -Ltree.c -u -p -r1.37 -r1.38
--- tree.c
+++ tree.c
@@ -28,8 +28,9 @@
 #include "man.h"
 #include "main.h"
 
-static	void	print_mdoc(const struct mdoc_node *, int);
+static	void	print_box(const struct eqn_box *, int);
 static	void	print_man(const struct man_node *, int);
+static	void	print_mdoc(const struct mdoc_node *, int);
 static	void	print_span(const struct tbl_span *, int);
 
 
@@ -63,6 +64,7 @@ print_mdoc(const struct mdoc_node *n, in
 	argv = NULL;
 	argc = sz = 0;
 	params = NULL;
+	t = p = NULL;
 
 	switch (n->type) {
 	case (MDOC_ROOT):
@@ -90,18 +92,14 @@ print_mdoc(const struct mdoc_node *n, in
 		t = "text";
 		break;
 	case (MDOC_TBL):
-		t = "tbl";
-		break;
+		/* FALLTHROUGH */
 	case (MDOC_EQN):
-		t = "eqn";
 		break;
 	default:
 		abort();
 		/* NOTREACHED */
 	}
 
-	p = NULL;
-
 	switch (n->type) {
 	case (MDOC_TEXT):
 		p = n->string;
@@ -130,9 +128,8 @@ print_mdoc(const struct mdoc_node *n, in
 		}
 		break;
 	case (MDOC_TBL):
-		break;
+		/* FALLTHROUGH */
 	case (MDOC_EQN):
-		p = n->eqn->data;
 		break;
 	case (MDOC_ROOT):
 		p = "root";
@@ -143,8 +140,11 @@ print_mdoc(const struct mdoc_node *n, in
 	}
 
 	if (n->span) {
-		assert(NULL == p);
+		assert(NULL == p && NULL == t);
 		print_span(n->span, indent);
+	} else if (n->eqn) {
+		assert(NULL == p && NULL == t);
+		print_box(n->eqn->root, indent);
 	} else {
 		for (i = 0; i < indent; i++)
 			putchar('\t');
@@ -164,11 +164,9 @@ print_mdoc(const struct mdoc_node *n, in
 		for (i = 0; i < (int)sz; i++)
 			printf(" [%s]", params[i]);
 
-		printf(" %d:%d", n->line, n->pos);
+		printf(" %d:%d\n", n->line, n->pos);
 	}
 
-	putchar('\n');
-
 	if (n->child)
 		print_mdoc(n->child, indent + 1);
 	if (n->next)
@@ -182,6 +180,8 @@ print_man(const struct man_node *n, int 
 	const char	 *p, *t;
 	int		  i;
 
+	t = p = NULL;
+
 	switch (n->type) {
 	case (MAN_ROOT):
 		t = "root";
@@ -205,18 +205,14 @@ print_man(const struct man_node *n, int 
 		t = "block-tail";
 		break;
 	case (MAN_TBL):
-		t = "tbl";
-		break;
+		/* FALLTHROUGH */
 	case (MAN_EQN):
-		t = "eqn";
 		break;
 	default:
 		abort();
 		/* NOTREACHED */
 	}
 
-	p = NULL;
-
 	switch (n->type) {
 	case (MAN_TEXT):
 		p = n->string;
@@ -236,9 +232,8 @@ print_man(const struct man_node *n, int 
 		p = "root";
 		break;
 	case (MAN_TBL):
-		break;
+		/* FALLTHROUGH */
 	case (MAN_EQN):
-		p = n->eqn->data;
 		break;
 	default:
 		abort();
@@ -246,16 +241,17 @@ print_man(const struct man_node *n, int 
 	}
 
 	if (n->span) {
-		assert(NULL == p);
+		assert(NULL == p && NULL == t);
 		print_span(n->span, indent);
+	} else if (n->eqn) {
+		assert(NULL == p && NULL == t);
+		print_box(n->eqn->root, indent);
 	} else {
 		for (i = 0; i < indent; i++)
 			putchar('\t');
-		printf("%s (%s) %d:%d", p, t, n->line, n->pos);
+		printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
 	}
 
-	putchar('\n');
-
 	if (n->child)
 		print_man(n->child, indent + 1);
 	if (n->next)
@@ -263,6 +259,30 @@ print_man(const struct man_node *n, int 
 }
 
 static void
+print_box(const struct eqn_box *ep, int indent)
+{
+	int		 i;
+
+	if (NULL == ep)
+		return;
+	for (i = 0; i < indent; i++)
+		putchar('\t');
+
+	switch (ep->type) {
+	case (EQN_ROOT):
+		puts("eqn-root");
+		print_box(ep->child, indent + 1);
+		return;
+	case (EQN_TEXT):
+		printf("eqn-text: [%s]\n", ep->text);
+		print_box(ep->next, indent);
+		return;
+	default:
+		break;
+	}
+}
+
+static void
 print_span(const struct tbl_span *sp, int indent)
 {
 	const struct tbl_dat *dp;
@@ -306,5 +326,5 @@ print_span(const struct tbl_span *sp, in
 		putchar(' ');
 	}
 
-	printf("(tbl) %d:1", sp->line);
+	printf("(tbl) %d:1\n", sp->line);
 }
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-07-21 11:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-21 11:34 mdocml: Flip eqn into using parsed nodes kristaps

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).