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]
을 반환입니까? 감사합니다