beelsebob / CoreParse

A shift/reduce parsing framework for Mac OS X and iOS
BSD 3-Clause "New" or "Revised" License
366 stars 49 forks source link

Tagging in productions #14

Closed beelsebob closed 12 years ago

beelsebob commented 12 years ago

Summary: At the moment, CPParseResult implementations are highly reliant on the position of elements within productions. This means that if the grammar is edited, some significant modifications to the relevant initWithSyntaxTree: method. It would be extremely useful to provide a method of accessing elements without relying on position in the rule.

Proposal: Allow the use of keys as so in productions: A ::= b@<B> c@<C> ('{' d@<B>* '}')? CPSyntaxTree would gain an additional method -childForKey:. In this instance, with the following results: [tree childForKey:@"b"] – equal to the current [[tree children] objectAtIndex:0]. [tree childForKey:@"c"] – equal to the current [[tree children] objectAtIndex:1]. [tree childForKey:@"d"] – equal to the current [[[[[tree children] objectAtIndex:2] objectAtIndex:0] objectAtIndex:0] objectAtIndex:1] if the ? production exists, nil otherwise. [tree childForKey:@"e"] – nil.

The use of keys inside ()*s and ()+s would be disallowed, as would duplicate keys on the same section of an alternative. All of the following would be invalid: A ::= b@<B> b@<B> A ::= ( b@<B> <C> )* A ::= ( b@<B> <C> )+ This however would be allowed: A :: = b@<B> | b@<C>

Notes: It has been suggested that : is a better character than @ for keys: A ::= b:<B> c:<C> ('{' d:<B>* '}')?