1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
#endif
module Text.RE.ZeInternals.PreludeMacros
( RegexType
, WithCaptures(..)
, MacroDescriptor(..)
, RegexSource(..)
, PreludeMacro(..)
, presentPreludeMacro
, preludeMacros
, preludeMacroTable
, preludeMacroSummary
, preludeMacroSources
, preludeMacroSource
, preludeMacroEnv
, preludeMacroDescriptor
) where
import Data.Array
import qualified Data.HashMap.Lazy as HML
import Data.List
import Data.Maybe
import qualified Data.Text as T
import Data.Time
import Prelude.Compat
import Text.RE.REOptions
import Text.RE.ZeInternals.TestBench
import Text.RE.ZeInternals.TestBench.Parsers
-- | generate the standard prelude Macros used to parse REs
preludeMacros :: (Monad m,Functor m)
=> (String->m r)
-> RegexType
-> WithCaptures
-> m (Macros r)
preludeMacros prs rty wc = mkMacros prs rty wc $ preludeMacroEnv rty
-- | format the standard prelude macros in a markdown table
preludeMacroTable :: RegexType -> String
preludeMacroTable rty = formatMacroTable rty $ preludeMacroEnv rty
-- | generate a textual summary of the prelude macros
preludeMacroSummary :: RegexType -> PreludeMacro -> String
preludeMacroSummary rty =
formatMacroSummary rty (preludeMacroEnv rty) . prelude_macro_id
-- | generate a plain text table giving the RE for each macro with all
-- macros expanded (to NF)
preludeMacroSources :: RegexType -> String
preludeMacroSources rty =
formatMacroSources rty ExclCaptures $ preludeMacroEnv rty
-- | generate plain text giving the expanded RE for a single macro
preludeMacroSource :: RegexType -> PreludeMacro -> String
preludeMacroSource rty =
formatMacroSource rty ExclCaptures (preludeMacroEnv rty) . prelude_macro_id
-- | generate the `MacroEnv` for the standard prelude macros
preludeMacroEnv :: RegexType -> MacroEnv
preludeMacroEnv rty = fix $ prelude_macro_env rty
prelude_macro_env :: RegexType -> MacroEnv -> MacroEnv
prelude_macro_env rty env = HML.fromList $ catMaybes
[ (,) (prelude_macro_id pm) <$> preludeMacroDescriptor rty env pm
| pm<-[minBound..maxBound]
]
-- | generate the `MacroDescriptor` for a given `PreludeMacro`
preludeMacroDescriptor :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
preludeMacroDescriptor rty env pm = case pm of
PM_nat -> natural_macro rty env pm
PM_hex -> natural_hex_macro rty env pm
PM_int -> integer_macro rty env pm
PM_frac -> decimal_macro rty env pm
PM_string -> string_macro rty env pm
PM_string_simple -> string_simple_macro rty env pm
PM_id -> id_macro rty env pm
PM_id' -> id'_macro rty env pm
PM_id_ -> id__macro rty env pm
PM_date -> date_macro rty env pm
PM_date_slashes -> date_slashes_macro rty env pm
PM_time -> time_macro rty env pm
PM_timezone -> timezone_macro rty env pm
PM_datetime -> datetime_macro rty env pm
PM_datetime_8601 -> datetime_8601_macro rty env pm
PM_datetime_clf -> datetime_clf_macro rty env pm
PM_shortmonth -> shortmonth_macro rty env pm
PM_address_ipv4 -> address_ipv4_macros rty env pm
PM_email_simple -> email_simple_macro rty env pm
PM_url -> url_macro rty env pm
PM_syslog_severity -> syslog_severity_macro rty env pm
-- | an enumeration of all of the prelude macros
data PreludeMacro
-- numbers
= PM_nat
| PM_hex
| PM_int
| PM_frac
-- strings
| PM_string
| PM_string_simple
-- identifiers
| PM_id
| PM_id'
| PM_id_
-- dates & times
| PM_date
| PM_date_slashes
| PM_time
| PM_timezone
| PM_datetime
| PM_datetime_8601
| PM_datetime_clf
| PM_shortmonth
-- addresses
| PM_address_ipv4
| PM_email_simple
| PM_url
-- syslog
| PM_syslog_severity
deriving (Bounded,Enum,Ord,Eq,Show)
-- | naming the macros
presentPreludeMacro :: PreludeMacro -> String
presentPreludeMacro pm = case pm of
PM_id_ -> prelude_prefix++"id-"
_ -> fmt pm
where
fmt = (prelude_prefix++) . map tr . drop 3 . show
tr '_' = '.'
tr c = c
-- | all prelude macros are prefixed with this
prelude_prefix :: String
prelude_prefix = "%"
prelude_macro_id :: PreludeMacro -> MacroID
prelude_macro_id = MacroID . presentPreludeMacro
natural_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
natural_macro rty env pm = Just $ run_tests rty parseInteger samples env pm
MacroDescriptor
{ macroSource = "[0-9]+"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseInteger"
, macroDescription = "a string of one or more decimal digits"
}
where
samples :: [(String,Int)]
samples =
[ (,) "0" 0
, (,) "1234567890" 1234567890
, (,) "00" 0
, (,) "01" 1
]
counter_samples =
[ ""
, "0A"
, "-1"
]
natural_hex_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
natural_hex_macro rty env pm = Just $ run_tests rty parseHex samples env pm
MacroDescriptor
{ macroSource = "[0-9a-fA-F]+"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseHex"
, macroDescription = "a string of one or more hexadecimal digits"
}
where
samples :: [(String,Int)]
samples =
[ (,) "0" 0x0
, (,) "12345678" 0x12345678
, (,) "0abcdef" 0xabcdef
, (,) "0ABCDEF" 0xabcdef
, (,) "00" 0x0
, (,) "010" 0x10
]
counter_samples =
[ ""
, "0x10"
, "0z"
, "-1a"
]
integer_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
integer_macro rty env pm = Just $ run_tests rty parseInteger samples env pm
MacroDescriptor
{ macroSource = "-?[0-9]+"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseInteger"
, macroDescription = "a decimal integer"
}
where
samples :: [(String,Int)]
samples =
[ (,) "0" 0
, (,) "1234567890" 1234567890
, (,) "00" 0
, (,) "01" 1
, (,) "-1" $ -1
, (,) "-0" 0
]
counter_samples =
[ ""
, "0A"
, "+0"
]
-- | a digit string macro
decimal_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
decimal_macro rty env pm = Just $ run_tests rty parseDouble samples env pm
MacroDescriptor
{ macroSource = "-?[0-9]+(?:\\.[0-9]+)?"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseInteger"
, macroDescription = "a decimal natural number"
}
where
samples :: [(String,Double)]
samples =
[ (,) "0" 0
, (,) "1234567890" 1234567890
, (,) "00" 0
, (,) "01" 1
, (,) "-1" $ -1
, (,) "-0" 0
, (,) "0.1234567890" 0.1234567890
, (,) "-1.0" $ -1.0
]
counter_samples =
[ ""
, "0A"
, "+0"
, "0."
, ".0"
, "."
, "-"
, "-."
, "-1."
, "-.1"
]
string_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
string_macro rty env pm
| isPCRE rty = Nothing
| otherwise =
Just $ run_tests rty (fmap T.unpack . parseString) samples env pm
MacroDescriptor
{ macroSource = "\"(?:[^\"\\]+|\\\\[\\\"])*\""
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseString"
, macroDescription = "a double-quote string, with simple \\ escapes for \\s and \"s"
}
where
samples :: [(String,String)]
samples =
[ (,) "\"\"" ""
, (,) "\"foo\"" "foo"
, (,) "\"\\\"\"" "\""
, (,) "\"\\\"\\\"\"" "\"\""
, (,) "\"\\\"\\\\\\\"\"" "\"\\\""
, (,) "\"\\\"foo\\\"\"" "\"foo\""
, (,) "\"\"" ""
]
counter_samples =
[ "\""
, "\"aa"
]
string_simple_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
string_simple_macro rty env pm =
Just $ run_tests rty (fmap T.unpack . parseSimpleString) samples env pm
MacroDescriptor
{ macroSource = "\"[^\"[:cntrl:]]*\""
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseSimpleString"
, macroDescription = "a simple quoted string"
}
where
samples :: [(String,String)]
samples =
[ (,) "\"\"" ""
, (,) "\"foo\"" "foo"
, (,) "\"\\\"" "\\"
, (,) "\"\"" ""
]
counter_samples =
[ ""
, "\""
, "\"\\\"\""
, "\"\\\"\\\"\""
, "\"\\\"\\\\\\\"\""
, "\"\\\"foo\\\"\""
, "\"aa"
]
id_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
id_macro rty env pm =
Just $ run_tests rty Just samples env pm
MacroDescriptor
{ macroSource = "_*[a-zA-Z][a-zA-Z0-9_]*"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Nothing
, macroDescription = "a standard C-style alphanumeric identifier (with _s)"
}
where
samples :: [(String,String)]
samples =
[ f "a"
, f "A"
, f "A1"
, f "a_"
, f "a1_B2"
, f "_abc"
, f "__abc"
]
where
f s = (s,s)
counter_samples =
[ ""
, "1"
, "_"
, "__"
, "__1"
, "1a"
, "a'"
]
id'_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
id'_macro rty env pm =
Just $ run_tests rty Just samples env pm
MacroDescriptor
{ macroSource = "_*[a-zA-Z][a-zA-Z0-9_']*"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Nothing
, macroDescription = "a standard Haskell-style alphanumeric identifier (with '_'s and '''s)"
}
where
samples :: [(String,String)]
samples =
[ f "a"
, f "A"
, f "A1"
, f "a_"
, f "a1_B2"
, f "_abc"
, f "__abc"
, f "a'"
, f "_a'"
, f "a'b"
]
where
f s = (s,s)
counter_samples =
[ ""
, "1"
, "_"
, "__"
, "__1"
, "1a"
, "'"
, "'a"
, "_'"
, "_1'"
]
id__macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
id__macro rty env pm =
Just $ run_tests rty Just samples env pm
MacroDescriptor
{ macroSource = "_*[a-zA-Z][a-zA-Z0-9_'-]*"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Nothing
, macroDescription = "an identifier with -s"
}
where
samples :: [(String,String)]
samples =
[ f "a"
, f "A"
, f "A1"
, f "a_"
, f "a1_B2"
, f "_abc"
, f "__abc"
, f "a'"
, f "_a'"
, f "a'b"
, f "a-"
, f "a1-B2"
, f "a1-B2-"
]
where
f s = (s,s)
counter_samples =
[ ""
, "1"
, "_"
, "__"
, "__1"
, "1a"
, "'"
, "'a"
, "_'"
, "_1'"
]
date_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
date_macro rty env pm =
Just $ run_tests rty parseDate samples env pm
MacroDescriptor
{ macroSource = "[0-9]{4}-[0-9]{2}-[0-9]{2}"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseDate"
, macroDescription = "a YYYY-MM-DD format date"
}
where
samples :: [(String,Day)]
samples =
[ f "2016-12-31"
, f "0001-01-01"
, f "1000-01-01"
]
where
f s = (s,read s)
counter_samples =
[ ""
, "2016/01/31"
, "2016-1-31"
, "2016-01-1"
, "2016-001-01"
]
date_slashes_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
date_slashes_macro rty env pm =
Just $ run_tests rty parseSlashesDate samples env pm
MacroDescriptor
{ macroSource = "[0-9]{4}/[0-9]{2}/[0-9]{2}"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseSlashesDate"
, macroDescription = "a YYYY/MM/DD format date"
}
where
samples :: [(String,Day)]
samples =
[ f "2016/12/31"
, f "0001/01/01"
, f "1000/01/01"
]
where
f s = (s,read $ map tr s)
where
tr '/' = '-'
tr c = c
counter_samples =
[ ""
, "2016-01-31"
, "2016/1/31"
, "2016/01/1"
, "2016/001/01"
]
time_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
time_macro rty env pm =
Just $ run_tests rty parseTimeOfDay samples env pm
MacroDescriptor
{ macroSource = "[0-9]{2}:[0-9]{2}:[0-9]{2}(?:[.][0-9]+)?"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseTimeOfDay"
, macroDescription = "a HH:MM:SS[.Q+]"
}
where
samples :: [(String,TimeOfDay)]
samples =
[ f "00:00:00" 00 00 0
, f "23:59:59" 23 59 59
, f "00:00:00.1234567890" 00 00 $ 123456789 / 1000000000
]
where
f s h m ps = (s,TimeOfDay h m ps)
counter_samples =
[ ""
, "235959"
, "10:20"
, "A00:00:00"
, "00:00:00A"
, "23:59:59."
]
timezone_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
timezone_macro rty env pm =
Just $ run_tests rty parseTimeZone samples env pm
MacroDescriptor
{ macroSource = "(?:Z|[+-][0-9]{2}:?[0-9]{2})"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseTimeZone"
, macroDescription = "an IOS-8601 TZ specification"
}
where
samples :: [(String,TimeZone)]
samples =
[ f "Z" $ minutesToTimeZone 0
, f "+00:00" $ minutesToTimeZone 0
, f "+0000" $ minutesToTimeZone 0
, f "+0200" $ minutesToTimeZone 120
, f "-0100" $ minutesToTimeZone $ -60
]
where
f = (,)
counter_samples =
[ ""
, "00"
, "A00:00"
, "UTC"
, "EST"
, " EST"
]
datetime_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
datetime_macro rty env pm = Just $ run_tests rty parseDateTime samples env pm
MacroDescriptor
{ macroSource = "@{%date}[ T]@{%time}(?:@{%timezone}| UTC)?"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseDateTime"
, macroDescription = "ISO-8601 format date and time + simple variants"
}
where
samples :: [(String,UTCTime)]
samples =
[ f "2016-12-31 23:37:22.525343 UTC" "2016-12-31 23:37:22.525343Z"
, f "2016-12-31 23:37:22.525343" "2016-12-31 23:37:22.525343Z"
, f "2016-12-31 23:37:22" "2016-12-31 23:37:22Z"
, f "2016-12-31T23:37:22+0100" "2016-12-31 23:37:22+0100"
, f "2016-12-31T23:37:22-01:00" "2016-12-31 23:37:22-0100"
, f "2016-12-31T23:37:22-23:59" "2016-12-31 23:37:22-2359"
, f "2016-12-31T23:37:22Z" "2016-12-31 23:37:22Z"
]
where
f :: String -> String -> (String,UTCTime)
f s r_s = (s,read r_s)
counter_samples =
[ ""
, "2016-12-31 23:37:22.525343 EST"
]
datetime_8601_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
datetime_8601_macro rty env pm =
Just $ run_tests rty parseDateTime samples env pm
MacroDescriptor
{ macroSource = "@{%date}T@{%time}@{%timezone}"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseDateTime8601"
, macroDescription = "YYYY-MM-DDTHH:MM:SS[.Q*](Z|[+-]HHMM) format date and time"
}
where
samples :: [(String,UTCTime)]
samples =
[ f "2016-12-31T23:37:22.343Z" "2016-12-31 23:37:22.343Z"
, f "2016-12-31T23:37:22-0100" "2016-12-31 23:37:22-0100"
, f "2016-12-31T23:37:22+23:59" "2016-12-31 23:37:22+2359"
]
where
f :: String -> String -> (String,UTCTime)
f s r_s = (s,read r_s)
counter_samples =
[ ""
, "2016-12-31 23:37:22.525343 EST"
]
datetime_clf_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor
datetime_clf_macro rty env pm =
Just $ run_tests rty parseDateTimeCLF samples env pm
MacroDescriptor
{ macroSource = re
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseDateTimeCLF"
, macroDescription = "Common Log Format date+time: %d/%b/%Y:%H:%M:%S %z"
}
where
samples :: [(String,UTCTime)]
samples =
[ f "10/Oct/2000:13:55:36 -0700" "2000-10-10 13:55:36-0700"
, f "10/Oct/2000:13:55:36 +07:00" "2000-10-10 13:55:36+0700"
]
where
f :: String -> String -> (String,UTCTime)
f s r_s = (s,read r_s)
counter_samples =
[ ""
, "2016-12-31T23:37+0100"
, "10/Oct/2000:13:55:36-0700"
, "10/OCT/2000:13:55:36 -0700"
, "10/Oct/2000:13:55 -0700"
, "10/Oct/2000:13:55Z"
]
re = RegexSource $ unwords
[ "[0-9]{2}/@{%shortmonth}/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2}"
, "[+-][0-9]{2}:?[0-9]{2}"
]
shortmonth_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
shortmonth_macro rty env pm =
Just $ run_tests rty parseShortMonth samples env pm
MacroDescriptor
{ macroSource = bracketedRegexSource $
intercalate "|" $ map T.unpack $ elems shortMonthArray
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseShortMonth"
, macroDescription = "three letter month name: Jan-Dec"
}
where
samples :: [(String,Int)]
samples =
[ f "Jan" 1
, f "Feb" 2
, f "Dec" 12
]
where
f = (,)
counter_samples =
[ ""
, "jan"
, "DEC"
, "January"
, "01"
, "1"
]
address_ipv4_macros :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
address_ipv4_macros rty env pm =
Just $ run_tests rty parseIPv4Address samples env pm
MacroDescriptor
{ macroSource = "[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseSeverity"
, macroDescription = "an a.b.c.d IPv4 address"
}
where
samples :: [(String,IPV4Address)]
samples =
[ f "0.0.0.0" ( 0, 0, 0, 0)
, f "123.45.6.78" (123, 45, 6, 78)
, f "9.9.9.9" ( 9, 9, 9, 9)
, f "255.255.255.255" (255,255,255,255)
]
where
f = (,)
counter_samples =
[ ""
, "foo"
, "1234.0.0.0"
, "1.2.3"
, "1.2.3."
, "1.2..4"
, "www.example.com"
, "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
]
syslog_severity_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
syslog_severity_macro rty env pm =
Just $ run_tests rty parseSeverity samples env pm
MacroDescriptor
{ macroSource = re
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Just "parseSeverity"
, macroDescription = "syslog severity keyword (debug-emerg)"
}
where
samples :: [(String,Severity)]
samples =
[ f "emerg" Emerg
, f "panic" Emerg
, f "alert" Alert
, f "crit" Crit
, f "err" Err
, f "error" Err
, f "warn" Warning
, f "warning" Warning
, f "notice" Notice
, f "info" Info
, f "debug" Debug
]
where
f = (,)
counter_samples =
[ ""
, "Emergency"
, "ALERT"
]
re = if isPCRE rty
then re_pcre
else re_tdfa
re_tdfa = bracketedRegexSource $
intercalate "|" $
[ T.unpack kw
| (kw0,kws) <- map severityKeywords [minBound..maxBound]
, kw <- kw0:kws
]
re_pcre = bracketedRegexSource $
intercalate "|" $
[ T.unpack kw
| (kw0,kws) <- map severityKeywords $
filter (/=Err) [minBound..maxBound]
, kw <- kw0:kws
] ++ ["err(?:or)?"]
email_simple_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
email_simple_macro rty env pm =
Just $ run_tests rty Just samples env pm
MacroDescriptor
{ macroSource = "[a-zA-Z0-9%_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9.-]+"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Nothing
, macroDescription = "an email address"
}
where
samples :: [(String,String)]
samples =
[ f "user-name%foo.bar.com@an-example.com"
]
where
f s = (s,s)
counter_samples =
[ ""
, "not-an-email-address"
, "@not-an-email-address"
]
-- | see https://mathiasbynens.be/demo/url-regex
-- (based on @stephenhay URL)
url_macro :: RegexType
-> MacroEnv
-> PreludeMacro
-> Maybe MacroDescriptor
url_macro rty env pm =
Just $ run_tests rty Just samples env pm
MacroDescriptor
{ macroSource = "([hH][tT][tT][pP][sS]?|[fF][tT][pP])://[^[:space:]/$.?#].[^[:space:]]*"
, macroSamples = map fst samples
, macroCounterSamples = counter_samples
, macroTestResults = []
, macroParser = Nothing
, macroDescription = "a URL"
}
where
samples :: [(String,String)]
samples =
[ f "https://mathiasbynens.be/demo/url-regex"
, f "http://foo.com/blah_blah"
, f "http://foo.com/blah_blah/"
, f "http://foo.com/blah_blah_(wikipedia)"
, f "http://foo.com/blah_blah_(wikipedia)_(again)"
, f "http://www.example.com/wpstyle/?p=364"
, f "HTTPS://foo.bar/?q=Test%20URL-encoded%20stuff"
, f "HTTP://223.255.255.254"
, f "ftp://223.255.255.254"
, f "FTP://223.255.255.254"
]
where
f s = (s,s)
counter_samples =
[ ""
, "http://"
, "http://."
, "http://.."
, "http://../"
, "http://?"
, "http://??"
, "http://foo.bar?q=Spaces should be encoded"
, "//"
, "http://##/"
, "http://##"
, "http://##/"
]
run_tests :: (Eq a,Show a)
=> RegexType
-> (String->Maybe a)
-> [(String,a)]
-> MacroEnv
-> PreludeMacro
-> MacroDescriptor
-> MacroDescriptor
run_tests rty parser vector env =
runTests rty parser vector env . prelude_macro_id
bracketedRegexSource :: String -> RegexSource
bracketedRegexSource re_s = RegexSource $ "(?:" ++ re_s ++ ")"
fix :: (a->a) -> a
fix f = f (fix f)
|