@@ -8,33 +8,68 @@ const Cthulhu = Base.PkgId(UUID("f68482b8-f384-11e8-15f7-abe071a5a75f"), "Cthulh
8
8
# syntax highlighting
9
9
#
10
10
11
- const _pygmentize = Ref {Union{String,Nothing}} ()
12
- function pygmentize ()
13
- if ! isassigned (_pygmentize)
14
- _pygmentize[] = Sys. which (" pygmentize" )
15
- end
16
- return _pygmentize[]
17
- end
11
+ # https://github.com/JuliaLang/julia/blob/dacd16f068fb27719b31effbe8929952ee2d5b32/stdlib/InteractiveUtils/src/codeview.jl
12
+ const hlscheme = Dict {Symbol, Tuple{Bool, Union{Symbol, Int}}} (
13
+ :default => (false , :normal ), # e.g. comma, equal sign, unknown token
14
+ :comment => (false , :light_black ),
15
+ :label => (false , :light_red ),
16
+ :instruction => ( true , :light_cyan ),
17
+ :type => (false , :cyan ),
18
+ :number => (false , :yellow ),
19
+ :bracket => (false , :yellow ),
20
+ :variable => (false , :normal ), # e.g. variable, register
21
+ :keyword => (false , :light_magenta ),
22
+ :funcname => (false , :light_yellow ),
23
+ )
18
24
19
25
function highlight (io:: IO , code, lexer)
20
- highlighter = pygmentize ()
21
- have_color = get (io, :color , false )
22
- if highlighter === nothing || ! have_color
26
+ if ! haskey (io, :color )
23
27
print (io, code)
28
+ elseif lexer == " ptx"
29
+ highlight_ptx (io, code)
24
30
else
25
- custom_lexer = joinpath (dirname (@__DIR__ ), " res" , " pygments" , " $lexer .py" )
26
- if isfile (custom_lexer)
27
- lexer = ` $custom_lexer -x`
28
- end
29
-
30
- pipe = open (` $highlighter -f terminal -P bg=dark -l $lexer ` , " r+" )
31
- print (pipe, code)
32
- close (pipe. in)
33
- print (io, read (pipe, String))
31
+ print (io, code)
34
32
end
35
- return
36
33
end
37
34
35
+ function highlight_ptx (io:: IO , code)
36
+ function get_token (s)
37
+ m = match (r" (\s *)([^\s ]+)(.*)" , s)
38
+ m isa RegexMatch && (return m. captures[1 : 3 ])
39
+ return nothing , nothing , nothing
40
+ end
41
+ print_tok (token, type) = Base. printstyled (io,
42
+ token,
43
+ bold = hlscheme[type][1 ],
44
+ color = hlscheme[type][2 ])
45
+ buf = IOBuffer (code)
46
+ while ! eof (buf)
47
+ line = readline (buf)
48
+ indent, tok, line = get_token (line)
49
+ while (tok != = nothing )
50
+ print (io, indent)
51
+ if match (r" ^\/\/ " , tok) isa RegexMatch # Comment
52
+ print_tok (tok, :comment )
53
+ print_tok (line, :comment )
54
+ break
55
+ elseif match (r" ^[\w ]+:" , tok) isa RegexMatch # Label
56
+ print_tok (tok, :label )
57
+ elseif match (r" ^[\w ]+\. [\w ]+(\.\w +)?" , tok) isa RegexMatch # Instruction
58
+ print_tok (tok, :instruction )
59
+ elseif match (r" ^\. [\w ]+" , tok) isa RegexMatch # Directive
60
+ print_tok (tok, :type )
61
+ elseif match (r" ^@!?%p.+" , tok) isa RegexMatch # Guard Predicate
62
+ print_tok (tok, :keyword )
63
+ elseif match (r" ^%[\w ]+" , tok) isa RegexMatch # Register
64
+ print_tok (tok, :number )
65
+ else # Default
66
+ print_tok (tok, :default )
67
+ end
68
+ indent, tok, line = get_token (line)
69
+ end
70
+ print (io, ' \n ' )
71
+ end
72
+ end
38
73
39
74
#
40
75
# code_* replacements
0 commit comments