blah

Page 153

T HE JSON D ATA L ANGUAGE

153

Note that I slipped a comment element in there in addition to the expression comments. This is another helpful tool for documenting your work in grammar files. Now, when you go to enter that rule into your growing grammar file, you can use another helpful shortcut. Language grammar files are edited in a property list file format. The syntax3 can be slightly tedious to work with, so it’s really best to let TextMate write most of it for you. You’ll find snippets for this in the Bundles TextMate Language Grammar Snippets. Open the grammar file in a TextMate editing window as you did before, and place your caret just after the last rule you entered. Add a comma to separate the current rule from the one you intend to add, and then type matA to trigger the Rule- –Match snippet. The snippet will start you in the name, and you can A to the match when you are ready. When you’re done with the new rule, expand the JSON example data with some numbers so you can see TextMate pick them up: [true, false, null, 10, 123.45, 0.1e-23]

TextMate’s Language Parser You’re ready to add JSON’s strings into the mix, but to get that right, I need to show you a little about how TextMate’s parser works. For example, change the sample JSON content to the following: "a JSON string"

Now, let’s introduce a naive rule to match the JSON string: { name = 'string.quoted.double.json' ; match = '"[^"]*"' ; },

I’m ignoring the escaping issues at the moment, but this rule does seem to identify the string just fine. The problem comes when you try a trickier JSON string, even without escapes. Switch the sample string to this: "a multiline JSON string"

The current string rule does not match this legal JSON string. However, if you think about it, the regular expression should match. It looks for zero or more nonquote characters surrounded by a pair of quotes, and 3.

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Articles/OldStylePListsConcept.html#//apple_ref/doc/


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.