n***@top-post
2004-11-14 15:25:25 UTC
# Awk program to convert Oberon keywords that are in lowercase
# to uppercase. Handles nested comments.
# Caution: if you create identifiers that contain only lowercase
# letters and that are spelled the same as an Oberon keyword,
# they will be uppercased. (Obviously.)
Thanks. Now please someone quickly translate the code below to# to uppercase. Handles nested comments.
# Caution: if you create identifiers that contain only lowercase
# letters and that are spelled the same as an Oberon keyword,
# they will be uppercased. (Obviously.)
Oberon or some other proper Algol-family language.
== Chris Glur
BEGIN {
quote = "\""
open_comment = "[(][*]"
close_comment = "[*][)]"
openers = quote "|" open_comment
sought = openers
setup_words()
}
## ----- Main loop. -----
{
while ( match( $0, sought ) )
{ Match( $0 )
$0 = RRIGHT
matched = RMATCH
process( RLEFT )
output( matched )
change_state( matched )
}
process( $0 )
output( "\n" )
}
## ----- Functions. -----
function change_state( s )
{
if ( s ~ open_comment )
{ in_comment++
sought = open_comment "|" close_comment
}
else if ( s ~ close_comment )
{ if ( --in_comment == 0 )
sought = openers
}
else if ( in_quote )
{ in_quote = 0
sought = openers
}
else
{ in_quote = 1
sought = quote
}
}
function process( s, accum )
{
accum = ""
if ( in_comment || in_quote )
accum = s
else
{
while ( match( s, /[a-zA-Z_]+/ ) )
{ Match( s )
if ( RMATCH in words )
RMATCH = toupper( RMATCH )
accum = accum RLEFT RMATCH
s = RRIGHT
}
accum = accum s
}
output( accum )
}
function output( s )
{ printf "%s", s
}
function setup_words( s,temp,i )
{
s = "array,begin,case,const,div,do,else,elsif,end,exit,if,import,in,is" \
",loop,mod,module,nil,of,or,pointer,procedure,record,repeat,return" \
",then,to,type,until,var,while,with,abs,ash,boolean,cap,char,chr,copy" \
",dec,entier,excl,false,halt,inc,incl,integer,len,long,longint" \
",longreal,max,min,new,odd,ord,real,set,short,shortint,size,true"
split( s, temp, /,/ )
for (i=1; i in temp; i++)
words[temp[i]] = 1
}
function Match( s )
{
if ( RSTART )
{ RMATCH = substr(s, RSTART, RLENGTH )
RLEFT = substr(s, 1, RSTART - 1)
RRIGHT = substr(s, RSTART + RLENGTH )
}
else
{ RMATCH = RLEFT = ""
RRIGHT = s
}
return RSTART
}
quote = "\""
open_comment = "[(][*]"
close_comment = "[*][)]"
openers = quote "|" open_comment
sought = openers
setup_words()
}
## ----- Main loop. -----
{
while ( match( $0, sought ) )
{ Match( $0 )
$0 = RRIGHT
matched = RMATCH
process( RLEFT )
output( matched )
change_state( matched )
}
process( $0 )
output( "\n" )
}
## ----- Functions. -----
function change_state( s )
{
if ( s ~ open_comment )
{ in_comment++
sought = open_comment "|" close_comment
}
else if ( s ~ close_comment )
{ if ( --in_comment == 0 )
sought = openers
}
else if ( in_quote )
{ in_quote = 0
sought = openers
}
else
{ in_quote = 1
sought = quote
}
}
function process( s, accum )
{
accum = ""
if ( in_comment || in_quote )
accum = s
else
{
while ( match( s, /[a-zA-Z_]+/ ) )
{ Match( s )
if ( RMATCH in words )
RMATCH = toupper( RMATCH )
accum = accum RLEFT RMATCH
s = RRIGHT
}
accum = accum s
}
output( accum )
}
function output( s )
{ printf "%s", s
}
function setup_words( s,temp,i )
{
s = "array,begin,case,const,div,do,else,elsif,end,exit,if,import,in,is" \
",loop,mod,module,nil,of,or,pointer,procedure,record,repeat,return" \
",then,to,type,until,var,while,with,abs,ash,boolean,cap,char,chr,copy" \
",dec,entier,excl,false,halt,inc,incl,integer,len,long,longint" \
",longreal,max,min,new,odd,ord,real,set,short,shortint,size,true"
split( s, temp, /,/ )
for (i=1; i in temp; i++)
words[temp[i]] = 1
}
function Match( s )
{
if ( RSTART )
{ RMATCH = substr(s, RSTART, RLENGTH )
RLEFT = substr(s, 1, RSTART - 1)
RRIGHT = substr(s, RSTART + RLENGTH )
}
else
{ RMATCH = RLEFT = ""
RRIGHT = s
}
return RSTART
}