Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,7 @@ indent: $(top_srcdir)/.indent.pro
echo $$f FAILED to indent; \
done;

.PHONY: indent
fix-parse: parse.c
sed -i -E -e 's/(yy[^,]+,[^,]+), gv\)/\1, yyscanner, gv)/' parse.c

.PHONY: indent fix-parse
24 changes: 12 additions & 12 deletions src/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@
*/

/* global buffers. */
struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */
struct Buf top_buf; /**< contains %top code. String buffer. */
//struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */
//struct Buf top_buf; /**< contains %top code. String buffer. */

/* Append a "%s" formatted string to a string buffer */
struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s)
struct Buf *buf_prints (FlexState* gv, struct Buf *buf, const char *fmt, const char *s)
{
char *t;
size_t tsz;

tsz = strlen(fmt) + strlen(s) + 1;
t = malloc(tsz);
if (!t)
flexfatal (_("Allocation of buffer to print string failed"));
flexfatal (gv, _("Allocation of buffer to print string failed"));
snprintf (t, tsz, fmt, s);
buf = buf_strappend (buf, t);
buf = buf_strappend (gv, buf, t);
free(t);
return buf;
}

/* Appends n characters in str to buf. */
struct Buf *buf_strnappend (struct Buf *buf, const char *str, int n)
struct Buf *buf_strnappend (FlexState* gv, struct Buf *buf, const char *str, int n)
{
buf_append (buf, str, n + 1);
buf_append (gv, buf, str, n + 1);

/* "undo" the '\0' character that buf_append() already copied. */
buf->nelts--;
Expand All @@ -77,9 +77,9 @@ struct Buf *buf_strnappend (struct Buf *buf, const char *str, int n)
}

/* Appends characters in str to buf. */
struct Buf *buf_strappend (struct Buf *buf, const char *str)
struct Buf *buf_strappend (FlexState* gv, struct Buf *buf, const char *str)
{
return buf_strnappend (buf, str, (int) strlen (str));
return buf_strnappend (gv, buf, str, (int) strlen (str));
}

/* create buf with 0 elements, each of size elem_size. */
Expand Down Expand Up @@ -107,7 +107,7 @@ void buf_destroy (struct Buf *buf)
* We grow by mod(512) boundaries.
*/

