Exemple #1
0
        private object Parse(string txt)
        {
            lock (this)
            {
                object result = null;

                txt = txt.Replace("\r\n", "\n") + "\n";

                parser.Open(ref txt);
                parser.TrimReductions = false;

                bool done = false;
                while (!done)
                {
                    GOLD.ParseMessage response = parser.Parse();

                    switch (response)
                    {
                    case GOLD.ParseMessage.LexicalError:
                    case GOLD.ParseMessage.SyntaxError:
                    case GOLD.ParseMessage.InternalError:
                    case GOLD.ParseMessage.NotLoadedError:
                    case GOLD.ParseMessage.GroupError:
                        Fail(response);
                        break;

                    case GOLD.ParseMessage.Reduction:                             // Reduction
                        parser.CurrentReduction = GrammarTableMap.CreateNewASTObject(parser.CurrentReduction as GOLD.Reduction, parser.CurrentPosition());
                        break;

                    case GOLD.ParseMessage.Accept:                             //Accepted!
                        result = parser.CurrentReduction;
                        done   = true;
                        break;

                    case GOLD.ParseMessage.TokenRead:                             //You don't have to do anything here.
                        break;
                    }
                }

                return(result);
            }
        }
        public bool Parse(TextReader reader)
        {
            //This procedure starts the GOLD Parser Engine and handles each of the
            //messages it returns. Each time a reduction is made, you can create new
            //custom object and reassign the .CurrentReduction property. Otherwise,
            //the system will use the Reduction object that was returned.
            //
            //The resulting tree will be a pure representation of the language
            //and will be ready to implement.

            GOLD.ParseMessage response;
            bool done;                      //Controls when we leave the loop
            bool accepted = false;          //Was the parse successful?

            parser.Open(reader);
            parser.TrimReductions = false;  //Please read about this feature before enabling

            done = false;
            while (!done)
            {
                response = parser.Parse();

                switch (response)
                {
                case GOLD.ParseMessage.LexicalError:
                    //Cannot recognize token
                    FailMessage = "Lexical Error:\n" +
                                  "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                  "Read: " + parser.CurrentToken().Data;
                    done = true;
                    break;

                case GOLD.ParseMessage.SyntaxError:
                    //Expecting a different token
                    FailMessage = "Syntax Error:\n" +
                                  "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                  "Read: " + parser.CurrentToken().Data + "\n" +
                                  "Expecting: " + parser.ExpectedSymbols().Text();
                    done = true;
                    break;

                case GOLD.ParseMessage.Reduction:
                    //For this project, we will let the parser build a tree of Reduction objects
                    //parser.CurrentReduction = CreateNewObject(parser.CurrentReduction);
                    break;

                case GOLD.ParseMessage.Accept:
                    //Accepted!
                    Root     = (GOLD.Reduction)parser.CurrentReduction;    //The root node!
                    done     = true;
                    accepted = true;
                    break;

                case GOLD.ParseMessage.TokenRead:
                    //You don't have to do anything here.
                    break;

                case GOLD.ParseMessage.InternalError:
                    //INTERNAL ERROR! Something is horribly wrong.
                    done = true;
                    break;

                case GOLD.ParseMessage.NotLoadedError:
                    //This error occurs if the CGT was not loaded.
                    FailMessage = "Tables not loaded";
                    done        = true;
                    break;

                case GOLD.ParseMessage.GroupError:
                    //GROUP ERROR! Unexpected end of file
                    FailMessage = "Runaway group";
                    done        = true;
                    break;
                }
            } //while

            return(accepted);
        }
