My goal is to parse defines in Objective-C code:
#define wowFont [UIFont fontWithName:(DEV)?@"one":@"two" size:(DEV? 10: 12)]
#define wowColor [UIColor colorWithRed:(DEV?240/255.0f:255.0f) green:((DEV)? 230.0f / 255.0f) blue: 10.0f]
I try to write grammar for it:
grammar Defines::Grammar {
token TOP {
\s*<define_expression>\n
}
token define_expression {
^^ #define $<keyword>=\w+ \s+ $<value> = <general_expression>
}
token general_expression{
# here, determine parenthes
<simple_expression>|[(]<general_expression>[)]|<value_expression>
}
token simple_expression{
# here, determine ?: behaviour
\s*<general_expression>\s*[?]\s*<general_expression>\s*[:]\s*<general_expression>\s*
}
token value_expression {
# stop here, need to determine ?: operator here
<numeric_expression>|<string_expression>|<function_invocation_expression>|<bareword_expression>
}
token function_invocation_expression {
[\[] (<class>|<bareword_expression>) \s+ <function_body>\s* [\]]
}
token function_body {
<bareword_expression>|<bareword_expression> \s* [:] <general_expression>
}
token bareword_expression {
[\w_]+
}
token string_expression {
\@["][^"]["]
}
token numeric_expression {
# we could +-*/ numeric expressions, also we can put lead sigil [-]. put parentheses and it could be a <number>
<numeric_expression> [*/+-] <numeric_expression> | [-+]<numberic_expression> | [(] <numeric_expression> [)] | <number>
}
token number {
# number expression here, ok
# not needed legal sign here, numeric_expression already have it
\d+ [\.] \d+
}
}
Could anybody help to improve it?
P.S: As a part of answer (I hope that somebody will find this documents helpful) I place here a link to Perl 6 book (I found out that Grammars section is good enough for beginning). Perl 6 Book