Import devel/greg

ok benoit@, tweaks and ok sthen@

greg is a recursive-descent parser generator based on Ian Piumarta's
peg/leg.  It implements a formalism called Parsing Expression Grammars.
This commit is contained in:
bcallah 2015-01-12 05:51:22 +00:00
parent cd6715e32a
commit f303cc72e2
10 changed files with 302 additions and 0 deletions

26
devel/greg/Makefile Normal file
View File

@ -0,0 +1,26 @@
# $OpenBSD: Makefile,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
V = 0.4.5
COMMENT = PEG-based parser generator
DISTNAME = ${GH_PROJECT}-${V}
CATEGORIES = devel
GH_ACCOUNT = fasterthanlime
GH_PROJECT = greg
GH_TAGNAME = v${V}
GH_COMMIT = e6b8bc461c0d948e97187d5ff6ea579d9addb7dc
HOMEPAGE = https://github.com/fasterthanlime/greg
MAINTAINER = Sean Levy <attila@stalphonsos.com>
# MIT
PERMIT_PACKAGE_CDROM = Yes
WANTLIB += c
MAKE_FLAGS = OFLAGS="${CFLAGS} -DNDEBUG -DYY_MAIN"
do-install:
${INSTALL_PROGRAM} ${WRKSRC}/greg ${PREFIX}/bin/
.include <bsd.port.mk>

2
devel/greg/distinfo Normal file
View File

@ -0,0 +1,2 @@
SHA256 (greg-0.4.5.tar.gz) = DbMVwFmmymjvIcgSO1/QzhTORKx+e4URmSFXFCzUcGM=
SIZE (greg-0.4.5.tar.gz) = 26770

View File

