pacc logo
pacc v0.3: Guards

Next: , Previous: , Up: Top   [Contents][Index]


16 Guards

In the last section, we introduced the any matcher ‘.’ which matches any character all, and then decided instead to use a negated character class matcher which matches almost any character. To make much use of the any matcher, we need guards.

A matcher really does two jobs. First, it checks to see if the input at the current position matches. Then, if it does, it consumes that input by moving the current position forwards.

A guard is a matcher modified so that it only checks, and never consumes. Any matcher can be turned into a guard by prefixing it with ‘&’. For example, the rule ‘Proto <- ( "ftp" / "http" ) &":"’ matches either of two well-known protocols, but only if it is immediately followed by a colon ‘:’.

Guards made with the ‘&’ are positive guards. They are not actually as useful as negative guards; any matcher can be turned into a negative guard by prefixing it with ‘!’. A negative guard succeeds, provided the input at the current position does not match. Negative guards work well in conjunction with the any matcher.

The venerable language Pascal surrounds comments with braces: ‘{’ and ‘}’. (Pascal comments do not nest: ‘{’ may not occur within a comment.) We can easily write a pacc rule to match Pascal comments, using a negated character class again:

PascalComment <- "{" [^{}]* "}"

How would we match C comments? They are surrounded by ‘/*’ and ‘*/’. (C comments also do not nest: ‘/*’ is permitted within a comment, but it doesn’t have any meaning.) Character classes aren’t going to help here, as they deal with just one character at a time. We need a combination of a negative guard and the any matcher:

CComment <- "/*" ( !"*/" . )* "*/"

The key to this rule is the parenthesized expression: ‘!"*/" .’. This means “check that the input at the current position is not ‘*/’, and then match any character at all”. Then we apply the repetition operator ‘*’ to that, and we get an expression that consumes everything up to, but not including, the next occurrence of ‘*/’ in the input.


Next: , Previous: , Up: Top   [Contents][Index]

Last updated: 2016-08-03 21:39:50 UTC

News

Porting and packaging

One thing pacc needs is more users. And, perhaps, one way to get more users is to reduce the friction in getting started with pacc. An obvious lubricant is packaging. Read More...

Release relief

Looking at _pacc_coords(), I noticed that it seemed to have the same realloc() bug that I'd just fixed in _pacc_result(). However, the "list of arrays" trick really wasn't going to work here. Read More...

See more news articles

feed