Syntax highlighting using Mediawiki formatting: Difference between revisions
Content added Content deleted
(→{{header|AWK}}: Tweak) |
(Added Algol W) |
||
Line 170: | Line 170: | ||
'''END''' |
'''END''' |
||
=={{header|ALGOL W}}== |
|||
'''begin''' ''comment syntax highlight an Algol W source using Mediawiki formatting'' |
|||
'' the source is read from standard input and written to standard output'' |
|||
'' ;'' |
|||
''% Algol W strings are limited to 256 characters in length so source lines %'' |
|||
''% are limited to 256 characters %'' |
|||
'''integer''' lineWidth, errorCount, lowerA, upperA, linePos, kwMax; |
|||
'''integer''' MAX_TOKEN_LENGTH; |
|||
'''string'''(1) nl; |
|||
'''string'''(256) line; |
|||
'''string'''(1) currChar, commentEnd; |
|||
'''string'''(9) '''array''' kw ( 1 :: 64 ); |
|||
'''logical''' inString, inComment; |
|||
''% returns true if currChar is in the inclusive range low to high, false otherwise %'' |
|||
'''logical''' '''procedure''' range( '''string'''(1) '''value''' low, high ) ; currChar >= low '''and''' currChar <= high; |
|||
'''procedure''' nextChar ; ''% gets the next source character %'' |
|||
'''if''' linePos = lineWidth '''then''' '''begin''' |
|||
currChar := nl; |
|||
linePos := linePos + 1 |
|||
'''end''' |
|||
'''else''' '''if''' linePos > lineWidth '''then''' '''begin''' |
|||
readcard( line ); |
|||
lineWidth := 256; |
|||
'''while''' lineWidth > 1 '''and''' line( lineWidth - 1 // 1 ) = " " '''do''' lineWidth := lineWidth - 1; |
|||
linePos := 1; |
|||
currChar := line( 0 // 1 ) |
|||
'''end''' |
|||
'''else''' '''begin''' |
|||
currChar := line( linePos // 1 ); |
|||
linePos := linePos + 1 |
|||
'''end''' nextChar ; |
|||
''% returns true if the current character can start an identifier, false otherwise %'' |
|||
'''logical''' '''procedure''' identifierStartChar ; range( "a", "z" ) '''or''' range( "A", "Z" ); |
|||
''% returns true if the current character can be pat of an identifier, false otherwise %'' |
|||
'''logical''' '''procedure''' identifierChar ; identifierStartChar '''or''' range( "0", "9" ) '''or''' currChar = "_"; |
|||
'''procedure''' outAndNextChar ; '''begin''' ''% output currChar and get the next %'' |
|||
'''if''' currChar = "'" '''then''' writeon( "&apos;" ) |
|||
'''else''' '''if''' currChar = "&" '''then''' writeon( "&amp;" ) |
|||
'''else''' '''if''' currChar = "<" '''then''' writeon( "&lt;" ) |
|||
'''else''' '''if''' currChar = ">" '''then''' writeon( "&gt;" ) |
|||
'''else''' '''if''' currChar = nl '''then''' '''begin''' |
|||
'''if''' inComment '''then''' writeon( "''" ); |
|||
write( " " ); |
|||
'''if''' inComment '''then''' writeon( "''" ) |
|||
'''end''' |
|||
'''else''' writeon( currChar ); |
|||
nextChar |
|||
'''end''' outAndNextChar ; |
|||
'''procedure''' identifierOrKeyword ; '''begin''' ''% handle an indentifier or keyword %'' |
|||
'''string'''(9) word, lWord; |
|||
'''integer''' wLength; |
|||
''% recursive keyword binary search %'' |
|||
'''logical''' '''procedure''' isKeyword ( '''integer''' '''value''' low, high ) ; |
|||
'''if''' high < low '''then''' '''false''' |
|||
'''else''' '''begin''' |
|||
'''integer''' mid; |
|||
mid := ( low + high ) '''div''' 2; |
|||
'''if''' kw( mid ) > lWord '''then''' isKeyword( low, mid - 1 ) |
|||
'''else''' '''if''' kw( mid ) = lWord '''then''' '''true''' |
|||
'''else''' isKeyword( mid + 1, high ) |
|||
'''end''' binarySearchR ; |
|||
wLength := 0; |
|||
'''for''' chPos := 0 '''until''' 8 '''do''' '''begin''' |
|||
'''if''' identifierChar '''then''' '''begin''' |
|||
word( chPos // 1 ) := currChar; |
|||
lWord( chPos // 1 ) := '''if''' range( "A", "Z" ) '''then''' code( ( decode( currChar ) - upperA ) + lowerA ) |
|||
'''else''' currChar;; |
|||
wLength := wLength + 1; |
|||
nextChar |
|||
'''end''' |
|||
'''else''' '''begin''' |
|||
lWord( chPos // 1 ) := " "; |
|||
word( chPos // 1 ) := " " |
|||
'''end''' if_identifierChar__ |
|||
'''end''' for_chPos ; |
|||
'''if''' identifierChar '''then''' '''begin''' |
|||
''% all keywords are <= 9 characters long so this must be an identifier %'' |
|||
writeon( word ); |
|||
'''while''' identifierChar '''do''' outAndNextChar |
|||
'''end''' |
|||
'''else''' '''if''' lWord = "comment" '''then''' '''begin''' |
|||
writeon( "''comment" ); |
|||
commentEnd := ";"; |
|||
inComment := '''true''' |
|||
'''end''' |
|||
'''else''' '''if''' isKeyword( 1, kwMax ) '''then''' '''begin''' |
|||
writeon( "'''" ); |
|||
'''for''' chPos := 0 '''until''' wLength - 1 '''do''' writeon( word( chPos // 1 ) ); |
|||
writeon( "'''" ) |
|||
'''end''' |
|||
'''else''' '''begin''' ''% identifier %'' |
|||
'''for''' chPos := 0 '''until''' wLength - 1 '''do''' writeon( word( chPos // 1 ) ) |
|||
'''end''' if_various_words |
|||
'''end''' identifierOrKeyword ; |
|||
s_w := 0; i_w := 1; ''% output formarting %'' |
|||
MAX_TOKEN_LENGTH := 256; |
|||
nl := code( 10 ); |
|||
lowerA := decode( "a" ); |
|||
upperA := decode( "A" ); |
|||
''% allow the program to continue after reaching end-of-file %'' |
|||
ENDFILE := EXCEPTION( '''false''', 1, 0, '''false''', "EOF" ); |
|||
''% ensure the first call to nextChar reads the first line %'' |
|||
lineWidth := 256; |
|||
linePos := lineWidth + 1; |
|||
currChar := " "; |
|||
'''begin''' ''% keywords %'' |
|||
'''procedure''' K ( '''string'''(9) '''value''' kwStr ) ; '''begin''' |
|||
kwMax := kwMax + 1; |
|||
kw( kwMax ) := kwStr |
|||
'''end''' K ; |
|||
kwMax := 0; |
|||
K("abs");K("algol");K("and");K("array");K("assert");K("begin");K("bits"); |
|||
K("case");K("complex");K("div");K("do");K("else");K("end");K("false"); |
|||
K("for");K("fortran");K("go");K("goto");K("if");K("integer");K("is"); |
|||
K("logical");K("long");K("not");K("null");K("of");K("or"); |
|||
K("procedure");K("real");K("record");K("reference");K("rem");K("result"); |
|||
K("shl");K("short");K("shr");K("step");K("string"); |
|||
K("then");K("to");K("true");K("until");K("value");K("while") |
|||
'''end''' keywords ; |
|||
inString := inComment := '''false'''; |
|||
outAndNextChar; |
|||
'''while''' '''not''' XCPNOTED(ENDFILE) '''do''' '''begin''' |
|||
'''if''' inString '''then''' '''begin''' ''% in a string %'' |
|||
inString := currChar '''not''' = """"; |
|||
outAndNextChar; |
|||
'''end''' |
|||
'''else''' '''if''' inComment '''then''' '''begin''' ''% in a comment %'' |
|||
inComment := currChar '''not''' = ";" '''and''' currChar '''not''' = commentEnd; |
|||
outAndNextChar; |
|||
'''if''' '''not''' inComment '''then''' writeon( "''" ); |
|||
'''end''' |
|||
'''else''' '''if''' identifierStartChar '''then''' identifierOrKeyword |
|||
'''else''' '''if''' currChar = """" '''then''' '''begin''' ''% string literal %'' |
|||
outAndNextChar; |
|||
inString := '''true''' |
|||
'''end''' |
|||
'''else''' '''if''' currChar = "%" '''then''' '''begin''' ''% brief comment %'' |
|||
writeon( "''" ); |
|||
commentEnd := "%"; |
|||
inComment := '''true'''; |
|||
outAndNextChar |
|||
'''end''' |
|||
'''else''' outAndNextChar |
|||
'''end''' while_not_ar_eof ; |
|||
'''if''' inComment '''then''' write( "**** unterminated comment" ) |
|||
'''else''' '''if''' inString '''then''' write( "**** unterminated string" ) |
|||
'''end'''. |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||