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. =# =# |
|||
=#'' |
|||
'''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 && error("error with terminator of quote at $idx") |
qlen == nothing && error("error with terminator of quote at $idx") |
||
app'''end'''!(outtxt, collect(replace(txt[idx:idx+qlen.stop+2], "\n" => "\n "))) |
app'''end'''!(outtxt, collect(replace(txt[idx:idx+qlen.stop+2], "\n" => "\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 && error("error with terminator of quote at $idx") |
qlen == nothing && error("error with terminator of quote at $idx") |
||
Line 577: | Line 577: | ||
idx += qlen.stop + 2 |
idx += qlen.stop + 2 |
||
'''end''' |
'''end''' |
||
'''elseif''' c == '#' && txt[max(1, idx - 1)] != ''' |
'''elseif''' c == '#' && txt[max(1, idx - 1)] != ''' ''# start comment'' |
||
'''if''' idx < len && txt[idx+1] == '=' |
'''if''' idx < len && txt[idx+1] == '=' ''#= comment =#'' |
||
start, stop = nestedcommentlimits(txt[idx:'''end''']) |
start, stop = nestedcommentlimits(txt[idx:'''end''']) |
||
s = replace(txt[idx:idx+stop-1], "\n" => " |
s = replace(txt[idx:idx+stop-1], "\n" => "\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(==('\n'), txt[idx+1:'''end''']), len - idx) |
newlinepos = something(findfirst(==('\n'), 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 ∈ 'a':'z' |
'''elseif''' c ∈ 'a':'z' ''# 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''' [''', '&', '<', '>'] |
'''elseif''' c '''in''' [''', '&', '<', '>'] ''# \x26 is char & for HTML entity translation'' |
||
s = c == ''' ? " |
s = c == ''' ? "\x26apos;" : c == '&' ? "\x26amp;" : c == '<' ? "\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( |
println(partialhighlight(read("/users/wherr/onedrive/documents/julia programs/test1.jl", String))) |
||
=={{header|Phix}}== |
=={{header|Phix}}== |