exception Eof
-type style = Normal | Comment | Constant | Keyword | Type | PreProcessor
-
-module Span = struct
- type t = { start : int; stop : int; style : int }
- let compare a b =
- if a.stop < b.start then -1
- else if a.start > b.stop then 1
- else 0
-end
+type style =
+ | Normal | Comment | Constant | String | Char | Number | Boolean | Float
+ | Variable | Function | Keyword | Operator | PreProcessor | Type | Statement
+ | Special
type ctx = {
lbuf : lexbuf;
}
let get_color = function
- | Normal -> 0
- | Comment -> 1
- | Constant -> 2
- | Keyword -> 3
- | Type -> 4
- | PreProcessor -> 5
+ | Normal -> 1 * 16
+ | Comment -> 2 * 16
+ | Constant -> 3 * 16
+ | String -> 4 * 16
+ | Char -> 5 * 16
+ | Number -> 6 * 16
+ | Boolean -> 7 * 16
+ | Float -> 8 * 16
+ | Variable -> 9 * 16
+ | Function -> 10 * 16
+ | Keyword -> 11 * 16
+ | Operator -> 12 * 16
+ | PreProcessor -> 13 * 16
+ | Type -> 14 * 16
+ | Statement -> 15 * 16
+ | Special -> 16 * 16
let set_color ctx clr =
Printf.printf "%d,%d,%d\n"
- (lexeme_start ctx.lbuf) ((lexeme_end ctx.lbuf) - 1) (get_color clr)
+ (lexeme_start ctx.lbuf) (lexeme_end ctx.lbuf) (get_color clr)
let range_start ctx =
ctx.pos <- (lexeme_start ctx.lbuf)
let range_stop ctx clr =
Printf.printf "%d,%d,%d\n"
- ctx.pos ((lexeme_end ctx.lbuf) - 1) (get_color clr)
+ ctx.pos (lexeme_end ctx.lbuf) (get_color clr)
rule scan ctx = parse
| "/*" { range_start ctx; comment ctx lexbuf }
| ln_cmt { set_color ctx Comment }
- | number { set_color ctx Constant }
- | character { set_color ctx Constant }
- | string { set_color ctx Constant }
+ | number { set_color ctx Number }
+ | character { set_color ctx Char }
+ | string { set_color ctx String }
| const { set_color ctx Constant }
| keyword { set_color ctx Keyword }
| typedef { set_color ctx Type }
rule scan ctx = parse
| "(*" { range_start ctx; comment ctx lexbuf }
- | number { set_color ctx Constant }
- | character { set_color ctx Constant }
- | string { set_color ctx Constant }
+ | number { set_color ctx Number }
+ | character { set_color ctx Char }
+ | string { set_color ctx String }
| const { set_color ctx Constant }
| keyword { set_color ctx Keyword }
| typedef { set_color ctx Type }
rule scan ctx = parse
| ln_cmt { set_color ctx Comment }
- | number { set_color ctx Constant }
- | string { set_color ctx Constant }
+ | number { set_color ctx Number }
+ | string { set_color ctx String }
| keyword { set_color ctx Keyword }
| typedef { set_color ctx Type }
| identifier { (* skip *) }
-
type filetype = {
syntax : Colormap.ctx -> Lexing.lexbuf -> unit;
names : string list;
in
(List.find_opt match_ftype filetypes)
-let scan_string lexfn string =
+let rec scan_string lexfn string =
let lbuf = Lexing.from_string string in
let ctx = Colormap.({ lbuf = lbuf; pos = 0; }) in
try while true do lexfn ctx lbuf done
with Colormap.Eof ->
Printf.printf "0,0,0\n";
- flush stdout
+ flush stdout;
+ scan_input lexfn
-let rec scan_input lexfn =
- try scan_string lexfn (really_input_string stdin (read_int ())) with
+and scan_input lexfn =
+ try
+ scan_string lexfn (really_input_string stdin (read_int ()));
+ with
| Failure _ -> scan_input lexfn
| End_of_file -> ()