• entries
    359
  • comments
    237
  • views
    188206

Parsing baby steps, part deux!

Sign in to follow this  
Telastyn

38 views

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\\t
rms.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 - t

After Escaping: moo moo\t
rms.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


Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now