Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fhirpath parsing now includes comments and whitespace into the Expression object model. #2779

Merged
merged 9 commits into from
May 21, 2024
18 changes: 9 additions & 9 deletions src/Hl7.Fhir.Base/FhirPath/Expressions/EchoVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ private void OutputIdentifierName(string identifier)

private void OutputPrecedingTokens(Expression? expr)
{
if (expr?.leadingWhitespace?.Any() == true)
_result.Append(System.String.Join("", expr.leadingWhitespace.Select(ws => ws.ToString())));
if (expr?.LeadingWhitespace?.Any() == true)
_result.Append(System.String.Join("", expr.LeadingWhitespace.Select(ws => ws.ToString())));
}
private void OutputTrailingTokens(Expression expr)
{
if (expr.trailingWhitespace?.Any() == true)
_result.Append(System.String.Join("", expr.trailingWhitespace.Select(ws => ws.ToString())));
if (expr.TrailingWhitespace?.Any() == true)
_result.Append(System.String.Join("", expr.TrailingWhitespace.Select(ws => ws.ToString())));
}

private void OutputSubToken(SubToken subtoken)
Expand All @@ -56,14 +56,14 @@ private void OutputSubToken(SubToken subtoken)
private void OutputPrecedingTokens(SubToken subtoken)
{
if (subtoken == null) return;
if (subtoken.leadingWhitespace?.Any() == true)
_result.Append(System.String.Join("", subtoken.leadingWhitespace.Select(ws => ws.ToString())));
if (subtoken.LeadingWhitespace?.Any() == true)
_result.Append(System.String.Join("", subtoken.LeadingWhitespace.Select(ws => ws.ToString())));
}
private void OutputTrailingTokens(SubToken subtoken)
{
if (subtoken == null) return;
if (subtoken.trailingWhitespace?.Any() == true)
_result.Append(System.String.Join("", subtoken.trailingWhitespace.Select(ws => ws.ToString())));
if (subtoken.TrailingWhitespace?.Any() == true)
_result.Append(System.String.Join("", subtoken.TrailingWhitespace.Select(ws => ws.ToString())));
}
#endregion

Expand Down Expand Up @@ -122,7 +122,7 @@ public override StringBuilder VisitConstant(ConstantExpression expression)
}
break;
case "String":
_result.Append($"'{t}'");
_result.Append("'" + Functions.StringOperators.EscapeJson(t) + "'");
break;
case "Ratio":
_result.Append($"{t}");
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/FhirPath/Expressions/ExpressionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected Expression(TypeSpecifier type, ISourcePositionInfo location) : this(ty
/// <summary>
/// Any leading whitespace or comments encountered immediately before this Expression
/// </summary>
public IEnumerable<WhitespaceSubToken> leadingWhitespace { get; internal set; }
public IEnumerable<WhitespaceSubToken> LeadingWhitespace { get; internal set; }

/// <summary>
/// Any trailing whitespace or comments encountered immediately after this Expression
/// </summary>
public IEnumerable<WhitespaceSubToken> trailingWhitespace { get; internal set; }
public IEnumerable<WhitespaceSubToken> TrailingWhitespace { get; internal set; }

/// <summary>
/// The DataType that would be returned be evaluating this ExpressionNode
Expand Down
20 changes: 10 additions & 10 deletions src/Hl7.Fhir.Base/FhirPath/Expressions/SubToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ public SubToken(char value, ISourcePositionInfo location = null)
/// <summary>
/// Any leading whitespace or comments encountered immediately before this SubToken
/// </summary>
public IEnumerable<WhitespaceSubToken> leadingWhitespace { get; internal set; }
public IEnumerable<WhitespaceSubToken> LeadingWhitespace { get; internal set; }

/// <summary>
/// Any trailing whitespace or comments encountered immediately after this SubToken
/// </summary>
public IEnumerable<WhitespaceSubToken> trailingWhitespace { get; internal set; }
public IEnumerable<WhitespaceSubToken> TrailingWhitespace { get; internal set; }

public override string ToString()
{
var sb = new StringBuilder();
if (leadingWhitespace?.Any() == true)
sb.Append(System.String.Join("", leadingWhitespace.Select(ws => ws.ToString())));
if (LeadingWhitespace?.Any() == true)
sb.Append(System.String.Join("", LeadingWhitespace.Select(ws => ws.ToString())));
sb.Append(Value);
if (trailingWhitespace?.Any() == true)
sb.Append(System.String.Join("", trailingWhitespace.Select(ws => ws.ToString())));
if (TrailingWhitespace?.Any() == true)
sb.Append(System.String.Join("", TrailingWhitespace.Select(ws => ws.ToString())));
return sb.ToString();
}

Expand Down Expand Up @@ -120,17 +120,17 @@ public CommentSubToken(string value, bool block, ISourcePositionInfo location =
public override string ToString()
{
var sb = new StringBuilder();
if (leadingWhitespace?.Any() == true)
sb.Append(System.String.Join("", leadingWhitespace.Select(ws => ws.ToString())));
if (LeadingWhitespace?.Any() == true)
sb.Append(System.String.Join("", LeadingWhitespace.Select(ws => ws.ToString())));
if (block)
sb.Append("/*");
else
sb.Append("//");
sb.Append(Value);
if (block)
sb.Append("*/");
if (trailingWhitespace?.Any() == true)
sb.Append(System.String.Join("", trailingWhitespace.Select(ws => ws.ToString())));
if (TrailingWhitespace?.Any() == true)
sb.Append(System.String.Join("", TrailingWhitespace.Select(ws => ws.ToString())));
return sb.ToString();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/FhirPath/FhirPathCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public FhirPathCompiler() : this(DefaultSymbolTable)
public Expression Parse(string expression)
#pragma warning restore CA1822 // This might access instance data in the future.
{
var parse = Grammar.Expression.End().Positioned().TryParse(expression);
var parse = Grammar.Expression.End().TryParse(expression);

return parse.WasSuccessful ? parse.Value : throw new FormatException("Compilation failed: " + parse.ToString());
}
Expand Down
Loading