ããŒã1ããªãããŒãã€ã«ãïŒ
ä»æ¥ãé¢æ°åããã°ã©ãã³ã°èšèªã®æ¥éãªæ®åã«ç §ãããŠãããŒãµãŒã³ã³ãããŒã¿ãŒã¯ãŸããŸã䜿çšãããããã«ãªã£ãŠããŸããããã¯ãåçŽãªäººéãããã¹ãã解æããããããããŒã«ã§ãã Parsec ïŒHaskellïŒãPlanck ïŒOCamlïŒãªã©ã®ã©ã€ãã©ãªãŒã¯ããšã³ã·ã¹ãã ã§ãã§ã«äŸ¡å€ã蚌æããŠããŸãã ãã®äŸ¿å©ããšæ©èœã«ãããScalaèšèªã®äœæè ã§ããMartin Oderskyã¯ã察å¿ããScala Parser Combinators ïŒçŸåšã¯scala-modulesã«é 眮ãããŠããŸã ïŒãæšæºã©ã€ãã©ãªã«è¿œå ããããã«ãªããŸããããã®ãããªããŒã«ã䜿çšããç¥èãšèœåã¯ãScalaéçºè ã®å¿ é èŠä»¶ã«ãããã®ã§ãA3ã¬ãã« ã
ãã®äžé£ã®èšäºã¯ãScala Parser Combinatorsã®åŒ·åãªä»£æ¿æ段ã§ãããå¯èœãªä»£æ¿æ段ã§ããParboiledã©ã€ãã©ãªãŒã«çŠç¹ãåœãŠãŠããŸãã ãã®äžã§ãçŸåšã®ããŒãžã§ã³ã®ã©ã€ãã©ãªã§ããParboiled2ã§ã®äœæ¥ã詳现ã«æ€èšããæ¢åã®ã³ãŒãã®ã»ãšãã©ããŸã 䜿çšããŠãããããParboiled1ã«ã泚æãæããŸãã
ãµã€ã¯ã«æ§é ïŒ
- ããŒã1ããªãããŒãã€ã«ãïŒ
- ããŒã2.äžèŽããããã¹ã
- ããŒã3.ããŒã¿æœåº
- ããŒã4.å³ããçŸå®
ã¯ããã«
Parboiledã¯ãããŒã¯ã¢ããèšèªïŒHTMLãXMLãJSONãªã©ïŒãããã°ã©ãã³ã°èšèªãæ§æãã¡ã€ã«ããã°ãããã¹ããããã³ã«ãããã³äžè¬ã«ããããããã¹ããç°¡åã«è§£æïŒè§£æïŒã§ããã©ã€ãã©ãªã§ãã ç¬èªã®ãã¡ã€ã³åºæèšèªïŒ DSL ïŒãéçºããå Žåã¯ãParboiledã圹ç«ã¡ãŸãããã®å©ããåããŠã æœè±¡æ§æããªãŒããã°ããååŸãã ã€ã³ã¿ãŒããªã¿ãŒãã¿ãŒã³ãèšæ¶ããŠããã¡ã€ã³èšèªã®ã³ãã³ããå®è¡ã§ããŸãã
çŸåšããã®ã©ã€ãã©ãªã«ã¯ããã€ãã®ããŒãžã§ã³ããããŸãã
- Parboiled for Javaã¯ãã©ã€ãã©ãªã®æåã®ããŒãžã§ã³ã§ãã Javaããã³Javaçšã®Matthias Doenizã«ãã£ãŠæžãããŸããã ãçµæ«æãã®ç¶æ ã§ã¯ãããŸãããäŸç¶ãšããŠäººæ°ããããŸãã 誀ã£ãŠãããç¶æ¿ããå ŽåããŸãã¯æå³çã«Javaãããžã§ã¯ããéå§ããå Žåã¯ãããã¯ããŒã fgeã®ãŠãŒã¶ãŒã«ãã£ãŠäœæ¥ç¶æ ãæ éã«ç¶æãããfork Parboiled1ã®ä»£ããã«grappaãæ€èšããããšããå§ãããŸãã
- ããŒãã€ã«ã-ã©ã€ãã©ãªã¯ãããã£ã¢ã¹ã岩ã貫éããåŸã«çãŸããŸããã 圌ã¯ãJavaããŒãžã§ã³ã®ãµããŒããæŸæ£ãããšåæã«ãParboiledã®Scalaããã³ããšã³ããäœæããŸããã Parboiled2ã®ãªãªãŒã¹ã«äŒŽããParboiled1ã®ScalaããŒãžã§ã³ã¯ãµããŒããããªããªããŸããããããã«ãããããããããããŸã æžãçãã䟡å€ã¯ãããŸããã
- Parboiled2ã¯ãParboiled1ã®ãã¹ãŠã®æ©èœããŸã åŠç¿ããŠããŸããã
- Parboiled1ã¯äŸç¶ãšããŠParboiled2ãããã¯ããã«åºã䜿çšãããŠãããããå€ãScalaãããžã§ã¯ãã«çªç¶ç§»è¡ããå Žåãããã«ééããå¯èœæ§ãé«ããªããŸãã
- Parboiled2-PB1ã®å€ãã®äžå©ãªç¹ãæé€ããã©ã€ãã©ãªã®ææ°ããŒãžã§ã³ã ããã¯ããéãåäœããæãéèŠãªããšã«ã¯ãéçºè ã«ãã£ãŠãµããŒããããŠããŸãã
ç§ã¯ãã®èšäºãParboiled2ã«çŠç¹ã眮ããŠæžããŸããïŒã¡ãªã¿ã«ããã©ã€ãã©ãªããšããèšèã䜿ããã«ç·æ§ã«ã€ããŠæžãç¶ããŸãïŒã
äž»ãªæ©èœ
Parboiled2ã®ç°¡åãªèª¬æïŒ
- PEGã®ååã«åŸããŸãã
- ã·ã³ã°ã«ãã¹ããŒãµãŒãçæããŸãã å¥ã®ã¬ã¯ãµãŒã¯å¿ èŠãããŸããã
- Scalaèšèªã®ãµãã»ããã§ããã¿ã€ãã»ãŒãDSLã䜿çšããŸãã
- æé©åã¯ã³ã³ãã€ã«æ®µéã§å®è¡ãããŸãã
å®éã«ã¯ãããã¯æ¬¡ã®ããšãæå³ããŸãã
- çŽ æã§ããŒãµãŒãæžãå¿ èŠã¯ãããŸããã
- æé«ã®BNFã°ã¬ãŒãã«å¹æµããèªã¿ãããïŒç§ã®æèŠã§ã¯ãPBã¯ããã«ã¯ãŒã«ã§ãïŒã
- å®çŸ©ã«ãã ãæ£èŠè¡šçŸã¯äœ¿çšã§ããŸããããPEGã®å
šæ©èœã䜿çšããŠååž°çãªããŒã¿æ§é ãèªç±ã«è§£æã§ããŸã ã ã¯ããæ£èŠè¡šçŸã䜿çšãããšãããã°ã©ãã³ã°èšèªã¯ãã¡ãããJSONãŸãã¯æãåçŽãªç®è¡åŒã®ãããã解æããŸããã StackOverflow ã§ã¯ã件åã«æªåé«ãåŒçšããããŸã ïŒ
ä»»æã®HTMLã解æããæ£èŠè¡šçŸãèŠæ±ããããšã¯ãParis Hiltonã«ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãèšè¿°ããããã«äŸé Œãããããªãã®ã§ãã
- ç·åœ¢æ§é ã解æããå¿ èŠãããå Žåã§ããParboiled2ïŒé©åãªæé©åã䜿çšããå ŽåïŒã¯ãæ£èŠè¡šçŸãããé«éã«å®è¡ãããŸãã 次ã®ã»ã¯ã·ã§ã³ã§èšŒæ ã瀺ããŸãã
- ANTLRãªã©ã®ããŒãµãŒãžã§ãã¬ãŒã¿ãŒãšã¯ç°ãªããåå¥ã®ã³ãŒãçæãšããã«ç¶ãã³ã³ãã€ã«ã®åé¡ãã解æŸãããŸãã ãã¹ãŠã®ããŒãã€ã«ãã³ãŒãã¯Scalaã§èšè¿°ãããŠãããããæ§æ匷調衚瀺ãšåãã§ãã¯ãããã«äœ¿çšã§ããŸãããŸããææ³ãã¡ã€ã«ã«å¯Ÿããè¿œå ã®æäœã¯ãããŸããããANTLRã«ãã£ãŠçæãããããŒãµãŒã«ã¯2段éã®è§£æããããŸãã ãã ããããã«ãé¢ããããANTLRã¯ããã«åŒ·åã§ãææžåãããããå®å®ããŠãããããå€ãã®ïŒ éåžžã«éèŠãªïŒå Žåã«é©ããŠããŸãã
- ã¹ã«ããããŒãµã³ã³ãããŒã¿ã¯é ãã§ãã ãšãŠãé ãã ãšãŠã€ããªãé ãã§ã Matthiasã¯ãParboiledãParboiled2ãScala Parser Combinatorsã䜿çšããŠèšè¿°ãããJacksonãšJSONã®ããŒãµãŒã®ããã©ãŒãã³ã¹ãæ¯èŒããŸããã åŸè ã®æåŸ ã¯ããã®çµæã¯ãæ¬æã§ããã«èŠã€ããããšãã§ããŸãã
- Language Workbenchesãšã¯ç°ãªããParboiledã¯å°ãããŠäœ¿ããããã©ã€ãã©ãªã§ãã ååã«ææžåãããŠããªãå¶åã¢ã³ã¹ã¿ãŒãããŠã³ããŒãããå¿ èŠã¯ãªãã貎éãªæéãè²»ãããŠãå°ããªDSLã説æããããã ãã«é©åãªã¡ãã¥ãŒãšãã¿ã³ãæ¢ãå¿ èŠã¯ãããŸããã äžæ¹ãæ¢è£œã®ããã¹ããšãã£ã¿ãŒã䜿çšããŠDSLãããã«åŒ·èª¿è¡šç€ºããããšã¯ã§ããŸããã代ããã«ãVimãEmacsããŸãã¯IDEã®ãã©ã°ã€ã³ãèªåã§äœæããå¿ èŠããããŸãããããã¯ãParboiledãå°ããªãã¡ã€ã³åºæã®èšèªãéçºããã®ã«ãµãããããªã代æ¿æ段ã«ããŸããã
- ããŒãã€ã«ãã¯ãè¡ãŸã¿ãã®äŒæ¥ãå«ãå€ãã®ãããžã§ã¯ãã§æåãåããŠããŸãã
ããŒãžã§ã³2ã®æ°æ©èœ
ãã®ã»ã¯ã·ã§ã³ã¯ãäž»ã«ã©ã€ãã©ãªã®æåã®ããŒãžã§ã³ã䜿çšããããšããã人ã«ãšã£ãŠæçšã§ç解ããããã§ãããã åå¿è ã¯ãã»ãšãã©ã®å Žåãäžé£ã®èšäºå šäœãèªãã åŸã«ãã®ãªã¹ãã«æ»ãå¿ èŠããããŸãã
ãŸããParboiled2ã¯ãæåã®ããŒãžã§ã³ã®å€ãã®å°å æçŸæ£ãæ£åžžã«é€å»ããŸãã
- ããã§ã
Rule7
ãããåºãã«ãŒã«ã䜿çšã§ããŸãã æåãªHList
æã€åœ¢ç¶ã®ãªãã©ã€ãã©ãªãããã«äœ¿çšãããŸãããçŸåšã1ã€ã®ã«ãŒã«ãã¹ã¿ãã¯äžã®å€æ°ã®å€ã§åäœã§ããŸãã ãŸããParboiled2ã«ã¯ãPB1ã«ã¯ãªãè¿œå ã®äŸåé¢ä¿ïŒåœ¢ç¶ã®ãªãã©ã€ãã©ãªèªäœïŒãããããšãæå³ããŸãã - äžè¶³ããŠãããã¶ã€ã³ãè¿œå ãããŸããã ãã®ãããParboiled1ã§ã¯ã
nTimes
ã«ãŒã«ã«åçãªç¹°ãè¿ãåæ°ãæå®ããããšã¯äžå¯èœã§ãããå¿ èŠãªææ³èšè¿°ã®æ£ç¢ºããäžããªããããæãããoneOrMore
ã«ãŒã«ã䜿çšããå¿ èŠããããŸããã - çµã¿èŸŒã¿ã®ããªããã£ãã¿ãŒããã«ãè¿œå ãããŸããã
AlphaNumeric
ãHex
ãPrintable
ãVisible
ãªã©ã®ãã£ãŒã«ããå«ãæ°ããCharPredicate
ã¯ã©ã¹ãCharPredicate
ãŸããã - è¿°èªãæ¡å€§ããã³çž®å°ããæ©èœãè¿œå ãããŸããã ã«ãŒã«ããè€æ°ã®æåãé€å€ããå¿ èŠã以åã«çããŸããããæåã®ãã¯ã€ããªã¹ããäœæããã®ã§ã¯ãªããç°¡åã«ååŸããŠå®è¡ã§ããããã«ãªããŸããã
ããã«ïŒ
- Parboiled2ã¯ãã¯ãã䜿çšããŸããããã«ãããParboiled1ã®ããã«å®è¡æã§ã¯ãªãã³ã³ãã€ã«æã«ææ³ãçæã§ããŸãã ããã«ãããããŒãµãŒã®ããã©ãŒãã³ã¹ã倧å¹
ã«åäžãããã§ãã¯ã®åæ°ãå¢ããŸãã ãã®ç¹ã§ã
rule
ãããã¯ã¯å¿ é ã«ãªããŸããããParboiled1ã¯ããã䜿çšããªãå ŽåããããŸããã å€ãã³ãŒãã移è¡ãããšãããŸããã®é©æ°ã«æ°ä»ãã§ãããã - æ¹åããããšã©ãŒå ±åã·ã¹ãã ã
- scala.jsã®ãµããŒããç»å ŽããŸãã ã ãã¢ãããžã§ã¯ãã¯ãã¡ãã§ã芧ããã ããŸã ã
æ§èœæ¯èŒ
Parboiled1ã¯ãäžèŽããã«ãŒã«ã®ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãå®è¡æã«å®è¡ãããã³ã³ãã€ã©ãŒããã®ãããªããŒãµãŒã§éèŠãªæé©åãå®è¡ã§ããªãã£ããããïŒANTLRã«ãã£ãŠçæãããããŒãµãŒã«é¢ããŠïŒãã®é ãã§ç¥ãããŠããŸãã Parboiled2ã§ã¯ãçç£æ§ãæéèŠã§ããããã¯ãã§å€ãã®ããšãããçŽããããããã³ã³ãã€ã©ãŒã¯æé©åäžã«ã¢ã¯ã·ã§ã³ã®èªç±ãåŸãŠããŠãŒã¶ãŒã¯åŸ æã®ããã©ãŒãã³ã¹ãåŸãŸããã 以äžã«ãéçºè ãéæããåªããçµæã瀺ããŸãã
解æãããJSONããŒãµãŒãšåçŽãªJSONããŒãµãŒ
Parboiledã¯ããŒãµãŒãäœæããããã®äžè¬åãããããŒã«ã§ãããåãã®ããã«ãç¹æ®åãããããŒã«ã¯ãç¹æ®åãããã¿ã¹ã¯ã解決ããããã«äžè¬åãããããŒã«ãããåžžã«åªããŠããŸãã Javaã®äžçã§ã¯ãå€ä»£ã®ãšã«ããã¹ã¿ãŒã«ãã£ãŠææžããããå°æ°ã®JSONããŒãµãŒããããAlexander MyltsevïŒParboiled2ã®éçºè ã®1人ïŒã¯ããããã®ã¢ãŒãã£ãã¡ã¯ãã«å¯ŸããŠParboiledãããã©ãŒãã³ã¹ãã©ãã ã倱ããã確èªããŸããã çµæã¯ãç¹ã«Parboiled2ã®å Žåãéåžžã«æ¥œèŠ³çã§ããã
- â , â âââââââââââââââââââââââââââââââââââââââŒââââââââââââŒâââââââââââââââââââââââââââââââââ Parboiled1JsonParser â 85.64 â ââââââââââââââââââââââââââââââ Parboiled2JsonParser â 13.17 â ââââ Json4SNative â 8.06 â âââ Argonaut â 7.01 â ââ Json4SJackson â 4.09 â â
ããŒãã€ã«ããšæ£èŠè¡šçŸ
éçãªæé©åã®äœ¿çšã«ãããParboiled2ã¯æ£èŠè¡šçŸïŒå°ãªããšãJavaã¯ã©ã¹ã©ã€ãã©ãªã«ä»å±ããŠãããã®ïŒãããã¯ããã«é«éã«åäœã§ããŸãã ã¡ãŒãªã³ã°ãªã¹ãããã®ããã€ãã®èšŒæ ã¯ããã«ãããŸã ïŒ
- â , â âââââââââââââââââââââââââââââââââââââââŒââââââââââââŒâââââââââââââââââââââââââââââââââââ Parboiled2 (warmup) â 1621.21 â ââââââââââââââââââââââââââââââââ Parboiled2 â 409.16 â ââââââââ Parboiled2 w/ better types (warmup) â 488.92 â ââââââââââ Parboiled2 w/ better types â 134.68 â âââ Regex (warmup) â 621.95 â ââââââââââââ Regex â 620.38 â ââââââââââââ
ããŒãã€ã«ãvs ScalaããŒãµãŒã³ã³ãããŒã¿ãŒ
ã¡ãŒãªã³ã°ãªã¹ãã§ã¯ã å¥ã®ããã©ãŒãã³ã¹ãã¹ããèŠã€ããããšãã§ããŸããããã¯ãæåã®ïŒJSONã«ã€ããŠïŒãšããäžèŽããScala Parser Combinatorsãšæ¯èŒããããã®ããŒã¿ãå«ãŸããŠããŸãã ãã¹ãŠããšãŠãæ²ããã§ãã
- â , â âââââââââââââââââââââââââââââââââââââââŒââââââââââââŒâââââââââââââââââââââââââââââââââ Parboiled1JsonParser | 73.81 | â Parboiled2JsonParser | 10.49 | â ParserCombinators | 2385.78 | ââââââââââââââââââââââââââââââ
ããŒãã€ã«ããã§ããªãããš
ããŒãµã³ã³ãããŒã¿ã«é¢ããã»ãšãã©ã®èšäºã¯ãPEGãäœã§ãããããããäœã§ãããããªããããæããå¿ èŠããããã«ã€ããŠã®åŸ¹åºçãªèª¬æããå§ãŸããŸãã æ§æã解æããããã«ããããå®å šã«ç解ããå¿ èŠã¯ãããŸãããããã®ã¿ã€ãã®ææ³ã®å¶éã«ã€ããŠç¥ãããšã¯ãŸã 䟡å€ããããŸãã ã ãããããŒãã€ã«ãã¯åºæ¬çã«ã§ããŸããïŒ
- å·Šã®ååž°ææ³ã解æããŸãã ããã¯ãPEGãå«ããã¹ãŠã®ãããããŠã³ããŒãµãŒã®èœåãè¶ ããŠããŸãã ãã ããå·Šååž°ææ³ã¯é©å¿ã§ããŸãã
- PythonãYAMLãªã©ã®ã€ã³ãã³ãããŒã¹ã®ææ³ã解æããŸãã çæãããããŒãµãŒãåäžãã¹ã§ãããå¥ã®ã¬ã¯ãµãŒããªããããããã¯ã§ããŸããã ã€ã³ãã³ãåæã¯ãåå¥è§£æã®æ®µéã§å®è¡ãããŸãã ãã®åé¡ã«ã¯ç°¡åãªè§£æ±ºçããããŸããã€ã³ãã³ãã®åïŒ
INDENT
ïŒãšåŸïŒDEDENT
ïŒã«ä»®æ³ããŒã«ãŒãé 眮ããããªããã»ããµãèšè¿°ããŸãã Parboiled1ã«ã¯ãã®ããã®æšæºããŒã«ããããŸãããParboiled2ã«ã€ããŠã¯ãåæ§ã®æé ããŸã ç¬ç«ããŠå®è¡ããå¿ èŠããããŸãã - ã¹ããªãŒãã³ã°å ¥åã䜿çšããŸãã PEGã¯ãªã¿ãŒã³æ€çŽ¢ã䜿çšããŸã; ããã¯ãã©ããã³ã°ã§ãã çè«çã«ã¯ããã®æ¬ ç¹ã¯ã¹ããªãŒã ãããã¡ãªã³ã°ã«ãã£ãŠè§£æ¶ã§ããŸãããæåã«æ»ãææ³ãæžãããšã劚ãããã®ã¯äœããããŸããã ãããã£ãŠããã®ã¢ã€ãã¢ãå®éã«æ©èœããããã«ã¯ãæ»ããäžå¯èœãªãã£ã³ã¯ã®ææ³å¢çã決å®ããããšãåŠã¶å¿ èŠããããŸãã Matthiasã¯ãã®æ©èœã®éçºã«éåžžã«èå³ãæã£ãŠãããããå°æ¥ã®ãªãªãŒã¹ã§ç»å Žããå¯èœæ§ããããŸãã
次ã®ããŒãã§ã¯ãã«ã¹ã¿ã ã°ã©ããŒãParboiledã§ã©ã®ããã«èšè¿°ããããã«ã€ããŠèª¬æããŸãããŸããèšå®ãã¡ã€ã«ã®ããªãŒã®ãããªåœ¢åŒã®ç°¡åãªèªèæ©èœãäœæããŸãã