From: Michael D. Lowis Date: Fri, 8 Dec 2017 20:10:33 +0000 (-0500) Subject: fleshed out highlighter rules for C X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=46624f7167f63bf54746a871579433fff52e8763;p=archive%2Ftide-ocaml.git fleshed out highlighter rules for C --- diff --git a/lib/cfg.ml b/lib/cfg.ml index 9a200d6..a107b57 100644 --- a/lib/cfg.ml +++ b/lib/cfg.ml @@ -27,8 +27,7 @@ let cmd_tags = strvar "tide.ui.font" "Quit Undo Redo Cut Copy Paste | Send Find " (* font settings *) -let font = strvar "tide.ui.tags.edit" - "Liberation Sans:size=11:antialias=true:autohint=true" +let font = strvar "tide.ui.tags.edit" "Verdana:size=11" let line_spacing = intvar "tide.ui.line_spacing" 1 (* user interface related options *) diff --git a/lib/colormap.ml b/lib/colormap.ml index 0169632..4fc7f2b 100644 --- a/lib/colormap.ml +++ b/lib/colormap.ml @@ -2,7 +2,7 @@ open Lexing exception Eof -type style = Normal | Comment | Constant | Keyword | Type +type style = Normal | Comment | Constant | Keyword | Type | PreProcessor (* String | Character | Number | Boolean | Variable | Function | Keyword | Operator | PreProcessor | Type @@ -29,6 +29,7 @@ let get_color = function | Constant -> Cfg.Color.Syntax.constant | Keyword -> Cfg.Color.Syntax.keyword | Type -> Cfg.Color.Syntax.typedef +| PreProcessor -> Cfg.Color.Syntax.preproc let set_color mapref lexbuf c = let span = Span.({ @@ -49,13 +50,7 @@ let make scanfn fetchfn = scanfn set_color lexbuf done; !mapref - with Eof -> - Printf.printf "%b\n" @@ SpanSet.is_empty !mapref; - SpanSet.iter - (fun x -> Printf.printf "%d-%d\n" x.start x.stop) - !mapref; - print_endline ""; - !mapref + with Eof -> !mapref let empty = SpanSet.empty diff --git a/lib/colormap.mli b/lib/colormap.mli index d691282..696dce2 100644 --- a/lib/colormap.mli +++ b/lib/colormap.mli @@ -2,7 +2,7 @@ open Lexing exception Eof -type style = Normal | Comment | Constant | Keyword | Type +type style = Normal | Comment | Constant | Keyword | Type | PreProcessor type t diff --git a/lib/lexers/lex_cpp.mll b/lib/lexers/lex_cpp.mll index 158851e..3029efe 100644 --- a/lib/lexers/lex_cpp.mll +++ b/lib/lexers/lex_cpp.mll @@ -1,11 +1,32 @@ { open Colormap } -(* Line and Block Comments *) -let ln_cmt = "//" [^ '\r' '\n']* -let blk_cmt = "/*" _* "*/" +let oct = ['0'-'9'] +let dec = ['0'-'9'] +let hex = ['0'-'9' 'a'-'f' 'A'-'F'] +let exp = ['e''E'] ['+''-']? (dec)+ + +let alpha = ['a'-'z' 'A'-'Z'] +let alpha_ = (alpha | '_') +let alnum = (alpha | dec) +let alnum_ = (alpha_ | dec) + +let fstyle = ['f' 'F' 'l' 'L'] +let istyle = ['u' 'U' 'l' 'L'] + +let ln_cmt = "//" [^ '\n']* let character = "'" ([^'\\' '\''] | '\\' _) "'" let string = '"' ([^'\\' '"'] | '\\' _)* ['"' '\n'] let identifier = ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']* +let preprocess = "#" [' ' '\t']* ['a'-'z' 'A'-'Z' '_']+ +let sys_incl = (' '|'\t')* '<' [^ '\n' '>']* '>' + +let number = ( + (dec)+ (istyle)* + | '0' ['x''X'] (hex)+ (istyle)* + | (dec)+ (exp)? (fstyle)? + | (dec)* '.' (dec)+ (exp)? (fstyle)? + | (dec)+ '.' (dec)* (exp)? (fstyle)? +) let const = "true" | "false" | "NULL" @@ -19,15 +40,25 @@ let typedef = "bool" | "short" | "int" | "long" | "unsigned" | "signed" | "char" | "float" | "double" rule scan color = parse - | ln_cmt { color Comment } -(* | blk_cmt { color Comment } *) - | character { color Constant } - | string { color Constant } + | "/*" { color Comment; comment color lexbuf } + | ln_cmt { color Comment } + | number { color Constant } + | character { color Constant } + | string { color Constant } + | const { color Constant } + | keyword { color Keyword } + | typedef { color Type } + | preprocess { color PreProcessor; preproc color lexbuf } + | identifier { (* skip *) } + | _ { scan color lexbuf } + | eof { raise Eof } - | const { color Constant } - | keyword { color Keyword } - | typedef { color Type } +and comment color = parse + | "*/" { color Comment } + | _ { comment color lexbuf } + | eof { raise Eof } - | identifier { (* skip *) } +and preproc color = parse + | sys_incl { color Constant } | _ { (* skip *) } | eof { raise Eof }