Пример #1
0
        private void ParseKag(string input, string title = "input")
        {
            ClearLog();
            DateTime opStart = DateTime.Now;

            var reader  = new StringSourceReader(title, input);
            var lexer   = new Lexer(reader);
            var morpher = new Morpher(lexer);
            var parser  = new KagParser(morpher);
            var sb      = new StringBuilder();

            int         numErrors  = 0;
            var         errors     = new StringBuilder();
            IExpression expression = null;

            while (!(expression is EofExpression))
            {
                expression?.Print(sb, true, 0);
                try
                {
                    expression = parser.ParseExpression <DocumentExpression>();
                }
                catch (ParseException ex)
                {
                    string error = Environment.NewLine +
                                   ex.Position + Environment.NewLine +
                                   ex.GetType() + Environment.NewLine +
                                   ex.Message + Environment.NewLine;
                    sb.AppendLine(error);
                    errors.AppendLine(error);
                    numErrors++;
                }
                catch (Exception ex)
                {
                    string error = Environment.NewLine +
                                   ex.GetType() + Environment.NewLine +
                                   ex.Message + Environment.NewLine;
                    sb.AppendLine(error);
                    errors.AppendLine(error);
                    numErrors++;
                }
            }
            if (DisplayOutput(title))
            {
                Log(sb.ToString());
            }
            if (errors.Length > 0)
            {
                Log(numErrors + " Errors:");
                Log(errors.ToString());
            }

            DateTime opEnd    = DateTime.Now;
            TimeSpan duration = opEnd - opStart;

            Log($"Done in {duration.TotalMilliseconds}ms." + Environment.NewLine);
        }
Пример #2
0
        private void LexKag(string input, string title = "input")
        {
            ClearLog();
            DateTime opStart = DateTime.Now;

            Token token;
            var   reader  = new StringSourceReader(title, input);
            var   lexer   = new Lexer(reader);
            var   morpher = new Morpher(lexer);
            var   sb      = new StringBuilder();

            do
            {
                try
                {
                    token = morpher.ReadToken();
                }
                catch (ParseException ex)
                {
                    sb.AppendLine(Environment.NewLine +
                                  ex.Position + Environment.NewLine +
                                  ex.GetType() + Environment.NewLine +
                                  ex.Message + Environment.NewLine);
                    break;
                }
                catch (Exception ex)
                {
                    sb.AppendLine(Environment.NewLine +
                                  ex.GetType() + Environment.NewLine +
                                  ex.Message + Environment.NewLine);
                    break;
                }
                if (DisplayOutput(title))
                {
                    sb.AppendLine(token.ToString());
                }
            } while (token.Type != TokenType.Eof);

            DateTime opEnd    = DateTime.Now;
            TimeSpan duration = opEnd - opStart;

            sb.AppendLine($"Done in {duration.TotalMilliseconds}ms." + Environment.NewLine);
            Log(sb.ToString());
        }