From 50bb66cdd2033f3bd1108108a3624d1503bcda43 Mon Sep 17 00:00:00 2001 From: aszlig Date: Sat, 18 Aug 2018 03:24:40 +0200 Subject: [PATCH 1/3] Highlight string escape sequence errors Before a2a9e1187b4abdb9d78a6f43c159c9ff14c54167, the check for the escape characters was quite strict and didn't match escape sequences where Nix just strips off the backslash (or ''\ for multiline strings). The strict match is useful if you want to see a change in highlighting so that you'll notice whenever you use a character that either is mistyped or the whole escape sequence is unnecessary. Now we have the old check in again but with a second group (nixInvalidSimpleStringEscape and nixInvalidStringEscape respectively), which should now highlight this with the Error highlighting group, so users will notice immediately. Signed-off-by: aszlig --- syntax/nix.vim | 83 ++++++++++++++++++++++++++------------------------ test/nix.vader | 25 +++++++++++++++ 2 files changed, 69 insertions(+), 39 deletions(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index 5a4126d..ff7f7bc 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -34,12 +34,15 @@ syn region nixComment start=+/\*+ end=+\*/+ contains=nixTodo,@Spell syn region nixInterpolation matchgroup=nixInterpolationDelimiter start="\${" end="}" contained contains=@nixExpr,nixInterpolationParam -syn match nixSimpleStringSpecial /\\./ contained +syn match nixSimpleStringSpecial /\\\%([nrt"\\$]\|$\)/ contained syn match nixStringSpecial /''['$]/ contained -syn match nixStringSpecial /''\\./ contained +syn match nixStringSpecial /''\\[nrt]/ contained -syn region nixSimpleString matchgroup=nixStringDelimiter start=+"+ skip=+\\"+ end=+"+ contains=nixInterpolation,nixSimpleStringSpecial -syn region nixString matchgroup=nixStringDelimiter start=+''+ skip=+''['$\\]+ end=+''+ contains=nixInterpolation,nixStringSpecial +syn match nixInvalidSimpleStringEscape /\\[^nrt"\\$]/ contained +syn match nixInvalidStringEscape /''\\[^nrt]/ contained + +syn region nixSimpleString matchgroup=nixStringDelimiter start=+"+ skip=+\\"+ end=+"+ contains=nixInterpolation,nixSimpleStringSpecial,nixInvalidSimpleStringEscape +syn region nixString matchgroup=nixStringDelimiter start=+''+ skip=+''['$\\]+ end=+''+ contains=nixInterpolation,nixStringSpecial,nixInvalidStringEscape syn match nixFunctionCall "[a-zA-Z_][a-zA-Z0-9_'-]*" @@ -147,41 +150,43 @@ syn keyword nixNamespacedBuiltin contained syn match nixBuiltin "builtins\.[a-zA-Z']\+"he=s+9 contains=nixComment,nixNamespacedBuiltin -hi def link nixArgOperator Operator -hi def link nixArgumentDefinition Identifier -hi def link nixArgumentEllipsis Operator -hi def link nixAssertKeyword Keyword -hi def link nixAttribute Identifier -hi def link nixAttributeDot Operator -hi def link nixBoolean Boolean -hi def link nixBuiltin Special -hi def link nixComment Comment -hi def link nixConditional Conditional -hi def link nixHomePath Include -hi def link nixIfExprKeyword Keyword -hi def link nixInherit Keyword -hi def link nixInteger Integer -hi def link nixInterpolation Macro -hi def link nixInterpolationDelimiter Delimiter -hi def link nixInterpolationParam Macro -hi def link nixLetExprKeyword Keyword -hi def link nixNamespacedBuiltin Special -hi def link nixNull Constant -hi def link nixOperator Operator -hi def link nixPath Include -hi def link nixPathDelimiter Delimiter -hi def link nixRecKeyword Keyword -hi def link nixSearchPath Include -hi def link nixSimpleBuiltin Keyword -hi def link nixSimpleFunctionArgument Identifier -hi def link nixSimpleString String -hi def link nixSimpleStringSpecial SpecialChar -hi def link nixString String -hi def link nixStringDelimiter Delimiter -hi def link nixStringSpecial Special -hi def link nixTodo Todo -hi def link nixURI Include -hi def link nixWithExprKeyword Keyword +hi def link nixArgOperator Operator +hi def link nixArgumentDefinition Identifier +hi def link nixArgumentEllipsis Operator +hi def link nixAssertKeyword Keyword +hi def link nixAttribute Identifier +hi def link nixAttributeDot Operator +hi def link nixBoolean Boolean +hi def link nixBuiltin Special +hi def link nixComment Comment +hi def link nixConditional Conditional +hi def link nixHomePath Include +hi def link nixIfExprKeyword Keyword +hi def link nixInherit Keyword +hi def link nixInteger Integer +hi def link nixInterpolation Macro +hi def link nixInterpolationDelimiter Delimiter +hi def link nixInterpolationParam Macro +hi def link nixInvalidSimpleStringEscape Error +hi def link nixInvalidStringEscape Error +hi def link nixLetExprKeyword Keyword +hi def link nixNamespacedBuiltin Special +hi def link nixNull Constant +hi def link nixOperator Operator +hi def link nixPath Include +hi def link nixPathDelimiter Delimiter +hi def link nixRecKeyword Keyword +hi def link nixSearchPath Include +hi def link nixSimpleBuiltin Keyword +hi def link nixSimpleFunctionArgument Identifier +hi def link nixSimpleString String +hi def link nixSimpleStringSpecial SpecialChar +hi def link nixString String +hi def link nixStringDelimiter Delimiter +hi def link nixStringSpecial Special +hi def link nixTodo Todo +hi def link nixURI Include +hi def link nixWithExprKeyword Keyword " This could lead up to slow syntax highlighting for large files, but usually " large files such as all-packages.nix are one large attribute set, so if we'd diff --git a/test/nix.vader b/test/nix.vader index 56fd14d..3ae4f4e 100644 --- a/test/nix.vader +++ b/test/nix.vader @@ -327,6 +327,31 @@ Execute (syntax): AssertEqual SyntaxOf('xxx'), 'nixString' AssertEqual SyntaxOf('}'), 'nixString' +Given nix (string-escape-errors): + '' + foo''\abar + foo''\nbar + ${"foo\xbar"} + ${"foo\tbar"} + ${"foo \ + bar"} + ${"foo\\bar"} + ${"complex\\\\\f\\bar"} + '' + +Execute (syntax): + AssertEqual SyntaxOf("''\\\\a"), 'nixInvalidStringEscape' + AssertEqual SyntaxOf("''\\\\n"), 'nixStringSpecial' + AssertEqual SyntaxOf("\\\\x"), 'nixInvalidSimpleStringEscape' + AssertEqual SyntaxOf("\\\\t"), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("\\\\$"), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("foo\\\\\\zs\\\\"), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("complex\\zs."), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("complex.\\zs."), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("complex..\\zs."), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("complex.*\\zs.\\zebar"), 'nixSimpleStringSpecial' + AssertEqual SyntaxOf("complex....\\zs."), 'nixInvalidSimpleStringEscape' + Given nix (lambda-attrs): { # very descriptive comment foo From b579f9c5c687868be108195710e117aec62b6b29 Mon Sep 17 00:00:00 2001 From: aszlig Date: Sat, 18 Aug 2018 04:31:58 +0200 Subject: [PATCH 2/3] test/multiline-string-escape: Fix SyntaxOf pattern When double quotes are used, backslashes need to be escaped twice, so the pattern for "''\\" would result in the regex /''\/ which is still valid, but a single backslash in regexes is also used as an escape character. So just to make sure this is correctly escaped I added another backslash. In essence, this doesn't have any changes in functionality, but is just a small nitpick ;-) Signed-off-by: aszlig --- test/nix.vader | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nix.vader b/test/nix.vader index 3ae4f4e..19d86f5 100644 --- a/test/nix.vader +++ b/test/nix.vader @@ -320,7 +320,7 @@ Execute (syntax): AssertEqual SyntaxOf('foo'), 'nixString' AssertEqual SyntaxOf("'''"), 'nixStringSpecial' AssertEqual SyntaxOf('bar'), 'nixString' - AssertEqual SyntaxOf("''\\"), 'nixStringSpecial' + AssertEqual SyntaxOf("''\\\\"), 'nixStringSpecial' AssertEqual SyntaxOf('end'), 'nixString' AssertEqual SyntaxOf("''\\$"), 'nixStringSpecial' AssertEqual SyntaxOf('{'), 'nixString' From d81a3e5c1862a962a504a7b805277d7666e1f786 Mon Sep 17 00:00:00 2001 From: aszlig Date: Sat, 18 Aug 2018 04:38:08 +0200 Subject: [PATCH 3/3] syntax: Fix regex for nixAttribute match Using dashes before the end of a character collection is quite dangerous because they represent character ranges. This is the case for the regex of the nixAttribute group which has been changed in a61495a762feacc00f24cab4392b09cc3250decf, the '-. now is a character range and thus accepts any of the following characters: * 39 0x27 ' * 40 0x28 ( * 41 0x29 ) * 42 0x2A * * 43 0x2B + * 44 0x2C , * 45 0x2D - * 46 0x2E . To fix this, I just moved the dash to the end of the character collection so now only "'", "." and "-" are matched. Signed-off-by: aszlig --- syntax/nix.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index ff7f7bc..8b869da 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -54,7 +54,7 @@ syn match nixSearchPathRef "<[a-zA-Z0-9._+-]\+\%(\/[a-zA-Z0-9._+-]\+\)*>" contai syn match nixURI "[a-zA-Z][a-zA-Z0-9.+-]*:[a-zA-Z0-9%/?:@&=$,_.!~*'+-]\+" syn match nixAttributeDot "\." contained -syn match nixAttribute "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%([^a-zA-Z0-9_'-.]\|$\)" contained +syn match nixAttribute "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%([^a-zA-Z0-9_'.-]\|$\)" contained syn region nixAttributeAssignment start="=" end="\ze;" contained contains=@nixExpr syn region nixAttributeDefinition start=/\ze[a-zA-Z_"$]/ end=";" contained contains=nixComment,nixAttribute,nixInterpolation,nixSimpleString,nixAttributeDot,nixAttributeAssignment