private static ErlangListComprehensionSyntax ParseListComprehension(ErlangLeftBracketToken leftBracket, ErlangExpressionSyntax expression, ErlangPipePipeToken doublePipe, TokenBuffer buffer, ParseStyle style) { var generators = ParseSyntaxListWithComma(buffer, style, ErlangListComprehensionGeneratorSyntax.Parse); var filters = ParseSyntaxListWithComma(buffer, style, ErlangListComprehensionFilterSyntax.Parse); ErlangRightBracketToken rightBracket = null; var right = buffer.Peek(); if (ErlangToken.IsRightBracket(right)) { buffer.Advance(); rightBracket = (ErlangRightBracketToken)right; } return(new ErlangListComprehensionSyntax(leftBracket, expression, doublePipe, generators, filters, rightBracket)); }
internal static ErlangListSyntax ParseList(TokenBuffer buffer, ParseStyle style) { var left = buffer.Peek(); if (ErlangToken.IsString(left)) { buffer.Advance(); return(new ErlangListStringSyntax((ErlangStringToken)left)); } else if (ErlangToken.IsLeftBracket(left)) { buffer.Advance(); var items = new List <ErlangListItemSyntax>(); ErlangExpressionSyntax tail = null; while (buffer.TokensRemain()) { var item = ParseListItem(buffer, style); if (item == null) { break; } items.Add(item); if (!ErlangToken.IsComma(item.Separator)) { break; } } if (items.Count == 1 && ErlangToken.IsDoublePipe(buffer.Peek())) { // list comprehension, parse generator expression and filters // result of generator expressions get filtered, e.g. // [X || {X, some_atom} <- some_list()]. var listItem = items.Single(); var doublePipe = (ErlangPipePipeToken)buffer.Peek(); buffer.Advance(); return(ParseListComprehension((ErlangLeftBracketToken)left, listItem.Item, doublePipe, buffer, style)); } else if (items.Count > 0) { var lastSep = items.Last().Separator; if (ErlangToken.IsPipe(lastSep)) { // list tail, only parse one more expression tail = ParseExpression(buffer, style); } else if (lastSep != null) { Debug.Assert(false, "unexpected list separator"); } } ErlangRightBracketToken rightBracket = null; var next = buffer.Peek(); if (ErlangToken.IsRightBracket(next)) { buffer.Advance(); rightBracket = (ErlangRightBracketToken)next; } else { Debug.Assert(false, "Missing closing bracket"); } return(new ErlangListRegularSyntax((ErlangLeftBracketToken)left, items, tail, rightBracket)); } return(null); }