🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Parsing baby steps, part deux!

Published July 10, 2007
Advertisement
Added escape sequences tonight. Took a little while to figure out how to do them well. Most parsers from what I gather pop tokens from a stream. Escape sequences are fairly straightforward there. If escaping, pop the translated token instead. My setup uses indexes over a common string. Taking the escape sequence then involves re-arranging the string and shuffling indexes to keep things in synch.

Annoying but not insurmountable. EscapeSequence defaults to '\' but can be reconfigured. It also includes "\\" or the equivalent by default with the proper order. Example time:

            EscapeSequence Slash = new EscapeSequence();            Slash.Add('t', ' ');            Parser EscapeTest = (new Expression(new System.Text.RegularExpressions.Regex(@"[a-zA-Z\ ]")) | Slash).OneOrMore;            string EscapeTestString = @"moo\tmoo\\t";            IndexTree EscapeResult = EscapeTest.Parse(EscapeTestString);            Console.WriteLine("Before Escaping: {0}", EscapeTestString);            foreach (string s in EscapeResult.ViewTree(EscapeTestString)) {                Console.WriteLine(s);            }            Console.WriteLine();            Slash.ProcessEscaping(EscapeResult, ref EscapeTestString);                        Console.WriteLine("After Escaping: {0}", EscapeTestString);            foreach (string s in EscapeResult.ViewTree(EscapeTestString)) {                Console.WriteLine(s);            }


Results:
Before Escaping: moo\tmoo\\trms.Support.Parsing.Series  0:11 - moo\tmoo\\t  rms.Support.Parsing.Expression  0:1 - m  rms.Support.Parsing.Expression  1:2 - o  rms.Support.Parsing.Expression  2:3 - o  rms.Support.Parsing.EscapeSequence  3:5 - \t  rms.Support.Parsing.Expression  5:6 - m  rms.Support.Parsing.Expression  6:7 - o  rms.Support.Parsing.Expression  7:8 - o  rms.Support.Parsing.EscapeSequence  8:10 - \\    rms.Support.Parsing.Expression  10:11 - tAfter Escaping: moo moo\trms.Support.Parsing.Series  0:9 - moo moo\t  rms.Support.Parsing.Expression  0:1 - m  rms.Support.Parsing.Expression  1:2 - o  rms.Support.Parsing.Expression  2:3 - o  rms.Support.Parsing.CharParser  3:4 -  rms.Support.Parsing.Expression  4:5 - m  rms.Support.Parsing.Expression  5:6 - o  rms.Support.Parsing.Expression  6:7 - o  rms.Support.Parsing.CharParser  7:8 - \    rms.Support.Parsing.Expression  8:9 - t
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement