Приключения с ParseKit продолжаются... и мое следующее препятствие - попытаться распознать символы новой строки.
Вот моя грамматика:
@symbolState = '\n';
@start = textline*;
textline = Any* eol;
eol = '\n';
Вот мой тестовый текст:
1
2
3
4
5
Текст читается из текстового файла UTF-8 с окончаниями строк в формате Unix (LF). Я проверил этот формат как в Xcode (инспектор файлов -> Настройки текста), так и извне, с помощью TextWrangler.
И вот соответствующий код:
#import "FileImporterThing.h"
#import <ParseKit/ParseKit.h>
@interface FileImporterThing ()
@property (nonatomic, retain)PKParser* parser;
- (void)parser:(PKParser *)p didMatchTextline:(PKAssembly *)a;
- (void)parser:(PKParser *)p didMatchEol:(PKAssembly *)a;
@end
@implementation FileImporterThing
@synthesize parser = _parser;
-(id)init
{
if (!(self = [super init])) return nil;
// Have also tried "textline = Any* '\n';"
NSString *g = @"@symbolState = '\n'; @start = textline*; textline = Any* eol; eol = '\n';";
self.parser = [[PKParserFactory factory] parserFromGrammar:g assembler:self];
return self;
}
- (void)testParse
{
// read string from UTF-8 file Unix (LF) line endings
// (this verified in project->file inspector->Text Settings and externally with TextWrangler)
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"LF-test" ofType:@"parsetext"];
/* file contains text:
1
2
3
4
5
*/
NSString *s = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.parser parse:s];
}
- (void)parser:(PKParser *)p didMatchEol:(PKAssembly *)a
{
NSLog(@"eol found");// stack = %@", [a stack]);
}
- (void)parser:(PKParser *)p didMatchTextline:(PKAssembly *)a
{
NSLog(@"textline matched");
}
@end
Но я боюсь, что краска просто не высыхает! Перепробовал всевозможные вариации вышеперечисленного. Я не могу на всю жизнь заставить синтаксический анализатор распознавать новую строку. Вероятно, я могу обойти это, читая файл-буфер построчно (что в любом случае может быть более производительным?), но все же было бы неплохо иметь возможность сопоставления '\n'.