]> git.mdlowis.com Git - projs/tide.git/commitdiff
fleshed out highlight engine more
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 27 Jun 2017 23:48:29 +0000 (19:48 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 27 Jun 2017 23:48:29 +0000 (19:48 -0400)
.gitignore
lib/colors.c
lib/config.c
tide-hl

index 929dd13661c46b20f6036be095f9f79e7c89412f..bec87bb117790134987ed2aa62c73f5e4699547b 100644 (file)
@@ -51,4 +51,3 @@ tests/tide
 tide
 pick
 tests/pick
-hl-cpp
index d90e19f51d0b0215ee052ce01af07f591abf2dbe..c482e1513f6a5270fcb198e3321c54bfbfa37d61 100644 (file)
@@ -15,7 +15,7 @@ static int read_byte(void);
 static int read_num(void);
 
 void colors_init(char* path) {
-    cmdspawn((char*[]){ "tide-hl", path, NULL }, &ChildIn, &ChildOut);
+    cmdspawn((char*[]){ "./tide-hl", path, NULL }, &ChildIn, &ChildOut);
 }
 
 SyntaxSpan* colors_scan(SyntaxSpan* spans, Buf* buf, size_t beg, size_t end) {
index 1feaf1225e5d8929b0e379e547b0ceda51b09e48..99719662368ee7323e70a65d6ae3e8deac7248a3 100644 (file)
@@ -41,7 +41,7 @@ struct {
     [TabWidth]     = { "tide.input.tab_width",     INTEGER, { .num = 4     } },
     [ScrollLines]  = { "tide.input.scroll_lines",  INTEGER, { .num = 4     } },
     [DblClickTime] = { "tide.input.click_time",    INTEGER, { .num = 500   } },
-    [MaxScanDist]  = { "tide.input.max_scan_dist", INTEGER, { .num = 8192  } },
+    [MaxScanDist]  = { "tide.input.max_scan_dist", INTEGER, { .num = 0     } },
 
     /* default color palette definition */
     [Color00] = { "tide.palette.00", INTEGER, { .num = 0xff002b36 } },
diff --git a/tide-hl b/tide-hl
index faefc06ba9b659f04631993ecb2acb738105f1e6..59f545e39e1bbee7b3fc656583c8fb9819b99d82 100755 (executable)
--- a/tide-hl
+++ b/tide-hl
@@ -1,7 +1,7 @@
 #!/usr/bin/env ruby
 
 if not ARGV[0] then
-  $stderr.puts "Usage: tide-hl [LANG]"
+  $stderr.puts "Usage: tide-hl [FILE]"
   exit 1
 end
 
@@ -39,16 +39,25 @@ Styles = {
     Special:      (16 << 4),
 }
 
+$language = nil
 $buf = StringIO.new
 $scan = StringScanner.new("")
 Rules = []
 
-def rule(regex, &block)
-  Rules << [regex, block]
+def languages(langmap)
+  file = ARGV[0]
+  ext  = File.extname(file).downcase
+  langmap.each do |k,v|
+    if (v.member?(ext) || v.member?(file))
+      return ($language = k)
+    end
+  end
 end
 
-def match(regex, style)
-  Rules << [regex, style]
+def language(name, &block)
+  if $language == name
+    block.call()
+  end
 end
 
 def style_range(spos, epos, style)
@@ -61,6 +70,10 @@ def style_match(s)
   style_range($scan.pos - $scan.matched_size, $scan.pos, s)
 end
 
+def rule(regex, &block)
+  Rules << [regex, block]
+end
+
 def range(start=nil, stop=nil, color=nil)
   rule start do
     beg = $scan.pos - $scan.matched_size
@@ -71,38 +84,58 @@ def range(start=nil, stop=nil, color=nil)
   end
 end
 
-def language(name, &block)
-  block.call()
+def match(regex, style)
+  Rules << [regex, style]
+end
+
+def match_sets(regex, setmap)
+  rule regex do |m|
+    setmap.each do |k,v|
+      if v.member?(m)
+        style_match(k)
+        break;
+      end
+    end
+  end
 end
 
 #-------------------------------------------------------------------------------
 
+languages({
+  "Ruby" => Set.new(%w[Rakefile Rakefile.rb .rb]),
+  "C"    => Set.new(%w[.c .h .cpp .hpp .cc .c++ .cxx]),
+})
+
 language "C" do
-  Types = Set.new %w[
+  types = Set.new %w[
     bool short int long unsigned signed char size_t
     void extern static inline struct typedef union
     int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t
   ]
 
-  Control = Set.new %w[
+  keywords = Set.new %w[
     goto break return continue asm case default if else switch while for do
   ]
 
+  constants = Set.new %w[
+    NULL true false
+  ]
+
   range start=/\/\*/, stop=/\*\//, :Comment
-  match(/\/\/.*$/, :Comment)
-  match(/#\s*\w+/, :PreProcessor)
-  match(/"(\\"|[^"\n])*"/, :String)
-  match(/'(\\.|[^'\n\\])'/, :Char)
-
-  # Highlight identifiers
-  rule /[a-zA-Z_][0-9a-zA-Z_]*/ do |match|
-    if Types.member? match
-      style_match(:Type)
-    elsif Control.member? match
-      style_match(:Keyword)
-    end
-  end
-  match(/[+-]?[0-9]+[ulUL]*/, :Number)
+  match /\/\/.*$/, :Comment
+  match /#\s*\w+/, :PreProcessor
+  match /"(\\"|[^"\n])*"/, :String
+  match /'(\\.|[^'\n\\])'/, :Char
+
+  match_sets /[a-zA-Z_][0-9a-zA-Z_]*/,
+    :Type => types, :Keyword => keywords, :Constant => constants
+
+  match /0x[0-9a-fA-F]+[ulUL]*/, :Number
+  match /[0-9]+[ulUL]*/, :Number
+end
+
+language "Ruby" do
+  match /#.*$/, :Comment
 end
 
 #-------------------------------------------------------------------------------