2017-12-20 12 views
3

이 코드는 "WEEKEND-SUNDAY"입력을 읽은 다음 "SATURDAY"를 반환합니다. "WEEKEND-SUNDAY"입력 "여전히"SATURDAY "=>이 파서는 무시합니다fparsec OperatorPrecedenceParser : 불완전한 괄호를 다루는 방법

let pDayOfWeekKeyWords = choice [ 
          pstring "MONDAY" ; 
          pstring "TUESDAY" ; 
          pstring "WEDNESDAY" ; 
          pstring "THURSDAY" ; 
          pstring "FRIDAY" ; 
          pstring "SATURDAY" ; 
          pstring "SUNDAY" ; 
          pstring "WEEKEND" ; 
          pstring "WEEKDAY" ; 
          pstring "ALL" ] 

let betweenParentheses p = 
    between (pstring "(") (pstring ")") p 

let opp = new OperatorPrecedenceParser<Set<DayOfWeek>,unit,unit>() 
let pExpr = opp.ExpressionParser 
let term = (betweenParentheses pExpr) <|> (pDayOfWeekKeyWords |>> (fun x -> 
    match x with 
    | "MONDAY" -> Set.ofList [DayOfWeek.Monday] 
    | "TUESDAY" -> Set.ofList [DayOfWeek.Tuesday] 
    | "WEDNESDAY" -> Set.ofList [DayOfWeek.Wednesday] 
    | "THURSDAY" -> Set.ofList [DayOfWeek.Thursday] 
    | "FRIDAY" -> Set.ofList [DayOfWeek.Friday] 
    | "SATURDAY" -> Set.ofList [DayOfWeek.Saturday] 
    | "SUNDAY" -> Set.ofList [DayOfWeek.Sunday] 
    | "WEEKDAY" -> Set.ofList [DayOfWeek.Monday ; DayOfWeek.Tuesday ; DayOfWeek.Wednesday;DayOfWeek.Thursday;DayOfWeek.Friday] 
    | "WEEKEND" -> Set.ofList [DayOfWeek.Saturday;DayOfWeek.Sunday] 
    | "ALL"-> Set.ofList [DayOfWeek.Monday ; DayOfWeek.Tuesday ; DayOfWeek.Wednesday;DayOfWeek.Thursday;DayOfWeek.Friday;DayOfWeek.Saturday;DayOfWeek.Sunday] 
    | _ -> failwith "ERROR MESSAGE")) 
opp.TermParser <- term 
opp.AddOperator(InfixOperator<Set<DayOfWeek>,unit,unit>("+", skipString "", 1, Associativity.Left, fun x y -> x + y)) 
opp.AddOperator(InfixOperator<Set<DayOfWeek>,unit,unit>("-", skipString "" , 1, Associativity.Left, fun x y -> x - y)) 

실행 ')'마지막

run pExpr "MONDAY+(WEEKEND-SUNDAY)" 

출력

Success: set [Monday; Saturday] 

홍보 뭔가 ..

내가 뭔가를 그리워했다 : oblem는

run pExpr "MONDAY+WEEKEND-SUNDAY)" or run pExpr "MONDAY)+WEEKEND-SUNDAY" 

이 여전히 나는 그것이 실패를 보여주고 싶은

Success: set [Monday; Saturday] 

을 반환입니까? 감사합니다

답변

2

두 번째 예제에서 pexpr은 일치하지 않는 닫는 괄호까지 입력 스트림을 성공적으로 파싱 한 후 반환됩니다. 따라서 마지막 예제에서 결과는 실제로 Success: set [Monday]이 아니고 Success: set [Monday; Saturday]이 아닙니다.

입력 스트림이 완전히 소모되지 않은 경우 오류가 강제로 eof 파서를 사용할 수 있습니다

> run (pExpr .>> eof) "MONDAY)+WEEKEND-SUNDAY" 
Error in Ln: 1 Col: 7 
MONDAY)+WEEKEND-SUNDAY 
    ^
Expecting: end of input or infix operator