문제

Parsekit Adventure는 계속됩니다 ... 그리고 내 다음 장애물은 개행 기호를 인식하게하려고합니다.

여기 내 문법이 있습니다 :

@symbolState = '\n';
@start = textline*;
textline = Any* eol;
eol = '\n';
.

여기 내 테스트 텍스트가 있습니다 :

1
2
3
4
5
.

텍스트는 UNIX 형식 (LF) 라인 엔딩이있는 UTF-8 텍스트 파일에서 읽습니다.Xcode (파일 관리자 -> 텍스트 설정)에서 텍스트 랭글러와 함께 외부에서 모두 형식을 모두 확인했습니다.

과 관련 코드가 있습니다 :

#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'일치하는 옵션을 갖는 것이 좋습니다.

도움이 되었습니까?

해결책

"nofollow"> parsekit 의 개발자. 내가 할 수있는 두 가지 :


1.

TextMate 또는 TextWrangler (.txt 파일로 저장)로 작성된 텍스트 파일을 사용하여 예제를 시도했으며 모든 것이 잘 작동하는 것처럼 보였습니다. 내 -parser:didMatchEol:-parser:didMatchTexline: 콜백이 예상대로 호출되었습니다.

이것이 당신을 위해 작동하지 않으면, 메모리 인 문자열 입력으로 시작하여 최소한 작동할지 확인하십시오.

NSString *s = @"foo bar\nbaz bat\n";
[parser parse:s];
.

어쨌든, 메모리 내 입력 또는 디스크 입력 중 하나를 사용하여 나에게 작업하는 DebugAppDelegate.m의 코드가 있습니다.

- (void)doTestGrammar {
    NSString *g = @"@symbolState = '\n'; @start = textline*; textline = Any* eol; eol = '\n';";
    PKParser *p = [[PKParserFactory factory] parserFromGrammar:g assembler:self];
    //NSString *s = @"foo bar\nbaz bat\n";
    NSString *path = [@"~/Desktop/text.txt" stringByExpandingTildeInPath];
    NSString *s = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [p parse:s];
}

- (void)parser:(PKParser *)p didMatchEol:(PKAssembly *)a {
    NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
}

- (void)parser:(PKParser *)p didMatchTextline:(PKAssembly *)a {
    NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
}
.


2.

실제로는 당신의 모범은 당신의 예에서 당신의 Any* 생산을 사용하는 것이 라인 끝에있는 \n 토큰을 탐욕스럽게 소비 할 것이므로, eol 생산이 일치하도록 아무 것도 남지 않았습니다. 그러나 언급했듯이, 이것은 당신의 예를 들어 (Parsekit trunk head of trunk)에 대하여 도망 칠 때 문제가되지 않았습니다.

아직도 좋은 측정을 위해 textline 프로덕션을 다음과 같이 변경하는 것이 좋습니다.

textline = ~eol* eol;
.

이 프로덕션의 정의는 다음과 같이 읽어야합니다.

0 이상 토큰은 eol가 일치하지 않고 eol

에 의해 일치하는 하나의 토큰이 뒤 따릅니다.

~는 ParseKit 문법 구문에있는 부울 부정 조작자입니다.

이 변경 사항을 예제 코드로 변경 한 후 모든 것이 예상대로 작동합니다.


이 두 가지 팁이 끝나면 여전히 작동하지 않으면 알려주세요.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top