|
| 1 | +(* Yoann Padioleau |
| 2 | + * |
| 3 | + * Copyright (C) 2010 Facebook |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public License |
| 7 | + * version 2.1 as published by the Free Software Foundation, with the |
| 8 | + * special exception on linking described in file license.txt. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, but |
| 11 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file |
| 13 | + * license.txt for more details. |
| 14 | + *) |
| 15 | + |
| 16 | +open Common |
| 17 | + |
| 18 | +open Ast_lisp |
| 19 | + |
| 20 | +module Ast = Ast_lisp |
| 21 | +module PI = Parse_info |
| 22 | + |
| 23 | +open Highlight_code |
| 24 | + |
| 25 | +module T = Parser_lisp |
| 26 | + |
| 27 | +(*****************************************************************************) |
| 28 | +(* Prelude *) |
| 29 | +(*****************************************************************************) |
| 30 | + |
| 31 | +(*****************************************************************************) |
| 32 | +(* Helpers when have global analysis information *) |
| 33 | +(*****************************************************************************) |
| 34 | + |
| 35 | +let fake_no_def2 = NoUse |
| 36 | +let fake_no_use2 = (NoInfoPlace, UniqueDef, MultiUse) |
| 37 | + |
| 38 | +(*****************************************************************************) |
| 39 | +(* Code highlighter *) |
| 40 | +(*****************************************************************************) |
| 41 | + |
| 42 | +let visit_toplevel ~tag_hook prefs (toplevel, toks) = |
| 43 | + |
| 44 | + let already_tagged = Hashtbl.create 101 in |
| 45 | + let tag = (fun ii categ -> |
| 46 | + tag_hook ii categ; |
| 47 | + Hashtbl.add already_tagged ii true |
| 48 | + ) |
| 49 | + in |
| 50 | + |
| 51 | + (* -------------------------------------------------------------------- *) |
| 52 | + (* ast phase 1 *) |
| 53 | + (* -------------------------------------------------------------------- *) |
| 54 | + |
| 55 | + (* -------------------------------------------------------------------- *) |
| 56 | + (* toks phase 1 *) |
| 57 | + (* -------------------------------------------------------------------- *) |
| 58 | + |
| 59 | + (* |
| 60 | + * note: all TCommentSpace are filtered in xs so easier to write |
| 61 | + * rules (but regular comments are kept as well as newlines). |
| 62 | + *) |
| 63 | + |
| 64 | + let rec aux_toks xs = |
| 65 | + match xs with |
| 66 | + | [] -> () |
| 67 | + |
| 68 | + (* a little bit pad specific *) |
| 69 | + | T.TComment(ii) |
| 70 | + ::T.TCommentNewline (ii2) |
| 71 | + ::T.TComment(ii3) |
| 72 | + ::T.TCommentNewline (ii4) |
| 73 | + ::T.TComment(ii5) |
| 74 | + ::xs -> |
| 75 | + |
| 76 | + let s = PI.str_of_info ii in |
| 77 | + let s5 = PI.str_of_info ii5 in |
| 78 | + (match () with |
| 79 | + | _ when s =~ ".*\\*\\*\\*\\*" && s5 =~ ".*\\*\\*\\*\\*" -> |
| 80 | + tag ii CommentEstet; |
| 81 | + tag ii5 CommentEstet; |
| 82 | + tag ii3 CommentSection1 |
| 83 | + | _ when s =~ ".*------" && s5 =~ ".*------" -> |
| 84 | + tag ii CommentEstet; |
| 85 | + tag ii5 CommentEstet; |
| 86 | + tag ii3 CommentSection2 |
| 87 | + | _ when s =~ ".*####" && s5 =~ ".*####" -> |
| 88 | + tag ii CommentEstet; |
| 89 | + tag ii5 CommentEstet; |
| 90 | + tag ii3 CommentSection0 |
| 91 | + | _ -> |
| 92 | + () |
| 93 | + ); |
| 94 | + aux_toks (T.TComment ii3::T.TCommentNewline ii4::T.TComment ii5::xs) |
| 95 | + |
| 96 | + |
| 97 | + | x::xs -> |
| 98 | + aux_toks xs |
| 99 | + in |
| 100 | + let toks' = toks +> Common.exclude (function |
| 101 | + | T.TCommentSpace _ -> true |
| 102 | + | _ -> false |
| 103 | + ) |
| 104 | + in |
| 105 | + aux_toks toks'; |
| 106 | + |
| 107 | + (* -------------------------------------------------------------------- *) |
| 108 | + (* toks phase 2 *) |
| 109 | + |
| 110 | + toks +> List.iter (fun tok -> |
| 111 | + match tok with |
| 112 | + | T.TComment ii -> |
| 113 | + if not (Hashtbl.mem already_tagged ii) |
| 114 | + then |
| 115 | + (* a little bit syncweb specific *) |
| 116 | + let s = PI.str_of_info ii in |
| 117 | + if s =~ "(\\*[sex]:" (* yep, s e x are the syncweb markers *) |
| 118 | + then tag ii CommentSyncweb |
| 119 | + else tag ii Comment |
| 120 | + |
| 121 | + | T.TCommentNewline ii | T.TCommentSpace ii |
| 122 | + -> () |
| 123 | + |
| 124 | + | T.TUnknown ii |
| 125 | + -> tag ii Error |
| 126 | + | T.EOF ii |
| 127 | + -> () |
| 128 | + |
| 129 | + | T.TString (s,ii) -> |
| 130 | + tag ii String |
| 131 | + |
| 132 | + | T.TNumber (s,ii) -> |
| 133 | + tag ii Number |
| 134 | + |
| 135 | + | T.TIdent (s, ii) -> |
| 136 | + (match s with |
| 137 | + | "defun" |
| 138 | + -> tag ii Keyword |
| 139 | + | "setq" |
| 140 | + -> tag ii KeywordObject (* hmm not really *) |
| 141 | + |
| 142 | + | "t" -> |
| 143 | + tag ii Boolean |
| 144 | + | "nil" -> |
| 145 | + tag ii Boolean (* or Null ? *) |
| 146 | + |
| 147 | + | "cond" | "if" -> |
| 148 | + tag ii KeywordConditional |
| 149 | + |
| 150 | + | "concat" |
| 151 | + | "getenv" |
| 152 | + -> tag ii Builtin |
| 153 | + |
| 154 | + | _ -> () |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | + | T.TCBracket ii |
| 159 | + | T.TOBracket ii |
| 160 | + -> tag ii TypeVoid (* TODO *) |
| 161 | + |
| 162 | + | T.TCParen ii |
| 163 | + | T.TOParen ii |
| 164 | + -> tag ii Punctuation |
| 165 | + |
| 166 | + | T.TQuote ii -> |
| 167 | + tag ii EmbededHtml (* quote stuff is kind of like XHP after all *) |
| 168 | + |
| 169 | + | T.TAt ii |
| 170 | + | T.TComma ii |
| 171 | + | T.TBackQuote ii |
| 172 | + -> |
| 173 | + tag ii EmbededHtml (* quote stuff is kind of like XHP after all *) |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + ); |
| 178 | + |
| 179 | + (* -------------------------------------------------------------------- *) |
| 180 | + (* ast phase 2 *) |
| 181 | + |
| 182 | + () |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
0 commit comments