From 31f0ae9771f8b34cc69d3031d4ef3ca247b16d27 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 4 Dec 2017 16:22:08 -0500 Subject: [PATCH] laid the ground work for lexer integration for syntax highlighting --- Makefile | 6 +----- deps.mk | 5 +++-- lib/cfg.ml | 5 +++-- lib/colormap.ml | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/colormap.mli | 19 ++++++++++++++++++ lib/draw.ml | 10 +++++----- lib/draw.mli | 2 +- utils | 20 +++++++++++++++++++ 8 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 lib/colormap.mli create mode 100644 utils diff --git a/Makefile b/Makefile index 9b13489..143b170 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,7 @@ LEXERS = \ LIBOBJS = \ $(LIBSRCS:.ml=.$(OBJEXT)) \ + $(LEXERS:.ml=.$(OBJEXT)) \ lib/x11_prims.o \ lib/misc_prims.o \ lib/utf8.o @@ -106,8 +107,3 @@ deps deps.mk: $(wildcard *.ml* lib/*.ml* tests/*.ml*) ocamlc $(OLDFLAGS) $(INCS) -o $@ $^ %.bin: ocamlopt $(OLDFLAGS) $(INCS) -o $@ $^ - -#.mly.ml : -# ocamlyacc $(OYACCFLAGS) -o $@ $< -#.mly.mli: -# ocamlyacc $(OYACCFLAGS) -o $@ $< diff --git a/deps.mk b/deps.mk index 75acadc..dab969b 100644 --- a/deps.mk +++ b/deps.mk @@ -5,8 +5,9 @@ lib/buf.cmx lib/buf.o : lib/rope.cmi lib/rope.cmx lib/misc.cmi lib/misc.cmx lib/ lib/buf.cmi : lib/cfg.cmo lib/cfg.cmi : lib/x11.cmi lib/cfg.ml lib/cfg.cmx lib/cfg.o lib/cfg.cmi : lib/x11.cmi lib/x11.cmx lib/cfg.ml -lib/colormap.cmo lib/colormap.cmi : lib/colormap.ml -lib/colormap.cmx lib/colormap.o lib/colormap.cmi : lib/colormap.ml +lib/colormap.cmo : lib/cfg.cmi lib/colormap.cmi lib/colormap.ml +lib/colormap.cmx lib/colormap.o : lib/cfg.cmi lib/cfg.cmx lib/colormap.cmi lib/colormap.ml +lib/colormap.cmi : lib/draw.cmo : lib/x11.cmi lib/cfg.cmi lib/buf.cmi lib/draw.cmi lib/draw.ml lib/draw.cmx lib/draw.o : lib/x11.cmi lib/x11.cmx lib/cfg.cmi lib/cfg.cmx lib/buf.cmi lib/buf.cmx lib/draw.cmi lib/draw.ml lib/draw.cmi : lib/x11.cmi lib/buf.cmi diff --git a/lib/cfg.ml b/lib/cfg.ml index 6a41fbf..9a200d6 100644 --- a/lib/cfg.ml +++ b/lib/cfg.ml @@ -88,14 +88,15 @@ module Color = struct let normal = intvar "tide.colors.syntax.normal" 5 let comment = intvar "tide.colors.syntax.comment" 3 let constant = intvar "tide.colors.syntax.constant" 14 + let keyword = intvar "tide.colors.syntax.keyword" 15 + let typedef = intvar "tide.colors.syntax.typedef" 8 + let number = intvar "tide.colors.syntax.number" 14 let boolean = intvar "tide.colors.syntax.boolean" 14 let float = intvar "tide.colors.syntax.float" 14 let string = intvar "tide.colors.syntax.string" 14 let char = intvar "tide.colors.syntax.character" 14 let preproc = intvar "tide.colors.syntax.preproc" 9 - let typedef = intvar "tide.colors.syntax.typedef" 8 - let keyword = intvar "tide.colors.syntax.keyword" 15 let statement = intvar "tide.colors.syntax.statement" 10 let procedure = intvar "tide.colors.syntax.function" 11 let variable = intvar "tide.colors.syntax.variable" 12 diff --git a/lib/colormap.ml b/lib/colormap.ml index ba435a2..eba9720 100644 --- a/lib/colormap.ml +++ b/lib/colormap.ml @@ -1,7 +1,57 @@ +open Lexing + exception Eof + type style = Normal | Comment | Constant | Keyword | Type (* String | Character | Number | Boolean | Variable | Function | Keyword | Operator | PreProcessor | Type | Statement | Special *) + +module Span = struct + type t = { start : int; stop : int; style : style } + let compare a b = + if a.stop < b.start then -1 + else if a.start > b.stop then 1 + else 0 +end + +module SpanSet = Set.Make(Span) + +type t = SpanSet.t + +type lexer = (style -> unit) -> Lexing.lexbuf -> unit + +let get_color = function +| Normal -> Cfg.Color.Syntax.normal +| Comment -> Cfg.Color.Syntax.comment +| Constant -> Cfg.Color.Syntax.constant +| Keyword -> Cfg.Color.Syntax.keyword +| Type -> Cfg.Color.Syntax.typedef + +let set_color mapref lexbuf c = + let span = Span.({ + start = (lexeme_start lexbuf); + stop = (lexeme_end lexbuf); + style = c }) + in + mapref := SpanSet.add span !mapref; + () + +let create scanfn fetchfn = + let mapref = ref SpanSet.empty in + try + let lexbuf = Lexing.from_function fetchfn in + let set_color = set_color mapref lexbuf in + while true do + scanfn set_color lexbuf + done; + !mapref + with Eof -> !mapref + +let find pos set = + let range = Span.({ start = pos; stop = pos; style = Normal }) in + match (SpanSet.find_opt range set) with + | Some r -> get_color Span.(r.style) + | None -> get_color Normal diff --git a/lib/colormap.mli b/lib/colormap.mli new file mode 100644 index 0000000..24703b4 --- /dev/null +++ b/lib/colormap.mli @@ -0,0 +1,19 @@ +open Lexing + +exception Eof + +type style = Normal | Comment | Constant | Keyword | Type + +type t + +type lexer = (style -> unit) -> Lexing.lexbuf -> unit + +val create : lexer -> (bytes -> int -> int) -> t + +(* +val from_channel : lexer -> in_channel -> t +val from_string : lexer -> string -> t +val from_function : lexer -> (bytes -> int -> int) -> t +*) + +val find : int -> t -> int diff --git a/lib/draw.ml b/lib/draw.ml index 6131223..23f66ac 100644 --- a/lib/draw.ml +++ b/lib/draw.ml @@ -51,18 +51,18 @@ module Cursor = struct let tabsz = (xoff * tabwidth) in csr.x <- (csr.startx + ((csr.x - csr.startx + tabsz) / tabsz * tabsz)) - let place_glyph csr glyph = + let place_glyph csr glyph clr = let xoff = (glyph_width glyph) in if (csr.x + xoff) > csr.width then (next_line csr); - let _ = X11.draw_glyph Cfg.Color.palette.(5) glyph (csr.x, csr.y) in + let _ = X11.draw_glyph Cfg.Color.palette.(clr) glyph (csr.x, csr.y) in csr.x <- csr.x + xoff - let draw_glyph csr c = + let draw_glyph csr c clr = match c with | 0x0A -> next_line csr | 0x0D -> () | 0x09 -> draw_tab csr - | _ -> place_glyph csr (X11.get_glyph font c) + | _ -> place_glyph csr (X11.get_glyph font c) clr let next_glyph csr c = let glyph = (X11.get_glyph font c) in @@ -102,7 +102,7 @@ let buffer csr buf off = dark_bkg (csr.width - csr.x) (csr.height - csr.y) csr; let num = ref 0 and csr = (restart csr 2 0) in let draw_rune c = - draw_glyph csr c; + draw_glyph csr c Cfg.Color.Syntax.normal; num := !num + 1; has_next_line csr in diff --git a/lib/draw.mli b/lib/draw.mli index 558ab19..e1847c2 100644 --- a/lib/draw.mli +++ b/lib/draw.mli @@ -7,7 +7,7 @@ module Cursor : sig val restart : t -> int -> int -> t val next_line : t -> unit val has_next_line : t -> bool - val draw_glyph : t -> int -> unit + val draw_glyph : t -> int -> int -> unit val next_glyph : t -> int -> bool end diff --git a/utils b/utils new file mode 100644 index 0000000..3656833 --- /dev/null +++ b/utils @@ -0,0 +1,20 @@ +# change indentation +'s/^/ /g' # increase indentation +'s/^ //g' # decrease indentation + +# change comment +'s,^\/\/[ ]\?,,g' # remove C comment +'s,^#[ ]\?,,g' # remove script comment + +'s,^,\/\/,g' # add C comment +'s,^,#,g' # add script comment + +'s/\n\n\n+/\n\n/g' # remove redundant newlines, keep max two +'s/[ ]+$//g' # remove trailing whitespace + +'s/->/./g' # pointer to struct +'s/\./->/g' # struct to pointer + +s,//[^\n]*\n,,g # strip C // comments from selection +s,/\*.*\*/\n,,g # strip C /* */ 1-line comments from selection +t "scratch" 0 # copy selection to scratch file -- 2.52.0