ack/util/ceg/as_parser/conversion.c

127 lines
2.2 KiB
C
Raw Normal View History

1988-01-07 15:52:52 +00:00
/* This file contains functions to handle %d, %$, %dist constructs in the
* as_table.
*/
1987-11-20 11:12:07 +00:00
pr_text_with_conversions( str)
char *str;
{
char *s, *ptr, *next_conversion(), *pr_conversion();
1987-11-20 11:12:07 +00:00
while ( ptr = next_conversion( str)) {
/* ptr points to '%'-sign */
*ptr = '\0';
out( "fprint( outfile, \"");
for ( s = str; *s != '\0'; s++)
if ( *s == '\n')
out( "\\n");
else
out( "%c", *s);
out( "\");");
1987-11-20 11:12:07 +00:00
*ptr = '%';
str = pr_conversion( ptr);
}
out( "fprint( outfile, \"");
for ( s = str; *s != '\0'; s++)
if ( *s == '\n')
out( "\\n");
else
out( "%c", *s);
out( "\");");
1987-11-20 11:12:07 +00:00
}
char *next_conversion( str)
char *str;
1988-01-07 15:52:52 +00:00
/* Look for a %-sign, but not in a comment or string! */
1987-11-20 11:12:07 +00:00
{
char *match();
while ( *str && *str != '%') {
switch ( *str++) {
case '\'' : str = match( '\'', str) + 1;
break;
case '"' : str = match( '"', str) + 1;
break;
}
}
return( *str == '%' ? str : (char *)0);
}
1988-01-07 15:52:52 +00:00
1987-11-20 11:12:07 +00:00
char *match( c, str)
char c, *str;
1988-01-07 15:52:52 +00:00
/* Look for charcter 'c', but watch out for things like \n */
1987-11-20 11:12:07 +00:00
{
while ( *str && ( *str != c || *(str-1) == '\\'))
str++;
return( *str ? str : str-1);
}
1988-01-07 15:52:52 +00:00
1987-11-20 11:12:07 +00:00
char *match_bracket( str)
char *str;
/* find ')', but look at nesting '('-')' pairs, return position of ')'.
1988-01-07 15:52:52 +00:00
* Skip strings and comments.
1987-11-20 11:12:07 +00:00
*/
{
int depth;
char *match();
depth = 1;
while ( *str && depth != 0) {
switch ( *str++) {
case '\'' : str = match( '\'', str+1) + 1;
break;
case '"' : str = match( '"', str+1) + 1;
break;
case '(' : depth++;
break;
case ')' : depth--;
break;
}
}
return( str-1);
}
char *find( c, str)
char c, *str;
{
while ( *str && *str != c)
str++;
return( str);
}
char *pr_conversion( str)
char *str;
/* str points to '%'-sign, returns pointer to first character AFTER the
1988-01-07 15:52:52 +00:00
* conversion. %$ will result in a call of eval(), %d will call dist().
1987-11-20 11:12:07 +00:00
*/
{
char *start, *ptr, *match_bracket(), *find();
start = find( '(', str+1);
*start++ = '\0';
ptr = match_bracket( start);
*ptr = '\0';
if ( *(str+1) == '$')
out( "eval( %s);", start);
else if ( strncmp( str+1, "dist", 4) == 0)
out( "dist( %s);", start);
else
out( "fprint( outfile, \"%%%s\", %s);", str+1, start);
return( ptr+1);
}