$ids=0 def color_by(color,text) "<span style=\"color:#{color}\">#{text}</span>" end def highligh(id,text) "<span onmouseover='document.getElementById(\"id#{id}\").style.background=\"#002020\";' onmouseout ='document.getElementById(\"id#{id}\").style.background=\"none\";'>#{text}</span>" end def highlighted(id,text) "<span id=id#{id}>#{text}</span>" end amethyst AST2Amethyst { trans = Or[ trans*:ary ] -> ary*"|" | Seq[ trans*:ary ] -> ary*" " | Apply[ .:name ] -> name | Bind[ trans:expr @name:name ] -> "("+expr+"):"+name[0] } amethyst Syntax_Highligth { root = ( Grammar[ @rules=>[rule*]:rules -> "amethyst #{@name} < #{@parent} {\n #{rules*""} }" ] | .)*:{it*""} rule = Rule[ {@body}=>trans:body -> "#{@name} = #{body}\n" ] trans = Or_AST[ trans*:ary ] { $ids+=1;highlighted($ids,"#{ary*highligh($ids,"|")}") } | Append_AST[ (@exp=>trans):expr @name:name] -> "#{expr}#{color_by("red",":[#{name[0]}]")}" | Parenthesis[ trans:t ] -> $ids+=1; highlighted($ids,highligh($ids,"(")+t+ highligh($ids,")")) | Seq[ trans*:ary ] -> " #{ary*" "} " | Act[ Args[.*:ary] ] -> color_by("brown","{#{ary*""}}") | CAct[ .* ] -> "" | Pass_AST[ @from=>trans:from @to=>trans:to ] -> highlighted($ids+=1, from + highligh($ids,color_by("yellow","=>")) + to) | Enter_AST[ @from=>trans:from @to=>trans:to ] -> highlighted($ids+=1, from + highligh($ids,color_by("yellow","[")) + to + highligh($ids,color_by("yellow","]"))) | Comment[ .:text ] -> "##{text}" | Apply[ [ "seq" ] CAct[.:str] ] -> "'#{str.inspect[1...-1]}'" | Apply[ ["token"] CAct[.:str] ] -> "\"#{str.inspect[1...-1]}\"" | Apply[ ["regch"] Act[.:str] ] -> "&lt;#{str[2...-2]}&gt;" | Apply[ ["anything"] ] -> "." | Apply[ .:name trans2+:args ] -> $ids+=1;highlighted($ids,highligh($ids, color_by("blue",name+"("))+args*color_by("blue",",")+highligh($ids,color_by("blue",")"))) | Apply[ .:name ] -> color_by("blue",name) | Bind[ trans:expr @name:name ] -> expr + (name[0]=="_result" ? "" : color_by("red",":#{name[0]}")) | .:x {x.inspect} trans2 = Act[ Args[ (Lambda[.:x {x[0]}=>trans:t {$ids+=1;bt=highligh($ids,color_by("yellow","`")); highlighted($ids,"#{bt}#{t}#{bt}")} ] | .)*:ary] ] -> color_by("brown",ary*"") }