(Obj-C)这种语法突出显示有什么问题?

时间:2011-01-14 17:59:49

标签: objective-c cocoa syntax-highlighting

我正在为某些东西制作IDE ...当我执行以下操作时语法突出显示会冻结:


int i = abs();
//then push " like so:
int i = abs(");
//when I push anything after that it gives me an error and deletes it:
/*
ERROR:
2011-01-14 11:53:29.554 PROJECT NAME GOES HERE[6767:a0f] HIToolbox: ignoring exception '*** -[NSBigMutableString substringWithRange:]: Range or index out of bounds' that raised inside Carbon event dispatch
(
    0   CoreFoundation                      0x00007fff800b57b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83aed0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff800b55d7 +[NSException raise:format:arguments:] + 103
    3   CoreFoundation                      0x00007fff800b5564 +[NSException raise:format:] + 148
    4   Foundation                          0x00007fff87e0ecf2 -[NSString substringWithRange:] + 204
    5   PROJECT NAME GOES HERE              0x00000001000014bb -[ScriptController textStorageDidProcessEditing:] + 741
    6   Foundation                          0x00007fff87deba66 _nsnote_callback + 167
    7   CoreFoundation                      0x00007fff8005d000 __CFXNotificationPost + 1008
    8   CoreFoundation                      0x00007fff80049578 _CFXNotificationPostNotification + 200
    9   Foundation                          0x00007fff87de29ce -[NSNotificationCenter postNotificationName:object:userInfo:] + 101
    10  AppKit                              0x00007fff8577cef6 -[NSTextStorage processEditing] + 137
    11  AppKit                              0x00007fff8577d9e3 -[NSTextStorage endEditing] + 80
    12  AppKit                              0x00007fff857a5b56 -[NSTextView insertText:replacementRange:] + 2745
    13  AppKit                              0x00007fff857a3471 -[NSTextInputContext handleTSMEvent:] + 2204
    14  AppKit                              0x00007fff857a2bc6 _NSTSMEventHandler + 156
    15  HIToolbox                           0x00007fff84d6d9d5 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1002
    16  HIToolbox                           0x00007fff84d6cf28 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 395
    17  HIToolbox                           0x00007fff84d8abeb SendEventToEventTarget + 45
    18  HIToolbox                           0x00007fff84dbc2bb SendTSMEvent + 48
    19  HIToolbox                           0x00007fff84dbbe61 SendUnicodeTextAEToUnicodeDoc + 468
    20  HIToolbox                           0x00007fff84dbbbdc TSMKeyEvent + 604
    21  HIToolbox                           0x00007fff84da6724 TSMProcessRawKeyEvent + 1909
    22  AppKit                              0x00007fff857a18bb -[NSTextInputContext handleEvent:] + 620
    23  AppKit                              0x00007fff857a15e5 -[NSView interpretKeyEvents:] + 186
    24  AppKit                              0x00007fff857a13be -[NSTextView keyDown:] + 819
    25  AppKit                              0x00007fff8571306f -[NSWindow sendEvent:] + 8769
    26  AppKit                              0x00007fff85647a86 -[NSApplication sendEvent:] + 4719
    27  AppKit                              0x00007fff855de4da -[NSApplication run] + 474
    28  AppKit                              0x00007fff855d71a8 NSApplicationMain + 364
*/

这是我的源代码:


EDIT: TOO LONG: Here is the line that is causing the problem:
// Otherwise a key word was found, lets go colorize it

                // Setup a temporary range to reflect the actual position of the string within the text view
                // by applying the offset of our current scanning location to the location of the key word
                fooRange.location = found.location;
                fooRange.length = found.length;
                fooRange.location += last_location;

                // Work around for an odd bug that only occurs occasionally
                if (![[string substringWithRange: fooRange] isEqual: keyWord]) {
                    fooRange.location++;
                }

和我的字典:


        [[textview textStorage] setDelegate:self];
    keyWords = [[NSMutableDictionary alloc] init];
    NSColor *blue = [NSColor colorWithCalibratedRed:0 green:0 blue:0.95 alpha:1];
    NSColor *green= [NSColor colorWithCalibratedRed:0 green:0.65 blue:0 alpha:1];
    NSColor *red =  [NSColor colorWithCalibratedRed:0.90 green:0 blue:0 alpha:1];
    NSColor *tBlue =[NSColor blueColor];
    [keyWords setObject:blue forKey:@"abs"];
    [keyWords setObject:blue forKey:@"sin"];
    [keyWords setObject:blue forKey:@"cos"];
    [keyWords setObject:blue forKey:@"pow"];
    [keyWords setObject:blue forKey:@"rand"];
    [keyWords setObject:tBlue forKey:@"int"];
    [keyWords setObject:tBlue forKey:@"float"];
    [keyWords setObject:tBlue forKey:@"double"];
    [keyWords setObject:tBlue forKey:@"BOOL"];
    [keyWords setObject:red forKey:@"debug"];
    [keyWords setObject:red forKey:@"quote-color"];
    [keyWords setObject:@"\"" forKey:@"quote-open"];
    [keyWords setObject:@"\"" forKey:@"quote-close"];
    [keyWords setObject:green forKey:@"comment-color"];

2 个答案:

答案 0 :(得分:1)

缺乏线索,这是一个疯狂的猜测。

            fooRange.location = found.location;
            fooRange.length = found.length;
            fooRange.location += last_location;

如果location在字符串末尾的length范围内,则该最后一行非常有效地将该位置推到字符串末尾之外并导致范围异常。

答案 1 :(得分:0)

好的,所以你知道错误发生在哪里,并且你有一个非常明确的错误信息(“范围或索引越界”)。

下一步是查看相关值(fooRange.locationfound.locationfooRange.lengthfound.lengthlast_location和{{1} })并查看范围扩展到字符串末尾之外的原因。用于计算范围的逻辑/算术可能不正确。

您可以通过打印它们([string length])或在那里放置断点并使用调试器运行来检查这些值。