I have a Location class which represents a location somewhere in a stream. (The class isn’t coupled to any specific stream.) The location information will be used to match tokens to location in the input in my parser, to allow for nicer error reporting to the user.
I want to add location tracking to a TextReader instance. This way, while reading tokens, I can grab the location (which is updated by the TextReader as data is read) and give it to the token during the tokenization process.
I am looking for a good approach on accomplishing this goal. I have come up with several designs.
Manual location tracking
Every time I need to read from the TextReader, I call AdvanceString on the Location object of the tokenizer with the data read.
Advantages
- Very simple.
- No class bloat.
- No need to rewrite the
TextReadermethods.
Disadvantages
- Couples location tracking logic to tokenization process.
- Easy to forget to track something (though unit testing helps with this).
- Bloats existing code.
Plain TextReader wrapper
Create a LocatedTextReaderWrapper class which surrounds each method call, tracking a Location property. Example:
public class LocatedTextReaderWrapper : TextReader {
private TextReader source;
public Location Location {
get;
set;
}
public LocatedTextReaderWrapper(TextReader source) :
this(source, new Location()) {
}
public LocatedTextReaderWrapper(TextReader source, Location location) {
this.Location = location;
this.source = source;
}
public override int Read(char[] buffer, int index, int count) {
int ret = this.source.Read(buffer, index, count);
if(ret >= 0) {
this.location.AdvanceString(string.Concat(buffer.Skip(index).Take(count)));
}
return ret;
}
// etc.
}
Advantages
- Tokenization doesn’t know about Location tracking.
Disadvantages
- User needs to create and dispose a
LocatedTextReaderWrapperinstance, in addition to theirTextReaderinstance. - Doesn’t allow different types of tracking or different location trackers to be added without layers of wrappers.
Event-based TextReader wrapper
Like LocatedTextReaderWrapper, but decouples it from the Location object raising an event whenever data is read.
Advantages
- Can be reused for other types of tracking.
- Tokenization doesn’t know about Location tracking or other tracking.
- Can have multiple, independent
Locationobjects (or other methods of tracking) tracking at once.
Disadvantages
- Requires boilerplate code to enable location tracking.
- User needs to create and dispose the wrapper instance, in addition to their
TextReaderinstance.
Aspect-orientated approach
Use AOP to perform like the event-based wrapper approach.
Advantages
- Can be reused for other types of tracking.
- Tokenization doesn’t know about Location tracking or other tracking.
- No need to rewrite the
TextReadermethods.
Disadvantages
- Requires external dependencies, which I want to avoid.
I am looking for the best approach in my situation. I would like to:
- Not bloat the tokenizer methods with location tracking.
- Not require heavy initialization in user code.
- Not have any/much boilerplate/duplicated code.
- (Perhaps) not couple the
TextReaderwith theLocationclass.
Any insight into this problem and possible solutions or adjustments are welcome. Thanks!
(For those who want a specific question: What is the best way to wrap the functionality of a TextReader?)
I have implemented the “Plain TextReader wrapper” and “Event-based TextReader wrapper” approaches and am displeased with both, for reasons mentioned in their disadvantages.
I agree that creating a wrapper that implements
TextReader, delegates the implementation to the underlyingTextReaderand adds some additional support for tracking of location is a good approach. You can think of this as the Decorator pattern, because you’re decorating theTextReaderclass with some additional functionality.Better wrapping: You can write it in a simpler way to decouple your
TextReaderfrom theLocationtype – this way you can easily modifyLocationindependently or even provide other features that are based on tracking the progress of reading.I would also encapsulate creation of the tracker into some factory method (so that you have a single place in the application that deals with construction). Note that you can use this simple desing to create a
TextReaderthat reports the progress to multipleLocationobjects too:This creates a chain of TrackingReader objects and each of the object is reporting the progress of reading to one of the trackers passed as arguments.
Regarding Event-based: I think that using standard .NET/C# events for this kind of thing isn’t done as frequently in the .NET libraries, but I think this approach may be quite interesting too – especially in C# 3 where you can use features like lambda expressions or even Reactive Framework.
Simple use doesn’t add that much boiler plate:
However, you could also use Reactive Extensions to process the events. To count the number of new lines in the source text, you could write something like this:
This would give you an event
resthat fires each time you read some string from theTextReaderand the value carried by the event would be the current number of line (in the text).