public void TestSingleElement() { var array = PhpArraySyntaxNode.Parse(new PhpTokeniser(@" [ 'Key' => 'Value', ]")); Assert.Single(array.Elements); Assert.Equal("Key", array.Elements[0].Key.Text); Assert.Equal("Value", ((PhpStringLiteralSyntaxNode)array.Elements[0].Value).Text); }
private static async Task <IEnumerable <LocalisationMember> > getMembersFromPhpFile(string file) { // Get the first array from the PHP file. string phpContents = await File.ReadAllTextAsync(file); int firstBracket = phpContents.IndexOf('['); int lastBracket = phpContents.LastIndexOf(']') + 1; phpContents = phpContents.Substring(firstBracket, lastBracket - firstBracket); return(getMembersFromPhpArray(PhpArraySyntaxNode.Parse(new PhpTokeniser(phpContents))));
static IEnumerable <LocalisationMember> getMembersFromPhpArray(PhpArraySyntaxNode arraySyntax, string?currentKey = null) { currentKey ??= string.Empty; foreach (var i in arraySyntax.Elements) { string thisKey = i.Key.Text; string fullKey = $"{currentKey}{thisKey}"; switch (i.Value) { case PhpArraySyntaxNode nestedArray: foreach (var nested in getMembersFromPhpArray(nestedArray, $"{fullKey}.")) { yield return(nested); } break; case PhpStringLiteralSyntaxNode str: string stringValue = str.Text; // Find all "format parameters" in the localisation string of type :text . var formatStrings = Regex.Matches(stringValue, @":([a-zA-Z\-_]+)"); // Each format parameter needs to be assigned a C# format index and parameter name (for the C# class). var formatIndices = new List <int>(); var formatParamNames = new List <string>(); for (int j = 0; j < formatStrings.Count; j++) { var match = formatStrings[j]; var paramName = match.Groups[1].Captures[0].Value; int existingIndex = formatParamNames.IndexOf(paramName); if (existingIndex >= 0) { // If this is a duplicated format string within the same string, refer to the same index as the others and forego addition of a new parameter. formatIndices.Add(formatIndices[existingIndex]); continue; } formatIndices.Add(formatIndices.Count == 0 ? 0 : formatIndices.Max() + 1); formatParamNames.Add(paramName); } // Replace the format parameters in the original string with the respective C# counterpart ({0}, {1}, ...). for (int j = formatStrings.Count - 1; j >= 0; j--) { var match = formatStrings[j]; stringValue = $"{stringValue[..match.Index]}{{{formatIndices[j]}}}{stringValue[(match.Index + match.Length)..]}";
public void TestMultipleElements() { var array = PhpArraySyntaxNode.Parse(new PhpTokeniser(@" [ 'Key1' => 'Value1', 'Key2' => 'Value2', ]")); Assert.Equal(2, array.Elements.Length); Assert.Equal("Key1", array.Elements[0].Key.Text); Assert.Equal("Value1", ((PhpStringLiteralSyntaxNode)array.Elements[0].Value).Text); Assert.Equal("Key2", array.Elements[1].Key.Text); Assert.Equal("Value2", ((PhpStringLiteralSyntaxNode)array.Elements[1].Value).Text); }
public void TestNestedArray() { var array = PhpArraySyntaxNode.Parse(new PhpTokeniser(@" [ 'Key1' => 'Value1', 'Key2' => [ 'Key3' => 'Value3' ], ]")); Assert.Equal(2, array.Elements.Length); Assert.Equal("Key1", array.Elements[0].Key.Text); Assert.Equal("Key2", array.Elements[1].Key.Text); var nestedArray = (PhpArraySyntaxNode)array.Elements[1].Value; Assert.Single(nestedArray.Elements); Assert.Equal("Key3", nestedArray.Elements[0].Key.Text); Assert.Equal("Value3", ((PhpStringLiteralSyntaxNode)nestedArray.Elements[0].Value).Text); }
public static PhpArrayElementSyntaxNode Parse(PhpTokeniser tokeniser) { tokeniser.SkipWhitespace(); PhpStringLiteralSyntaxNode key = tokeniser.GetTrivia() switch { '\'' => PhpStringLiteralSyntaxNode.Parse(tokeniser), '"' => PhpStringLiteralSyntaxNode.Parse(tokeniser), _ => throw tokeniser.ConstructError($"Invalid array value identifier ({tokeniser.GetTrivia()}).") }; tokeniser.SkipWhitespace(); tokeniser.SkipPattern(new[] { '=', '>' }); tokeniser.SkipWhitespace(); PhpSyntaxNode value = tokeniser.GetTrivia() switch { '\'' => PhpStringLiteralSyntaxNode.Parse(tokeniser), '"' => PhpStringLiteralSyntaxNode.Parse(tokeniser), '[' => PhpArraySyntaxNode.Parse(tokeniser), _ => throw tokeniser.ConstructError($"Invalid array value identifier ({tokeniser.GetTrivia()}).") }; tokeniser.SkipWhitespace(); // Skip trailing trivia for this element. if (tokeniser.TryGetTrivia(out var trivia) && trivia == ',') { tokeniser.Advance(); } tokeniser.SkipWhitespace(); return(new PhpArrayElementSyntaxNode(key, value)); } } }
public void TestEmptyArray() { var array = PhpArraySyntaxNode.Parse(new PhpTokeniser("[]")); Assert.Empty(array.Elements); }