@ -0,0 +1,48 @@
$OpenBSD: patch-Makefile,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
We don't care about rebuilding when source files change since they
aren't going to change in this case (ports). Clean up a bit wrt
samples/test so at least all of the targets work.
--- Makefile.orig Tue Oct 8 22:39:24 2013
+++ Makefile Fri Jan 2 14:31:43 2015
@@ -24,33 +24,25 @@ uninstall : .FORCE
rm -f $(BINDIR)/greg
# bootstrap greg from greg.g
-greg.c : greg.g compile.c tree.c
- $(MAKE) greg-new
- ./greg-new -o greg-new.c greg.g
- $(CC) $(CFLAGS) -o greg-new greg-new.c $(SRCS)
- cp greg-new.c greg.c
- cp greg-new greg
+greg-new.c : greg greg.g
+ ./greg -o greg-new.c greg.g
# bootstrap: call make greg-new when you updated compile.c and greg-new.c
-greg-new : greg-new.c $(SRCS)
+greg-new : greg-new.c
$(CC) $(CFLAGS) -o greg-new greg-new.c $(SRCS)
grammar : .FORCE
./greg -o greg.c greg.g
clean : .FORCE
- rm -rf *~ *.o *.greg.[cd] greg ${SAMPLES:.leg=.o} ${SAMPLES:.leg=} ${SAMPLES:.leg=.c} samples/*.dSYM testing1.c testing2.c *.dSYM selftest/
+ rm -rf *~ *.o *.greg.[cd] greg greg-new greg-new.c ${SAMPLES:.leg=.o} ${SAMPLES:.leg=} ${SAMPLES:.leg=.c} samples/*.dSYM testing1.c testing2.c *.dSYM selftest/
spotless : clean .FORCE
rm -f greg
+ (cd samples; $(MAKE) $(MFLAGS) clean)
-samples/calc.c: samples/calc.leg greg
- ./greg -o $@ $<
-
-samples/calc: samples/calc.c
- $(CC) $(CFLAGS) -o $@ $<
-
samples: ${SAMPLES:.leg=} greg
+ (cd samples; $(MAKE) $(MFLAGS))
%.c: %.leg
./greg $< > $@

View File

@ -0,0 +1,69 @@
$OpenBSD: patch-compile_c,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
Widen the YY_INPUT() interface to four args, since that seems to be
how it is actually used in the only real application that uses greg
I've tried so far (MMD-4). Also convert two instances of sprintf(3)
to snprintf(3).
--- compile.c.orig Tue Oct 8 22:39:24 2013
+++ compile.c Fri Jan 2 14:31:43 2015
@@ -136,8 +136,12 @@ static char *yyqq(char* s) {
} else if (*s == 92) { // '\'
*d++ = '\\'; *d++ = *s++;
} else if (*(signed char *)s<32) {
- sprintf(d,"\\%03o", *s); // octal \000
- d += 4; s++;
+ char octbuf[8];
+ size_t noct = snprintf(octbuf,sizeof(octbuf),"\\%03o",*s);
+ assert(noct < sizeof(octbuf));
+ memcpy(d,octbuf,noct+1);
+ d += noct;
+ s++;
} else {
*d++ = *s++;
}
@@ -152,7 +156,7 @@ static char *makeCharClass(unsigned char *cclass)
setter set;
int c, prev= -1;
static char string[256];
- char *ptr;
+ size_t left, off;
if ('^' == *cclass)
{
@@ -179,9 +183,14 @@ static char *makeCharClass(unsigned char *cclass)
}
}
- ptr= string;
- for (c= 0; c < 32; ++c)
- ptr += sprintf(ptr, "\\%03o", bits[c]);
+ left = sizeof(string);
+ off = 0;
+ for (c= 0; c < 32; ++c) {
+ size_t n = snprintf(&string[off],left,"\\%03o",bits[c]);
+ assert(n < left);
+ left -= n;
+ off += n;
+ }
return string;
}
@@ -502,7 +511,7 @@ static char *preamble= "\
#define YY_NAME(N) yy##N\n\
#endif\n\
#ifndef YY_INPUT\n\
-#define YY_INPUT(buf, result, max_size) \\\n\
+#define YY_INPUT(buf, result, max_size, D) \\\n\
{ \\\n\
int yyc= fgetc(G->input); \\\n\
if ('\\n' == yyc) ++G->lineno; \\\n\
@@ -624,7 +633,7 @@ YY_LOCAL(int) yyrefill(GREG *G)\n\
G->buflen *= 2;\n\
G->buf= (char*)YY_REALLOC(G->buf, G->buflen, G->data);\n\
}\n\
- YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));\n\
+ YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos), G->data);\n\
if (!yyn) return 0;\n\
G->limit += yyn;\n\
return 1;\n\

View File

@ -0,0 +1,24 @@
$OpenBSD: patch-greg_c,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
Carry along the compiles.c diff.
--- greg.c.orig Tue Oct 8 22:39:24 2013
+++ greg.c Fri Jan 2 14:31:43 2015
@@ -67,7 +67,7 @@ int main()\n\
#define YY_NAME(N) yy##N
#endif
#ifndef YY_INPUT
-#define YY_INPUT(buf, result, max_size) \
+#define YY_INPUT(buf, result, max_size, D) \
{ \
int yyc= fgetc(G->input); \
if ('\n' == yyc) ++G->lineno; \
@@ -189,7 +189,7 @@ YY_LOCAL(int) yyrefill(GREG *G)
G->buflen *= 2;
G->buf= (char*)YY_REALLOC(G->buf, G->buflen, G->data);
}
- YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));
+ YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos), G->data);
if (!yyn) return 0;
G->limit += yyn;
return 1;

View File

@ -0,0 +1,75 @@
$OpenBSD: patch-samples_Makefile,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
Clean up so "make test" works at top level, all expected binaries
are built with -DYY_MAIN. Fix the clean target.
--- samples/Makefile.orig Tue Oct 8 22:39:24 2013
+++ samples/Makefile Fri Jan 2 14:31:43 2015
@@ -1,6 +1,6 @@
-EXAMPLES = test rule accept wc dc dcv calc basic
+EXAMPLES = test rule accept wc dc dcv calc basic erract
-CFLAGS = -g -O3
+CFLAGS += -g -DYY_MAIN
DIFF = diff
TEE = cat >
@@ -9,7 +9,7 @@ all : $(EXAMPLES)
test : .FORCE
../greg -o test.leg.c test.leg
- $(CC) $(CFLAGS) -o test test.c
+ $(CC) $(CFLAGS) -o test test.leg.c
echo 'ab.ac.ad.ae.afg.afh.afg.afh.afi.afj.' | ./$@ | $(TEE) $@.out
$(DIFF) $@.ref $@.out
rm -f $@.out
@@ -17,7 +17,7 @@ test : .FORCE
rule : .FORCE
../greg -o rule.leg.c rule.leg
- $(CC) $(CFLAGS) -o rule rule.c
+ $(CC) $(CFLAGS) -o rule rule.leg.c
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
$(DIFF) $@.ref $@.out
rm -f $@.out
@@ -25,7 +25,7 @@ rule : .FORCE
accept : .FORCE
../greg -o accept.leg.c accept.leg
- $(CC) $(CFLAGS) -o accept accept.c
+ $(CC) $(CFLAGS) -o accept accept.leg.c
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
$(DIFF) $@.ref $@.out
rm -f $@.out
@@ -41,7 +41,7 @@ wc : .FORCE
dc : .FORCE
../greg -o dc.leg.c dc.leg
- $(CC) $(CFLAGS) -o dc dc.c
+ $(CC) $(CFLAGS) -o dc dc.leg.c
echo ' 2 *3 *(3+ 4) ' | ./dc | $(TEE) $@.out
$(DIFF) $@.ref $@.out
rm -f $@.out
@@ -49,7 +49,7 @@ dc : .FORCE
dcv : .FORCE
../greg -o dcv.leg.c dcv.leg
- $(CC) $(CFLAGS) -o dcv dcv.c
+ $(CC) $(CFLAGS) -o dcv dcv.leg.c
echo 'a = 6; b = 7; a * b' | ./dcv | $(TEE) $@.out
$(DIFF) $@.ref $@.out
rm -f $@.out
@@ -71,8 +71,12 @@ basic : .FORCE
rm -f $@.out
@echo
+erract : .FORCE
+ ../greg -o erract.leg.c erract.leg
+ $(CC) $(CFLAGS) -o erract erract.leg.c
+
clean : .FORCE
- rm -f *~ *.o *.[pl]eg.[cd] $(EXAMPLES)
+ rm -f *~ *.o *.[pl]eg.[cd] *.out $(EXAMPLES)
rm -rf *.dSYM
spotless : clean

View File

@ -0,0 +1,34 @@
$OpenBSD: patch-samples_basic_leg,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
Use a four-arg YY_INPUT() as per patch-compile_c. Turn sprintf(3) into
snprintf(3).
--- samples/basic.leg.orig Tue Oct 8 22:39:24 2013
+++ samples/basic.leg Fri Jan 2 14:31:43 2015
@@ -20,6 +20,7 @@
%{
# include <stdio.h>
+# include <assert.h>
typedef struct line line;
@@ -39,7 +40,7 @@
# define min(x, y) ((x) < (y) ? (x) : (y))
-# define YY_INPUT(buf, result, max_size) \
+# define YY_INPUT(buf, result, max_size, D) \
{ \
if ((pc >= 0) && (pc < numLines)) \
{ \
@@ -286,7 +287,8 @@ char *extend(char *name)
{
static char path[1024];
int len= strlen(name);
- sprintf(path, "%s%s", name, (((len > 4) && !strcasecmp(".bas", name + len - 4)) ? "" : ".bas"));
+ int n= snprintf(path, sizeof(path), "%s%s", name, (((len > 4) && !strcasecmp(".bas", name + len - 4)) ? "" : ".bas"));
+ assert(n < sizeof(path));
return path;
}

View File

@ -0,0 +1,20 @@
$OpenBSD: patch-tree_c,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
Switch from sprintf(3) to snprintf(3) for cases involving strings that
come from the user, at least. They already use assert(3) in the code
so I just followed along.
--- tree.c.orig Tue Oct 8 22:39:24 2013
+++ tree.c Fri Jan 2 14:31:43 2015
@@ -140,8 +140,10 @@ Node *makeAction(char *text)
{
Node *node= newNode(Action);
char name[1024];
+ size_t n;
assert(thisRule);
- sprintf(name, "_%d_%s", ++actionCount, thisRule->rule.name);
+ n = snprintf(name,sizeof(name),"_%d_%s",++actionCount,thisRule->rule.name);
+ assert(n < sizeof(name));
node->action.name= strdup(name);
node->action.text= strdup(text);
node->action.list= actions;

2
devel/greg/pkg/DESCR Normal file
View File

@ -0,0 +1,2 @@
greg is a recursive-descent parser generator based on Ian Piumarta's
peg/leg. It implements a formalism called Parsing Expression Grammars.

2
devel/greg/pkg/PLIST Normal file
View File

@ -0,0 +1,2 @@
@comment $OpenBSD: PLIST,v 1.1.1.1 2015/01/12 05:51:22 bcallah Exp $
@bin bin/greg