BNFParser is a Backus-Naur Form Framework for Objective C written by Mike Friesen released under the Apache 2 Open Source License.
An Java version is also available.
BNFParser was inspired by the framework ParseKit by Todd Ditchendorf.
The BNFParser Framework offers 3 basic services of general interest to developers:
-
String Tokenization via the
BNFTokenizerFactory
andBNFToken
classes -
Property Key/Value mapper via
PropertyParser
-
Text Parsing via Grammars via BNFParser see grammar syntax
The string tokenizer breaks down any string into a series of letter/number/symbols for easy processing.
NSString *text = @"The cow jumped over the moon!";
BNFTokenizerFactory *factory = [[BNFTokenizerFactory alloc] init];
BNFToken *token = [factory tokens:text];
while (token) {
NSLog(@"TOKEN %@", [token stringValue]);
token = [token nextToken];
}
Uses the string tokenizer to parse a string and create key/value mapping based on the '='
symbol.
NSString *text = @"sample key = sample value";
PropertyParser *propertyParser = [[PropertyParser alloc] init];
NSMutableDictionary *keyValueMap = [propertyParser parse:text];
STAssertNotNil([keyValueMap objectForKey:@"sample key"], @"expect key exists");
BNFParser currently only ships with a JSON grammar so the example are based on that.
// Create String Tokens
NSString *text = @"{ \"key\":\"value\"}";
BNFTokenizerFactory *factory = [[BNFTokenizerFactory alloc] init];
BNFToken *token = [factory tokens:text];
// Create Backus-Naur Form State Definitions
BNFSequenceFactory *sf = [[BNFSequenceFactory alloc] init];
NSMutableDictionary *map = [sf json];
// Run Tokens through Parser
BNFParser *parser = [[BNFParser alloc] initWithStateDefinitions:dic];
BNFParseResult *result = [parser parse:token];
// Verify results
// verify text passes grammar:
STAssertTrue([result success], @"assume success");
// the "first" token, same as the token returned from the tokenizer factory:
STAssertNotNil([result top], @"assume not nil");
// the "first" error token, this token and any afterwards are considered to not have passed the grammar:
STAssertNil([result error], @"assume nil");
For bugs, questions and discussions please use the Github Issues.
We love contributions! If you'd like to contribute please submit a pull request via Github.
GoJSON - JSON Editor
This library is distributed under the Apache 2 Open Source License.