Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 164 lines (116 sloc) 5.069 kb
05eedb5 Todd Ditchendorf initial add
authored
1 // Copyright 2010 Todd Ditchendorf
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #import "DemoTreesViewController.h"
16 #import "PKParseTreeView.h"
17 #import "TDSourceCodeTextView.h"
18 #import "PKParseTree.h"
19 #import "PKRuleNode.h"
20 #import "PKTokenNode.h"
21 #import "PKParseTreeAssembler.h"
22 #import <ParseKit/ParseKit.h>
23
24 @interface DemoTreesViewController ()
25 - (void)renderGutters;
26 @end
27
28 @implementation DemoTreesViewController
29
30 - (id)init {
31 return [self initWithNibName:@"TreesView" bundle:nil];
32 }
33
34
35 - (id)initWithNibName:(NSString *)name bundle:(NSBundle *)b {
36 if (self = [super initWithNibName:name bundle:b]) {
37
38 }
39 return self;
40 }
41
42
43 - (void)dealloc {
44 [[NSNotificationCenter defaultCenter] removeObserver:self];
45 self.grammarString = nil;
46 self.inString = nil;
47 [super dealloc];
48 }
49
50
51 - (void)awakeFromNib {
52 // self.grammarString = @"@allowsScientificNotation=YES;\n@start = expr;\nexpr = addExpr;\naddExpr = multExpr (('+'|'-') multExpr)*;\nmultExpr = atom (('*'|'/') atom)*;\natom = Number;";
53 // self.grammarString = @"@start = array;array = '[' Number (commaNumber)* ']';commaNumber = ',' Number;";
54 // self.grammarString = @"@start = array;array = foo | Word; foo = 'foo';";
55 self.grammarString = @"@allowsScientificNotation = YES; @start = Empty | array | object; object = '{' (Empty | property (',' property)*) '}'; property = name ':' value; name = QuotedString; array = '[' (Empty | value (',' value)*) ']'; value = 'null' | boolean | array | object | number | string; string = QuotedString; number = Number; boolean = 'true' | 'false';";
56
57
58 // self.inString = @"4.0*.4 + 2e-12/-47 +3";
59 // self.inString = @"[1,2]";
60 // self.inString = @"foo";
61 self.inString = @"[42e-12, null,{'foo':false}]";
62
63 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:[[self view] window]];
64 }
65
66
67 - (void)windowDidBecomeMain:(NSNotification *)n {
68 [self renderGutters];
69 }
70
71
72 - (void)renderGutters {
73 [grammarTextView renderGutter];
74 [inputTextView renderGutter];
75 }
76
77
78 - (IBAction)parse:(id)sender {
79 if (![inString length] || ![grammarString length]) {
80 NSBeep();
81 return;
82 }
83
84 self.busy = YES;
85
86 [NSThread detachNewThreadSelector:@selector(doParse) toTarget:self withObject:nil];
87 }
88
89
90 - (void)doParse {
91 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
92
93 PKParseTreeAssembler *as = [[[PKParseTreeAssembler alloc] init] autorelease];
94 PKParser *p = [[PKParserFactory factory] parserFromGrammar:grammarString assembler:as preassembler:as];
95 PKParseTree *tr = [p parse:inString];
96 [parseTreeView drawParseTree:tr];
97
98 // release
99 PKReleaseSubparserTree(p);
100
101 [self performSelectorOnMainThread:@selector(done) withObject:nil waitUntilDone:NO];
102
103 [pool drain];
104 }
105
106
107 - (void)done {
108 self.busy = NO;
109 }
110
111
112 #pragma mark -
113 #pragma mark NSSplitViewDelegate
114
115 - (BOOL)splitView:(NSSplitView *)sv canCollapseSubview:(NSView *)v {
116 return v == [[sv subviews] objectAtIndex:1];
117 }
118
119
120 - (BOOL)splitView:(NSSplitView *)sv shouldCollapseSubview:(NSView *)v forDoubleClickOnDividerAtIndex:(NSInteger)i {
121 return [self splitView:sv canCollapseSubview:v];
122 }
123
124
125
126 // maintain constant splitview width when resizing window
127 - (void)splitView:(NSSplitView *)sv resizeSubviewsWithOldSize:(NSSize)oldSize {
128 NSRect newFrame = [sv frame]; // get the new size of the whole splitView
129 NSView *top = [[sv subviews] objectAtIndex:0];
130 NSRect topFrame = [top frame];
131 NSView *bottom = [[sv subviews] objectAtIndex:1];
132 NSRect bottomFrame = [bottom frame];
133
134 CGFloat dividerThickness = [sv dividerThickness];
135 topFrame.size.width = newFrame.size.width;
136
137 bottomFrame.size.height = newFrame.size.height - topFrame.size.height - dividerThickness;
138 bottomFrame.size.width = newFrame.size.width;
139 topFrame.origin.y = 0;
140
141 [top setFrame:topFrame];
142 [bottom setFrame:bottomFrame];
143 }
144
145
146 - (CGFloat)splitView:(NSSplitView *)sv constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)i {
147 if (0 == i) {
148 return 200;
149 } else {
150 return proposedMin;
151 }
152 }
153
154
155 - (CGFloat)splitView:(NSSplitView *)sv constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)i {
156 return proposedMax;
157 }
158
159 @synthesize grammarString;
160 @synthesize inString;
161 @synthesize busy;
162 @end
163
Something went wrong with that request. Please try again.