提交 c69a50d9 编写于 作者: C Cyrus Najmabadi

Move VirtualChar code into its own namespace.

上级 969a5fb3
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp.VirtualChars;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ValidateRegexString;
......
......@@ -5,10 +5,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.DocumentHighlighting
......
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.ValidateRegexString
{
......
......@@ -4,7 +4,7 @@
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp.VirtualChars;
using Microsoft.CodeAnalysis.PooledObjects;
namespace Microsoft.CodeAnalysis.CSharp.Classification.Classifiers
......
......@@ -3,13 +3,12 @@
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.CSharp.RegularExpressions
namespace Microsoft.CodeAnalysis.CSharp.VirtualChars
{
[ExportLanguageService(typeof(IVirtualCharService), LanguageNames.CSharp), Shared]
internal class CSharpVirtualCharService : AbstractVirtualCharService
......
......@@ -6,15 +6,16 @@
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.CSharp.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp.VirtualChars;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.VirtualChars;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.RegularExpressions
{
public partial class CSharpRegexParserTests
{
private readonly IVirtualCharService _service = new CSharpVirtualCharService();
private readonly IVirtualCharService _service = CSharpVirtualCharService.Instance;
private const string _statmentPrefix = "var v = ";
private SyntaxToken GetStringToken(string text)
......
......@@ -2,11 +2,11 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.RegularExpressions;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp.VirtualChars;
using Microsoft.CodeAnalysis.VirtualChars;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.RegularExpressions
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.VirtualChars
{
public class CSharpVirtualCharServiceTests
{
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.RegularExpressions;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.Classification
{
......
......@@ -5,6 +5,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
......@@ -6,6 +6,7 @@
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
......@@ -6,10 +6,10 @@
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
using System;
using static RegexHelpers;
internal partial struct RegexParser
......
......@@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
......@@ -10,6 +10,7 @@
using System.Threading;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.VirtualChars;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.RegularExpressions
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
......@@ -2,6 +2,7 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
......@@ -2,6 +2,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.VirtualChars;
namespace Microsoft.CodeAnalysis.RegularExpressions
{
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
namespace Microsoft.CodeAnalysis.VirtualChars
{
internal abstract class AbstractVirtualCharService : IVirtualCharService
{
public ImmutableArray<VirtualChar> TryConvertToVirtualChars(SyntaxToken token)
{
if (token.ContainsDiagnostics)
{
return default;
}
var result = TryConvertToVirtualCharsWorker(token);
#if DEBUG
// Do some invariant checking to make sure we processed the string token the same
// way the C# compiler did.
if (!result.IsDefault)
{
// Ensure that we properly broke up the token into a sequence of characters that
// matches what the compiler did.
var expectedValueText = token.ValueText;
var actualValueText = new string(result.Select(vc => vc.Char).ToArray());
Debug.Assert(expectedValueText == actualValueText);
if (result.Length > 0)
{
var currentVC = result[0];
Debug.Assert(currentVC.Span.Start > token.SpanStart, "First span has to start after the start of the string token (including its delimeter)");
Debug.Assert(currentVC.Span.Start == token.SpanStart + 1 || currentVC.Span.Start == token.SpanStart + 2, "First span should start on the second or third char of the string.");
for (var i = 1; i < result.Length; i++)
{
var nextVC = result[i];
Debug.Assert(currentVC.Span.End == nextVC.Span.Start, "Virtual character spans have to be touching.");
currentVC = nextVC;
}
var lastVC = result.Last();
Debug.Assert(lastVC.Span.End == token.Span.End - 1, "Last span has to end right before the end of hte string token (including its trailing delimeter).");
}
}
#endif
return result;
}
protected abstract ImmutableArray<VirtualChar> TryConvertToVirtualCharsWorker(SyntaxToken token);
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Host;
namespace Microsoft.CodeAnalysis.VirtualChars
{
/// <summary>
/// Helper service that takes the raw text of a string token and produces the individual
/// characters that raw string token represents (i.e. with escapes collapsed). The difference
/// between this and the result from token.ValueText is that for each collapsed character returned
/// the original span of text in the original token can be found. i.e. if you had the following
/// in C#:
///
/// "G\u006fo"
///
/// Then you'd get back:
///
/// 'G' -> [0, 1)
/// 'o' -> [1, 7)
/// 'o' -> [7, 1)
///
/// This allows for regex processing that can refer back to the users' original code instead of
/// the escaped value we're processing.
///
/// </summary>
internal interface IVirtualCharService : ILanguageService
{
ImmutableArray<VirtualChar> TryConvertToVirtualChars(SyntaxToken token);
}
}
......@@ -3,12 +3,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.RegularExpressions
namespace Microsoft.CodeAnalysis.VirtualChars
{
/// <summary>
/// The Regex parser wants to work over an array of characters, however this array of characters
......@@ -64,74 +61,4 @@ public override int GetHashCode()
public static implicit operator char(VirtualChar vc) => vc.Char;
}
/// <summary>
/// Helper service that takes the raw text of a string token and produces the individual
/// characters that raw string token represents (i.e. with escapes collapsed). The difference
/// between this and the result from token.ValueText is that for each collapsed character returned
/// the original span of text in the original token can be found. i.e. if you had the following
/// in C#:
///
/// "G\u006fo"
///
/// Then you'd get back:
///
/// 'G' -> [0, 1)
/// 'o' -> [1, 7)
/// 'o' -> [7, 1)
///
/// This allows for regex processing that can refer back to the users' original code instead of
/// the escaped value we're processing.
///
/// </summary>
internal interface IVirtualCharService : ILanguageService
{
ImmutableArray<VirtualChar> TryConvertToVirtualChars(SyntaxToken token);
}
internal abstract class AbstractVirtualCharService : IVirtualCharService
{
public ImmutableArray<VirtualChar> TryConvertToVirtualChars(SyntaxToken token)
{
if (token.ContainsDiagnostics)
{
return default;
}
var result = TryConvertToVirtualCharsWorker(token);
#if DEBUG
// Do some invariant checking to make sure we processed the string token the same
// way the C# compiler did.
if (!result.IsDefault)
{
// Ensure that we properly broke up the token into a sequence of characters that
// matches what the compiler did.
var expectedValueText = token.ValueText;
var actualValueText = new string(result.Select(vc => vc.Char).ToArray());
Debug.Assert(expectedValueText == actualValueText);
if (result.Length > 0)
{
var currentVC = result[0];
Debug.Assert(currentVC.Span.Start > token.SpanStart, "First span has to start after the start of the string token (including its delimeter)");
Debug.Assert(currentVC.Span.Start == token.SpanStart + 1 || currentVC.Span.Start == token.SpanStart + 2, "First span should start on the second or third char of the string.");
for (var i = 1; i < result.Length; i++)
{
var nextVC = result[i];
Debug.Assert(currentVC.Span.End == nextVC.Span.Start, "Virtual character spans have to be touching.");
currentVC = nextVC;
}
var lastVC = result.Last();
Debug.Assert(lastVC.Span.End == token.Span.End - 1, "Last span has to end right before the end of hte string token (including its trailing delimeter).");
}
}
#endif
return result;
}
protected abstract ImmutableArray<VirtualChar> TryConvertToVirtualCharsWorker(SyntaxToken token);
}
}
......@@ -2,8 +2,8 @@
Imports System.Composition
Imports Microsoft.CodeAnalysis.Host.Mef
Imports Microsoft.CodeAnalysis.PooledObjects
Imports Microsoft.CodeAnalysis.RegularExpressions
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VirtualChars
Namespace Microsoft.CodeAnalysis.VisualBasic.RegularExpressions
<ExportLanguageService(GetType(IVirtualCharService), LanguageNames.VisualBasic), [Shared]>
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Linq
Imports Microsoft.CodeAnalysis.CSharp.RegularExpressions
Imports Microsoft.CodeAnalysis.RegularExpressions
Imports Microsoft.CodeAnalysis.VirtualChars
Imports Microsoft.CodeAnalysis.VisualBasic.RegularExpressions
Imports Xunit
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.RegularExpressions
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.VirtualChars
Public Class VisualBasicVirtualCharServiceTests
Private Const _statmentPrefix As String = "dim v = "
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册