Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Parser"/> class.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="scanner">The scanner.</param>
        /// <param name="root">The root.</param>
        /// <exception cref="Exception">
        ///   </exception>
        public Parser(LanguageData language, Scanner scanner, NonTerminal root)
        {
            Language = language;
            Context  = new ParsingContext(this);
            Scanner  = scanner ?? language.CreateScanner();

            if (Scanner != null)
            {
                Scanner.Initialize(this);
            }
            else
            {
                Language.Errors.Add(GrammarErrorLevel.Error, null, "Scanner is not initialized for this grammar");
            }
            CoreParser = new CoreParser(this);
            Root       = root;
            if (Root == null)
            {
                Root         = Language.Grammar.Root;
                InitialState = Language.ParserData.InitialState;
            }
            else
            {
                if (Root != Language.Grammar.Root && !Language.Grammar.SnippetRoots.Contains(Root))
                {
                    throw new Exception(string.Format(Resources.ErrRootNotRegistered, root.Name));
                }

                InitialState = Language.ParserData.InitialStates[Root];
            }
        }
Ejemplo n.º 2
0
        public StringSet GetExpectedTermSet()
        {
            if (CurrentParserState == null)
            {
                return(new StringSet());
            }
            //See note about multi-threading issues in ComputeReportedExpectedSet comments.
            if (CurrentParserState.ReportedExpectedSet == null)
            {
                CurrentParserState.ReportedExpectedSet = CoreParser.ComputeGroupedExpectedSetForState(Language.Grammar, CurrentParserState);
            }
            //Filter out closing braces which are not expected based on previous input.
            // While the closing parenthesis ")" might be expected term in a state in general,
            // if there was no opening parenthesis in preceding input then we would not
            //  expect a closing one.
            var expectedSet = FilterBracesInExpectedSet(CurrentParserState.ReportedExpectedSet);

            return(expectedSet);
        }