Exemple #3
0
        public bool Parse(TextReader reader)
        {
            // This procedure starts the GOLD Parser Engine and handles each of the
            // messages it returns. Each time a reduction is made, you can create new
            // custom object and reassign the .CurrentReduction property. Otherwise,
            // the system will use the Reduction object that was returned.
            //
            // The resulting tree will be a pure representation of the language
            // and will be ready to implement.

            GOLD.ParseMessage response;
            bool done;                      // Controls when we leave the loop
            bool accepted = false;          // Was the parse successful?

            parser.Open(reader);
            parser.TrimReductions = false;  // Please read about this feature before enabling

            done = false;
            while (!done)
            {
                response = parser.Parse();

                switch (response)
                {
                case GOLD.ParseMessage.LexicalError:
                    // Cannot recognize token
                    done = true;
                    break;

                case GOLD.ParseMessage.SyntaxError:
                    // Expecting a different token
                    done = true;
                    break;

                case GOLD.ParseMessage.Reduction:
                    // Create a customized object to store the reduction

                    parser.CurrentReduction = CreateNewObject(parser.CurrentReduction as GOLD.Reduction);
                    break;

                case GOLD.ParseMessage.Accept:
                    // Accepted!
                    //program = parser.CurrentReduction   // The root node!
                    done     = true;
                    accepted = true;
                    break;

                case GOLD.ParseMessage.TokenRead:
                    // You don't have to do anything here.
                    break;

                case GOLD.ParseMessage.InternalError:
                    // INTERNAL ERROR! Something is horribly wrong.
                    done = true;
                    break;

                case GOLD.ParseMessage.NotLoadedError:
                    // This error occurs if the CGT was not loaded.
                    done = true;
                    break;

                case GOLD.ParseMessage.GroupError:
                    // GROUP ERROR! Unexpected end of file
                    done = true;
                    break;
                }
            }

            return(accepted);
        }
        public bool Parse(TextReader reader)
        {
            GOLD.ParseMessage response;
            bool done;                      //Controls when we leave the loop
            bool accepted = false;          //Was the parse successful?

            parser.Open(reader);
            parser.TrimReductions = false;  //Please read about this feature before enabling

            done = false;
            while (!done)
            {
                response = parser.Parse();

                switch (response)
                {
                case GOLD.ParseMessage.LexicalError:
                    //Cannot recognize token
                    FailMessage = "Lexical Error:\n" +
                                  "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                  "Read: " + parser.CurrentToken().Data;
                    done = true;
                    break;

                case GOLD.ParseMessage.SyntaxError:
                    //Expecting a different token
                    FailMessage = "Syntax Error:\n" +
                                  "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                  "Read: " + parser.CurrentToken().Data + "\n" +
                                  "Expecting: " + parser.ExpectedSymbols().Text();
                    done = true;
                    break;

                case GOLD.ParseMessage.Reduction:
                    //For this project, we will let the parser build a tree of Reduction objects
                    //parser.CurrentReduction = CreateNewObject(parser.CurrentReduction);
                    break;

                case GOLD.ParseMessage.Accept:
                    //Accepted!
                    Root     = (GOLD.Reduction)parser.CurrentReduction;    //The root node!
                    done     = true;
                    accepted = true;
                    break;

                case GOLD.ParseMessage.TokenRead:
                    //You don't have to do anything here.
                    break;

                case GOLD.ParseMessage.InternalError:
                    //INTERNAL ERROR! Something is horribly wrong.
                    done = true;
                    break;

                case GOLD.ParseMessage.NotLoadedError:
                    //This error occurs if the CGT was not loaded.
                    FailMessage = "Tables not loaded";
                    done        = true;
                    break;

                case GOLD.ParseMessage.GroupError:
                    //GROUP ERROR! Unexpected end of file
                    FailMessage = "Runaway group";
                    done        = true;
                    break;
                }
            } //while

            return(accepted);
        }
