Ejemplo n.º 1
0
        public object Parse(string source)
        {
            NodesStack = new Stack();
            LRParser = new GoldParser.Parser(new StringReader(source), LanguageGrammar);
            parsertools.parser = LRParser;
            LRParser.TrimReductions = true;
            try
            {
                while (true)
                {
                    switch (LRParser.Parse())
                    {
                        case GoldParser.ParseMessage.LexicalError:
                            errors.Add(new TokenReadError(this));
                            LRParser.PopInputToken();
                            if (errors.Count >= max_errors)
                                return null;
                            break;

                        //sdes nugno vesti chtoto vrode steka poslednix oshibok. podumat
                        case GoldParser.ParseMessage.SyntaxError:
                            GoldParser.Symbol sym = LRParser.PopInputToken();
                            error = new HaskellParser.Errors.UnexpectedToken(this);
                            //error = new UnexpectedToken(this, parsertools.symbol_collection_to_string(this.LRParser.GetExpectedTokens()));
                            if (sym.SymbolType == GoldParser.SymbolType.End && errors.Count > 0)
                                return null;
                            errors.Add(error);
                            if (errors.Count >= max_errors)
                                return null;
                            break;
                        case GoldParser.ParseMessage.Reduction:
                            LRParser.TokenSyntaxNode = CreateNonTerminalObject(LRParser.ReductionRule.Index);
                            if ((LRParser.TokenSyntaxNode as syntax_tree_node) != null)
                                prev_node = LRParser.TokenSyntaxNode;
                            break;

                        case GoldParser.ParseMessage.Accept:
                            return LRParser.TokenSyntaxNode;

                        case GoldParser.ParseMessage.TokenRead:
                            LRParser.TokenSyntaxNode = CreateTerminalObject(LRParser.TokenSymbol.Index);
                            break;

                        case GoldParser.ParseMessage.InternalError:
                            errors.Add(new CompilerInternalError("HaskellParser", new Exception("ParseMessage.InternalError")));
                            return null;

                        case GoldParser.ParseMessage.NotLoadedError:
                            errors.Add(new CompilerInternalError("HaskellParser", new Exception("ParseMessage.NotLoadedError")));
                            return null;

                        case GoldParser.ParseMessage.CommentError:
                            errors.Add(new PascalABCCompiler.Errors.UnexpectedToken(this, "(EOF)"));
                            return null;

                        /*case ParseMessage.CommentBlockRead:
                            break;

                        case ParseMessage.CommentLineRead:
                            break;*/
                    }
                }
            }
            catch (Exception e)
            {
                Error_Clear();
                if (errors.Count > 0)
                    return null;
                else
                    throw;
            }

        }
Ejemplo n.º 2
0
        public object Parse(string source)
        {
            NodesStack = new Stack();
            LRParser = new GoldParser.Parser(new StringReader(source), LanguageGrammar);
            parsertools.parser  = LRParser;
            LRParser.TrimReductions = true;
            try
            {
                while (true)
                {
                    switch (LRParser.Parse())
                    {
                        case GoldParser.ParseMessage.LexicalError:
                            errors.Add(new TokenReadError(this));
                            LRParser.PopInputToken();
                            if (errors.Count >= max_errors)
                                return null;
                            break;

                        //sdes nugno vesti chtoto vrode steka poslednix oshibok. podumat
                        case GoldParser.ParseMessage.SyntaxError:
                            GoldParser.Symbol sym = LRParser.PopInputToken();
                            error = new PABCNETUnexpectedToken(this);
                            //error = new UnexpectedToken(this, parsertools.symbol_collection_to_string(this.LRParser.GetExpectedTokens()));
                            if (sym.SymbolType == GoldParser.SymbolType.End && errors.Count > 0)
                                return null;
                            errors.Add(error);
                            if (errors.Count >= max_errors)
                                return null;
                            break;
                        case GoldParser.ParseMessage.Reduction:
                            LRParser.TokenSyntaxNode = CreateNonTerminalObject(LRParser.ReductionRule.Index);
                            if ((LRParser.TokenSyntaxNode as syntax_tree_node) != null)
                                prev_node = LRParser.TokenSyntaxNode;
                            break;

                        case GoldParser.ParseMessage.Accept:
                            return LRParser.TokenSyntaxNode;

                        case GoldParser.ParseMessage.TokenRead:
                            LRParser.TokenSyntaxNode = CreateTerminalObject(LRParser.TokenSymbol.Index);
                            break;

                        case GoldParser.ParseMessage.InternalError:
                            errors.Add(new CompilerInternalError("PascalABCParser", new Exception("ParseMessage.InternalError")));
                            return null;

                        case GoldParser.ParseMessage.NotLoadedError:
                            errors.Add(new CompilerInternalError("PascalABCParser", new Exception("ParseMessage.NotLoadedError")));
                            return null;

                        case GoldParser.ParseMessage.CommentError:
                            errors.Add(new UnexpectedToken(this, "(EOF)"));
                            return null;
                            
                        case GoldParser.ParseMessage.CommentBlockRead:
                            {
                                /*
                                compiler_directive cd = new compiler_directive();
                                cd.source_context = parsertools.GetTokenSourceContext(this.LRParser);
                                string comment = this.LRParser.CommentText;
                                cd.Name = new token_info(comment);
                                cd.Directive = new token_info(comment);
                                cd.source_context = new SourceContext(cd.source_context,parsertools.GetTokenSourceContext(this.LRParser));
                                if (cd.Directive.text[0] == '{' && cd.Directive.text[1] == '$')
                                    this.errors.Add(new Preprocessor_2.Errors.SyntaxErrorInDirective(cd.source_context.FileName, cd.source_context, cd.Directive.text));
                                    
                                this.CompilerDirectives.Add(cd);
                                */
                                                                                                                                         
                            } break;

                        case GoldParser.ParseMessage.CommentLineRead:
                            {
                                 /* 
                                 compiler_directive cd = new compiler_directive();
                                 cd.source_context = parsertools.GetTokenSourceContext(this.LRParser);
                                 string comment = this.LRParser.CommentText;
                                 cd.Name = new token_info(comment);
                                 cd.Directive = new token_info(comment);
                                 cd.source_context = new SourceContext(cd.source_context.begin_position.line_num, cd.source_context.begin_position.column_num, cd.source_context.end_position.line_num, cd.source_context.end_position.column_num + comment.Length - Environment.NewLine.Length);
                                 this.CompilerDirectives.Add(cd); 
                                 */
                            } break;

                    }
                }
            }
            catch (GoldParser.UnexpectedEOFinParseCommentBlock)
            {
                throw new TokenReadError(this);
            }
            catch (Exception e)
            {
                if (errors.Count > 0)
                    return null;
                else
                    throw;
            }

        }