我正在尝试使用 Lemon 和 Apple Core Foundation 编写一个简单的 JSON 解析器。
这是到目前为止的代码:
%include {
#import <CoreFoundation/CoreFoundation.h>
#import "state.h" // struct ParserState { CFTypeRef result; };
#import "tuple.h" // struct Tuple { CFTypeRef one; CFTypeRef two; };
}
%start_symbol json
%token_type { CFTypeRef }
%token_prefix T
%extra_argument { ParserStateRef state }
%type simple_value { CFTypeRef }
%type member { TupleRef }
%type members { CFMutableDictionaryRef }
%type object { CFMutableDictionaryRef }
%type array { CFMutableArrayRef }
simple_value(A) ::= STRING(B). { A = B; }
simple_value(A) ::= INT(B). { A = B; }
simple_value(A) ::= FLOAT(B). { A = B; }
simple_value(A) ::= FALSE. { A = kCFBooleanFalse; }
simple_value(A) ::= TRUE. { A = kCFBooleanTrue; }
simple_value(A) ::= NULL. { A = kCFNull; }
member(A) ::= STRING(B) COLON simple_value(C). {
A = TupleCreate(B,C);
}
member ::= STRING COLON object.
member ::= STRING COLON array.
members(A) ::= member(B). {
A = CFDictionaryCreateMutable(kCFAllocatorDefault,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(A, B->first, B->second);
CFRelease(B->first);
CFRelease(B->second);
TupleRelease(B);
}
members(A) ::= members(B) COMMA member(C). {
CFDictionarySetValue(B, C->first, C->second);
CFRelease(C->first);
CFRelease(C->second);
TupleRelease(C);
A = B;
}
values ::= value.
values ::= values COMMA value.
object(A) ::= LCB RCB. {
/* THIS NEVER GETS CALLED */
A = CFDictionaryCreateMutable(kCFAllocatorDefault,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
}
object(A) ::= LCB members(B) RCB. {
/* THIS NEVER GETS CALLED */
A = B;
}
array ::= LSB RSB.
array ::= LSB values RSB.
value ::= array.
value ::= object.
value ::= simple_value.
json ::= object(A). { state->result = A; }
json ::= array.
使用这样的简单 JSON
{ \"hello\" : \"world\" }
我不能超越成员规则(此时,字典设置正确)。
永远不会调用对象规则,而json ::= 对象也是如此!
我在做傻事吗?
任何输入将不胜感激!