Need help with delimited values

Nov 20, 2009 at 4:16 PM

I have a file that I would like to parse that is made up of pipe delimited lines of text where the first value describes the type of line it is.  So for example, the file may look something like this:

ABC | Name | Description | 01234ClienNo | 01-01-1900 | | true
L1 | Name | Description | formula
D1 | Name | 03-15-2010 | N
DA | D1 | Line 1 | formula
DB | D1 | Line 2 | formula

Note that DA and DB are children under D1 and some fields like Description can span multiple lines, but each line type has a set number of fields.

Anyhow, I am currently using the System.String Split method and I'm leaning toward switching to Irony so that the result would be a tree and the lines with formulas would continued to be parsed and maintain awareness of their child/parent relationship.  Originally, I felt that I could continue to use this design and simply parse the formulas, however, Irony seems to have no way to pass the parser meta data to maintain the needed awareness.

The only meta data that seems to be passed in is a filename string which defaults to <source> - referring to the Parse methods in Parser.cs.

So,  what's the most efficient way to handle this scenario? Should I continue to use the Split method from System.String and only use the Parser for the formulas, or is there an easier way for Irony to parse the pipe delimited data and the formulas together while maintaining it's parent/child relationship?


Nov 20, 2009 at 5:15 PM

If you need to pass some "context" values to formula parser, you can use ParsingContext.Values dictionary - it is for storing custom context, values, whatever, and to exchange this information between terminals or between caller program and terminals or custom processing code in grammar. You can reach the ParsingContext object through parser.Context property right after you create Parser instance but before you call parser.Parse(...)

Nov 20, 2009 at 6:30 PM

Great, that should be helpful and reduce my work. I will continue on my original path and use the ParsingContext.Values dictionary to pass custom data between calls.


On a related note, what is an example on where the DsvLiteral would but used?  I know it wouldn't help in my scenario, but while researching, I couldn't think of any practical scenario where I would have some delimited text that's being parsed and convert all of the values to one specific System.TypeCode.




Nov 20, 2009 at 7:04 PM

I will publish a sample grammar using DsvTerminal for reading some comma-delimited files, it will be clear from an example I hope how to use it.

good luck