From 2e0e206951adefe71ef559a2a42b23901f5c4531 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 2 Jul 2018 20:59:12 +1000 Subject: [PATCH] Make recurse regex opt-in with a -recurse switch instead of opt-out --- doc/pages/changelog.asciidoc | 4 ++- doc/pages/highlighters.asciidoc | 16 +++++----- rc/base/css.kak | 8 ++--- rc/base/d.kak | 20 ++++++------- rc/base/fish.kak | 6 ++-- rc/base/gas.kak | 8 ++--- rc/base/go.kak | 10 +++---- rc/base/haskell.kak | 10 +++---- rc/base/html.kak | 12 ++++---- rc/base/ini.kak | 2 +- rc/base/java.kak | 6 ++-- rc/base/javascript.kak | 28 ++++++++--------- rc/base/json.kak | 2 +- rc/base/julia.kak | 4 +-- rc/base/lisp.kak | 4 +-- rc/base/lua.kak | 10 +++---- rc/base/markdown.kak | 6 ++-- rc/base/ocaml.kak | 4 +-- rc/base/perl.kak | 8 ++--- rc/base/restructuredtext.kak | 4 +-- rc/base/ruby.kak | 30 +++++++++---------- rc/base/rust.kak | 6 ++-- rc/base/scala.kak | 8 ++--- rc/base/sql.kak | 10 +++---- rc/base/swift.kak | 6 ++-- rc/base/yaml.kak | 6 ++-- rc/core/c-family.kak | 12 ++++---- rc/core/kakrc.kak | 22 +++++++------- rc/core/makefile.kak | 4 +-- rc/core/python.kak | 12 ++++---- rc/core/sh.kak | 8 ++--- rc/extra/cabal.kak | 4 +-- rc/extra/cmake.kak | 8 ++--- rc/extra/coffee.kak | 20 ++++++------- rc/extra/cucumber.kak | 4 +-- rc/extra/dockerfile.kak | 6 ++-- rc/extra/elixir.kak | 10 +++---- rc/extra/elm.kak | 6 ++-- rc/extra/haml.kak | 10 +++---- rc/extra/hbs.kak | 6 ++-- rc/extra/i3.kak | 8 ++--- rc/extra/just.kak | 10 +++---- rc/extra/kickstart.kak | 10 +++---- rc/extra/latex.kak | 2 +- rc/extra/moon.kak | 8 ++--- rc/extra/nim.kak | 6 ++-- rc/extra/php.kak | 16 +++++----- rc/extra/pony.kak | 8 ++--- rc/extra/pug.kak | 16 +++++----- rc/extra/ragel.kak | 6 ++-- rc/extra/sass.kak | 6 ++-- rc/extra/scss.kak | 2 +- rc/extra/toml.kak | 10 +++---- rc/extra/tupfile.kak | 4 +-- src/highlighters.cc | 14 +++++---- test/highlight/regions-recurse/rc | 2 +- test/highlight/regions/rc | 4 +-- .../1195-infinite-loop-in-regex-matching/rc | 2 +- 58 files changed, 256 insertions(+), 248 deletions(-) diff --git a/doc/pages/changelog.asciidoc b/doc/pages/changelog.asciidoc index 264019e5c..8c9e7c9c0 100644 --- a/doc/pages/changelog.asciidoc +++ b/doc/pages/changelog.asciidoc @@ -43,9 +43,11 @@ change to make Kakoune command model cleaner and more robust. ------------------------------------------------------------------ add-highlighter / regions add-highlighter // region \ - + ------------------------------------------------------------------ + The recursion regex is opt-in through a `-recurse ` flag. + They also are not necessarily groups anymore, a region can directly apply any other highlighter diff --git a/doc/pages/highlighters.asciidoc b/doc/pages/highlighters.asciidoc index 34bab82ed..d9c1a70b4 100644 --- a/doc/pages/highlighters.asciidoc +++ b/doc/pages/highlighters.asciidoc @@ -188,7 +188,8 @@ Individual region definitions can then be added to that highlighter ---------------------------------------------------- add-highlighter // region \ - ... + [-match-capture] [-recurse ] \ + ... ---------------------------------------------------- *opening*:: @@ -199,7 +200,8 @@ add-highlighter // region \ *recurse*:: regex that defines the text that matches recursively an end token - into the region + into the region, every match of *recurse* will consume a following + match of *end*, preventing it from closing the region. *type* and *params*:: An highlighter type, and associated params, as they would be passed @@ -224,9 +226,9 @@ accepts nested braces scopes ('{ ... }') so the following string is valid: This region can be defined with: ------------------------- -shell_expand %sh\{ \} \{ ------------------------- +--------------------------------- +shell_expand -recurse \{ %sh\{ \} +--------------------------------- Regions are matched using the left-most rule: the left-most region opening starts a new region. When a region closes, the closest next opening start @@ -247,8 +249,8 @@ highlighter as root: ----------------------------------------------------------------- add-highlighter / regions -add-highlighter //string '"' '"' '' fill string -add-highlighter //comment '//' '$' '' fill comment +add-highlighter //string '"' '"' fill string +add-highlighter //comment '//' '$' fill comment add-highlighter //code default-region group add-highlighter //code/variable regex ... add-highlighter //code/function regex ... diff --git a/rc/base/css.kak b/rc/base/css.kak index c7669da47..79bfc69ed 100644 --- a/rc/base/css.kak +++ b/rc/base/css.kak @@ -13,12 +13,12 @@ hook global BufCreate .*[.](css) %{ add-highlighter shared/css regions add-highlighter shared/css/selector default-region group -add-highlighter shared/css/declaration region [{] [}] '' regions -add-highlighter shared/css/comment region /[*] [*]/ '' fill comment +add-highlighter shared/css/declaration region [{] [}] regions +add-highlighter shared/css/comment region /[*] [*]/ fill comment add-highlighter shared/css/declaration/base default-region group -add-highlighter shared/css/declaration/double_string region '"' (??@\\\^|~=]|$) $ '' fill comment +add-highlighter shared/haskell/string region (??@\\\^|~=]|$) $ fill comment add-highlighter shared/haskell/code/ regex (? '' fill comment -add-highlighter shared/html/tag region < > '' regions -add-highlighter shared/html/style region \K (?=) '' ref css -add-highlighter shared/html/script region \K (?=) '' ref javascript +add-highlighter shared/html/comment region fill comment +add-highlighter shared/html/tag region < > regions +add-highlighter shared/html/style region \K (?=) ref css +add-highlighter shared/html/script region \K (?=) ref javascript add-highlighter shared/html/tag/base default-region group -add-highlighter shared/html/tag/ region '"' (?])(?!>\()) (|/>) (?])(?!>\()) (|/>) regions + add-highlighter "shared/%arg{1}/division" region '[\w\)\]]\K(/|(\h+/\h+))' '(?=\w)' group # Help Kakoune to better detect /…/ literals # Regular expression flags are: g → global match, i → ignore case, m → multi-lines, u → unicode, y → sticky # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp @@ -70,14 +70,14 @@ define-command -hidden init-javascript-filetype -params 1 %~ # We inline a small XML highlighter here since it anyway need to recurse back up to the starting highlighter. # To make things simple we assume that jsx is always enabled. - add-highlighter "shared/%arg{1}/jsx/tag" region <(?=[/a-zA-Z]) (? < regions - add-highlighter "shared/%arg{1}/jsx/expr" region \{ \} \{ ref %arg{1} + add-highlighter "shared/%arg{1}/jsx/tag" region -recurse < <(?=[/a-zA-Z]) (? regions + add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{ \} ref %arg{1} add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group - add-highlighter "shared/%arg{1}/jsx/tag/double_string" region =\K" (?|<=|=|>=) 0:operator diff --git a/rc/base/lua.kak b/rc/base/lua.kak index 6a20fc56e..13e0459f5 100644 --- a/rc/base/lua.kak +++ b/rc/base/lua.kak @@ -13,11 +13,11 @@ hook global BufCreate .*[.](lua) %{ add-highlighter shared/lua regions add-highlighter shared/lua/code default-region group -add-highlighter shared/lua/double_string region '"' (? < fill meta -add-highlighter shared/ruby/heredoc region '<<[-~]?(\w+)' '^\h*(\w+)$' '' fill string -add-highlighter shared/ruby/division region '[\w\)\]](/|(\h+/\h+))' '\w' '' group # Help Kakoune to better detect /…/ literals +add-highlighter shared/ruby/double_string region '"' (? fill meta +add-highlighter shared/ruby/heredoc region '<<[-~]?(\w+)' '^\h*(\w+)$' fill string +add-highlighter shared/ruby/division region '[\w\)\]](/|(\h+/\h+))' '\w' group # Help Kakoune to better detect /…/ literals # Regular expression flags are: i → ignore case, m → multi-lines, o → only interpolate #{} blocks once, x → extended mode (ignore white spaces) # Literals are: i → array of symbols, q → string, r → regular expression, s → symbol, w → array of words, x → capture shell result add-highlighter shared/ruby/double_string/ default-region fill string -add-highlighter shared/ruby/double_string/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/double_string/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/backtick/ default-region fill meta -add-highlighter shared/ruby/backtick/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/backtick/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/regex/ default-region fill meta -add-highlighter shared/ruby/regex/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/regex/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/code/ regex \b([A-Za-z]\w*:(?!:))|([$@][A-Za-z]\w*)|((?' '<' ref sh -add-highlighter shared/kakrc/shell5 region '(^|\h)\K-shell-(completion|candidates)\h+%\{' '\}' '\{' ref sh -add-highlighter shared/kakrc/shell6 region '(^|\h)\K-shell-(completion|candidates)\h+%\(' '\)' '\(' ref sh -add-highlighter shared/kakrc/shell7 region '(^|\h)\K-shell-(completion|candidates)\h+%\[' '\]' '\[' ref sh -add-highlighter shared/kakrc/shell8 region '(^|\h)\K-shell-(completion|candidates)\h+%<' '>' '<' ref sh +add-highlighter shared/kakrc/comment region (^|\h)\K# $ fill comment +add-highlighter shared/kakrc/double_string region -recurse %{(?' ref sh +add-highlighter shared/kakrc/shell5 region -recurse '\{' '(^|\h)\K-shell-(completion|candidates)\h+%\{' '\}' ref sh +add-highlighter shared/kakrc/shell6 region -recurse '\(' '(^|\h)\K-shell-(completion|candidates)\h+%\(' '\)' ref sh +add-highlighter shared/kakrc/shell7 region -recurse '\[' '(^|\h)\K-shell-(completion|candidates)\h+%\[' '\]' ref sh +add-highlighter shared/kakrc/shell8 region -recurse '<' '(^|\h)\K-shell-(completion|candidates)\h+%<' '>' ref sh evaluate-commands %sh{ # Grammar diff --git a/rc/core/makefile.kak b/rc/core/makefile.kak index bf1d79de9..ad509296a 100644 --- a/rc/core/makefile.kak +++ b/rc/core/makefile.kak @@ -11,8 +11,8 @@ hook global BufCreate .*/?[mM]akefile %{ add-highlighter shared/makefile regions add-highlighter shared/makefile/content default-region group -add-highlighter shared/makefile/comment region '#' '$' '' fill comment -add-highlighter shared/makefile/evaluate-commands region '\$\(' '\)' '\(' fill value +add-highlighter shared/makefile/comment region '#' '$' fill comment +add-highlighter shared/makefile/evaluate-commands region -recurse '\(' '\$\(' '\)' fill value add-highlighter shared/makefile/content/ regex ^[\w.%-]+\h*:\s 0:variable add-highlighter shared/makefile/content/ regex [+?:]= 0:operator diff --git a/rc/core/python.kak b/rc/core/python.kak index 1dbf7c69f..f844ba4ec 100644 --- a/rc/core/python.kak +++ b/rc/core/python.kak @@ -13,10 +13,10 @@ hook global BufCreate .*[.](py) %{ add-highlighter shared/python regions add-highlighter shared/python/code default-region group -add-highlighter shared/python/docstring region -match-capture ("""|''') ("""|''') '' regions -add-highlighter shared/python/double_string region '"' (?>> \K' '\z' '' ref python -add-highlighter shared/python/docstring/ region '\.\.\. \K' '\z' '' ref python +add-highlighter shared/python/docstring/ region '>>> \K' '\z' ref python +add-highlighter shared/python/docstring/ region '\.\.\. \K' '\z' ref python evaluate-commands %sh{ # Grammar diff --git a/rc/core/sh.kak b/rc/core/sh.kak index 8bba36eb6..caa687206 100644 --- a/rc/core/sh.kak +++ b/rc/core/sh.kak @@ -4,10 +4,10 @@ hook global BufCreate .*\.(z|ba|c|k|mk)?sh(rc|_profile)? %{ add-highlighter shared/sh regions add-highlighter shared/sh/code default-region group -add-highlighter shared/sh/double_string region %{(?]?=?)?\d+(\.\d+)+) 0:value add-highlighter shared/cabal/code/ regex \b(if|else)\b 0:keyword diff --git a/rc/extra/cmake.kak b/rc/extra/cmake.kak index 58f60f29a..d2348becc 100644 --- a/rc/extra/cmake.kak +++ b/rc/extra/cmake.kak @@ -4,14 +4,14 @@ hook global BufCreate .+\.cmake|.*/CMakeLists.txt %{ add-highlighter shared/cmake regions add-highlighter shared/cmake/code default-region group -add-highlighter shared/cmake/comment region '#' '$' '' fill comment -add-highlighter shared/cmake/argument region '\w+\h*\(\K' '(?=\))' '\(' regions +add-highlighter shared/cmake/comment region '#' '$' fill comment +add-highlighter shared/cmake/argument region -recurse '\(' '\w+\h*\(\K' '(?=\))' regions add-highlighter shared/cmake/code/ regex '\w+\h*(?=\()' 0:meta add-highlighter shared/cmake/argument/args default-region regex '\$\{\w+\}' 0:variable -add-highlighter shared/cmake/argument/quoted region '"' '(?' '' ref php +add-highlighter shared/php-file/php region '<\?(php)?' '\?>' ref php # Commands # ‾‾‾‾‾‾‾‾ diff --git a/rc/extra/pony.kak b/rc/extra/pony.kak index 853cdecb0..3c8d105dc 100644 --- a/rc/extra/pony.kak +++ b/rc/extra/pony.kak @@ -13,10 +13,10 @@ hook global BufCreate .*[.](pony) %{ add-highlighter shared/pony regions add-highlighter shared/pony/code default-region group -add-highlighter shared/pony/triple_string region '"""' '"""' '' fill string -add-highlighter shared/pony/double_string region '"' (?' 0:variable diff --git a/rc/extra/sass.kak b/rc/extra/sass.kak index fe203b5c3..6367628b9 100644 --- a/rc/extra/sass.kak +++ b/rc/extra/sass.kak @@ -13,9 +13,9 @@ hook global BufCreate .*[.](sass) %{ add-highlighter shared/sass regions add-highlighter shared/sass/code default-region group -add-highlighter shared/sass/single_string region '"' (?value.factory(parser.positionals_from(4), nullptr); - return std::make_unique(std::move(delegate), Regex{parser[0], flags}, Regex{parser[1], flags}, parser[2].empty() ? Regex{} : Regex{parser[2], flags}, match_capture); + Regex recurse; + if (auto recurse_switch = parser.get_switch("recurse")) + recurse = Regex{*recurse_switch, flags}; + + auto delegate = it->value.factory(parser.positionals_from(3), nullptr); + return std::make_unique(std::move(delegate), Regex{parser[0], flags}, Regex{parser[1], flags}, recurse, match_capture); } static std::unique_ptr create_default_region(HighlighterParameters params, Highlighter* parent) diff --git a/test/highlight/regions-recurse/rc b/test/highlight/regions-recurse/rc index 942441588..416c96694 100644 --- a/test/highlight/regions-recurse/rc +++ b/test/highlight/regions-recurse/rc @@ -1,3 +1,3 @@ add-highlighter window/regions_test regions add-highlighter window/regions_test/code default-region fill yellow -add-highlighter window/regions_test/argument region '\w+\h*\(\K' '(?=\))' '\(' fill red +add-highlighter window/regions_test/argument region -recurse '\(' '\w+\h*\(\K' '(?=\))' fill red diff --git a/test/highlight/regions/rc b/test/highlight/regions/rc index 21d886cb0..dccc2a89a 100644 --- a/test/highlight/regions/rc +++ b/test/highlight/regions/rc @@ -1,4 +1,4 @@ add-highlighter window/regions_test regions add-highlighter window/regions_test/code default-region fill yellow -add-highlighter window/regions_test/string region %{"} %{(?