]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
laid the ground work for lexer integration for syntax highlighting
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 4 Dec 2017 21:22:08 +0000 (16:22 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 4 Dec 2017 21:22:08 +0000 (16:22 -0500)
Makefile
deps.mk
lib/cfg.ml
lib/colormap.ml
lib/colormap.mli [new file with mode: 0644]
lib/draw.ml
lib/draw.mli
utils [new file with mode: 0644]

index 9b1348959c50bd68ab2aa07b99a737dc6df5d654..143b17046f5a9749130127dec180bd65536a7efc 100644 (file)
--- 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 75acadc57cfc060b646086a5efb2b75e31dbe110..dab969b0eb3dbfc631c1de16dd56ece3c8ac1ab8 100644 (file)
--- 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
index 6a41fbf609f96a3797d3657fd231ad448869ab82..9a200d6660c3d04d63062d3216a4097975dd4e9e 100644 (file)
@@ -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
index ba435a22c8f388ac413744f44ba6be3cf4c4cc01..eba97205e4eb9538a2fecf4619d97157f391630a 100644 (file)
@@ -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 (file)
index 0000000..24703b4
--- /dev/null
@@ -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
index 61312234ef1516d4b6c5baa45dbea91aef4913c0..23f66ac46adf271ec5034ac2d4038329d3ffce56 100644 (file)
@@ -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
index 558ab19723333bdbd098d0f43def9ae6b2d889e4..e1847c2113e5a7ecb30dc9004f484a8876471e27 100644 (file)
@@ -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 (file)
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