Syntax highlighting using Mediawiki formatting: Difference between revisions

m
Moved Oberon-07 to the right place
(Added Oberon-07)
m (Moved Oberon-07 to the right place)
Line 614:
println(partialhighlight(read(PROGRAM_FILE, String)))
 
=={{header|Phix}}==
Note the utility I use for this on a day-to-day basis (pwa/p2js.exw/<Ctrl M>) must be easily over 50,000 lines of code by now...<br>
The following is deliberately the simplest possible thing that gets the job done, and there are of course 1,001 things missing:
No support for [multiline] shebangs, C-style comments, nested block comments, or (as noted) Eu-compatible block comments; and keywords c/should easily be several hundred entries long, and tested/constructed using A-Z and 0-9, ...
''--''
''-- demo\rosetta\syntax_highlight.exw''
''-- =================================''
''--''
'''string''' pgm = '''substitute'''('''get_text'''('''command_line'''()[$]),"\r\n","\n")
''-- or(/for javascript compatibility) specify constant pgm = """...""" ''
'''constant''' qqq = `""`&amp;`"`, ''/* (split to assist with permitting ^^^) */''
keywords = {`and`,`assert`,`bool`,`command_line`,`constant`,`do`,`else`,`elsif`,`end`,
`find`,`for`,`function`,`get_text`,`if`,`iff`,`in`,`integer`,`length`,`match`,`not`,
`procedure`,`puts`,`return`,`sequence`,`string`,`substitute`,`then`,`wait_key`,`while`},
htmlify = {"&apos;&amp;&lt;&gt;",{`apos`,`amp`,`lt`,`gt`}}
'''integer''' i = 1, l = '''length'''(pgm), word_start = 0
'''string''' out = " "
'''procedure''' spacenl('''sequence''' s)
'''for''' ch '''in''' s '''do'''
'''integer''' k = '''find'''(ch,htmlify[1])
'''if''' k '''then''' ch = &apos;&amp;&apos; &amp; htmlify[2][k] &amp; &apos;;&apos; '''end''' '''if'''
out &amp;= ch
'''if''' ch=&apos;\n&apos; '''then''' out &amp;= &apos; &apos; '''end''' '''if'''
'''end''' '''for'''
'''end''' '''procedure'''
'''function''' do_string('''integer''' i, ni, l, '''string''' stype)
'''assert'''(ni&gt;0,"%d quoted string not closed",{stype})
ni += l
spacenl(pgm[i..ni])
'''return''' ni
'''end''' '''function'''
'''while''' i&lt;=l '''do'''
'''integer''' ch = pgm[i]
'''if''' (ch&gt;=&apos;a&apos; '''and''' ch&lt;=&apos;z&apos;) or ch=&apos;_&apos; '''then'''
'''if''' '''not''' word_start '''then''' word_start := i '''end''' '''if'''
'''else'''
'''if''' word_start '''then'''
'''string''' one_word = pgm[word_start..i-1]
'''bool''' is_key = '''find'''(one_word,keywords)
'''if''' is_key '''then''' out &amp;= `&apos;&apos;&apos;` '''end''' '''if'''
out &amp;= one_word
'''if''' is_key '''then''' out &amp;= `&apos;&apos;&apos;` '''end''' '''if'''
word_start = 0
'''end''' '''if'''
'''if''' ch=&apos;-&apos; '''and''' i&lt;l '''and''' pgm[i+1]=&apos;-&apos; '''then'''
''-- nb: does not handle --/* style comments''
'''integer''' line_comment = i
'''while''' i&lt;l '''and''' pgm[i+1]!=&apos;\n&apos; '''do''' i += 1 '''end''' '''while'''
out &amp;= `&apos;&apos;`
spacenl(pgm[line_comment..i])
out &amp;= `&apos;&apos;`
'''elsif''' ch=&apos;/&apos; '''and''' i&lt;l '''and''' pgm[i+1]=&apos;*&apos; '''then'''
''-- nb: does not handle nested block comments''
'''integer''' block_comment = i
i = '''match'''(`*/`,pgm,i+2)+1
'''assert'''(i&gt;1,"missing closing block comment")
out &amp;= `&apos;&apos;`
spacenl(pgm[block_comment..i])
out &amp;= `&apos;&apos;`
'''elsif''' ch=&apos;"&apos; '''then'''
'''if''' i+1&lt;l '''and''' pgm[i..i+2]=qqq '''then'''
i = do_string(i,'''match'''(qqq,pgm,i+3),2,"triple")
'''else'''
i = do_string(i,'''find'''(&apos;"&apos;,pgm,i+1),0,"double")
'''end''' '''if'''
'''elsif''' '''find'''(ch,"`&apos;") '''then'''
'''string''' stype = '''iff'''(ch=&apos;`&apos;?"backtick":"single")
i = do_string(i,'''find'''(ch,pgm,i+1),0,stype)
'''else'''
spacenl({ch})
'''end''' '''if'''
'''end''' '''if'''
i += 1
'''end''' '''while'''
'''puts'''(1,out)
{} = '''wait_key'''()
 
=={{header|Oberon-07}}==
Line 927 ⟶ 847:
'''END''' ToWiki.
=={{header|Phix}}==
Note the utility I use for this on a day-to-day basis (pwa/p2js.exw/<Ctrl M>) must be easily over 50,000 lines of code by now...<br>
The following is deliberately the simplest possible thing that gets the job done, and there are of course 1,001 things missing:
No support for [multiline] shebangs, C-style comments, nested block comments, or (as noted) Eu-compatible block comments; and keywords c/should easily be several hundred entries long, and tested/constructed using A-Z and 0-9, ...
''--''
''-- demo\rosetta\syntax_highlight.exw''
''-- =================================''
''--''
'''string''' pgm = '''substitute'''('''get_text'''('''command_line'''()[$]),"\r\n","\n")
''-- or(/for javascript compatibility) specify constant pgm = """...""" ''
'''constant''' qqq = `""`&amp;`"`, ''/* (split to assist with permitting ^^^) */''
keywords = {`and`,`assert`,`bool`,`command_line`,`constant`,`do`,`else`,`elsif`,`end`,
`find`,`for`,`function`,`get_text`,`if`,`iff`,`in`,`integer`,`length`,`match`,`not`,
`procedure`,`puts`,`return`,`sequence`,`string`,`substitute`,`then`,`wait_key`,`while`},
htmlify = {"&apos;&amp;&lt;&gt;",{`apos`,`amp`,`lt`,`gt`}}
'''integer''' i = 1, l = '''length'''(pgm), word_start = 0
'''string''' out = " "
'''procedure''' spacenl('''sequence''' s)
'''for''' ch '''in''' s '''do'''
'''integer''' k = '''find'''(ch,htmlify[1])
'''if''' k '''then''' ch = &apos;&amp;&apos; &amp; htmlify[2][k] &amp; &apos;;&apos; '''end''' '''if'''
out &amp;= ch
'''if''' ch=&apos;\n&apos; '''then''' out &amp;= &apos; &apos; '''end''' '''if'''
'''end''' '''for'''
'''end''' '''procedure'''
'''function''' do_string('''integer''' i, ni, l, '''string''' stype)
'''assert'''(ni&gt;0,"%d quoted string not closed",{stype})
ni += l
spacenl(pgm[i..ni])
'''return''' ni
'''end''' '''function'''
'''while''' i&lt;=l '''do'''
'''integer''' ch = pgm[i]
'''if''' (ch&gt;=&apos;a&apos; '''and''' ch&lt;=&apos;z&apos;) or ch=&apos;_&apos; '''then'''
'''if''' '''not''' word_start '''then''' word_start := i '''end''' '''if'''
'''else'''
'''if''' word_start '''then'''
'''string''' one_word = pgm[word_start..i-1]
'''bool''' is_key = '''find'''(one_word,keywords)
'''if''' is_key '''then''' out &amp;= `&apos;&apos;&apos;` '''end''' '''if'''
out &amp;= one_word
'''if''' is_key '''then''' out &amp;= `&apos;&apos;&apos;` '''end''' '''if'''
word_start = 0
'''end''' '''if'''
'''if''' ch=&apos;-&apos; '''and''' i&lt;l '''and''' pgm[i+1]=&apos;-&apos; '''then'''
''-- nb: does not handle --/* style comments''
'''integer''' line_comment = i
'''while''' i&lt;l '''and''' pgm[i+1]!=&apos;\n&apos; '''do''' i += 1 '''end''' '''while'''
out &amp;= `&apos;&apos;`
spacenl(pgm[line_comment..i])
out &amp;= `&apos;&apos;`
'''elsif''' ch=&apos;/&apos; '''and''' i&lt;l '''and''' pgm[i+1]=&apos;*&apos; '''then'''
''-- nb: does not handle nested block comments''
'''integer''' block_comment = i
i = '''match'''(`*/`,pgm,i+2)+1
'''assert'''(i&gt;1,"missing closing block comment")
out &amp;= `&apos;&apos;`
spacenl(pgm[block_comment..i])
out &amp;= `&apos;&apos;`
'''elsif''' ch=&apos;"&apos; '''then'''
'''if''' i+1&lt;l '''and''' pgm[i..i+2]=qqq '''then'''
i = do_string(i,'''match'''(qqq,pgm,i+3),2,"triple")
'''else'''
i = do_string(i,'''find'''(&apos;"&apos;,pgm,i+1),0,"double")
'''end''' '''if'''
'''elsif''' '''find'''(ch,"`&apos;") '''then'''
'''string''' stype = '''iff'''(ch=&apos;`&apos;?"backtick":"single")
i = do_string(i,'''find'''(ch,pgm,i+1),0,stype)
'''else'''
spacenl({ch})
'''end''' '''if'''
'''end''' '''if'''
i += 1
'''end''' '''while'''
'''puts'''(1,out)
{} = '''wait_key'''()
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
3,049

edits