2011-06-14 5 views
5

내 AST 모델은 위치 정보 (파일 이름, 라인, 색인)를 가지고 있어야합니다. 이 정보에 액세스 할 수있는 방법이 있습니까? 레퍼런스 문서에서 스트림이 위치를 전달하는 것처럼 보이지만 위치를 저장하기 위해 더미 파서를 구현하지 않아도되고 모든 것을 추가 할 수 있습니다. 사전에fparsec의 위치 정보

감사

답변

7

파서 실제로 응답에 스트림에서 기능에 대한 약어를 입력됩니다

let position : CharStream<_> -> Reply<Position> = fun stream -> Reply(stream.Position) 
(* OR *) 
let position : Parser<_,_> = fun stream -> Reply stream.Position 
:
Parser<_,_> is just CharStream<_> -> Reply<_> 

마음에, 당신은 쉽게 위치에 대한 사용자 정의 파서를 쓸 수 있도록 유지

및 구문 분석 할 모든 비트에 위치 정보 첨부

position .>>. yourParser (*or tuple2 position yourParser*) 

위치 구문 분석기는 입력을 소비하지 않으므로 이러한 방식으로 결합하는 것이 안전합니다.

당신 한 줄에 제한 필요한 코드 변경을 유지하고 제어 할 수없는 코드 확산 방지 할 수 있습니다

type AST = Slash of int64 
     | Hash of int64 

let slash : Parser<AST,_> = char '/' >>. pint64 |>> Slash 
let hash : Parser<AST,_> = char '#' >>. pint64 |>> Hash 
let ast : Parser<AST,_> = slash <|> hash 

(*if this is the final parser used for parsing lists of your ASTs*) 
let manyAst : Parser<   AST list,_> = many    (ast .>> spaces) 

let manyAstP : Parser<(Position * AST) list,_> = many ((position .>>. ast) .>> spaces) 
(*you can opt in to parse position information for every bit 
    you parse just by modifiying only the combined parser  *) 

업데이트을 : FParsec이 위치에 대한 사전 정의 된 파서를 가지고 : http://www.quanttec.com/fparsec/reference/charparsers.html#members.getPosition

+1

당신 돈 실제로 위치 구문 분석기를 직접 정의해야합니다. http://www.quanttec.com/fparsec/reference/charparsers.html#members.getPosition –

+0

죄송합니다. fparsec 전체 참조를 읽지 못했습니다.^__^" –

+1

질문에 대답 해 주셔서 감사합니다 .-) Btw, 파서 개요도 있습니다 : http://www.quanttec.com/fparsec/reference/parser-overview.html#user-state-handling-and-getting- the-input-stream-position –

관련 문제