1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* cook - file construction tool
* Copyright (C) 1999, 2001 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate matching words lists
*/
#include <expr/position.h>
#include <match/wl.h>
#include <str_list.h>
#include <trace.h>
/*
* NAME
* wl_match - find a pattern in a word list
*
* SYNOPSIS
* match_ty *wl_match(string_list_ty *pattern, string_ty *target);
*
* DESCRIPTION
* Wl_match is used to determine whether any one of the words in
* the wordlist (wlp) match the pattern given.
*
* RETURNS
* A zero is returned if not one of the words matches the pattern;
* otherwise a pointer to a "match structure" is returned,
* in a similar fashion to match().
*
* CAVEAT
* The information returned resides in dynamic memory.
* It is the responsibility of the
* caller to ensure that it is freed when it is finished with,
* by a call to match_delete();
*/
int
match_wl_attempt(mp, formal, actual, pp)
match_ty *mp;
string_list_ty *formal;
string_ty *actual;
const expr_position_ty *pp;
{
size_t j;
int result;
trace(("match_wl_attempt(mp = %08lX, formal = %08lX, actual = \"%s\", \
pp = %08lX)\n{\n", (long)mp, (long)formal, actual->str_text, (long)pp));
result = 0;
for (j = 0; j < formal->nstrings; j++)
{
result = match_attempt(mp, formal->string[j], actual, pp);
if (result)
break;
}
trace(("return %d;\n", result));
trace(("}\n"));
return result;
}
/*
* NAME
* wl_reconstruct - reconstruct a word list
*
* SYNOPSIS
* void wl_reconstruct(string_list_ty *to, string_list_ty *from,
* match_ty *field)
*
* DESCRIPTION
* Wl_reconstruct is used to reconstruct an entire word list,
* sort of the convers of wl_match().
*
* RETURNS
* 'To' is a word list of reconstructed strings.
*
* CAVEAT
* It is the responsibility of the caller to ensire that the
* reconstructed word list in 'to' is freed when finished with,
* by a call to string_list_destructor().
*/
int
match_wl_reconstruct_lhs(mp, to, from, pp)
const match_ty *mp;
string_list_ty *to;
string_list_ty *from;
const expr_position_ty *pp;
{
size_t j;
string_ty *s;
string_list_constructor(to);
for (j = 0; j < from->nstrings; j++)
{
s = match_reconstruct_lhs(mp, from->string[j], pp);
if (!s)
return -1;
string_list_append(to, s);
str_free(s);
}
return 0;
}
int
match_wl_reconstruct_rhs(mp, to, from, pp)
const match_ty *mp;
string_list_ty *to;
string_list_ty *from;
const expr_position_ty *pp;
{
size_t j;
string_ty *s;
string_list_constructor(to);
for (j = 0; j < from->nstrings; j++)
{
s = match_reconstruct_rhs(mp, from->string[j], pp);
if (!s)
return -1;
string_list_append(to, s);
str_free(s);
}
return 0;
}
int
match_wl_usage_mask(mp, wlp, pp)
const match_ty *mp;
string_list_ty *wlp;
const expr_position_ty *pp;
{
int result;
size_t j;
int ok;
result = 0;
for (j = 0; j < wlp->nstrings; ++j)
{
ok = match_usage_mask(mp, wlp->string[j], pp);
if (ok < 0)
return -1;
result |= ok;
}
return result;
}
|