/
PKJSParser.m
160 lines (132 loc) · 5.73 KB
/
PKJSParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "PKJSParser.h"
#import "PKJSUtils.h"
#import "PKJSAssemblerAdapter.h"
#import "PKJSAssembly.h"
#import "PKJSTokenAssembly.h"
#import "PKJSCharacterAssembly.h"
#import <ParseKit/PKParser.h>
#import <ParseKit/PKAssembly.h>
#import <ParseKit/PKTokenAssembly.h>
#import <ParseKit/PKCharacterAssembly.h>
#pragma mark -
#pragma mark Methods
static JSValueRef PKParser_toString(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *ex) {
PKPreconditionInstaceOf(PKParser_class, "toString");
PKParser *data = JSObjectGetPrivate(this);
return PKNSStringToJSValue(ctx, [data description], ex);
}
static JSValueRef PKParser_bestMatch(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *ex) {
PKPreconditionInstaceOf(PKParser_class, "bestMatch");
PKPreconditionMethodArgc(1, "bestMatch");
PKParser *data = JSObjectGetPrivate(this);
JSObjectRef arg = (JSObjectRef)argv[0];
PKAssembly *a = (PKAssembly *)JSObjectGetPrivate(arg);
a = [data bestMatchFor:a];
JSObjectRef result = NULL;
if ([a isMemberOfClass:[PKTokenAssembly class]]) {
result = PKTokenAssembly_new(ctx, a);
} else if ([a isMemberOfClass:[PKCharacterAssembly class]]) {
result = PKCharacterAssembly_new(ctx, a);
}
return result;
}
static JSValueRef PKParser_completeMatch(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *ex) {
PKPreconditionInstaceOf(PKParser_class, "completeMatch");
PKPreconditionMethodArgc(1, "completeMatch");
PKParser *data = JSObjectGetPrivate(this);
JSObjectRef arg = (JSObjectRef)argv[0];
PKAssembly *a = (PKAssembly *)JSObjectGetPrivate(arg);
a = [data completeMatchFor:a];
JSObjectRef result = NULL;
if ([a isMemberOfClass:[PKTokenAssembly class]]) {
result = PKTokenAssembly_new(ctx, a);
} else if ([a isMemberOfClass:[PKCharacterAssembly class]]) {
result = PKCharacterAssembly_new(ctx, a);
}
return result;
}
#pragma mark -
#pragma mark Properties
static JSValueRef PKParser_getAssembler(JSContextRef ctx, JSObjectRef this, JSStringRef propName, JSValueRef *ex) {
PKParser *data = JSObjectGetPrivate(this);
id assembler = data.assembler;
if ([assembler isMemberOfClass:[PKJSAssemblerAdapter class]]) {
return [assembler assemblerFunction];
} else {
return NULL;
}
}
static bool PKParser_setAssembler(JSContextRef ctx, JSObjectRef this, JSStringRef propertyName, JSValueRef value, JSValueRef *ex) {
if (!JSValueIsObject(ctx, value) || !JSObjectIsFunction(ctx, (JSObjectRef)value)) {
(*ex) = PKNSStringToJSValue(ctx, @"only a function object can be set as a parser's assembler property", ex);
return false;
}
PKParser *data = JSObjectGetPrivate(this);
PKJSAssemblerAdapter *adapter = [[PKJSAssemblerAdapter alloc] init]; // retained. released in PKParser_finalize
[adapter setAssemblerFunction:(JSObjectRef)value fromContext:ctx];
[data setAssembler:adapter selector:@selector(parser:didMatch:)];
return true;
}
static JSValueRef PKParser_getName(JSContextRef ctx, JSObjectRef this, JSStringRef propName, JSValueRef *ex) {
PKParser *data = JSObjectGetPrivate(this);
return PKNSStringToJSValue(ctx, data.name, ex);
}
static bool PKParser_setName(JSContextRef ctx, JSObjectRef this, JSStringRef propertyName, JSValueRef value, JSValueRef *ex) {
PKParser *data = JSObjectGetPrivate(this);
data.name = PKJSValueGetNSString(ctx, value, ex);
return true;
}
#pragma mark -
#pragma mark Initializer/Finalizer
static void PKParser_initialize(JSContextRef ctx, JSObjectRef this) {
}
static void PKParser_finalize(JSObjectRef this) {
PKParser *data = (PKParser *)JSObjectGetPrivate(this);
id assembler = data.assembler;
data.assembler = nil;
if ([assembler isMemberOfClass:[PKJSAssemblerAdapter class]]) {
[assembler autorelease];
}
[data autorelease];
}
static JSStaticFunction PKParser_staticFunctions[] = {
{ "toString", PKParser_toString, kJSPropertyAttributeDontDelete },
{ "bestMatch", PKParser_bestMatch, kJSPropertyAttributeDontDelete },
{ "completeMatch", PKParser_completeMatch, kJSPropertyAttributeDontDelete },
{ 0, 0, 0 }
};
static JSStaticValue PKParser_staticValues[] = {
{ "assembler", PKParser_getAssembler, PKParser_setAssembler, kJSPropertyAttributeDontDelete }, // Function
{ "name", PKParser_getName, PKParser_setName, kJSPropertyAttributeDontDelete }, // String
{ 0, 0, 0, 0 }
};
#pragma mark -
#pragma mark Public
JSClassRef PKParser_class(JSContextRef ctx) {
static JSClassRef jsClass = NULL;
if (!jsClass) {
JSClassDefinition def = kJSClassDefinitionEmpty;
def.staticFunctions = PKParser_staticFunctions;
def.staticValues = PKParser_staticValues;
def.initialize = PKParser_initialize;
def.finalize = PKParser_finalize;
jsClass = JSClassCreate(&def);
}
return jsClass;
}
JSObjectRef PKParser_new(JSContextRef ctx, void *data) {
return JSObjectMake(ctx, PKParser_class(ctx), data);
}