Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. open FParsec
  2.  
  3. type UserState =
  4. {
  5. ArgsParsed : int
  6. }
  7.  
  8. type Parser<'t> = Parser<'t, UserState>
  9.  
  10. let (<!>) (p: Parser<_,_>) label : Parser<_,_> =
  11. fun stream ->
  12. printfn "%A: Entering %s" stream.Position label
  13. let reply = p stream
  14. printfn "%A: Leaving %s (%A)" stream.Position label reply.Status
  15. reply
  16.  
  17. let pIp4Address : Parser<_> =
  18. regex "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
  19.  
  20. let pIp6Address : Parser<_> =
  21. pipe4
  22. (pstring "0:0:0:0:0:")
  23. (pstring "0" <|> pstring "FFFF")
  24. (pstring ":")
  25. pIp4Address
  26. (fun a b c d -> a + b + c + d)
  27. <|> regex @"[0-9A-F]+(?:\:[0-9A-F]+){7}"
  28.  
  29. let pShortName : Parser<_> =
  30. regex @"[A-z0-9](?:[A-z0-9\-]+(?<!-))?"
  31.  
  32. let pHostAddr =
  33. pIp4Address <|> pIp6Address
  34.  
  35. let pHostName : Parser<_> =
  36. stringsSepBy1 pShortName (pstring ".")
  37.  
  38. let pHost : Parser<_> =
  39. pHostName <|> pHostAddr
  40.  
  41. let pServerName : Parser<_> = pHostName
  42.  
  43. let pUser : Parser<_> =
  44. many1Chars (noneOf ['\000'; '\r'; '\n'; ' '; '@']) <!> "pUser"
  45.  
  46. let pConnection : Parser<_> =
  47. pchar '%'
  48. >>. pHost <!> "pConnection"
  49.  
  50. let pDestination : Parser<_> =
  51. pchar '@'
  52. >>. pServerName <!> "pDestination"
  53.  
  54. let pUserConnectionAndDestination : Parser<_> =
  55. pipe3
  56. pUser
  57. pConnection
  58. pDestination
  59. (fun user connectedFrom server ->
  60. (user, Some connectedFrom, Some server))
  61.  
  62. (*
  63. The below fails with:
  64. (Ln: 1, Col: 1): Entering pUser
  65. (Ln: 1, Col: 28): Leaving pUser (Ok)
  66. (Ln: 1, Col: 28): Entering pConnection
  67. (Ln: 1, Col: 28): Leaving pConnection (Error)
  68. val it : ParserResult<Destination,UserState> =
  69. Failure:
  70. Error in Ln: 1 Col: 28
  71. kalt%millennium.stealth.net@irc.stealth.net
  72. ^
  73. Expecting: any char not in ‘ @’ or '%'
  74. *)
  75. runParserOnString pUserConnectionAndDestination { ArgsParsed = 0 } "" @"kalt%millennium.stealth.net@irc.stealth.net"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement