Exemplo n.º 1
0
        public static string Parse(Afx.Lexer lexer)
        {
            var contents   = new StringBuilder();
            var braceCount = 0;

            if (lexer.IsOpeningBrace())
            {
                lexer.Consume();
            }
            else
            {
                throw new AfxException("Expression without braces");
            }
            while (true)
            {
                if (lexer.IsEnd())
                {
                    throw new AfxException("Unfinished Expression \"" + contents.ToString() + "\"");
                }
                if (lexer.IsOpeningBrace())
                {
                    braceCount++;
                }
                if (lexer.IsClosingBrace())
                {
                    if (braceCount == 0)
                    {
                        lexer.Consume();
                        return(contents.ToString());
                    }
                    braceCount--;
                }
                contents.Append(lexer.Consume());
            }
        }
Exemplo n.º 2
0
        public static AstNode Parse(Afx.Lexer lexer)
        {
            if (lexer.IsOpeningBrace() && lexer.Peek(4) == "{...")
            {
                lexer.Consume();
                lexer.Consume();
                lexer.Consume();
                lexer.Consume();
            }
            else
            {
                throw new AfxException("Spread without braces");
            }
            string contents   = string.Empty;
            int    braceCount = 0;

            while (true)
            {
                if (lexer.IsEnd())
                {
                    throw new AfxException("Unifinished Spread");
                }
                if (lexer.IsOpeningBrace())
                {
                    braceCount++;
                }
                if (lexer.IsClosingBrace())
                {
                    if (braceCount == 0)
                    {
                        lexer.Consume();
                        return(new AstNode()
                        {
                            Type = AstNodeType.Expression,
                            Payload = contents
                        });
                    }
                    braceCount--;
                }
                contents += lexer.Consume();
            }
        }
Exemplo n.º 3
0
        public static string Parse(Afx.Lexer lexer)
        {
            char openingQuoteSign;
            char closingQuoteSign;
            var  contents      = new StringBuilder();
            var  willBeEscaped = false;

            if (lexer.IsSingleQuote() || lexer.IsDoubleQuote())
            {
                openingQuoteSign = lexer.Consume();
            }
            else
            {
                throw new AfxException("Unquoted String literal");
            }
            while (true)
            {
                if (lexer.IsEnd())
                {
                    throw new AfxException($"Unfinished string literal \"{contents.ToString()}\"");
                }
                if (lexer.IsBackSlash() && !willBeEscaped)
                {
                    willBeEscaped = true;
                    lexer.Consume();
                    continue;
                }
                if (lexer.IsSingleQuote() || lexer.IsDoubleQuote())
                {
                    closingQuoteSign = lexer.Consume();
                    if (!willBeEscaped && openingQuoteSign == closingQuoteSign)
                    {
                        return(contents.ToString());
                    }
                    contents.Append(closingQuoteSign);
                    willBeEscaped = false;
                    continue;
                }
                contents.Append(lexer.Consume());
                willBeEscaped = false;
            }
        }
Exemplo n.º 4
0
        public static AstNode[] Parse(Afx.Lexer lexer)
        {
            var currentText = new StringBuilder();
            var contents    = new List <AstNode>();

            while (!lexer.IsEnd())
            {
                if (lexer.IsOpeningBracket())
                {
                    lexer.Consume();
                    if (lexer.IsForwardSlash())
                    {
                        lexer.Rewind();
                        if (currentText.Length > 0)
                        {
                            contents.Add(new AstNode()
                            {
                                Type    = AstNodeType.Text,
                                Payload = currentText.ToString()
                            });
                        }
                        return(contents.ToArray());
                    }
                    else
                    {
                        lexer.Rewind();
                        if (currentText.Length > 0)
                        {
                            contents.Add(new AstNode()
                            {
                                Type    = AstNodeType.Text,
                                Payload = currentText.ToString()
                            });
                        }
                        contents.Add(new AstNode()
                        {
                            Type    = AstNodeType.Node,
                            Payload = Node.Parse(lexer)
                        });
                        currentText.Clear();
                        continue;
                    }
                }
                if (lexer.IsOpeningBrace())
                {
                    if (currentText.Length > 0)
                    {
                        contents.Add(new AstNode()
                        {
                            Type    = AstNodeType.Text,
                            Payload = currentText.ToString()
                        });
                    }
                    contents.Add(new AstNode()
                    {
                        Type    = AstNodeType.Expression,
                        Payload = Expression.Parse(lexer)
                    });
                    currentText.Clear();
                    continue;
                }
                currentText.Append(lexer.Consume());
            }
            if (lexer.IsEnd() && currentText.Length > 0)
            {
                contents.Add(new AstNode()
                {
                    Type    = AstNodeType.Text,
                    Payload = currentText.ToString()
                });
            }
            return(contents.ToArray());
        }