Exemple #5
0
        public override bool Parse(string source, out IReduction root)
        {
            //This procedure starts the GOLD Parser Engine and handles each of the
            //messages it returns. Each time a reduction is made, you can create new
            //custom object and reassign the .CurrentReduction property. Otherwise,
            //the system will use the Reduction object that was returned.
            //
            //The resulting tree will be a pure representation of the language
            //and will be ready to implement.

            GOLD.ParseMessage response;
            bool done;                      //Controls when we leave the loop
            bool accepted = false;          //Was the parse successful?

            root = null;

            StringReader reader = new StringReader(source);

            parser.Open(reader);
            parser.TrimReductions = false;  //Please read about this feature before enabling

            done = false;
            while (!done)
            {
                response = parser.Parse();

                switch (response)
                {
                case GOLD.ParseMessage.LexicalError:
                    //Cannot recognize token
                    failLineNumber   = parser.CurrentPosition().Line;
                    failColumnNumber = parser.CurrentPosition().Column;
                    failLength       = parser.CurrentToken().Data.ToString().Length;
                    failMessage      = "Lexical Error:" + Environment.NewLine +
                                       "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + Environment.NewLine +
                                       "Read: " + parser.CurrentToken().Data;
                    done = true;
                    break;

                case GOLD.ParseMessage.SyntaxError:
                    //Expecting a different token
                    failLineNumber   = parser.CurrentPosition().Line;
                    failColumnNumber = parser.CurrentPosition().Column;
                    failLength       = parser.CurrentToken().Data.ToString().Length;
                    failMessage      = "Syntax Error:" + Environment.NewLine +
                                       "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + Environment.NewLine +
                                       "Read: " + parser.CurrentToken().Data + Environment.NewLine +
                                       "Expecting one of: " + parser.ExpectedSymbols().Text();
                    done = true;
                    break;

                case GOLD.ParseMessage.Reduction:
                    //For this project, we will let the parser build a tree of Reduction objects
                    // parser.CurrentReduction = CreateNewObject(parser.CurrentReduction);
                    break;

                case GOLD.ParseMessage.Accept:
                    //Accepted!
                    root     = new CookV5Reduction((GOLD.Reduction)parser.CurrentReduction);    //The root node!
                    done     = true;
                    accepted = true;
                    break;

                case GOLD.ParseMessage.TokenRead:
                    //You don't have to do anything here.
                    break;

                case GOLD.ParseMessage.InternalError:
                    //INTERNAL ERROR! Something is horribly wrong.
                    done = true;
                    break;

                case GOLD.ParseMessage.NotLoadedError:
                    //This error occurs if the CGT was not loaded.
                    failLineNumber   = 0;
                    failColumnNumber = 0;
                    failLength       = 0;
                    failMessage      = "Tables not loaded";
                    done             = true;
                    break;

                case GOLD.ParseMessage.GroupError:
                    //GROUP ERROR! Unexpected end of file
                    failLineNumber   = 0;
                    failColumnNumber = 0;
                    failLength       = 0;
                    failMessage      = "COMMENT ERROR! Unexpected end of file";
                    done             = true;
                    break;
                }
            } //while

            return(accepted);
        }
Exemple #6
0
        public List <string> Parse(TextReader reader)
        {
            //This procedure starts the GOLD Parser Engine and handles each of the
            //messages it returns. Each time a reduction is made, you can create new
            //custom object and reassign the .CurrentReduction property. Otherwise,
            //the system will use the Reduction object that was returned.
            //
            //The resulting tree will be a pure representation of the language
            //and will be ready to implement.

            //reset root node
            root = null;

            GOLD.ParseMessage response;
            bool   done;                    //Controls when we leave the loop
            bool   accepted = false;        //Was the parse successful?
            string statementType;

            statementType = "unknown";
            string errorType;

            errorType = "none";

            parser.Open(reader);
            parser.TrimReductions = false;  //Please read about this feature before enabling

            done = false;
            while (!done)
            {
                response = parser.Parse();

                switch (response)
                {
                case GOLD.ParseMessage.LexicalError:
                    //Cannot recognize token
                    errorType = "lexical";
                    done      = true;
                    break;

                case GOLD.ParseMessage.SyntaxError:
                    //Expecting a different token
                    errorType = "syntax";
                    done      = true;
                    break;

                case GOLD.ParseMessage.Reduction:
                    //Create a customized object to store the reduction
                    string tmp = (string)CreateNewObject(parser.CurrentReduction as GOLD.Reduction);
                    if (tmp != null)
                    {
                        statementType = tmp;
                    }
                    break;

                case GOLD.ParseMessage.Accept:
                    //Accepted!
                    root     = (GOLD.Reduction)parser.CurrentReduction;   //The root node!
                    done     = true;
                    accepted = true;
                    break;

                case GOLD.ParseMessage.TokenRead:
                    //You don't have to do anything here.
                    break;

                case GOLD.ParseMessage.InternalError:
                    //INTERNAL ERROR! Something is horribly wrong.
                    errorType = "internal";
                    done      = true;
                    break;

                case GOLD.ParseMessage.NotLoadedError:
                    //This error occurs if the CGT was not loaded.
                    errorType = "cgt";
                    done      = true;
                    break;

                case GOLD.ParseMessage.GroupError:
                    //GROUP ERROR! Unexpected end of file
                    errorType = "eof";
                    done      = true;
                    break;
                }
            } //while

            List <string> tokens = new List <string>();

            if (root != null)
            {
                tokens = GetSQLLeafNodes(root);
            }

            tokens.Add(statementType);
            tokens.Add(done.ToString());
            tokens.Add(accepted.ToString());
            tokens.Add(errorType);

            return(tokens);
        }