Syntax highlighting using Mediawiki formatting: Difference between revisions

Content added Content deleted
(Added Algol W)
m (→‎{{header|Julia}}: more comments)
Line 485: Line 485:
=={{header|Julia}}==
=={{header|Julia}}==
''#= Keywords in Julia. Handles two word reserved keywords. #= Also''
''#= Keywords in Julia. Handles two word reserved keywords. #= Also
'' #= handles nested comments such as this. =# =#''
#= handles nested comments such as this. =# =#
''=#''
=#''
'''const''' KEYWORDS = map(
'''const''' KEYWORDS = map(
w -> Regex("^" * w * "\\W"),
w -> Regex("^" * w * "\\W"),
Line 556: Line 556:
idx, len = 1, length(txt)
idx, len = 1, length(txt)
'''while''' idx <= len
'''while''' idx <= len
'''if''' !isvalid(txt, idx)
'''if''' !isvalid(txt, idx) ''# exclude internal positions of multibyte Char''
idx += 1
idx += 1
'''continue'''
'''continue'''
'''end'''
'''end'''
c = txt[idx]
c = txt[idx]
'''if''' c == '\\'
'''if''' c == '\\' ''# escape the next char, send as is''
push!(outtxt, c, txt[idx+1])
push!(outtxt, c, txt[idx+1])
idx += 2
idx += 2
'''elseif''' c == '\"'
'''elseif''' c == '\"' ''# quotation start''
'''if''' idx < len - 2 && c == txt[idx+1] == txt[idx+2]
'''if''' idx < len - 2 && c == txt[idx+1] == txt[idx+2] ''# """ quotes """''
qlen = findfirst(r"(?<!\\)\"\"\""sa, txt[idx+3:'''end'''])
qlen = findfirst(r"(?<!\\)\"\"\""sa, txt[idx+3:'''end'''])
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
app'''end'''!(outtxt, collect(replace(txt[idx:idx+qlen.stop+2], "\n" =&gt; "\n ")))
app'''end'''!(outtxt, collect(replace(txt[idx:idx+qlen.stop+2], "\n" =&gt; "\n ")))
idx += qlen.stop + 3
idx += qlen.stop + 3
'''else'''
'''else''' ''# " quote "''
qlen = findfirst(r"(?<!\\)\"", txt[idx+1:'''end'''])
qlen = findfirst(r"(?<!\\)\"", txt[idx+1:'''end'''])
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
Line 577: Line 577:
idx += qlen.stop + 2
idx += qlen.stop + 2
'''end'''
'''end'''
'''elseif''' c == &apos;#&apos; &amp;&amp; txt[max(1, idx - 1)] != &apos;&apos;&apos;
'''elseif''' c == &apos;#&apos; &amp;&amp; txt[max(1, idx - 1)] != &apos;&apos;&apos; ''# start comment''
'''if''' idx &lt; len &amp;&amp; txt[idx+1] == &apos;=&apos;
'''if''' idx &lt; len &amp;&amp; txt[idx+1] == &apos;=&apos; ''#= comment =#''
start, stop = nestedcommentlimits(txt[idx:'''end'''])
start, stop = nestedcommentlimits(txt[idx:'''end'''])
s = replace(txt[idx:idx+stop-1], "\n" =&gt; "''\n ''")
s = replace(txt[idx:idx+stop-1], "\n" =&gt; "\n ")
app'''end'''!(outtxt, collect("''$s''"))
app'''end'''!(outtxt, collect("\'\'$s\'\'"))
idx += stop
idx += stop
'''else'''
'''else''' ''# found a line comment, like this comment''
newlinepos = something(findfirst(==(&apos;\n&apos;), txt[idx+1:'''end''']), len - idx)
newlinepos = something(findfirst(==(&apos;\n&apos;), txt[idx+1:'''end''']), len - idx)
app'''end'''!(outtxt, collect("''$(txt[idx:idx+newlinepos-1])''"))
app'''end'''!(outtxt, collect("\'\'$(txt[idx:idx+newlinepos-1])\'\'"))
idx += newlinepos
idx += newlinepos
'''end'''
'''end'''
'''elseif''' c ∈ &apos;a&apos;:&apos;z&apos;
'''elseif''' c ∈ &apos;a&apos;:&apos;z&apos; ''# lowercase char so check for keyword match''
'''for''' (j, reg) '''in''' enumerate(KEYWORDS)
'''for''' (j, reg) '''in''' enumerate(KEYWORDS)
m = match(reg, txt[idx:'''end'''])
m = match(reg, txt[idx:'''end'''])
'''if''' m != nothing
'''if''' m != nothing
wlen = m.match.ncodeunits - 2
wlen = m.match.ncodeunits - 2
app'''end'''!(outtxt, collect("'''$(txt[idx:idx+wlen])'''"))
app'''end'''!(outtxt, collect("\'\'\'$(txt[idx:idx+wlen])\'\'\'"))
idx += wlen + 1
idx += wlen + 1
'''break'''
'''break'''
'''elseif''' j == lastindex(KEYWORDS)
'''elseif''' j == lastindex(KEYWORDS) ''# no keyword found, send char to output''
push!(outtxt, c)
push!(outtxt, c)
idx += 1
idx += 1
'''end'''
'''end'''
'''end'''
'''end'''
'''elseif''' c '''in''' [&apos;&apos;&apos;, &apos;&amp;&apos;, &apos;&lt;&apos;, &apos;&gt;&apos;]
'''elseif''' c '''in''' [&apos;&apos;&apos;, &apos;&amp;&apos;, &apos;&lt;&apos;, &apos;&gt;&apos;] ''# \x26 is char & for HTML entity translation''
s = c == &apos;&apos;&apos; ? "&apos;" : c == &apos;&amp;&apos; ? "&amp;" : c == &apos;&lt;&apos; ? "&lt;" : "&gt;"
s = c == &apos;&apos;&apos; ? "\x26apos;" : c == &apos;&amp;&apos; ? "\x26amp;" : c == &apos;&lt;&apos; ? "\x26lt;" : "\x26gt;"
app'''end'''!(outtxt, collect(s))
app'''end'''!(outtxt, collect(s))
idx += 1
idx += 1
'''else''' ''# nothing special found, so pass char to output and increment index into input''
'''else'''
push!(outtxt, c)
push!(outtxt, c)
idx += 1
idx += 1
Line 614: Line 614:
'''end'''
'''end'''
println(partialhighlight(read(PROGRAM_FILE, String)), "\n")
println(partialhighlight(read("/users/wherr/onedrive/documents/julia programs/test1.jl", String)))


=={{header|Phix}}==
=={{header|Phix}}==