Beispiel #1
0
        public void Parse(string code)
        {
            this.Structures = new List<LogicStructure>();
            this.Functions = new List<LogicFunction>();

            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Protogame.Scripting.LogicControl.Grammar.egt");

            var parser = new Parser();
            if (stream != null)
            {
                using (var reader = new BinaryReader(stream))
                {
                    parser.LoadTables(reader);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }

            parser.Open(ref code);

            Reduction root = null;
            var done = false;
            while (!done)
            {
                var response = parser.Parse();

                switch (response)
                {
                    case GOLD.ParseMessage.LexicalError:
                        throw new Exception("Lexical Error:\n" +
                                      "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                      "Read: " + parser.CurrentToken().Data);
                    case GOLD.ParseMessage.SyntaxError:
                        throw new Exception("Syntax Error:\n" +
                                      "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" +
                                      "Read: " + parser.CurrentToken().Data + "\n" +
                                      "Expecting: " + parser.ExpectedSymbols().Text());
                    case GOLD.ParseMessage.Accept:
                        root = (GOLD.Reduction)parser.CurrentReduction;
                        done = true;
                        break;

                    case GOLD.ParseMessage.InternalError:
                        throw new Exception("Internal error");
                    case GOLD.ParseMessage.NotLoadedError:
                        throw new Exception("Tables not loaded");
                    case GOLD.ParseMessage.GroupError:
                        throw new Exception("Runaway group");
                }
            }

            this.TraverseNodes(root);
        }
Beispiel #2
0
 public AbstractParser(bool trimReductions)
 {
     parser = new GOLD.Parser();
     parser.TrimReductions = trimReductions;
 }