Syntax highlighting using Mediawiki formatting: Difference between revisions

→‎{{header|ALGOL 68}}: Much simpler parsing (as in other samples), fixed multi-line comments
m (Fixed typo in task description.)
(→‎{{header|ALGOL 68}}: Much simpler parsing (as in other samples), fixed multi-line comments)
Line 35:
Handles upper-stropping Algol 68 sources (as used by ALGOL 68G and most other compilers).
 
''#CO Convert an upper-stropped Algol 68 source to "wiki" format #''
''# each line is preceeded by a space, #''
''# bold words are enclosed in ''' and ''' and comments in '' and '' #''
''# ', &, < and > are converted to ' & < and > #''
''# everything else if output as is #''
'' quote-stropping, point-stropping and res-stropping is not suppoered''
''# the source is read from stand in and written to stand out #''
'' the source is read from stand in and written to stand out''
''# the last line in the file must end with a newline #''
'' the last line in the file must end with a newline''
''# { and } are assumed to be alternatives for ( and ), if { } should be #''
'' { and } are assumed to be alternatives for ( and ), if { } should be''
''# treated as comments ( as in ALGOL68RS/algol68toc ) #''
''# treated change rs style briefas comments to TRUE ( as in ALGOL68RS/algol68toc #)''
'' change rs style brief comments to TRUE''
''CO''
'''BEGIN'''
'''BOOL''' in string := '''FALSE''';
'''BOOL''' in brief comment := '''FALSE''';
'''INT''' rs comment depth := 0;
'''STRING''' comment delimiter := "";
''# TRUE if {} delimits a nestable brief comment, as in ALGOL 68RS and #''
Line 59 ⟶ 66:
);
'''CHAR''' nl = '''REPR''' 10; ''# newline character #''
'''INT''' error count := 0; ''# number of errors reported #''
'''STRING''' line := nl; ''# current source line #''
'''INT''' pos := '''LWB''' line; ''# current position in line #''
'''CHAR''' c := " "; ''# current source character #''
'''PROC''' error = ( '''STRING''' message )'''VOID''': ''# reports an error #''
'''BEGIN'''
error count +:= 1;
print( ( newline, newline, "**** ", message, newline ) )
'''END''' ''# error #'' ;
''# reports an unterminated construct ( e.g. string, comment ) #''
'''PROC''' unterminated = ( '''STRING''' construct )'''VOID''': error( "Unterminated " + construct );
'''PROC''' next char = '''VOID''': ''# gets the next source character, stores it in c #''
'''IF''' pos <= '''UPB''' line '''THEN'''
Line 81 ⟶ 80:
'''THEN'''
line +:= nl; ''# have another line #''
posc := line[ pos := '''LWB''' line ];
c := line[ pos ];
pos +:= 1
'''ELSE'''
Line 89 ⟶ 87:
'''FI''' ''# next char #'' ;
'''PROC''' out char = ( '''CHAR''' ch )'''VOID''': ''# conveerts and outputs ch #''
'''IF''' ch = nl '''THEN''' print( ( newline, " " ) )
'''IF''' '''NOT''' in brief comment '''AND''' rs comment depth = 0 '''AND''' comment delimiter = "" '''THEN'''
print( ( newline, " " ) ) ''# newline not in a comment #''
'''ELSE''' ''# newline in a comment #''
italic delimiter; print( ( newline, " " ) ); italic delimiter
'''FI'''
'''ELIF''' ch = "<" '''THEN''' print( ( "<" ) )
'''ELIF''' ch = ">" '''THEN''' print( ( ">" ) )
Line 96 ⟶ 99:
'''ELSE''' print( ch )
'''FI''' ''# out char #'' ;
''# outputs the current character and gets the next #''
'''PROC''' out and next char = '''VOID''': '''BEGIN''' out char( c ); next char '''END''';
''# outputs a wiki start/end italic delimiter #''
'''PROC''' italic delimiter = '''VOID''': print( ( "''" ) );
''# outputs a wiki start/end bold delimiter #''
'''PROC''' bold delimiter = '''VOID''': print( ( "'''" ) );
''# returns TRUE if the current character is a string delimiter #''
'''PROC''' have string delimiter = '''BOOL''': c = """";
''# returns TRUE if the current character can start a bold word #''
'''PROC''' have bold = '''BOOL''': c >= "A" '''AND''' c <= "Z";
''# outputs a brief comment to stand out #''
''# end char is the closing delimiter, #''
''# nested char is the opening delimiter for nestable brief comments #''
''# if nested char is blank, the brief comment does not nest #''
''# this handles ALGOL 68RS and algol68toc style {} comments #''
'''PROC''' copy brief comment = ( '''CHAR''' end char, '''CHAR''' nested char )'''VOID''':
'''BEGIN'''
out char( c );
'''WHILE''' next char;
'''NOT''' at eof '''AND''' c /= end char
'''DO'''
'''IF''' c = nested char '''AND''' nested char /= " " '''THEN'''
''# nested brief comment #''
copy brief comment( end char, nested char )
'''ELSE'''
''# notmal comment char #''
out char( c )
'''FI'''
'''OD''';
'''IF''' at eof '''THEN'''
''# unterminated comment #''
unterminated( """" + end char + """ comment" );
c := end char
'''FI''';
out char( c );
next char
'''END''' ''# copy brief comment #'' ;
'''PROC''' copy string = '''VOID''': ''# outputs a string denotation from the source #''
'''WHILE''' have string delimiter '''DO''' ''# within a string denotation, #''
'''WHILE''' out char( c ); ''# "" denotes the " character #''
next char;
'''NOT''' at eof '''AND''' '''NOT''' have string delimiter
'''DO''' '''SKIP''' '''OD''';
'''IF''' '''NOT''' have string delimiter '''THEN'''
unterminated( "string" );
c := """"
'''FI''';
out char( c );
next char
'''OD''' ''# copy string #'' ;
'''PROC''' get bold word = '''STRING''': ''# gets a bold word from then source #''
'''BEGIN'''
Line 150 ⟶ 111:
result
'''END''' ''# get bold word #'' ;
'''PROC''' copy to bold = '''STRING''': ''# copies the source to the output #''
'''IF''' at eof ''# until a bold word is encountered #''
'''THEN''' ""
'''ELSE''' '''STRING''' result := "";
'''WHILE''' out char( c );
next char;
'''NOT''' at eof
'''AND''' '''NOT''' have bold
'''DO''' '''SKIP''' '''OD''';
'''IF''' '''NOT''' at eof '''THEN''' result := get bold word '''FI''';
result
'''FI''' ''# copy to bold #'' ;
'''PROC''' bold word or comment = '''VOID''': ''# handles a bold COMMENT #''
'''IF''' '''STRING''' bold word := get bold word; ''# or other bold word #''
bold word = "CO" '''OR''' bold word = "COMMENT"
'''THEN'''
italic delimiter; ''# have a bold comment #''
'''STRING''' delimiter = bold word;
'''WHILE''' print( ( bold word ) );
bold word := copy to bold;
'''NOT''' at eof
'''AND''' bold word /= delimiter
'''DO''' '''SKIP''' '''OD''';
'''IF''' at eof '''THEN'''
unterminated( """" + delimiter + """ comment" )
'''FI''';
print( ( delimiter ) );
italic delimiter
'''ELSE''' ''# some other bold word #''
bold delimiter;
print( ( bold word ) );
bold delimiter
'''FI''' ''# bold word or comment #'' ;
''# copy the source to stand out, conveerting to wiki format #''
next char;
'''WHILE''' '''NOT''' at eof '''DO'''
'''IF''' cin = "#"string '''THEN''' ''# briefcurrently in a commentstring #''
in string := c /="""";
out and next char
'''ELIF''' in brief comment '''THEN''' ''# currently in a brief comment #''
in brief comment := c /= "#";
out and next char;
'''IF''' '''NOT''' in brief comment '''THEN''' italic delimiter '''FI'''
'''ELIF''' rs comment depth > 0 '''THEN''' ''# currently in a nesting {...} comment #''
'''IF''' c = "}" '''THEN''' rs comment depth -:= 1 '''FI''';
out and next char;
'''IF''' rs comment depth < 1 '''THEN''' italic delimiter '''FI'''
'''ELIF''' comment delimiter /= "" '''THEN''' ''# in a CO/COMMENT comment #''
'''IF''' '''NOT''' have bold '''THEN'''
out and next char ''# haven't reached a bold word #''
'''ELSE'''
'''STRING''' word = get bold word; ''# at the start of a bold word #''
print( ( word ) );
'''IF''' word = comment delimiter '''THEN'''
''# reached the end of the comment #''
italic delimiter;
comment delimiter := ""
'''FI'''
'''FI'''
'''ELIF''' c = """" '''THEN''' ''# start of a string or character denotation #''
out and next char;
in string := '''TRUE'''
'''ELIF''' c = "#" '''THEN''' ''# start of a brief comment such as this one #''
italic delimiter;
copyout briefand comment(next "#", " " )char;
italicin delimiterbrief comment := '''TRUE'''
'''ELIF''' c = "{" '''AND''' rs style brief comments '''THEN''' ''# nestable brief #''
italic delimiter; ''# nestable brief comment ( ALGOL 68RS and algol68toc ) #''
italicout delimiterand next char;
copy briefrs comment( "}",depth "{":= );1
italic delimiter
'''ELIF''' have string delimiter '''THEN''' ''# STRING or CHAR denotation #''
copy string
'''ELIF''' have bold '''THEN''' ''# have a bold word #''
bold'''STRING''' word or= commentget bold word;
'''ELSEIF''' word /= "CO" '''AND''' word /= "COMMENT" '''THEN'''
''# anything else print( ( "'''", word, "'''" ) ) ''# non-comment bold word #''
out char( c );'''ELSE'''
italic delimiter; ''# start of a bold comment #''
next char
print( ( word ) );
comment delimiter := word
'''FI'''
'''ELSE''' ''# anything else #''
out and next char
'''FI'''
'''OD''';
'''IF''' in string '''THEN''' print( ( "**** unterminated string", newline ) )
'''IFELIF''' errorin brief comment count > 0 '''THEN''' print( ( "**** unterminated brief comment", newline ) )
''#'ELIF''' hadrs errorscomment processingdepth the> source0 '''THEN''' print( ( "**** unterminated {...} comment", newline ) #'')
'''ELIF''' comment delimiter /= "" '''THEN''' print( ( "**** unterminated ", whole(comment error count, 0 ), " errors"delimiter, newline ) )
'''FI'''
3,048

edits