public bool WriteRefError(TextWriter writer, IInlineExpression exp) { AddError(ResolverFluentError.Reference(exp)); try { writer.Write('{'); exp.WriteError(writer); writer.Write('}'); return(true); } catch (Exception) { return(false); } }
public void Track(TextWriter writer, Pattern pattern, IInlineExpression exp) { if (_travelled.Contains(pattern)) { AddError(ResolverFluentError.Cyclic(pattern)); writer.Write('{'); exp.WriteError(writer); writer.Write('}'); } else { _travelled.Add(pattern); pattern.Write(writer, this); PopTraveled(); } }
public static void WriteInlineExpression(Utf8JsonWriter writer, IInlineExpression value, JsonSerializerOptions options) { if (value is TextLiteral textLiteral) { writer.WriteStartObject(); writer.WritePropertyName("value"); writer.WriteStringValue(textLiteral.Value.Span); writer.WritePropertyName("type"); writer.WriteStringValue("StringLiteral"); writer.WriteEndObject(); } else if (value is NumberLiteral numberLiteral) { writer.WriteStartObject(); writer.WritePropertyName("value"); writer.WriteStringValue(numberLiteral.Value.Span); writer.WritePropertyName("type"); writer.WriteStringValue("NumberLiteral"); writer.WriteEndObject(); } else if (value is MessageReference msgRef) { JsonSerializer.Serialize(writer, msgRef, options); } else if (value is FunctionReference funcRef) { JsonSerializer.Serialize(writer, funcRef, options); } else if (value is Placeable placeable) { JsonSerializer.Serialize(writer, placeable, options); } else if (value is TermReference termReference) { JsonSerializer.Serialize(writer, termReference, options); } else if (value is VariableReference variableReference) { JsonSerializer.Serialize(writer, variableReference, options); } }
private static void ProcessMsgRef(IInlineExpression self, TextWriter writer, Scope scope, MessageReference msgRef) { var id = msgRef.Id; var attribute = msgRef.Attribute; if (scope.Bundle.TryGetAstMessage(id.Name.ToString(), out var msg)) { if (attribute != null) { var found = msg.Attributes.Find(e => e.Id.Equals(attribute)); if (found != null) { scope.Track(writer, found.Value, self); } else { scope.WriteRefError(writer, self); return; } } else { if (msg.Value != null) { scope.Track(writer, msg.Value, self); } else { scope.AddError(ResolverFluentError.NoValue(id.Name)); writer.Write('{'); self.WriteError(writer); writer.Write('}'); } } } else { scope.WriteRefError(writer, self); } }
public static void WriteError(this IInlineExpression self, TextWriter writer) { if (self is MessageReference msgRef) { if (msgRef.Attribute == null) { writer.Write($"{msgRef.Id}"); return; } writer.Write($"{msgRef.Id}.{msgRef.Attribute}"); return; } if (self is TermReference termRef) { if (termRef.Attribute == null) { writer.Write($"-{termRef.Id}"); return; } writer.Write($"-{termRef.Id}.{termRef.Attribute}"); } else if (self is FunctionReference funcRef) { writer.Write($"{funcRef.Id}()"); return; } else if (self is VariableReference varRef) { writer.Write($"${varRef.Id}"); return; } throw new ArgumentException($"Unexpected inline expression `{self.GetType()}`!"); }
public static IFluentType Resolve(this IInlineExpression self, Scope scope) { if (self is TextLiteral textLiteral) { StringWriter stringWriter = new(); UnicodeUtil.WriteUnescapedUnicode(textLiteral.Value, stringWriter); return((FluentString)stringWriter.ToString()); } if (self is NumberLiteral numberLiteral) { return(FluentNumber.TryNumber(numberLiteral.Value.Span)); } if (self is VariableReference varRef) { var args = scope.LocalArgs ?? scope.Args; if (args != null && args.TryGetValue(varRef.Id.ToString(), out var arg)) { return(arg.Copy()); } if (scope.LocalArgs == null) { scope.AddError(ResolverFluentError.UnknownVariable(varRef)); } return(new FluentErrType()); } var writer = new StringWriter(); self.TryWrite(writer, scope); return((FluentString)writer.ToString()); }
public SelectExpression(IInlineExpression selector, List <Variant> variants) { Selector = selector; Variants = variants; }
public NamedArgument(Identifier name, IInlineExpression value) { Name = name; Value = value; }
private static void Write(this IInlineExpression self, TextWriter writer, Scope scope) { if (self is TextLiteral textLiteral) { UnicodeUtil.WriteUnescapedUnicode(textLiteral.Value, writer); return; } if (self is NumberLiteral numberLiteral) { var value = FluentNumber.TryNumber(numberLiteral.Value.Span); value.Write(writer, scope); return; } if (self is MessageReference msgRef) { ProcessMsgRef(self, writer, scope, msgRef); return; } if (self is TermReference termRef) { var res = scope.GetArguments(termRef.Arguments); scope.SetLocalArgs(res.Named); if (scope.Bundle.TryGetAstTerm(termRef.Id.ToString(), out var term)) { var attrName = termRef.Attribute; var attr = term .Attributes .Find(a => a.Id.Equals(attrName)); scope.Track(writer, attr != null ? attr.Value : term.Value, self); } else { scope.WriteRefError(writer, self); } scope.SetLocalArgs(null); return; } if (self is FunctionReference funcRef) { var(resolvedPosArgs, resolvedNamedArgs) = scope.GetArguments(funcRef.Arguments); if (scope.Bundle.TryGetFunction(funcRef.Id, out var func)) { var result = func.Function(resolvedPosArgs, resolvedNamedArgs); if (result.IsError()) { self.WriteError(writer); } else { writer.Write(result.AsString()); } } else { scope.WriteRefError(writer, self); } } if (self is VariableReference varRef) { var id = varRef.Id; var args = scope.LocalArgs ?? scope.Args; if (args != null && args.TryGetValue(id.ToString(), out var arg)) { arg.Write(writer, scope); } else { if (scope.LocalArgs == null) { scope.AddError(ResolverFluentError.Reference(self)); } writer.Write('{'); self.WriteError(writer); writer.Write('}'); } } if (self is Placeable placeable) { placeable.Expression.TryWrite(writer, scope); } }