Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (62 sloc) 2.7 KB
A Fake Grammar for JSON with comments (JSON does not have comments).
@singleLineComments = '#';
@multiLineComments = '/*' '*/';
@before {
PKTokenizer *t = self.tokenizer;
// whitespace
self.silentlyConsumesWhitespace = YES;
t.whitespaceState.reportsWhitespaceTokens = YES;
self.assembly.preservesWhitespaceTokens = YES;
// comments
t.commentState.reportsCommentTokens = YES;
[t setTokenizerState:t.commentState from:'/' to:'/'];
[t.commentState addSingleLineStartMarker:@"//"];
[t.commentState addMultiLineStartMarker:@"/*" endMarker:@"*/"];
= (Empty | array | object) comment?;
object = openCurly comment? objectContent closeCurly;
objectContent = Empty | actualObject;
actualObject = property commaProperty*;
property = propertyName colon comment? val;
commaProperty = comma comment? property;
propertyName = QuotedString; // NOTE: property names are quoted in JSON
array = openBracket comment? arrayContent closeBracket;
arrayContent = Empty | actualArray;
actualArray = val commaValue*;
commaValue = comma comment? val;
val = (nullLiteral | trueLiteral | falseLiteral | number | string | array | object) comment?;
comment = Comment;
string = QuotedString;
number = Number;
nullLiteral = 'null';
trueLiteral = 'true';
falseLiteral = 'false';
openCurly = '{';
closeCurly = '}';
openBracket = '[';
closeBracket = ']';
comma = ',';
colon = ':';
The built-in terminal production types are (note titlecase) : Empty, Number, QuotedString, Word, etc.
You define collections (alternations, repetitions or sequences) and literal terminals (in quotes). These should be lowercase.
The special '@start' production is the outermost production.
you can discard tokens by following them with a bang ('!') like:
closeCurly = '}'!;
Feed this grammar to -[PKParserFactory parserFromGrammar:assembler:] along with a reference
to an assembler (a call back delegate), and it will return a PKParser object which can
parse strings conforming to this language/grammar.
The when the returned parser is used, the provided assembler will receive method callbacks for each production matched, like:
Each callback method must accept a single PKAssembly argument. This assembly argument
will contain information about the progress made on the the string currently being parsed.
Also the assembly's stack will contain the tokens parsed so far in the current statement.