コード例 #1
0
        internal static ErlangListComprehensionGeneratorSyntax Parse(TokenBuffer buffer, ParseStyle style)
        {
            var start      = buffer.Offset;
            var expression = ParseExpression(buffer, style);

            if (expression != null)
            {
                var next = buffer.Peek();
                if (ErlangToken.IsLeftArrow(next))
                {
                    buffer.Advance();
                    var arrow    = (ErlangLessMinusToken)next;
                    var function = ParseExpression(buffer, style);
                    if (function != null)
                    {
                        ErlangCommaToken comma = null;
                        next = buffer.Peek();
                        if (ErlangToken.IsComma(next))
                        {
                            buffer.Advance();
                            comma = (ErlangCommaToken)next;
                        }

                        return(new ErlangListComprehensionGeneratorSyntax(expression, arrow, function, comma));
                    }
                }
            }

            buffer.SetOffset(start);
            return(null);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        internal static ErlangListComprehensionFilterSyntax Parse(TokenBuffer buffer, ParseStyle style)
        {
            var expression = ParseExpression(buffer, style);

            if (expression != null)
            {
                ErlangCommaToken comma = null;
                var next = buffer.Peek();
                if (ErlangToken.IsComma(next))
                {
                    buffer.Advance();
                    comma = (ErlangCommaToken)next;
                }

                return(new ErlangListComprehensionFilterSyntax(expression, comma));
            }

            return(null);
        }
コード例 #4
0
        private static ErlangListItemSyntax ParseListItem(TokenBuffer buffer, ParseStyle style)
        {
            var expression = ParseExpression(buffer, style);

            if (expression != null)
            {
                ErlangPunctuationToken separator = null;
                var sep = buffer.Peek();
                if (ErlangToken.IsComma(sep) || ErlangToken.IsPipe(sep))
                {
                    buffer.Advance();
                    separator = (ErlangPunctuationToken)sep;
                }

                return(new ErlangListItemSyntax(expression, separator));
            }

            return(null);
        }
コード例 #5
0
        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);
        }