Partilhar via


Working on a XAML parser

In the large, I can't yet tell the world what I'm working on (hint, PDC).  But the past few weeks I've been hard at work on a XAML parser and serializer.   Now Marc Clifton has shown you can implement a XAML parser in 300 lines of code, so you may wonder why it's taking me so long.

Well, my colleague Chuck and I are working on a source code preserving system.  Read XAML in, change the object graph, write it back out and we change as little source as possible:  comments and whitespace etc. are preserved.  This too wouldn't be hard if we did the editing against an XML-style DOM, but we doing editing against the live (Avalon) objects, and the system has to work for objects we don't know about and who don't know about source code preservation. 

Sunday I implemented the compact syntax parser.  For example, in XAML you can say:
<TransformDecorator Child="*Button(Content=Hello World;Width=100)" />
instead of:
<TransformDecorator>
    <Button Width="100">
       Hello World
    </Button>
</TransformDecorator>

Please, don't use this syntax for an example like this.  I see its usefulness for *Bind and *Alias, but in cases like above it simply provides two ways to do the same thing, one standard XML and one custom.   If you need further incentive, I am advocating dropping the compact syntax before Avalon ships...and I'm fairly confident of success.

Anyway, the parser wasn't that tricky.  I wrote a scanner that returned a set of tokens:  the type is the first token, followed by name-value pairs.  Given the type and the name-value pairs I could reuse the code that parses normal XAML element/attribute sets to create our model.  Next up is Style parsing.  That's pretty much the last thing between us and complete XAML support. 

But Chuck is doing all the hard work--writing the serializer that puts this stuff back where we found it.