package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file highlight_nw.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
(* Yoann Padioleau
 *
 * Copyright (C) 2010 Facebook
 * Copyright (C) 2015, 2018 Yoann Padioleau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)
open Common

module T = Lexer_nw
module TH = Token_helpers_nw
module F = Ast_fuzzy

open Highlight_code

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

let span_newline xs = xs +> Common2.split_when (function 
  | T.TCommentNewline _ -> true | _ -> false)

let tag_all_tok_with ~tag categ xs = 
  xs |> List.iter (fun tok ->
    let info = TH.info_of_tok tok in
    tag info categ
  )

let tag_all_tok_trees_with ~tag categ trees =
  let xs = F.toks_of_trees trees in
  xs |> List.iter (fun info -> tag info categ)

(*****************************************************************************)
(* Code highlighter *)
(*****************************************************************************)

(* The idea of the code below is to visit the program either through its
 * AST or its list of tokens. The tokens are easier for tagging keywords,
 * number and basic entities. The AST is better for other things.
 *)
let visit_program ~tag_hook _prefs (trees, toks) =
  let already_tagged = Hashtbl.create 101 in
  let tag = (fun ii categ ->
    tag_hook ii categ;
    Hashtbl.replace already_tagged ii true
  )
  in

  (* -------------------------------------------------------------------- *)
  (* toks phase 1 (sequence of tokens) *)
  (* -------------------------------------------------------------------- *)

  let rec aux_toks xs = 
    match xs with
    | [] -> ()

    (* pad-specific: *)
    |   T.TComment(ii)
      ::T.TCommentNewline (_ii2)
      ::T.TComment(ii3)
      ::T.TCommentNewline (_ii4)
      ::T.TComment(ii5)
      ::xs ->
        let s = Parse_info.str_of_info ii in
        let s5 =  Parse_info.str_of_info ii5 in
        (match () with
        | _ when s =~ "^%\\*\\*\\*\\*" && s5 =~ "^%\\*\\*\\*\\*" ->
          tag ii CommentEstet; tag ii5 CommentEstet;
          tag ii3 CommentSection0
        | _ when s =~ "^%------" && s5 =~ "^%------" ->
          tag ii CommentEstet; tag ii5 CommentEstet;
          tag ii3 CommentSection1
        | _ when s =~ "^%####" && s5 =~ "^%####" ->
          tag ii CommentEstet; tag ii5 CommentEstet;
          tag ii3 CommentSection2
        | _ ->
            ()
        );
        aux_toks xs

    (* syncweb-specific: *)
    | T.TSymbol("#", _ii)::T.TWord("include", ii2)::xs ->
        tag ii2 Include;
        aux_toks xs

    (* specific to texinfo *)
    | T.TSymbol("@", _)::T.TWord(s, ii)::xs ->
           let categ_opt = 
             (match s with
             | "title" -> Some CommentSection0
             | "chapter" -> Some CommentSection0
             | "section" -> Some CommentSection1
             | "subsection" -> Some CommentSection2
             | "subsubsection" -> Some CommentSection3
             | "c" -> Some Comment
             (* don't want to polluate my view with indexing "aspect" *)
             | "cindex" -> 
                 tag ii Comment;
                 Some Comment
             | _ -> None
             )
           in
           (match categ_opt with
           | None -> 
               tag ii Keyword;
               aux_toks xs
           | Some categ ->
               let (before, _, _) = span_newline xs in
               tag_all_tok_with ~tag categ before;
               (* repass on tokens, in case there are nested tex commands *)
               aux_toks xs
           )

    (* specific to web TeX source: ex: @* \[24] Getting the next token. *)
    |    T.TSymbol("@*", _)
      :: T.TCommentSpace _
      :: T.TSymbol("\\", _)
      :: T.TSymbol("[", ii1)
      :: T.TNumber(_, iinum)
      :: T.TSymbol("]", ii2)
      :: T.TCommentSpace _
      :: xs 
      ->
       let (before, _, _) = span_newline xs in
       [ii1;iinum;ii2] |> List.iter (fun ii -> tag ii CommentSection0);
       tag_all_tok_with ~tag CommentSection0 before;
       (* repass on tokens, in case there are nested tex commands *)
       aux_toks xs
    (* less: \item TWord => purple? or maybe purple until end of line *)

    | _x::xs ->
        aux_toks xs
  in
  let toks' = toks |> Common.exclude (function
    (* needed ? *)
    (* | T.TCommentSpace _ -> true *)
    | _ -> false
  )
  in
  aux_toks toks';

  (* -------------------------------------------------------------------- *)
  (* AST phase 1 *) 
  (* -------------------------------------------------------------------- *)
  trees |> F.mk_visitor { F.default_visitor with
    F.ktrees = (fun (k, _v) trees ->
      match trees with
      (* \xxx{...} *)
      | F.Tok (s, _)::F.Braces (_, brace_trees, _)::_ ->
        let categ_opt = 
          match s with

          | ("\\chapter" | "\\chapter*") -> Some CommentSection0
          | "\\section" -> Some CommentSection1
          | "\\subsection" -> Some CommentSection2
          | "\\subsubsection" -> Some CommentSection3

          | "\\label" -> Some (Label Def)
          | "\\ref" -> Some (Label Def)
          | "\\cite" -> Some (Label Def)
          (* principia-specific: *)
          | "\\book" -> Some (Label Def)

          | "\\begin" | "\\end" -> Some KeywordExn (* TODO *)
          | "\\input" | "\\usepackage" | "\\bibliography" -> 
            Some IncludeFilePath
          | "\\url" | "\\furl" -> Some EmbededUrl

          | _ when s =~ "^\\" -> Some (Parameter Use)
          | _ -> None
        in
       categ_opt |> Common.do_option (fun categ -> 
         tag_all_tok_trees_with ~tag categ brace_trees;
       );
       k trees

      (* \xxx[...]{...} *)
      | F.Tok (s, _)::F.Bracket(_,params,_)::F.Braces (_,body, _)::_ ->
         if s =~ "^\\" then begin
           tag_all_tok_trees_with ~tag (Parameter Use) params;
           tag_all_tok_trees_with ~tag (Parameter Use) body;
         end;
         k trees
      (* {...}{...} *)
      | F.Braces (_, brace_trees1,_)::F.Braces (_, brace_trees2,_)::_ ->
         tag_all_tok_trees_with ~tag (Parameter Use) brace_trees1;
         tag_all_tok_trees_with ~tag (Parameter Use) brace_trees2;
         k trees

      (* {\xxx ... } *)
      | F.Braces (_, (F.Tok (s, _)::brace_trees),_)::_ ->
        let categ_opt = 
          match s with
          | "\\em" -> Some CommentWordImportantNotion
          | _ -> None
        in
        categ_opt |> Common.do_option (fun categ -> 
          tag_all_tok_trees_with ~tag categ brace_trees;
        );
        k trees

      | _ -> k trees
    );
  };

  (* -------------------------------------------------------------------- *)
  (* toks phase 2 (individual tokens) *)
  (* -------------------------------------------------------------------- *)

  toks |> List.iter (fun tok -> 
    match tok with
    | T.TComment ii ->
        if not (Hashtbl.mem already_tagged ii)
        then 
         let s = Parse_info.str_of_info ii |> String.lowercase_ascii in
         (match s with
         | _ when s =~ "^%todo:" -> tag ii BadSmell
         | _ -> tag ii CommentImportance0
         )
    | T.TCommentSpace _ii -> ()
    | T.TCommentNewline _ii -> ()

    | T.TCommand (s, ii) -> 
        let categ = 
          (match s with
          | s when s =~ "^if" -> KeywordConditional
          | s when s =~ ".*true$" -> Boolean
          | s when s =~ ".*false$" -> Boolean

          | "fi" -> KeywordConditional
          | "input" | "usepackage" -> Include
          | "appendix" -> CommentSection0

          | _ -> Keyword
          )
        in
        tag ii categ
      
    | T.TWord (s, ii) ->
        (match s with
        | "TODO" -> tag ii BadSmell
        | _ -> ()
        )

    (* noweb-specific: (obviously) *)
    | T.TBeginNowebChunk ii
    | T.TEndNowebChunk ii
      ->
        tag ii KeywordExn (* TODO *)

    | T.TNowebChunkStr (_, ii) ->
        tag ii EmbededCode
    | T.TNowebCode (_, ii) ->
        tag ii EmbededCode
    | T.TNowebCodeLink (_, ii) ->
        tag ii (Label Def) (* TODO *)

    | T.TNowebChunkName (_, ii) ->
        tag ii KeywordObject (* TODO *)

    | T.TBeginVerbatim ii | T.TEndVerbatim ii -> tag ii Keyword

    | T.TVerbatimLine (_, ii) ->
        tag ii Verbatim

    (* syncweb-specific: *)
    | T.TFootnote (c, ii) ->
        (match c with
        | 't' -> tag ii BadSmell
        | 'n' -> tag ii Comment
        | 'l' -> tag ii CommentImportance1
        | _ -> failwith (spf "syncweb \\x special macro not recognized:%c" c)
        )

    | T.TNumber (_, ii) | T.TUnit (_, ii) -> 
        if not (Hashtbl.mem already_tagged ii)
        then tag ii Number


    | T.TSymbol (s, ii) -> 
      (match s with
      | "&" | "\\\\" ->
        tag ii Punctuation
      | _ -> ()
      )

    | T.TOBrace ii | T.TCBrace ii 
    | T.TOBracket ii | T.TCBracket ii
      ->  tag ii Punctuation

    | T.TUnknown ii -> tag ii Error
    | T.EOF _ii-> ()

  );

  (* -------------------------------------------------------------------- *)
  (* AST phase 2 *)  

  ()
OCaml

Innovation. Community. Security.