<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PineTree.Language.Lexer;
using PineTree.Language.Syntax;

namespace PineTree.Language.Parser
{
    public class PineTreeParser
    {
        private Token _current;
        private bool _enforceSemi;
        private Token _last;
        private PineTreeLexer _lexer;

        private Token _next;

        /// <summary>
        /// Initializes a new instance of the <see cref="PineTreeParser"/> class.
        /// </summary>
        public PineTreeParser()
            : this(new PineTreeLexer())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PineTreeParser"/> class with a given <see cref="PineTreeLexer"/>.
        /// </summary>
        /// <param name="lexer"></param>
        public PineTreeParser(PineTreeLexer lexer)
        {
            _lexer = lexer;
            _enforceSemi = true;
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file must be classes or methods and semicolons are enforced.
        /// </summary>
        /// <seealso cref="Parse(string)"/>
        /// <returns></returns>
        public SourceDocument Parse()
        {
            InitializeTokens(true);
            List<SyntaxNode> nodes = new List<SyntaxNode>();

            try
            {
                while (_current != TokenType.EOF)
                {
                    nodes.Add(ParseClassOrMethod());
                }
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }

            return new SourceDocument(nodes);
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file must be classes or methods and semicolons are enforced.
        /// </summary>
        /// <param name="source">The file to parse.</param>
        /// <seealso cref="Parse"/>
        /// <returns></returns>
        public SourceDocument Parse(string source)
        {
            _lexer.Feed(source);
            try
            {
                return Parse();
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }
        }

        /// <summary>
        /// Parses an <see cref="Expression"/> and returns an AST object containing the contents.
        /// Semicolons are not enforced.
        /// </summary>
        /// <param name="source">The expression to parse.</param>
        /// <returns></returns>
        public Expression ParseExpression(string source)
        {
            _lexer.Feed(source);
            InitializeTokens(false);
            try
            {
                return ParseExpression();
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file may be classes, methods, or statements.  Semicolons are not enforced.
        /// </summary>
        /// <param name="source">The source code to parse.</param>
        /// <returns></returns>
        public SourceDocument ParseScript(string source)
        {
            _lexer.Feed(source);
            InitializeTokens(false);
            List<SyntaxNode> nodes = new List<SyntaxNode>();
            try
            {
                while (_current != TokenType.EOF)
                {
                    if (_current == "import")
                    {
                        nodes.Add(ParseImportStatement());
                    }
                    else if ((PeekToken(1) == TokenType.Identifier && _next.Catagory == TokenCatagory.Identifier)
                         || (_next == "class" || _current == "class"))
                    {
                        nodes.Add(ParseClassOrMethod());
                    }<commit_msg>Temporarily Fix Parsing of the Raise Statement.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PineTree.Language.Lexer;
using PineTree.Language.Syntax;

namespace PineTree.Language.Parser
{
    public class PineTreeParser
    {
        private Token _current;
        private bool _enforceSemi;
        private Token _last;
        private PineTreeLexer _lexer;

        private Token _next;

        /// <summary>
        /// Initializes a new instance of the <see cref="PineTreeParser"/> class.
        /// </summary>
        public PineTreeParser()
            : this(new PineTreeLexer())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PineTreeParser"/> class with a given <see cref="PineTreeLexer"/>.
        /// </summary>
        /// <param name="lexer"></param>
        public PineTreeParser(PineTreeLexer lexer)
        {
            _lexer = lexer;
            _enforceSemi = true;
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file must be classes or methods and semicolons are enforced.
        /// </summary>
        /// <seealso cref="Parse(string)"/>
        /// <returns></returns>
        public SourceDocument Parse()
        {
            InitializeTokens(true);
            List<SyntaxNode> nodes = new List<SyntaxNode>();

            try
            {
                while (_current != TokenType.EOF)
                {
                    nodes.Add(ParseClassOrMethod());
                }
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }

            return new SourceDocument(nodes);
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file must be classes or methods and semicolons are enforced.
        /// </summary>
        /// <param name="source">The file to parse.</param>
        /// <seealso cref="Parse"/>
        /// <returns></returns>
        public SourceDocument Parse(string source)
        {
            _lexer.Feed(source);
            try
            {
                return Parse();
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }
        }

        /// <summary>
        /// Parses an <see cref="Expression"/> and returns an AST object containing the contents.
        /// Semicolons are not enforced.
        /// </summary>
        /// <param name="source">The expression to parse.</param>
        /// <returns></returns>
        public Expression ParseExpression(string source)
        {
            _lexer.Feed(source);
            InitializeTokens(false);
            try
            {
                return ParseExpression();
            }
            catch (SyntaxException)
            {
                _lexer.Clear();
                throw;
            }
        }

        /// <summary>
        /// Parses all of the given source code and returns a <see cref="SourceDocument"/> containing the created Abstract Syntax Tree.
        /// Root members of the file may be classes, methods, or statements.  Semicolons are not enforced.
        /// </summary>
        /// <param name="source">The source code to parse.</param>
        /// <returns></returns>
        public SourceDocument ParseScript(string source)
        {
            _lexer.Feed(source);
            InitializeTokens(false);
            List<SyntaxNode> nodes = new List<SyntaxNode>();
            try
            {
                while (_current != TokenType.EOF)
                {
                    if (_current == "import")
                    {
                        nodes.Add(ParseImportStatement());
                    }
                    else if (_current == "public" || _current == "private" || _current == "protected" || _current == "class")
                    {
                        nodes.Add(ParseClassOrMethod());
                    }
{'fraction_non_alphanumeric': 0.10679048171793383, 'fraction_numerical': 0.00011607661056297156, 'mean_word_length': 1.4243106359032076, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5313362', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2035, 'n_words': 592}