From c71ca222d3a9a4c8825e88cc7ed4c4fdcd55be20 Mon Sep 17 00:00:00 2001 From: Joey Marianer Date: Tue, 24 Jan 2017 15:02:42 -0500 Subject: [PATCH 1/2] Support literal interpolated strings ($@"") --- src/csharp.ts | 20 ++++++++++++++------ test/csharp.test.ts | 23 ++++++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/csharp.ts b/src/csharp.ts index 07e53199..99ba93bd 100644 --- a/src/csharp.ts +++ b/src/csharp.ts @@ -90,16 +90,12 @@ export var language = { // delimiters and operators [/}/, { cases: { '$S2==interpolatedstring' : { token: 'string.quote', next: '@pop' } + '$S2==litinterpstring' : { token: 'string.quote', next: '@pop' } , '@default' : '@brackets' } }], [/[{}()\[\]]/, '@brackets'], [/[<>](?!@symbols)/, '@brackets'], [/@symbols/, { cases: { '@operators': 'delimiter', '@default' : '' } } ], - // literal string - [/\@"/, { token: 'string.quote', next: '@litstring' } ], - - // interpolated string - [/\$"/, { token: 'string.quote', next: '@interpolatedstring' } ], // numbers [/\d*\.\d+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'], @@ -112,6 +108,9 @@ export var language = { // strings [/"([^"\\]|\\.)*$/, 'string.invalid' ], // non-teminated string [/"/, { token: 'string.quote', next: '@string' } ], + [/\$\@"/, { token: 'string.quote', next: '@litinterpstring' } ], + [/\@"/, { token: 'string.quote', next: '@litstring' } ], + [/\$"/, { token: 'string.quote', next: '@interpolatedstring' } ], // characters [/'[^\\']'/, 'string'], @@ -155,6 +154,15 @@ export var language = { [/"/, { token: 'string.quote', next: '@pop' } ] ], + litinterpstring: [ + [/[^"{]+/, 'string'], + [/""/, 'string.escape'], + [/{{/, 'string.escape'], + [/}}/, 'string.escape'], + [/{/, { token: 'string.quote', next: 'root.litinterpstring' } ], + [/"/, { token: 'string.quote', next: '@pop' } ] + ], + interpolatedstring: [ [/[^\\"{]+/, 'string'], [/@escapes/, 'string.escape'], @@ -173,4 +181,4 @@ export var language = { [/\/\/.*$/, 'comment'], ], }, -}; \ No newline at end of file +}; diff --git a/test/csharp.test.ts b/test/csharp.test.ts index 8d121e5f..71cb333c 100644 --- a/test/csharp.test.ts +++ b/test/csharp.test.ts @@ -740,5 +740,26 @@ testTokenization('csharp', [ { startIndex: 0, type: 'string.cs' }, { startIndex: 6, type: 'string.quote.cs' }, { startIndex: 7, type: 'delimiter.cs' } - ]}] + ]}], + + [{ + line: 'x = $@"verbatim {interpolated} string{{}}"" ";', + tokens: [ + { startIndex: 0, type: "identifier.cs" } + { startIndex: 1, type: "" } + { startIndex: 2, type: "delimiter.cs" } + { startIndex: 3, type: "" } + { startIndex: 4, type: "string.quote.cs" } + { startIndex: 7, type: "string.cs" } + { startIndex: 16, type: "string.quote.cs" } + { startIndex: 17, type: "identifier.cs" } + { startIndex: 29, type: "string.quote.cs" } + { startIndex: 30, type: "string.cs" } + { startIndex: 37, type: "string.escape.cs" } + { startIndex: 39, type: "string.cs" } + { startIndex: 41, type: "string.escape.cs" } + { startIndex: 43, type: "string.cs" } + { startIndex: 44, type: "string.quote.cs" } + { startIndex: 45, type: "delimiter.cs" } + ]}], ]); From bd847cb2a0e6a5e621519b286f51522be898436f Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Thu, 8 Jun 2017 15:33:52 +0200 Subject: [PATCH 2/2] Fix a few missing commas and indentation --- src/csharp.ts | 8 ++++---- test/csharp.test.ts | 40 ++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/csharp.ts b/src/csharp.ts index 99ba93bd..fc9ed2b4 100644 --- a/src/csharp.ts +++ b/src/csharp.ts @@ -90,7 +90,7 @@ export var language = { // delimiters and operators [/}/, { cases: { '$S2==interpolatedstring' : { token: 'string.quote', next: '@pop' } - '$S2==litinterpstring' : { token: 'string.quote', next: '@pop' } + , '$S2==litinterpstring' : { token: 'string.quote', next: '@pop' } , '@default' : '@brackets' } }], [/[{}()\[\]]/, '@brackets'], [/[<>](?!@symbols)/, '@brackets'], @@ -154,14 +154,14 @@ export var language = { [/"/, { token: 'string.quote', next: '@pop' } ] ], - litinterpstring: [ - [/[^"{]+/, 'string'], + litinterpstring: [ + [/[^"{]+/, 'string'], [/""/, 'string.escape'], [/{{/, 'string.escape'], [/}}/, 'string.escape'], [/{/, { token: 'string.quote', next: 'root.litinterpstring' } ], [/"/, { token: 'string.quote', next: '@pop' } ] - ], + ], interpolatedstring: [ [/[^\\"{]+/, 'string'], diff --git a/test/csharp.test.ts b/test/csharp.test.ts index 71cb333c..94187b79 100644 --- a/test/csharp.test.ts +++ b/test/csharp.test.ts @@ -742,24 +742,24 @@ testTokenization('csharp', [ { startIndex: 7, type: 'delimiter.cs' } ]}], - [{ - line: 'x = $@"verbatim {interpolated} string{{}}"" ";', - tokens: [ - { startIndex: 0, type: "identifier.cs" } - { startIndex: 1, type: "" } - { startIndex: 2, type: "delimiter.cs" } - { startIndex: 3, type: "" } - { startIndex: 4, type: "string.quote.cs" } - { startIndex: 7, type: "string.cs" } - { startIndex: 16, type: "string.quote.cs" } - { startIndex: 17, type: "identifier.cs" } - { startIndex: 29, type: "string.quote.cs" } - { startIndex: 30, type: "string.cs" } - { startIndex: 37, type: "string.escape.cs" } - { startIndex: 39, type: "string.cs" } - { startIndex: 41, type: "string.escape.cs" } - { startIndex: 43, type: "string.cs" } - { startIndex: 44, type: "string.quote.cs" } - { startIndex: 45, type: "delimiter.cs" } - ]}], + [{ + line: 'x = $@"verbatim {interpolated} string{{}}"" ";', + tokens: [ + { startIndex: 0, type: "identifier.cs" }, + { startIndex: 1, type: "" }, + { startIndex: 2, type: "delimiter.cs" }, + { startIndex: 3, type: "" }, + { startIndex: 4, type: "string.quote.cs" }, + { startIndex: 7, type: "string.cs" }, + { startIndex: 16, type: "string.quote.cs" }, + { startIndex: 17, type: "identifier.cs" }, + { startIndex: 29, type: "string.quote.cs" }, + { startIndex: 30, type: "string.cs" }, + { startIndex: 37, type: "string.escape.cs" }, + { startIndex: 39, type: "string.cs" }, + { startIndex: 41, type: "string.escape.cs" }, + { startIndex: 43, type: "string.cs" }, + { startIndex: 44, type: "string.quote.cs" }, + { startIndex: 45, type: "delimiter.cs" }, + ]}], ]);