From: Michael D. Lowis Date: Tue, 27 Jun 2017 23:48:29 +0000 (-0400) Subject: fleshed out highlight engine more X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=60a0f845a52973e5d0aae9620355ca8025d647d6;p=projs%2Ftide.git fleshed out highlight engine more --- diff --git a/.gitignore b/.gitignore index 929dd13..bec87bb 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,3 @@ tests/tide tide pick tests/pick -hl-cpp diff --git a/lib/colors.c b/lib/colors.c index d90e19f..c482e15 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -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) { diff --git a/lib/config.c b/lib/config.c index 1feaf12..9971966 100644 --- a/lib/config.c +++ b/lib/config.c @@ -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 faefc06..59f545e 100755 --- 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 #-------------------------------------------------------------------------------