Exemple #1
0
 /// <summary>Initializes a new instance of the <see cref="Parser"/> class.</summary>
 /// <param name="globalState"><see cref="DynamicRuntimeState"/> for the parse</param>
 /// <param name="diagnostics">Diagnostic representations to generate when parsing</param>
 public Parser(DynamicRuntimeState globalState
               , DiagnosticRepresentations diagnostics
               )
 {
     GlobalState = globalState.ValidateNotNull(nameof(globalState));
     Diagnostics = diagnostics;
 }
        public void Run(TextReader input, DiagnosticRepresentations diagnostics = DiagnosticRepresentations.None)
        {
            var parser = new Parser(LanguageFeatureLevel, diagnostics);

            using var generator = CreateGenerator(parser.GlobalState);

            ShowPrompt(ReadyState.StartExpression);

            // Create sequence of parsed AST RootNodes to feed the REPL loop
            var replSeq = from stmt in input.ToStatements(ShowPrompt)
                          let node = parser.Parse(stmt)
                                     where !ErrorLogger.CheckAndShowParseErrors(node)
                                     select node;

            foreach (IAstNode node in replSeq)
            {
                try
                {
                    var result = generator.Generate(node);
                    if (result.HasValue)
                    {
                        ShowResults(result.Value !);
                    }
                }
                catch (CodeGeneratorException ex)
                {
                    // This is an internal error that is not recoverable.
                    // Show the error and stop additional processing
                    ErrorLogger.ShowError(ex.ToString( ));
                    break;
                }
            }
        }
Exemple #3
0
 /// <summary>Initializes a new instance of the <see cref="Parser"/> class.</summary>
 /// <param name="globalState"><see cref="DynamicRuntimeState"/> for the parse</param>
 /// <param name="diagnostics">Diagnostic representations to generate when parsing</param>
 /// <param name="lexErrorListener">Error listener for Lexer errors</param>
 /// <param name="parseErrorListener">Error listener for parer errors</param>
 public Parser(DynamicRuntimeState globalState
               , DiagnosticRepresentations diagnostics
               , IAntlrErrorListener <int> lexErrorListener
               , IAntlrErrorListener <IToken> parseErrorListener
               )
 {
     GlobalState        = globalState.ValidateNotNull(nameof(globalState));
     Diagnostics        = diagnostics;
     LexErrorListener   = lexErrorListener;
     ParseErrorListener = parseErrorListener;
     ErrorStrategy      = new ReplErrorStrategy( );
 }
Exemple #4
0
 /// <summary>Initializes a new instance of the <see cref="Parser"/> class.</summary>
 /// <param name="level"><see cref="LanguageLevel"/> for the parser</param>
 /// <param name="listener">Combined error listener for lexer and parser errors</param>
 /// <param name="diagnostics">Diagnostic representations to generate when parsing</param>
 public Parser(LanguageLevel level, IUnifiedErrorListener listener, DiagnosticRepresentations diagnostics = DiagnosticRepresentations.None)
     : this(new DynamicRuntimeState(level), diagnostics, listener, listener)
 {
 }
Exemple #5
0
 /// <summary>Initializes a new instance of the <see cref="Parser"/> class configured for the specified language level</summary>
 /// <param name="level"><see cref="LanguageLevel"/> for the parser</param>
 /// <param name="diagnostics">Diagnostic representations to generate when parsing</param>
 public Parser(LanguageLevel level, DiagnosticRepresentations diagnostics = DiagnosticRepresentations.None)
     : this(level, new FormattedConsoleErrorListener( ), diagnostics)
 {
 }
Exemple #6
0
 /// <summary>Initializes a new instance of the <see cref="Parser"/> class.</summary>
 /// <param name="level"><see cref="LanguageLevel"/> for the parser</param>
 /// <param name="diagnostics">Diagnostic representations to generate when parsing</param>
 public Parser(LanguageLevel level, DiagnosticRepresentations diagnostics = DiagnosticRepresentations.None)
     : this(new DynamicRuntimeState(level), diagnostics)
 {
 }