struct Buf *buf_append (struct Buf *buf, const void *ptr, int n_elem)
struct Buf *buf_append (FlexState* gv, struct Buf *buf, const void *ptr, int n_elem)
{
int n_alloc = 0;

Expand All @@ -130,10 +130,10 @@ struct Buf *buf_append (struct Buf *buf, const void *ptr, int n_elem)

if (!buf->elts)
buf->elts =
allocate_array ((int) n_alloc, buf->elt_size);
allocate_array (gv, (int) n_alloc, buf->elt_size);
else
buf->elts =
reallocate_array (buf->elts, (int) n_alloc,
reallocate_array (gv, buf->elts, (int) n_alloc,
buf->elt_size);

buf->nmax = n_alloc;
Expand Down
142 changes: 71 additions & 71 deletions src/ccl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,86 +35,86 @@

/* return true if the chr is in the ccl. Takes negation into account. */
static bool
ccl_contains (const int cclp, const int ch)
ccl_contains (FlexState* gv, const int cclp, const int ch)
{
int ind, len, i;

len = ccllen[cclp];
ind = cclmap[cclp];
len = gv->ccllen[cclp];
ind = gv->cclmap[cclp];

for (i = 0; i < len; ++i)
if (ccltbl[ind + i] == ch)
return !cclng[cclp];
if (gv->ccltbl[ind + i] == ch)
return !gv->cclng[cclp];

return cclng[cclp];
return gv->cclng[cclp];
}


/* ccladd - add a single character to a ccl */

void ccladd (int cclp, int ch)
void ccladd (FlexState* gv, int cclp, int ch)
{
int ind, len, newpos, i;

check_char (ch);
check_char (gv, ch);

len = ccllen[cclp];
ind = cclmap[cclp];
len = gv->ccllen[cclp];
ind = gv->cclmap[cclp];

/* check to see if the character is already in the ccl */

for (i = 0; i < len; ++i)
if (ccltbl[ind + i] == ch)
if (gv->ccltbl[ind + i] == ch)
return;

/* mark newlines */
if (ch == nlch)
ccl_has_nl[cclp] = true;
if (ch == gv->nlch)
gv->ccl_has_nl[cclp] = true;

newpos = ind + len;

/* For a non-last cclp, expanding the set will overflow and overwrite a
* char in the next cclp.
* FIXME: Need another allocation scheme for ccl's. */
if (cclp != lastccl) {
flexfatal(_("internal error: trying to add a char to a non-last ccl.\n"));
if (cclp != gv->lastccl) {
flexfatal(gv, _("internal error: trying to add a char to a non-last ccl.\n"));
}

if (newpos >= current_max_ccl_tbl_size) {
current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
if (newpos >= gv->current_max_ccl_tbl_size) {
gv->current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;

++num_reallocs;
++gv->num_reallocs;

ccltbl = reallocate_Character_array (ccltbl,
current_max_ccl_tbl_size);
gv->ccltbl = reallocate_Character_array (gv->ccltbl,
gv->current_max_ccl_tbl_size);
}

ccllen[cclp] = len + 1;
ccltbl[newpos] = (unsigned char) ch;
gv->ccllen[cclp] = len + 1;
gv->ccltbl[newpos] = (unsigned char) ch;
}

/* dump_cclp - same thing as list_character_set, but for cclps. */

static void dump_cclp (FILE* file, int cclp)
static void dump_cclp (FlexState* gv, FILE* file, int cclp)
{
int i;

putc ('[', file);

for (i = 0; i < ctrl.csize; ++i) {
if (ccl_contains(cclp, i)){
for (i = 0; i < gv->ctrl.csize; ++i) {
if (ccl_contains(gv, cclp, i)){
int start_char = i;

putc (' ', file);

fputs (readable_form (i), file);
fputs (readable_form (gv, i), file);

while (++i < ctrl.csize && ccl_contains(cclp,i)) ;
while (++i < gv->ctrl.csize && ccl_contains(gv, cclp, i)) ;

if (i - 1 > start_char)
/* this was a run */
fprintf (file, "-%s",
readable_form (i - 1));
readable_form (gv, i - 1));

putc (' ', file);
}
Expand All @@ -127,61 +127,61 @@ static void dump_cclp (FILE* file, int cclp)

/* ccl_set_diff - create a new ccl as the set difference of the two given ccls. */
int
ccl_set_diff (int a, int b)
ccl_set_diff (FlexState* gv, int a, int b)
{
int d, ch;

/* create new class */
d = cclinit();
d = cclinit(gv);

/* In order to handle negation, we spin through all possible chars,
* adding each char in a that is not in b.
* (This could be O(n^2), but n is small and bounded.)
*/
for ( ch = 0; ch < ctrl.csize; ++ch )
if (ccl_contains (a, ch) && !ccl_contains(b, ch))
ccladd (d, ch);
for ( ch = 0; ch < gv->ctrl.csize; ++ch )
if (ccl_contains (gv, a, ch) && !ccl_contains(gv, b, ch))
ccladd (gv, d, ch);

/* debug */
if (0){
fprintf(stderr, "ccl_set_diff (");
fprintf(stderr, "\n ");
dump_cclp (stderr, a);
dump_cclp (gv, stderr, a);
fprintf(stderr, "\n ");
dump_cclp (stderr, b);
dump_cclp (gv, stderr, b);
fprintf(stderr, "\n ");
dump_cclp (stderr, d);
dump_cclp (gv, stderr, d);
fprintf(stderr, "\n)\n");
}
return d;
}

/* ccl_set_union - create a new ccl as the set union of the two given ccls. */
int
ccl_set_union (int a, int b)
ccl_set_union (FlexState* gv, int a, int b)
{
int d, i;

/* create new class */
d = cclinit();
d = cclinit(gv);

/* Add all of a */
for (i = 0; i < ccllen[a]; ++i)
ccladd (d, ccltbl[cclmap[a] + i]);
for (i = 0; i < gv->ccllen[a]; ++i)
ccladd (gv, d, gv->ccltbl[gv->cclmap[a] + i]);

/* Add all of b */
for (i = 0; i < ccllen[b]; ++i)
ccladd (d, ccltbl[cclmap[b] + i]);
for (i = 0; i < gv->ccllen[b]; ++i)
ccladd (gv, d, gv->ccltbl[gv->cclmap[b] + i]);

/* debug */
if (0){
fprintf(stderr, "ccl_set_union (%d + %d = %d", a, b, d);
fprintf(stderr, "\n ");
dump_cclp (stderr, a);
dump_cclp (gv, stderr, a);
fprintf(stderr, "\n ");
dump_cclp (stderr, b);
dump_cclp (gv, stderr, b);
fprintf(stderr, "\n ");
dump_cclp (stderr, d);
dump_cclp (gv, stderr, d);
fprintf(stderr, "\n)\n");
}
return d;
Expand All @@ -190,48 +190,48 @@ ccl_set_union (int a, int b)

/* cclinit - return an empty ccl */

int cclinit (void)
int cclinit (FlexState* gv)
{
if (++lastccl >= current_maxccls) {
current_maxccls += MAX_CCLS_INCREMENT;
if (++gv->lastccl >= gv->current_maxccls) {
gv->current_maxccls += MAX_CCLS_INCREMENT;

++num_reallocs;
++gv->num_reallocs;

cclmap =
reallocate_integer_array (cclmap, current_maxccls);
ccllen =
reallocate_integer_array (ccllen, current_maxccls);
cclng = reallocate_integer_array (cclng, current_maxccls);
ccl_has_nl = reallocate_array(ccl_has_nl, current_maxccls, sizeof(char));
gv->cclmap =
reallocate_integer_array (gv->cclmap, gv->current_maxccls);
gv->ccllen =
reallocate_integer_array (gv->ccllen, gv->current_maxccls);
gv->cclng = reallocate_integer_array (gv->cclng, gv->current_maxccls);
gv->ccl_has_nl = reallocate_array(gv, gv->ccl_has_nl, gv->current_maxccls, sizeof(char));
}

if (lastccl == 1)
if (gv->lastccl == 1)
/* we're making the first ccl */
cclmap[lastccl] = 0;
gv->cclmap[gv->lastccl] = 0;

else
/* The new pointer is just past the end of the last ccl.
* Since the cclmap points to the \first/ character of a
* ccl, adding the length of the ccl to the cclmap pointer
* will produce a cursor to the first free space.
*/
cclmap[lastccl] =
cclmap[lastccl - 1] + ccllen[lastccl - 1];
gv->cclmap[gv->lastccl] =
gv->cclmap[gv->lastccl - 1] + gv->ccllen[gv->lastccl - 1];

ccllen[lastccl] = 0;
cclng[lastccl] = 0; /* ccl's start out life un-negated */
ccl_has_nl[lastccl] = false;
gv->ccllen[gv->lastccl] = 0;
gv->cclng[gv->lastccl] = 0; /* ccl's start out life un-negated */
gv->ccl_has_nl[gv->lastccl] = false;

return lastccl;
return gv->lastccl;
}


/* cclnegate - negate the given ccl */

void cclnegate (int cclp)
void cclnegate (FlexState* gv, int cclp)
{
cclng[cclp] = 1;
ccl_has_nl[cclp] = !ccl_has_nl[cclp];
gv->cclng[cclp] = 1;
gv->ccl_has_nl[cclp] = !gv->ccl_has_nl[cclp];
}


Expand All @@ -242,26 +242,26 @@ void cclnegate (int cclp)
* has a non-zero value in the cset array.
*/

void list_character_set (FILE *file, int cset[])
void list_character_set (FlexState* gv, FILE *file, int cset[])
{
int i;

putc ('[', file);

for (i = 0; i < ctrl.csize; ++i) {
for (i = 0; i < gv->ctrl.csize; ++i) {
if (cset[i]) {
int start_char = i;

putc (' ', file);

fputs (readable_form (i), file);
fputs (readable_form (gv, i), file);

while (++i < ctrl.csize && cset[i]) ;
while (++i < gv->ctrl.csize && cset[i]) ;

if (i - 1 > start_char)
/* this was a run */
fprintf (file, "-%s",
readable_form (i - 1));
readable_form (gv, i - 1));

putc (' ', file);
}
Expand Down
Loading