-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathCompositeExpression.cs
More file actions
46 lines (39 loc) · 1.45 KB
/
CompositeExpression.cs
File metadata and controls
46 lines (39 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.OpenApi
{
/// <summary>
/// String literal with embedded expressions
/// </summary>
public class CompositeExpression : RuntimeExpression
{
private readonly string template;
private Regex expressionPattern = new(@"{(?<exp>\$[^}]*)");
/// <summary>
/// Expressions embedded into string literal
/// </summary>
public List<RuntimeExpression> ContainedExpressions = new();
/// <summary>
/// Create a composite expression from a string literal with an embedded expression
/// </summary>
/// <param name="expression"></param>
public CompositeExpression(string expression)
{
template = expression;
// Extract subexpressions and convert to RuntimeExpressions
var matches = expressionPattern.Matches(expression);
foreach (var item in matches.Cast<Match>())
{
var value = item.Groups["exp"].Captures.Cast<Capture>().First().Value;
ContainedExpressions.Add(Build(value));
}
}
/// <summary>
/// Return original string literal with embedded expression
/// </summary>
public override string Expression => template;
}
}