From 367bb857f453e3ca07452fee136f4cb52fdff357 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 12 Oct 2020 16:47:19 -0400 Subject: [PATCH 1/6] enh(autodetect) tcl: fewer false positives on variables For languages with $ident and @ident style variables this attempts to prevent positives for $ident$ and @ident@ type expressions, which are likely something else entirely. - Improves TCL variable detection to be more explicit - Adds TCL variable detection tests --- src/languages/tcl.js | 11 +++++++---- test/markup/tcl/variables.expect.txt | 24 ++++++++++++++++++++++++ test/markup/tcl/variables.txt | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/markup/tcl/variables.expect.txt create mode 100644 test/markup/tcl/variables.txt diff --git a/src/languages/tcl.js b/src/languages/tcl.js index f1e0b0df31..c0e91ec234 100644 --- a/src/languages/tcl.js +++ b/src/languages/tcl.js @@ -5,7 +5,11 @@ Author: Radek Liska Website: https://www.tcl.tk/about/language.html */ +import * as regex from '../lib/regex.js'; + export default function(hljs) { + const ARRAY_ACCESS = '\\(([a-zA-Z0-9_]+)*\\)'; + return { name: 'Tcl', aliases: ['tk'], @@ -40,14 +44,13 @@ export default function(hljs) { }, { excludeEnd: true, + className: "variable", variants: [ { - begin: '\\$(\\{)?(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*\\(([a-zA-Z0-9_])*\\)', - end: '[^a-zA-Z0-9_\\}\\$]' + begin: '\\$(::)?[a-zA-Z_]+((::)?[a-zA-Z0-9_]+)*' + regex.optional(ARRAY_ACCESS), }, { - begin: '\\$(\\{)?(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*', - end: '(\\))?[^a-zA-Z0-9_\\}\\$]' + begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*' + regex.optional(ARRAY_ACCESS) + '}', } ] }, diff --git a/test/markup/tcl/variables.expect.txt b/test/markup/tcl/variables.expect.txt new file mode 100644 index 0000000000..3e43c6a3cb --- /dev/null +++ b/test/markup/tcl/variables.expect.txt @@ -0,0 +1,24 @@ +$name +${name} +${::name} +${::namespace::name} +${::namespace::another::name} + +$name() +$name(32) +$name(index) + +$with_underscore::oth_er::and_finally(32) + +${name()} +${name(32)} +${name(index)} + +${ns::other::name()} +${ns::other::name(32)} +${ns::other::name(index)} + + +$ns::other::name() +$ns::other::name(32) +$ns::other::name(index) diff --git a/test/markup/tcl/variables.txt b/test/markup/tcl/variables.txt new file mode 100644 index 0000000000..9b17664761 --- /dev/null +++ b/test/markup/tcl/variables.txt @@ -0,0 +1,24 @@ +$name +${name} +${::name} +${::namespace::name} +${::namespace::another::name} + +$name() +$name(32) +$name(index) + +$with_underscore::oth_er::and_finally(32) + +${name()} +${name(32)} +${name(index)} + +${ns::other::name()} +${ns::other::name(32)} +${ns::other::name(index)} + + +$ns::other::name() +$ns::other::name(32) +$ns::other::name(index) From 9cffe63bf2bfc6a8eefe314dc31e136ba7c4169b Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 14 Feb 2021 09:41:15 -0500 Subject: [PATCH 2/6] tcl: fixup --- test/markup/pgsql/dollar_strings.expect.txt | 4 ++-- test/markup/php/comments.expect.txt | 1 - test/markup/tcl/default.expect.txt | 18 +++++++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/test/markup/pgsql/dollar_strings.expect.txt b/test/markup/pgsql/dollar_strings.expect.txt index 978866a3a5..cf30a759bb 100644 --- a/test/markup/pgsql/dollar_strings.expect.txt +++ b/test/markup/pgsql/dollar_strings.expect.txt @@ -5,5 +5,5 @@ $$ $$ language sql STRICT; -SELECT sql_expression($sql$SELECT hello_world($phrase$Regina's elephant's dog$phrase$) - || $phrase$ I made a cat's meow today.$phrase$ $sql$); +SELECT sql_expression($sql$SELECT hello_world($phrase$Regina's elephant's dog$phrase$) + || $phrase$ I made a cat's meow today.$phrase$ $sql$); diff --git a/test/markup/php/comments.expect.txt b/test/markup/php/comments.expect.txt index c477b66da6..fbbcbb1468 100644 --- a/test/markup/php/comments.expect.txt +++ b/test/markup/php/comments.expect.txt @@ -17,4 +17,3 @@ function isOdd($a) { return ($a % 2) === 1; } - diff --git a/test/markup/tcl/default.expect.txt b/test/markup/tcl/default.expect.txt index 2d12c9ee86..79c23a6fd9 100644 --- a/test/markup/tcl/default.expect.txt +++ b/test/markup/tcl/default.expect.txt @@ -5,22 +5,22 @@ set ::rand 4 proc give::recursive::count {base p} { ; # 2 mandatory params - while {$p > 0} { - set result [expr $result * $base]; incr p -1 + while {$p > 0} { + set result [expr $result * $base]; incr p -1 } - return $result + return $result } set a {a}; set b "bcdef"; set lst [list "item"] -puts [llength $a$b] +puts [llength $a$b] -set ::my::tid($id) $::my::tid(def) -lappend lst $arr($idx) $::my::arr($idx) $ar(key) -lreplace ::my::tid($id) 4 4 -puts $::rand ${::rand} ${::AWESOME::component::variable} +set ::my::tid($id) $::my::tid(def) +lappend lst $arr($idx) $::my::arr($idx) $ar(key) +lreplace ::my::tid($id) 4 4 +puts $::rand ${::rand} ${::AWESOME::component::variable} puts "$x + $y is\t [expr $x + $y]" proc isprime x { - expr {$x>1 && ![regexp {^(oo+?)\1+$} [string repeat o $x]]} + expr {$x>1 && ![regexp {^(oo+?)\1+$} [string repeat o $x]]} } From 88d6ae9ec2e7033c4a124eaa53cb5c7e4a762c0a Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 2 Nov 2020 22:01:37 -0500 Subject: [PATCH 3/6] fix(tcl) Make regex UTF-8 compatible --- src/languages/tcl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/tcl.js b/src/languages/tcl.js index c0e91ec234..8002183080 100644 --- a/src/languages/tcl.js +++ b/src/languages/tcl.js @@ -50,7 +50,7 @@ export default function(hljs) { begin: '\\$(::)?[a-zA-Z_]+((::)?[a-zA-Z0-9_]+)*' + regex.optional(ARRAY_ACCESS), }, { - begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*' + regex.optional(ARRAY_ACCESS) + '}', + begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*' + regex.optional(ARRAY_ACCESS) + '\\}', } ] }, From 16ccfabfae31a9aeafa00d3857b4b4f62f650946 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 18 Dec 2020 14:10:21 -0500 Subject: [PATCH 4/6] wip maybe --- src/languages/tcl.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/languages/tcl.js b/src/languages/tcl.js index 8002183080..25f1eb34c3 100644 --- a/src/languages/tcl.js +++ b/src/languages/tcl.js @@ -10,6 +10,11 @@ import * as regex from '../lib/regex.js'; export default function(hljs) { const ARRAY_ACCESS = '\\(([a-zA-Z0-9_]+)*\\)'; + const NUMBER = { + className: 'number', + variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE] + }; + return { name: 'Tcl', aliases: ['tk'], @@ -43,14 +48,17 @@ export default function(hljs) { ] }, { - excludeEnd: true, className: "variable", variants: [ { - begin: '\\$(::)?[a-zA-Z_]+((::)?[a-zA-Z0-9_]+)*' + regex.optional(ARRAY_ACCESS), + begin: '\\$(::)?[a-zA-Z_]+((::)?[a-zA-Z0-9_]+)*', }, { - begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*' + regex.optional(ARRAY_ACCESS) + '\\}', + begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*', + end: '\\}', + contains: [ + NUMBER + ] } ] }, @@ -61,10 +69,7 @@ export default function(hljs) { hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null}) ] }, - { - className: 'number', - variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE] - } + NUMBER ] } } From 7202e842a5d9ac7ece181da6e46084c05630a411 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 14 Feb 2021 09:45:08 -0500 Subject: [PATCH 5/6] fix tests --- test/markup/tcl/default.expect.txt | 4 ++-- test/markup/tcl/variables.expect.txt | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/markup/tcl/default.expect.txt b/test/markup/tcl/default.expect.txt index 79c23a6fd9..717586adbb 100644 --- a/test/markup/tcl/default.expect.txt +++ b/test/markup/tcl/default.expect.txt @@ -14,8 +14,8 @@ set a {a}; set b "bcdef"; set lst [list "item"] puts [llength $a$b] -set ::my::tid($id) $::my::tid(def) -lappend lst $arr($idx) $::my::arr($idx) $ar(key) +set ::my::tid($id) $::my::tid(def) +lappend lst $arr($idx) $::my::arr($idx) $ar(key) lreplace ::my::tid($id) 4 4 puts $::rand ${::rand} ${::AWESOME::component::variable} diff --git a/test/markup/tcl/variables.expect.txt b/test/markup/tcl/variables.expect.txt index 3e43c6a3cb..e963e216f0 100644 --- a/test/markup/tcl/variables.expect.txt +++ b/test/markup/tcl/variables.expect.txt @@ -4,21 +4,21 @@ ${::namespace::name} ${::namespace::another::name} -$name() -$name(32) -$name(index) +$name() +$name(32) +$name(index) -$with_underscore::oth_er::and_finally(32) +$with_underscore::oth_er::and_finally(32) ${name()} -${name(32)} +${name(32)} ${name(index)} ${ns::other::name()} -${ns::other::name(32)} +${ns::other::name(32)} ${ns::other::name(index)} -$ns::other::name() -$ns::other::name(32) -$ns::other::name(index) +$ns::other::name() +$ns::other::name(32) +$ns::other::name(index) From f607072e04ecd219d4ac39996595842ece1ca96f Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 14 Feb 2021 09:55:36 -0500 Subject: [PATCH 6/6] fix runaway regex --- src/languages/tcl.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/languages/tcl.js b/src/languages/tcl.js index 25f1eb34c3..3adcb34c91 100644 --- a/src/languages/tcl.js +++ b/src/languages/tcl.js @@ -8,7 +8,7 @@ Website: https://www.tcl.tk/about/language.html import * as regex from '../lib/regex.js'; export default function(hljs) { - const ARRAY_ACCESS = '\\(([a-zA-Z0-9_]+)*\\)'; + const TCL_IDENT = /[a-zA-Z_][a-zA-Z0-9_]*/; const NUMBER = { className: 'number', @@ -51,7 +51,14 @@ export default function(hljs) { className: "variable", variants: [ { - begin: '\\$(::)?[a-zA-Z_]+((::)?[a-zA-Z0-9_]+)*', + begin: regex.concat( + /\$/, + regex.optional(/::/), + TCL_IDENT, + '(::', + TCL_IDENT, + ')*' + ) }, { begin: '\\$\\{(::)?[a-zA-Z_]((::)?[a-zA-Z0-9_])*',