Compare commits
367 Commits
Author | SHA1 | Date |
---|---|---|
Futago-za Ryuu | b7b87ea8ae | 5 years ago |
Futago-za Ryuu | 56722b6d41 | 5 years ago |
Futago-za Ryuu | afeb2a3330 | 5 years ago |
Futago-za Ryuu | 5f8344395c | 5 years ago |
Futago-za Ryuu | 04275b23da | 5 years ago |
Futago-za Ryuu | 3c05ffe3f9 | 5 years ago |
Futago-za Ryuu | a1cf32d65a | 5 years ago |
Futago-za Ryuu | 68ca388d91 | 5 years ago |
Futago-za Ryuu | 5a209ff186 | 5 years ago |
Futago-za Ryuu | 9360abb554 | 5 years ago |
Futago-za Ryuu | 77b5fe7254 | 5 years ago |
Futago-za Ryuu | c8afff5864 | 5 years ago |
Futago-za Ryuu | f897df619e | 5 years ago |
Futago-za Ryuu | 8c653757e2 | 5 years ago |
Futago-za Ryuu | a7373c7345 | 5 years ago |
Futago-za Ryuu | e9920c8c4c | 5 years ago |
Futago-za Ryuu | 1159660cf7 | 5 years ago |
Futago-za Ryuu | d615cd7975 | 5 years ago |
Futago-za Ryuu | c523cf5693 | 5 years ago |
Futago-za Ryuu | 6484145542 | 5 years ago |
Futago-za Ryuu | 1cd8ba29b0 | 5 years ago |
Futago-za Ryuu | f6ddc51745 | 5 years ago |
Futago-za Ryuu | 61eaba4c01 | 5 years ago |
Futago-za Ryuu | 7d13580855 | 5 years ago |
Futago-za Ryuu | 8b7bff7ac3 | 5 years ago |
Futago-za Ryuu | 61377b1bd7 | 5 years ago |
Futago-za Ryuu | cf022429bb | 5 years ago |
Futago-za Ryuu | f00b516cd7 | 5 years ago |
Futago-za Ryuu | 310252f7c5 | 5 years ago |
Futago-za Ryuu | d08e8bf52d | 5 years ago |
Futago-za Ryuu | 138fb96aed | 5 years ago |
Futago-za Ryuu | e16dc50686 | 5 years ago |
Futago-za Ryuu | fae001e4ed | 5 years ago |
Nils-Hero Lindemann | 0b102d29a8 | 5 years ago |
Futago-za Ryuu | 30f3260008 | 5 years ago |
Futago-za Ryuu | 56f3c98579 | 5 years ago |
Futago-za Ryuu | 42514c4951 | 5 years ago |
Futago-za Ryuu | 910a51bf5f | 5 years ago |
Futago-za Ryuu | 42a6f774fb | 5 years ago |
Futago-za Ryuu | 9b8b1c5e64 | 5 years ago |
Futago-za Ryuu | 85dbe5a396 | 5 years ago |
Futago-za Ryuu | 0253215c24 | 5 years ago |
Futago-za Ryuu | 53b966289b | 5 years ago |
Futago-za Ryuu | e82994582a | 5 years ago |
Futago-za Ryuu | f69239dcc0 | 5 years ago |
Futago-za Ryuu | ec3f2962a8 | 5 years ago |
Futago-za Ryuu | 83616b4d5d | 5 years ago |
Futago-za Ryuu | 6a810cc78f | 5 years ago |
Futago-za Ryuu | 38cd65d193 | 5 years ago |
Futago-za Ryuu | baf8b3a339 | 5 years ago |
Futago-za Ryuu | d7c11c54e8 | 5 years ago |
Futago-za Ryuu | ef2d318c15 | 5 years ago |
Futago-za Ryuu | 703fbbf8d0 | 5 years ago |
Futago-za Ryuu | caa8b4cf7b | 5 years ago |
Futago-za Ryuu | fcbfa39102 | 5 years ago |
Futago-za Ryuu | ea991cd65c | 5 years ago |
Futago-za Ryuu | ed6690e5bb | 5 years ago |
Futago-za Ryuu | 6525825f2e | 5 years ago |
Futago-za Ryuu | f7a52c70a4 | 5 years ago |
Futago-za Ryuu | c35a61f524 | 5 years ago |
Futago-za Ryuu | a2c677ef38 | 5 years ago |
Futago-za Ryuu | 5542547091 | 5 years ago |
Futago-za Ryuu | b1350d46f8 | 5 years ago |
Futago-za Ryuu | 05c6fc9a34 | 5 years ago |
Futago-za Ryuu | 9801b966c0 | 5 years ago |
Futago-za Ryuu | 97f98122f8 | 5 years ago |
Futago-za Ryuu | 8d1a9a6ceb | 5 years ago |
Futago-za Ryuu | da3ea8e47d | 5 years ago |
Futago-za Ryuu | b2e6958698 | 5 years ago |
Futago-za Ryuu | 7a78b7cfdf | 5 years ago |
Futago-za Ryuu | f13d0268d6 | 5 years ago |
Futago-za Ryuu | 06f782d397 | 5 years ago |
Futago-za Ryuu | e72e85bc1d | 5 years ago |
Futago-za Ryuu | 0259640d3c | 5 years ago |
Futago-za Ryuu | e6c4d8289d | 5 years ago |
Futago-za Ryuu | 935f80d740 | 5 years ago |
Futago-za Ryuu | 9906f21c57 | 5 years ago |
Futago-za Ryuu | dedb372076 | 5 years ago |
Futago-za Ryuu | c67fdeb59b | 5 years ago |
Futago-za Ryuu | ba6cac15b3 | 5 years ago |
Futago-za Ryuu | 41b5f44288 | 5 years ago |
Futago-za Ryuu | 0074d0456a | 5 years ago |
Futago-za Ryuu | 9716fe5f38 | 5 years ago |
Futago-za Ryuu | c17e9b2df7 | 5 years ago |
Futago-za Ryuu | c59c81c318 | 5 years ago |
Futago-za Ryuu | dcd43eb251 | 5 years ago |
Futago-za Ryuu | 9d381f0a09 | 5 years ago |
Futago-za Ryuu | b240b77386 | 5 years ago |
Futago-za Ryuu | 1f1ac34009 | 5 years ago |
Futago-za Ryuu | 8b5d384e32 | 5 years ago |
Futago-za Ryuu | 7f908e9a7b | 5 years ago |
Futago-za Ryuu | d8b7e15169 | 5 years ago |
Futago-za Ryuu | 15cb8ff968 | 5 years ago |
azure-pipelines[bot] | 024ac1a1b5 | 5 years ago |
Futago-za Ryuu | aa0412b2fb | 5 years ago |
Futago-za Ryuu | 3986ca7048 | 5 years ago |
Futago-za Ryuu | e17cdabd4d | 5 years ago |
Futago-za Ryuu | 2dc0a1b64a | 5 years ago |
Futago-za Ryuu | 485f37c290 | 5 years ago |
Futago-za Ryuu | bdde25a9e4 | 5 years ago |
Futago-za Ryuu | 8c30834ccc | 5 years ago |
Futago-za Ryuu | b7d4d01ff5 | 5 years ago |
Futago-za Ryuu | 498c13f7a9 | 5 years ago |
Futago-za Ryuu | 132a68b4d5 | 5 years ago |
Futago-za Ryuu | 2433c76ffb | 5 years ago |
Futago-za Ryuu | 4095a05420 | 5 years ago |
Futago-za Ryuu | 9b805a955d | 5 years ago |
Futago-za Ryuu | f10d93f32f | 5 years ago |
Futago-za Ryuu | 12c98b7098 | 5 years ago |
Futago-za Ryuu | 22c280f069 | 5 years ago |
Futago-za Ryuu | a8e7534595 | 5 years ago |
Futago-za Ryuu | a04486711f | 5 years ago |
Futago-za Ryuu | 29943edf60 | 5 years ago |
Futago-za Ryuu | f754684fae | 5 years ago |
Futago-za Ryuu | 1dd5a04ef2 | 5 years ago |
Futago-za Ryuu | 5dfb6b14dc | 5 years ago |
Futago-za Ryuu | 011b28be03 | 5 years ago |
Futago-za Ryuu | 9751980261 | 6 years ago |
Futago-za Ryuu | 4f9cec5937 | 6 years ago |
Futago-za Ryuu | 7cf887ec4b | 6 years ago |
Futago-za Ryuu | 4723176e20 | 6 years ago |
Futago-za Ryuu | 104edadf27 | 6 years ago |
Futago-za Ryuu | 93ceeff597 | 6 years ago |
Futago-za Ryuu | 4866d6ddb6 | 6 years ago |
Futago-za Ryuu | 7deb6a779e | 6 years ago |
Futago-za Ryuu | 0f2426bdbf | 6 years ago |
Futago-za Ryuu | 6d4ad48319 | 6 years ago |
Futago-za Ryuu | 1c81df1245 | 6 years ago |
Futago-za Ryuu | 71414cfb19 | 6 years ago |
Futago-za Ryuu | aa58b73cae | 6 years ago |
Futago-za Ryuu | 390478d61e | 6 years ago |
Futago-za Ryuu | 70a6fab636 | 6 years ago |
Futago-za Ryuu | 163ff20508 | 6 years ago |
Futago-za Ryuu | 9c039ce79b | 6 years ago |
Futago-za Ryuu | 32477337c1 | 6 years ago |
Futago-za Ryuu | 87dcc13d0f | 6 years ago |
Futago-za Ryuu | e69646cc78 | 6 years ago |
Futago-za Ryuu | 2ae32a6510 | 6 years ago |
Pascal Brandt | d29704ff9c | 6 years ago |
Futago-za Ryuu | afc653c46f | 6 years ago |
Futago-za Ryuu | 27687615e5 | 6 years ago |
Futago-za Ryuu | d233660161 | 6 years ago |
Futago-za Ryuu | a70fc76648 | 6 years ago |
Futago-za Ryuu | 176f89b9c2 | 6 years ago |
Noah Doersing | 1d2ed4ca22 | 6 years ago |
Futago-za Ryuu | 3d755b8196 | 6 years ago |
Futago-za Ryuu | a1b8013b6b | 6 years ago |
Futago-za Ryuu | 4f4af756d3 | 6 years ago |
Futago-za Ryuu | 40b7cdfa81 | 6 years ago |
Futago-za Ryuu | fe2237476f | 6 years ago |
Futago-za Ryuu | 213c70ced4 | 6 years ago |
Futago-za Ryuu | 14b8b76a79 | 6 years ago |
Futago-za Ryuu | 616749377b | 6 years ago |
Futago-za Ryuu | e636ee654c | 6 years ago |
Futago-za Ryuu | ec93e64939 | 6 years ago |
Futago-za Ryuu | 9dc4d4a5ef | 6 years ago |
Futago-za Ryuu | fd3dbb77ac | 6 years ago |
Futago-za Ryuu | e0e9fbcd30 | 6 years ago |
Futago-za Ryuu | e64118f3b7 | 6 years ago |
Futago-za Ryuu | 4964b9af7e | 6 years ago |
Futago-za Ryuu | c65e9dd95f | 6 years ago |
Futago-za Ryuu | 460f0cc5bc | 6 years ago |
Futago-za Ryuu | 26969475f7 | 6 years ago |
Futago-za Ryuu | 6bc85de256 | 6 years ago |
Futago-za Ryuu | 4b0f637626 | 6 years ago |
Futago-za Ryuu | 8b43c8419f | 6 years ago |
Futago-za Ryuu | 95abd42c95 | 6 years ago |
Futago-za Ryuu | ecb81504cb | 6 years ago |
Futago-za Ryuu | aadc318fcd | 6 years ago |
Futago-za Ryuu | 7813c07025 | 6 years ago |
Futago-za Ryuu | 506419474d | 6 years ago |
Futago-za Ryuu | dd044b8cfb | 6 years ago |
Futago-za Ryuu | 46280b3992 | 6 years ago |
Futago-za Ryuu | dd76ff85d4 | 6 years ago |
Futago-za Ryuu | 8c751e6917 | 6 years ago |
Futago-za Ryuu | 445cdcde44 | 6 years ago |
Futago-za Ryuu | 2b543ed97a | 6 years ago |
Futago-za Ryuu | be88239543 | 6 years ago |
Futago-za Ryuu | 35a650dc50 | 6 years ago |
Futago-za Ryuu | 60c81d1cba | 6 years ago |
Futago-za Ryuu | 46ce02404f | 6 years ago |
Futago-za Ryuu | a8d8afbbda | 6 years ago |
Futago-za Ryuu | f09f2f2cb4 | 6 years ago |
Futago-za Ryuu | 5e7134936b | 6 years ago |
Futago-za Ryuu | 4596daae28 | 6 years ago |
Futago-za Ryuu | dfbfdd350b | 6 years ago |
Futago-za Ryuu | 7c93ffb22b | 6 years ago |
Futago-za Ryuu | 8bd4d390a9 | 6 years ago |
Futago-za Ryuu | 7edc7d902b | 6 years ago |
Futago-za Ryuu | f66751ba39 | 6 years ago |
Futago-za Ryuu | 60f69d6558 | 6 years ago |
Futago-za Ryuu | b92690392e | 6 years ago |
Futago-za Ryuu | 0089da6698 | 6 years ago |
Futago-za Ryuu | 2790479154 | 6 years ago |
Futago-za Ryuu | 3cedf4439d | 6 years ago |
Futago-za Ryuu | 06c9400bca | 6 years ago |
Futago-za Ryuu | 4089aac71e | 6 years ago |
Futago-za Ryuu | b8db835af9 | 6 years ago |
Futago-za Ryuu | 94ca36469b | 6 years ago |
Futago-za Ryuu | 146d021d21 | 6 years ago |
Futago-za Ryuu | fbc8ea2bdf | 6 years ago |
Futago-za Ryuu | b81396a904 | 6 years ago |
Futago-za Ryuu | b31009f73d | 6 years ago |
Futago-za Ryuu | 01555ebbac | 6 years ago |
Futago-za Ryuu | 8d3dc109ed | 6 years ago |
Futago-za Ryuu | 878421ab75 | 6 years ago |
Futago-za Ryuu | 83e3b9edc4 | 6 years ago |
Futago-za Ryuu | af917b1e4d | 6 years ago |
Futago-za Ryuu | dbdba75b15 | 6 years ago |
Futago-za Ryuu | 7e10622b9a | 6 years ago |
Futago-za Ryuu | 1fca2daff6 | 6 years ago |
Futago-za Ryuu | b5d01f7505 | 6 years ago |
Futago-za Ryuu | c2c823196f | 6 years ago |
Futago-za Ryuu | 699038c507 | 6 years ago |
Futago-za Ryuu | ab9b8d41bb | 6 years ago |
Futago-za Ryuu | 87ad103177 | 6 years ago |
Futago-za Ryuu | 94a5f24817 | 6 years ago |
Futago-za Ryuu | b3329e626e | 6 years ago |
Futago-za Ryuu | 6335818cb7 | 6 years ago |
Futago-za Ryuu | 3745195315 | 6 years ago |
Futago-za Ryuu | 72ba85b3af | 6 years ago |
Futago-za Ryuu | 7cd291cbe0 | 6 years ago |
Futago-za Ryuu | 70dea07923 | 6 years ago |
Futago-za Ryuu | 46c3dcf40b | 6 years ago |
Futago-za Ryuu | f4ca7ca461 | 6 years ago |
Futago-za Ryuu | 6d25da2d2b | 6 years ago |
Futago-za Ryuu | 126a4c95f4 | 6 years ago |
Futago-za Ryuu | 2f0680ca16 | 6 years ago |
Futago-za Ryuu | 21a6de06d5 | 6 years ago |
Futago-za Ryuu | 15d364587c | 6 years ago |
Futago-za Ryuu | 2ac387e1c9 | 6 years ago |
Futago-za Ryuu | 418f441d88 | 6 years ago |
Futago-za Ryuu | fedf0f3480 | 6 years ago |
Futago-za Ryuu | 43a305eaef | 6 years ago |
Futago-za Ryuu | b3135690b0 | 6 years ago |
Futago-za Ryuu | 30cfa29553 | 6 years ago |
Futago-za Ryuu | 04dfef3b63 | 6 years ago |
Futago-za Ryuu | ef0595596f | 6 years ago |
Futago-za Ryuu | a11d217167 | 6 years ago |
Futago-za Ryuu | 621b11b715 | 6 years ago |
Futago-za Ryuu | 6500189d58 | 6 years ago |
Futago-za Ryuu | 851d8edfdd | 6 years ago |
Futago-za Ryuu | f4c67993f6 | 6 years ago |
Futago-za Ryuu | 8e9be9afea | 6 years ago |
Futago-za Ryuu | 11bc94f4c3 | 6 years ago |
Futago-za Ryuu | a5d7cc11a4 | 6 years ago |
Futago-za Ryuu | fe6f09238a | 6 years ago |
Futago-za Ryuu | 8d711627c1 | 6 years ago |
Futago-za Ryuu | 269b36b9db | 6 years ago |
Futago-za Ryuu | a79757683f | 6 years ago |
Futago-za Ryuu | 5476eca59f | 6 years ago |
Futago-za Ryuu | f41ef82ce9 | 6 years ago |
Futago-za Ryuu | 4b6ceb2b46 | 6 years ago |
Futago-za Ryuu | b0a5db1ab9 | 6 years ago |
Futago-za Ryuu | 9ecb21b749 | 6 years ago |
Futago-za Ryuu | 9d266625b4 | 6 years ago |
Mingun | 1a713d0175 | 6 years ago |
Mingun | 0dab14d652 | 6 years ago |
Futago-za Ryuu | b6bc0d905e | 6 years ago |
Futago-za Ryuu | d06a5b52ef | 6 years ago |
Futago-za Ryuu | 75a4101622 | 6 years ago |
Futago-za Ryuu | f5b323b401 | 6 years ago |
Mingun | 9b90fa1d81 | 6 years ago |
Futago-za Ryuu | 617b6b7425 | 6 years ago |
Futago-za Ryuu | 27ec5ed9b1 | 6 years ago |
Futago-za Ryuu | 7a19d46e8a | 6 years ago |
Futago-za Ryuu | 93cc6c5b26 | 6 years ago |
Futago-za Ryuu | 7cdfc03e9f | 6 years ago |
Futago-za Ryuu | 02486cef7f | 6 years ago |
Futago-za Ryuu | 1b2072f239 | 6 years ago |
Futago-za Ryuu | b0056d7a9f | 6 years ago |
Mingun | 4cc9185a78 | 6 years ago |
Futago-za Ryuu | 1434346332 | 6 years ago |
Futago-za Ryuu | 93f9068c99 | 6 years ago |
Mingun | fde1c480f4 | 6 years ago |
Mingun | a480c7d39c | 6 years ago |
Mingun | 534dc53ac2 | 6 years ago |
Futago-za Ryuu | f5699d63fb | 6 years ago |
Futago-za Ryuu | cb385476c1 | 6 years ago |
Futago-za Ryuu | 92f1798308 | 6 years ago |
Futago-za Ryuu | 06f1565a32 | 6 years ago |
Mingun | 42ec335d13 | 6 years ago |
Mingun | a7a0a0d9ac | 6 years ago |
Mingun | 669f782a5f | 6 years ago |
Futago-za Ryuu | 1b20aa5427 | 6 years ago |
Futago-za Ryuu | 03cc2eae95 | 6 years ago |
Futago-za Ryuu | ea688abd02 | 6 years ago |
Futago-za Ryuu | cd02806c94 | 6 years ago |
Futago-za Ryuu | 95409f0155 | 6 years ago |
Futago-za Ryuu | 01a2be7b78 | 6 years ago |
Futago-za Ryuu | d3cbbee592 | 6 years ago |
Futago-za Ryuu | a02f8091e3 | 6 years ago |
Mingun | 3e89534bb7 | 6 years ago |
Futago-za Ryuu | 75d655409c | 6 years ago |
Mingun | 3f8cf7cd58 | 6 years ago |
Mingun | f0e5eebe1a | 6 years ago |
Futago-za Ryuu | d6142772f8 | 6 years ago |
Futago-za Ryuu | 1dd1487f9b | 6 years ago |
Futago-za Ryuu | db70215c4a | 6 years ago |
Futago-za Ryuu | 247a0bf707 | 6 years ago |
Futago-za Ryuu | f7dd11698a | 6 years ago |
Futago-za Ryuu | 2a7dcdcfbe | 7 years ago |
Futago-za Ryuu | 881eb67545 | 7 years ago |
Futago-za Ryuu | a33eb0b45c | 7 years ago |
Futago-za Ryuu | 30d39f6267 | 7 years ago |
Futago-za Ryuu | 280466cc7b | 7 years ago |
Futago-za Ryuu | 9568522d9c | 7 years ago |
Futago-za Ryuu | 123a20a83c | 7 years ago |
Futago-za Ryuu | 37698d8981 | 7 years ago |
Futago-za Ryuu | b4e5b97a2e | 7 years ago |
Futago-za Ryuu | 5f0001c3e5 | 7 years ago |
Futago-za Ryuu | e6d018a88d | 7 years ago |
Futago-za Ryuu | 3c6523ff83 | 7 years ago |
Futago-za Ryuu | 50cbcd6704 | 7 years ago |
Futago-za Ryuu | 35832f474f | 7 years ago |
Futago-za Ryuu | 9802bae15f | 7 years ago |
Futago-za Ryuu | abadaf4d5d | 7 years ago |
Futago-za Ryuu | 96a386ff15 | 7 years ago |
Futago-za Ryuu | 5c36738a52 | 7 years ago |
Futago-za Ryuu | 08bbd6d5b6 | 7 years ago |
Futago-za Ryuu | 5a833bd982 | 7 years ago |
Futago-za Ryuu | a1ad351c2c | 7 years ago |
Futago-za Ryuu | 14948dad93 | 7 years ago |
felix | 6a9a9a5d23 | 7 years ago |
felix | cb3c5f4473 | 7 years ago |
felix | 369b8cdcc6 | 7 years ago |
felix | ee00a1ae6a | 7 years ago |
felix | 958e15879d | 7 years ago |
Futago-za Ryuu | 5cde815341 | 7 years ago |
Alan Mimms | 4cdc6d6353 | 7 years ago |
Futago-za Ryuu | 57bb28414e | 7 years ago |
Futago-za Ryuu | 0ed8c6f89a | 7 years ago |
Futago-za Ryuu | d7d5f375f1 | 7 years ago |
Futago-za Ryuu | 48a0c37997 | 7 years ago |
Futago-za Ryuu | 6661f6b958 | 7 years ago |
Futago-za Ryuu | b926be7d71 | 7 years ago |
Futago-za Ryuu | dcf9790da0 | 7 years ago |
Futago-za Ryuu | 30ea7d5ed0 | 7 years ago |
Futago-za Ryuu | 55bcaa46ef | 7 years ago |
Futago-za Ryuu | 10ec7f0576 | 7 years ago |
Futago-za Ryuu | 8cf81b47b7 | 7 years ago |
Futago-za Ryuu | 1afe682c65 | 7 years ago |
Mingun | c98fee1629 | 7 years ago |
Mingun | 575db38d1f | 7 years ago |
Futago-za Ryuu | 03912dc761 | 7 years ago |
Futago-za Ryuu | 928fdca6ac | 7 years ago |
Futago-za Ryuu | 8856913ae8 | 7 years ago |
Futago-za Ryuu | 9658f4a92d | 7 years ago |
Futago-za Ryuu | 081856cdeb | 7 years ago |
Futago-za Ryuu | 5e3ed79f95 | 7 years ago |
Futago-za Ryuu | 6939d1eb8d | 7 years ago |
Futago-za Ryuu | bc5c8a0f97 | 7 years ago |
fatfisz | 9c60380f86 | 7 years ago |
fatfisz | d1f62596f1 | 7 years ago |
fatfisz | c541911c04 | 7 years ago |
fatfisz | e3b7f0c3a9 | 7 years ago |
fatfisz | aab928de91 | 7 years ago |
Futago-za Ryuu | 35f3c5267a | 7 years ago |
Futago-za Ryuu | f18cf30d76 | 7 years ago |
Futago-za Ryuu | 91c85d824d | 7 years ago |
Futago-za Ryuu | 624f87f66b | 7 years ago |
David Majda | ba2f8bf3c2 | 7 years ago |
David Berneda | 962db9d090 | 7 years ago |
Adhir Ramjiawan | f861c2f476 | 7 years ago |
Caleb Hearon | 770ca6e723 | 7 years ago |
Adrian Sampson | ceebc8cda1 | 8 years ago |
Adrian Sampson | 3330ceb7b8 | 8 years ago |
@ -0,0 +1,13 @@
|
||||
codecov:
|
||||
ci:
|
||||
- github.com
|
||||
|
||||
ignore:
|
||||
- "**/*/dist/*.js"
|
||||
- "**/*.{spec,test}.js"
|
||||
- "**/*.eslintrc.js"
|
||||
- "{public,website}/**/*.js"
|
||||
|
||||
parsers:
|
||||
javascript:
|
||||
enable_partials: yes
|
@ -0,0 +1,8 @@
|
||||
!.eslintrc.js
|
||||
/.idea
|
||||
/.nyc_output
|
||||
/public
|
||||
/packages/pegjs/dist
|
||||
/coverage
|
||||
node_modules
|
||||
/website/assets
|
@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
|
||||
"extends": [
|
||||
|
||||
"@futagoza/node/8",
|
||||
|
||||
],
|
||||
|
||||
"rules": {
|
||||
|
||||
"no-duplicate-imports": [ "error", { "includeExports": false } ],
|
||||
"no-eval": 0,
|
||||
|
||||
},
|
||||
|
||||
"overrides": [ {
|
||||
|
||||
"files": [
|
||||
"test/unit/compiler/passes/helpers.js",
|
||||
"*.spec.js",
|
||||
"*.test.js",
|
||||
],
|
||||
"extends": "@futagoza/dev/test",
|
||||
"rules": {
|
||||
|
||||
"func-names": 0,
|
||||
"no-mixed-operators": 0,
|
||||
|
||||
},
|
||||
|
||||
} ],
|
||||
|
||||
"root": true,
|
||||
|
||||
};
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"extends": "dmajda"
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at futagoza.ryuu+pegjs@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
@ -0,0 +1,54 @@
|
||||
### Issue type
|
||||
|
||||
<!-- What type of issue is this (choose _yes_ or _no_): -->
|
||||
|
||||
- **Bug Report:**
|
||||
- **Feature Request:**
|
||||
- **Question:**
|
||||
- **Not an issue:**
|
||||
|
||||
### Prerequisites
|
||||
|
||||
<!-- Have you done the following (choose _yes_ or _no_): -->
|
||||
|
||||
- **Can you reproduce the issue?:**
|
||||
- **Did you search the repository issues?:**
|
||||
- **Did you check the forums?:**
|
||||
- **Did you perform a web search (google, yahoo, etc)?:**
|
||||
|
||||
<!-- For more information, see the CONTRIBUTING document. -->
|
||||
|
||||
### Description
|
||||
|
||||
<!-- A description of the bug, feature or question -->
|
||||
|
||||
### Steps to Reproduce
|
||||
|
||||
1. <!-- First Step -->
|
||||
2. <!-- Second Step -->
|
||||
3. <!-- and so on... -->
|
||||
|
||||
**Example code:**
|
||||
|
||||
```js
|
||||
// An example of code related to this issue
|
||||
```
|
||||
|
||||
**Expected behavior:**
|
||||
|
||||
<!-- What you expected to happen -->
|
||||
|
||||
**Actual behavior:**
|
||||
|
||||
<!-- What actually happened -->
|
||||
|
||||
### Software
|
||||
|
||||
<!-- Having more details about your software might help pinpoint the source of a bug: -->
|
||||
|
||||
- **PEG.js:**
|
||||
- **Node.js:**
|
||||
- **NPM or Yarn:**
|
||||
- **Browser:**
|
||||
- **OS:**
|
||||
- **Editor:**
|
@ -0,0 +1,20 @@
|
||||
### PR type
|
||||
|
||||
<!--- What types of changes does your code introduce? (choose _yes_ or _no_): -->
|
||||
|
||||
- **Bug fix (non-breaking change which fixes an issue):**
|
||||
- **New feature (non-breaking change which adds functionality):**
|
||||
- **Breaking change (fix or feature that would cause existing functionality to change):**
|
||||
- **Documentation change:**
|
||||
|
||||
### Prerequisites
|
||||
|
||||
<!-- Have you done the following (choose _yes_ or _no_): -->
|
||||
|
||||
- **I have read the [CONTRIBUTING.md](CONTRIBUTING.md) document:**
|
||||
- **I have updated the documentation accordingly:**
|
||||
- **I have added tests to cover my changes:**
|
||||
|
||||
### Description
|
||||
|
||||
<!-- A description of the PR and commits included -->
|
@ -0,0 +1,87 @@
|
||||
name: Github Actions
|
||||
|
||||
on: [ push, pull_request ]
|
||||
|
||||
jobs:
|
||||
|
||||
lint:
|
||||
name: Lint
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@master
|
||||
with:
|
||||
node-version: 10.x
|
||||
|
||||
- name: Install Yarn
|
||||
run: npm install --global yarn
|
||||
|
||||
- name: Install dependencies
|
||||
run: yarn --ignore-engines
|
||||
|
||||
- name: Lint JavaScript files using ESLint
|
||||
run: yarn lint
|
||||
|
||||
ci:
|
||||
name: Test
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [ 8.x, 10.x, 12.x ]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@master
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install Yarn
|
||||
run: npm install --global yarn
|
||||
|
||||
- name: Install dependencies
|
||||
run: yarn --ignore-engines
|
||||
|
||||
- name: Run tests
|
||||
run: yarn test
|
||||
|
||||
- name: Publish code coverage results
|
||||
run: yarn coverage
|
||||
env:
|
||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
||||
cd:
|
||||
name: Publish
|
||||
needs: [ lint, ci ]
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@master
|
||||
with:
|
||||
node-version: 10.x
|
||||
|
||||
- name: Install Yarn
|
||||
run: npm install --global yarn
|
||||
|
||||
- name: Install dependencies
|
||||
run: yarn --ignore-engines
|
||||
|
||||
- name: 'Build "pegjs/dist/*.js" files'
|
||||
run: yarn build-dist
|
||||
|
||||
- name: Publish pegjs@dev to NPM
|
||||
run: node tools/publish-dev
|
||||
env:
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
@ -1,3 +1,11 @@
|
||||
browser/*
|
||||
examples/*.js
|
||||
node_modules/*
|
||||
/.idea
|
||||
/packages/pegjs/dist
|
||||
/packages/pegjs/.npmrc
|
||||
/public
|
||||
/examples/*.js
|
||||
node_modules
|
||||
/coverage
|
||||
/.nyc_output
|
||||
/*.log
|
||||
/sh.exe.stackdump
|
||||
/test/junit-results.xml
|
||||
|
@ -0,0 +1,12 @@
|
||||
/.github
|
||||
/.idea
|
||||
/.nyc_output
|
||||
/coverage
|
||||
node_modules
|
||||
/public
|
||||
/.codecov.yml
|
||||
/.eslint*
|
||||
/.nycrc
|
||||
/CHANGELOG.md
|
||||
/tsconfig.json
|
||||
/README.md
|
@ -0,0 +1,7 @@
|
||||
{
|
||||
"include": [ "packages/**" ],
|
||||
"reporter": [
|
||||
"lcov",
|
||||
"text-summary"
|
||||
]
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
sudo: false
|
||||
|
||||
language: node_js
|
||||
|
||||
node_js:
|
||||
- "4.0"
|
||||
- "5.0"
|
||||
- "6.0"
|
||||
- "7.0"
|
||||
|
||||
before_script:
|
||||
- npm install -g gulp
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
@ -1,527 +1,64 @@
|
||||
[![Build status](https://img.shields.io/travis/pegjs/pegjs.svg)](https://travis-ci.org/pegjs/pegjs)
|
||||
[![npm version](https://img.shields.io/npm/v/pegjs.svg)](https://www.npmjs.com/package/pegjs)
|
||||
[![Bower version](https://img.shields.io/bower/v/pegjs.svg)](https://github.com/pegjs/bower)
|
||||
[![License](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)
|
||||
|
||||
PEG.js
|
||||
======
|
||||
|
||||
PEG.js is a simple parser generator for JavaScript that produces fast parsers
|
||||
with excellent error reporting. You can use it to process complex data or
|
||||
computer languages and build transformers, interpreters, compilers and other
|
||||
tools easily.
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
* Simple and expressive grammar syntax
|
||||
* Integrates both lexical and syntactical analysis
|
||||
* Parsers have excellent error reporting out of the box
|
||||
* Based on [parsing expression
|
||||
grammar](http://en.wikipedia.org/wiki/Parsing_expression_grammar) formalism
|
||||
— more powerful than traditional LL(*k*) and LR(*k*) parsers
|
||||
* Usable [from your browser](https://pegjs.org/online), from the command line,
|
||||
or via JavaScript API
|
||||
|
||||
Getting Started
|
||||
---------------
|
||||
|
||||
[Online version](https://pegjs.org/online) is the easiest way to generate a
|
||||
parser. Just enter your grammar, try parsing few inputs, and download generated
|
||||
parser code.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
### Node.js
|
||||
|
||||
To use the `pegjs` command, install PEG.js globally:
|
||||
|
||||
```console
|
||||
$ npm install -g pegjs
|
||||
```
|
||||
|
||||
To use the JavaScript API, install PEG.js locally:
|
||||
|
||||
```console
|
||||
$ npm install pegjs
|
||||
```
|
||||
|
||||
If you need both the `pegjs` command and the JavaScript API, install PEG.js both
|
||||
ways.
|
||||
|
||||
### Browser
|
||||
|
||||
[Download](https://pegjs.org/#download) the PEG.js library (regular or minified
|
||||
version) or install it using Bower:
|
||||
|
||||
```console
|
||||
$ bower install pegjs
|
||||
```
|
||||
|
||||
Generating a Parser
|
||||
-------------------
|
||||
|
||||
PEG.js generates parser from a grammar that describes expected input and can
|
||||
specify what the parser returns (using semantic actions on matched parts of the
|
||||
input). Generated parser itself is a JavaScript object with a simple API.
|
||||
|
||||
### Command Line
|
||||
|
||||
To generate a parser from your grammar, use the `pegjs` command:
|
||||
|
||||
```console
|
||||
$ pegjs arithmetics.pegjs
|
||||
```
|
||||
|
||||
This writes parser source code into a file with the same name as the grammar
|
||||
file but with “.js” extension. You can also specify the output file explicitly:
|
||||
|
||||
```console
|
||||
$ pegjs -o arithmetics-parser.js arithmetics.pegjs
|
||||
```
|
||||
|
||||
If you omit both input and output file, standard input and output are used.
|
||||
|
||||
By default, the generated parser is in the Node.js module format. You can
|
||||
override this using the `--format` option.
|
||||
|
||||
You can tweak the generated parser with several options:
|
||||
|
||||
* `--allowed-start-rules` — comma-separated list of rules the parser will be
|
||||
allowed to start parsing from (default: the first rule in the grammar)
|
||||
* `--cache` — makes the parser cache results, avoiding exponential parsing
|
||||
time in pathological cases but making the parser slower
|
||||
* `--dependency` — makes the parser require a specified dependency (can be
|
||||
specified multiple times)
|
||||
* `--export-var` — name of a global variable into which the parser object is
|
||||
assigned to when no module loader is detected
|
||||
* `--extra-options` — additional options (in JSON format) to pass to
|
||||
`peg.generate`
|
||||
* `--extra-options-file` — file with additional options (in JSON format) to
|
||||
pass to `peg.generate`
|
||||
* `--format` — format of the generated parser: `amd`, `commonjs`, `globals`,
|
||||
`umd` (default: `commonjs`)
|
||||
* `--optimize` — selects between optimizing the generated parser for parsing
|
||||
speed (`speed`) or code size (`size`) (default: `speed`)
|
||||
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple
|
||||
times)
|
||||
* `--trace` — makes the parser trace its progress
|
||||
|
||||
### JavaScript API
|
||||
|
||||
In Node.js, require the PEG.js parser generator module:
|
||||
|
||||
```javascript
|
||||
var peg = require("pegjs");
|
||||
```
|
||||
|
||||
In browser, include the PEG.js library in your web page or application using the
|
||||
`<script>` tag. If PEG.js detects an AMD loader, it will define itself as a
|
||||
module, otherwise the API will be available in the `peg` global object.
|
||||
|
||||
To generate a parser, call the `peg.generate` method and pass your grammar as a
|
||||
parameter:
|
||||
|
||||
```javascript
|
||||
var parser = peg.generate("start = ('a' / 'b')+");
|
||||
```
|
||||
|
||||
The method will return generated parser object or its source code as a string
|
||||
(depending on the value of the `output` option — see below). It will throw an
|
||||
exception if the grammar is invalid. The exception will contain `message`
|
||||
property with more details about the error.
|
||||
|
||||
You can tweak the generated parser by passing a second parameter with an options
|
||||
object to `peg.generate`. The following options are supported:
|
||||
|
||||
* `allowedStartRules` — rules the parser will be allowed to start parsing from
|
||||
(default: the first rule in the grammar)
|
||||
* `cache` — if `true`, makes the parser cache results, avoiding exponential
|
||||
parsing time in pathological cases but making the parser slower (default:
|
||||
`false`)
|
||||
* `dependencies` — parser dependencies, the value is an object which maps
|
||||
variables used to access the dependencies in the parser to module IDs used
|
||||
to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, or
|
||||
`"umd"` (default: `{}`)
|
||||
* `exportVar` — name of a global variable into which the parser object is
|
||||
assigned to when no module loader is detected; valid only when `format` is
|
||||
set to `"globals"` or `"umd"` (default: `null`)
|
||||
* `format` — format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`,
|
||||
`"globals"`, or `"umd"`); valid only when `output` is set to `"source"`
|
||||
(default: `"bare"`)
|
||||
* `optimize`— selects between optimizing the generated parser for parsing
|
||||
speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
|
||||
* `output` — if set to `"parser"`, the method will return generated parser
|
||||
object; if set to `"source"`, it will return parser source code as a string
|
||||
(default: `"parser"`)
|
||||
* `plugins` — plugins to use
|
||||
* `trace` — makes the parser trace its progress (default: `false`)
|
||||
|
||||
Using the Parser
|
||||
----------------
|
||||
|
||||
Using the generated parser is simple — just call its `parse` method and pass an
|
||||
input string as a parameter. The method will return a parse result (the exact
|
||||
value depends on the grammar used to generate the parser) or throw an exception
|
||||
if the input is invalid. The exception will contain `location`, `expected`,
|
||||
`found`, and `message` properties with more details about the error.
|
||||
|
||||
```javascript
|
||||
parser.parse("abba"); // returns ["a", "b", "b", "a"]
|
||||
|
||||
parser.parse("abcd"); // throws an exception
|
||||
```
|
||||
|
||||
You can tweak parser behavior by passing a second parameter with an options
|
||||
object to the `parse` method. The following options are supported:
|
||||
|
||||
* `startRule` — name of the rule to start parsing from
|
||||
* `tracer` — tracer to use
|
||||
|
||||
Parsers can also support their own custom options.
|
||||
|
||||
Grammar Syntax and Semantics
|
||||
----------------------------
|
||||
|
||||
The grammar syntax is similar to JavaScript in that it is not line-oriented and
|
||||
ignores whitespace between tokens. You can also use JavaScript-style comments
|
||||
(`// ...` and `/* ... */`).
|
||||
|
||||
Let's look at example grammar that recognizes simple arithmetic expressions like
|
||||
`2*(3+4)`. A parser generated from this grammar computes their values.
|
||||
|
||||
```pegjs
|
||||
start
|
||||
= additive
|
||||
|
||||
additive
|
||||
= left:multiplicative "+" right:additive { return left + right; }
|
||||
/ multiplicative
|
||||
|
||||
multiplicative
|
||||
= left:primary "*" right:multiplicative { return left * right; }
|
||||
/ primary
|
||||
|
||||
primary
|
||||
= integer
|
||||
/ "(" additive:additive ")" { return additive; }
|
||||
|
||||
integer "integer"
|
||||
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
|
||||
```
|
||||
|
||||
On the top level, the grammar consists of *rules* (in our example, there are
|
||||
five of them). Each rule has a *name* (e.g. `integer`) that identifies the rule,
|
||||
and a *parsing expression* (e.g. `digits:[0-9]+ { return
|
||||
parseInt(digits.join(""), 10); }`) that defines a pattern to match against the
|
||||
input text and possibly contains some JavaScript code that determines what
|
||||
happens when the pattern matches successfully. A rule can also contain
|
||||
*human-readable name* that is used in error messages (in our example, only the
|
||||
`integer` rule has a human-readable name). The parsing starts at the first rule,
|
||||
which is also called the *start rule*.
|
||||
|
||||
A rule name must be a JavaScript identifier. It is followed by an equality sign
|
||||
(“=”) and a parsing expression. If the rule has a human-readable name, it is
|
||||
written as a JavaScript string between the name and separating equality sign.
|
||||
Rules need to be separated only by whitespace (their beginning is easily
|
||||
recognizable), but a semicolon (“;”) after the parsing expression is allowed.
|
||||
|
||||
The first rule can be preceded by an *initializer* — a piece of JavaScript code
|
||||
in curly braces (“{” and “}”). This code is executed before the generated parser
|
||||
starts parsing. All variables and functions defined in the initializer are
|
||||
accessible in rule actions and semantic predicates. The code inside the
|
||||
initializer can access options passed to the parser using the `options`
|
||||
variable. Curly braces in the initializer code must be balanced. Let's look at
|
||||
the example grammar from above using a simple initializer.
|
||||
|
||||
```pegjs
|
||||
{
|
||||
function makeInteger(o) {
|
||||
return parseInt(o.join(""), 10);
|
||||
}
|
||||
}
|
||||
|
||||
start
|
||||
= additive
|
||||
|
||||
additive
|
||||
= left:multiplicative "+" right:additive { return left + right; }
|
||||
/ multiplicative
|
||||
|
||||
multiplicative
|
||||
= left:primary "*" right:multiplicative { return left * right; }
|
||||
/ primary
|
||||
|
||||
primary
|
||||
= integer
|
||||
/ "(" additive:additive ")" { return additive; }
|
||||
|
||||
integer "integer"
|
||||
= digits:[0-9]+ { return makeInteger(digits); }
|
||||
```
|
||||
|
||||
The parsing expressions of the rules are used to match the input text to the
|
||||
grammar. There are various types of expressions — matching characters or
|
||||
character classes, indicating optional parts and repetition, etc. Expressions
|
||||
can also contain references to other rules. See detailed description below.
|
||||
|
||||
If an expression successfully matches a part of the text when running the
|
||||
generated parser, it produces a *match result*, which is a JavaScript value. For
|
||||
example:
|
||||
|
||||
* An expression matching a literal string produces a JavaScript string
|
||||
containing matched text.
|
||||
* An expression matching repeated occurrence of some subexpression produces a
|
||||
JavaScript array with all the matches.
|
||||
|
||||
The match results propagate through the rules when the rule names are used in
|
||||
expressions, up to the start rule. The generated parser returns start rule's
|
||||
match result when parsing is successful.
|
||||
|
||||
One special case of parser expression is a *parser action* — a piece of
|
||||
JavaScript code inside curly braces (“{” and “}”) that takes match results of
|
||||
some of the the preceding expressions and returns a JavaScript value. This value
|
||||
is considered match result of the preceding expression (in other words, the
|
||||
parser action is a match result transformer).
|
||||
|
||||
In our arithmetics example, there are many parser actions. Consider the action
|
||||
in expression `digits:[0-9]+ { return parseInt(digits.join(""), 10); }`. It
|
||||
takes the match result of the expression [0-9]+, which is an array of strings
|
||||
containing digits, as its parameter. It joins the digits together to form a
|
||||
number and converts it to a JavaScript `number` object.
|
||||
|
||||
### Parsing Expression Types
|
||||
|
||||
There are several types of parsing expressions, some of them containing
|
||||
subexpressions and thus forming a recursive structure:
|
||||
|
||||
#### "*literal*"<br>'*literal*'
|
||||
|
||||
Match exact literal string and return it. The string syntax is the same as in
|
||||
JavaScript. Appending `i` right after the literal makes the match
|
||||
case-insensitive.
|
||||
|
||||
#### .
|
||||
|
||||
Match exactly one character and return it as a string.
|
||||
|
||||
#### [*characters*]
|
||||
|
||||
Match one character from a set and return it as a string. The characters in the
|
||||
list can be escaped in exactly the same way as in JavaScript string. The list of
|
||||
characters can also contain ranges (e.g. `[a-z]` means “all lowercase letters”).
|
||||
Preceding the characters with `^` inverts the matched set (e.g. `[^a-z]` means
|
||||
“all character but lowercase letters”). Appending `i` right after the right
|
||||
bracket makes the match case-insensitive.
|
||||
|
||||
#### *rule*
|
||||
|
||||
Match a parsing expression of a rule recursively and return its match result.
|
||||
|
||||
#### ( *expression* )
|
||||
|
||||
Match a subexpression and return its match result.
|
||||
|
||||
#### *expression* \*
|
||||
|
||||
Match zero or more repetitions of the expression and return their match results
|
||||
in an array. The matching is greedy, i.e. the parser tries to match the
|
||||
expression as many times as possible. Unlike in regular expressions, there is no
|
||||
backtracking.
|
||||
|
||||
#### *expression* +
|
||||
|
||||
Match one or more repetitions of the expression and return their match results
|
||||
in an array. The matching is greedy, i.e. the parser tries to match the
|
||||
expression as many times as possible. Unlike in regular expressions, there is no
|
||||
backtracking.
|
||||
|
||||
#### *expression* ?
|
||||
|
||||
Try to match the expression. If the match succeeds, return its match result,
|
||||
otherwise return `null`. Unlike in regular expressions, there is no
|
||||
backtracking.
|
||||
|
||||
#### & *expression*
|
||||
|
||||
Try to match the expression. If the match succeeds, just return `undefined` and
|
||||
do not consume any input, otherwise consider the match failed.
|
||||
|
||||
#### ! *expression*
|
||||
|
||||
Try to match the expression. If the match does not succeed, just return
|
||||
`undefined` and do not consume any input, otherwise consider the match failed.
|
||||
|
||||
#### & { *predicate* }
|
||||
|
||||
The predicate is a piece of JavaScript code that is executed as if it was inside
|
||||
a function. It gets the match results of labeled expressions in preceding
|
||||
expression as its arguments. It should return some JavaScript value using the
|
||||
`return` statement. If the returned value evaluates to `true` in boolean
|
||||
context, just return `undefined` and do not consume any input; otherwise
|
||||
consider the match failed.
|
||||
|
||||
The code inside the predicate can access all variables and functions defined in
|
||||
the initializer at the beginning of the grammar.
|
||||
|
||||
The code inside the predicate can also access location information using the
|
||||
`location` function. It returns an object like this:
|
||||
|
||||
```javascript
|
||||
{
|
||||
start: { offset: 23, line: 5, column: 6 },
|
||||
end: { offset: 23, line: 5, column: 6 }
|
||||
}
|
||||
```
|
||||
|
||||
The `start` and `end` properties both refer to the current parse position. The
|
||||
`offset` property contains an offset as a zero-based index and `line` and
|
||||
`column` properties contain a line and a column as one-based indices.
|
||||
|
||||
The code inside the predicate can also access options passed to the parser using
|
||||
the `options` variable.
|
||||
|
||||
Note that curly braces in the predicate code must be balanced.
|
||||
|
||||
#### ! { *predicate* }
|
||||
|
||||
The predicate is a piece of JavaScript code that is executed as if it was inside
|
||||
a function. It gets the match results of labeled expressions in preceding
|
||||
expression as its arguments. It should return some JavaScript value using the
|
||||
`return` statement. If the returned value evaluates to `false` in boolean
|
||||
context, just return `undefined` and do not consume any input; otherwise
|
||||
consider the match failed.
|
||||
|
||||
The code inside the predicate can access all variables and functions defined in
|
||||
the initializer at the beginning of the grammar.
|
||||
|
||||
The code inside the predicate can also access location information using the
|
||||
`location` function. It returns an object like this:
|
||||
|
||||
```javascript
|
||||
{
|
||||
start: { offset: 23, line: 5, column: 6 },
|
||||
end: { offset: 23, line: 5, column: 6 }
|
||||
}
|
||||
```
|
||||
|
||||
The `start` and `end` properties both refer to the current parse position. The
|
||||
`offset` property contains an offset as a zero-based index and `line` and
|
||||
`column` properties contain a line and a column as one-based indices.
|
||||
|
||||
The code inside the predicate can also access options passed to the parser using
|
||||
the `options` variable.
|
||||
|
||||
Note that curly braces in the predicate code must be balanced.
|
||||
|
||||
#### $ *expression*
|
||||
|
||||
Try to match the expression. If the match succeeds, return the matched text
|
||||
instead of the match result.
|
||||
|
||||
#### *label* : *expression*
|
||||
|
||||
Match the expression and remember its match result under given label. The label
|
||||
must be a JavaScript identifier.
|
||||
|
||||
Labeled expressions are useful together with actions, where saved match results
|
||||
can be accessed by action's JavaScript code.
|
||||
|
||||
#### *expression<sub>1</sub>* *expression<sub>2</sub>* ... *expression<sub>n</sub>*
|
||||
|
||||
Match a sequence of expressions and return their match results in an array.
|
||||
|
||||
#### *expression* { *action* }
|
||||
|
||||
Match the expression. If the match is successful, run the action, otherwise
|
||||
consider the match failed.
|
||||
|
||||
The action is a piece of JavaScript code that is executed as if it was inside a
|
||||
function. It gets the match results of labeled expressions in preceding
|
||||
expression as its arguments. The action should return some JavaScript value
|
||||
using the `return` statement. This value is considered match result of the
|
||||
preceding expression.
|
||||
|
||||
To indicate an error, the code inside the action can invoke the `expected`
|
||||
function, which makes the parser throw an exception. The function takes two
|
||||
parameters — a description of what was expected at the current position and
|
||||
optional location information (the default is what `location` would return — see
|
||||
below). The description will be used as part of a message of the thrown
|
||||
exception.
|
||||
|
||||
The code inside an action can also invoke the `error` function, which also makes
|
||||
the parser throw an exception. The function takes two parameters — an error
|
||||
message and optional location information (the default is what `location` would
|
||||
return — see below). The message will be used by the thrown exception.
|
||||
|
||||
The code inside the action can access all variables and functions defined in the
|
||||
initializer at the beginning of the grammar. Curly braces in the action code
|
||||
must be balanced.
|
||||
|
||||
The code inside the action can also access the text matched by the expression
|
||||
using the `text` function.
|
||||
|
||||
|
||||
The code inside the action can also access location information using the
|
||||
`location` function. It returns an object like this:
|
||||
|
||||
```javascript
|
||||
{
|
||||
start: { offset: 23, line: 5, column: 6 },
|
||||
end: { offset: 25, line: 5, column: 8 }
|
||||
}
|
||||
```
|
||||
|
||||
The `start` property refers to the position at the beginning of the expression,
|
||||
the `end` property refers to position after the end of the expression. The
|
||||
`offset` property contains an offset as a zero-based index and `line` and
|
||||
`column` properties contain a line and a column as one-based indices.
|
||||
|
||||
The code inside the action can also access options passed to the parser using
|
||||
the `options` variable.
|
||||
|
||||
Note that curly braces in the action code must be balanced.
|
||||
|
||||
#### *expression<sub>1</sub>* / *expression<sub>2</sub>* / ... / *expression<sub>n</sub>*
|
||||
|
||||
Try to match the first expression, if it does not succeed, try the second one,
|
||||
etc. Return the match result of the first successfully matched expression. If no
|
||||
expression matches, consider the match failed.
|
||||
|
||||
Compatibility
|
||||
-------------
|
||||
|
||||
Both the parser generator and generated parsers should run well in the following
|
||||
environments:
|
||||
|
||||
* Node.js 4+
|
||||
* Internet Explorer 9+
|
||||
* Edge
|
||||
* Firefox
|
||||
* Chrome
|
||||
* Safari
|
||||
* Opera
|
||||
|
||||
Development
|
||||
-----------
|
||||
|
||||
* [Project website](https://pegjs.org/)
|
||||
* [Wiki](https://github.com/pegjs/pegjs/wiki)
|
||||
* [Source code](https://github.com/pegjs/pegjs)
|
||||
* [Issue tracker](https://github.com/pegjs/pegjs/issues)
|
||||
* [Google Group](http://groups.google.com/group/pegjs)
|
||||
* [Twitter](http://twitter.com/peg_js)
|
||||
|
||||
PEG.js is developed by [David Majda](https://majda.cz/)
|
||||
([@dmajda](http://twitter.com/dmajda)). The [Bower
|
||||
package](https://github.com/pegjs/bower) is maintained by [Michel
|
||||
Krämer](http://www.michel-kraemer.com/)
|
||||
([@michelkraemer](https://twitter.com/michelkraemer)).
|
||||
|
||||
You are welcome to contribute code. Unless your contribution is really trivial
|
||||
you should get in touch with me first — this can prevent wasted effort on both
|
||||
sides. You can send code both as a patch or a GitHub pull request.
|
||||
|
||||
Note that PEG.js is still very much work in progress. There are no compatibility
|
||||
guarantees until version 1.0.
|
||||
![GitHub Actions](https://github.com/pegjs/pegjs/workflows/Github%20Actions/badge.svg)
|
||||
[![Codecov](https://codecov.io/gh/pegjs/pegjs/branch/master/graph/badge.svg)](https://codecov.io/gh/pegjs/pegjs)
|
||||
[![CodeFactor](https://www.codefactor.io/repository/github/pegjs/pegjs/badge)](https://www.codefactor.io/repository/github/pegjs/pegjs)
|
||||
[![license](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)
|
||||
|
||||
PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily.
|
||||
|
||||
> PEG.js is still very much work in progress. There are no compatibility guarantees until version 1.0
|
||||
|
||||
### public packages
|
||||
|
||||
| package | version | dependency status |
|
||||
| ------- | ------- | ----------------- |
|
||||
| [pegjs][P001] | [![release][P002]][P003] [![dev][P004]][P005] | [![dependencies][P006]][P007] |
|
||||
|
||||
### local packages
|
||||
|
||||
| package | dependency status |
|
||||
| ------- | ----------------- |
|
||||
| [~/][L013] | [![dependencies][L014]][L015] |
|
||||
| [~/test][L001] | [![dependencies][L002]][L003] |
|
||||
| [~/tools/benchmark][L004] | [![dependencies][L005]][L006] |
|
||||
| [~/tools/bundler][L007] | [![dependencies][L008]][L009] |
|
||||
| [~/tools/impact][L010] | [![dependencies][L011]][L012] |
|
||||
| [~/tools/publish-dev][L016] | [![dependencies][L017]][L018] |
|
||||
|
||||
<!-- packages/pegjs -->
|
||||
[P001]: https://github.com/pegjs/pegjs/tree/master/packages/pegjs
|
||||
[P002]: https://img.shields.io/npm/v/pegjs.svg
|
||||
[P003]: https://www.npmjs.com/package/pegjs
|
||||
[P004]: https://img.shields.io/npm/v/pegjs/dev.svg
|
||||
[P005]: https://github.com/pegjs/pegjs
|
||||
[P006]: https://img.shields.io/david/pegjs/pegjs.svg?path=packages/pegjs
|
||||
[P007]: https://david-dm.org/pegjs/pegjs?path=packages/pegjs
|
||||
|
||||
<!-- https://github.com/pegjs/pegjs/ -->
|
||||
[L013]: https://github.com/pegjs/pegjs/tree/master/
|
||||
[L014]: https://img.shields.io/david/pegjs/pegjs.svg
|
||||
[L015]: https://david-dm.org/pegjs/pegjs
|
||||
|
||||
<!-- test -->
|
||||
[L001]: https://github.com/pegjs/pegjs/tree/master/test
|
||||
[L002]: https://img.shields.io/david/pegjs/pegjs.svg?path=test
|
||||
[L003]: https://david-dm.org/pegjs/pegjs?path=test
|
||||
|
||||
<!-- tools/benchmark -->
|
||||
[L004]: https://github.com/pegjs/pegjs/tree/master/tools/benchmark
|
||||
[L005]: https://img.shields.io/david/pegjs/pegjs.svg?path=tools/benchmark
|
||||
[L006]: https://david-dm.org/pegjs/pegjs?path=tools/benchmark
|
||||
|
||||
<!-- tools/bundler -->
|
||||
[L007]: https://github.com/pegjs/pegjs/tree/master/tools/bundler
|
||||
[L008]: https://img.shields.io/david/pegjs/pegjs.svg?path=tools/bundler
|
||||
[L009]: https://david-dm.org/pegjs/pegjs?path=tools/bundler
|
||||
|
||||
<!-- tools/impact -->
|
||||
[L010]: https://github.com/pegjs/pegjs/tree/master/tools/impact
|
||||
[L011]: https://img.shields.io/david/pegjs/pegjs.svg?path=tools/impact
|
||||
[L012]: https://david-dm.org/pegjs/pegjs?path=tools/impact
|
||||
|
||||
<!-- tools/publish-dev -->
|
||||
[L016]: https://github.com/pegjs/pegjs/tree/master/tools/publish-dev
|
||||
[L017]: https://img.shields.io/david/pegjs/pegjs.svg?path=tools/publish-dev
|
||||
[L018]: https://david-dm.org/pegjs/pegjs?path=tools/publish-dev
|
||||
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"commonjs": true
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
PEG.js Benchmark Suite
|
||||
======================
|
||||
|
||||
This is the PEG.js benchmark suite. It measures speed of the parsers generated
|
||||
by PEG.js on various inputs. Its main goal is to provide data for code generator
|
||||
optimizations.
|
||||
|
||||
Running in Node.js
|
||||
------------------
|
||||
|
||||
All commands in the following steps need to be executed in PEG.js root directory
|
||||
(one level up from this one).
|
||||
|
||||
1. Install all PEG.js dependencies, including development ones:
|
||||
|
||||
```console
|
||||
$ npm install
|
||||
```
|
||||
|
||||
2. Execute the benchmark suite:
|
||||
|
||||
```console
|
||||
$ gulp benchmark
|
||||
```
|
||||
|
||||
3. Wait for results.
|
||||
|
||||
Running in the Browser
|
||||
----------------------
|
||||
|
||||
All commands in the following steps need to be executed in PEG.js root directory
|
||||
(one level up from this one).
|
||||
|
||||
1. Make sure you have Node.js installed.
|
||||
|
||||
2. Install all PEG.js dependencies, including development ones:
|
||||
|
||||
```console
|
||||
$ npm install
|
||||
```
|
||||
|
||||
3. Serve the benchmark suite using a web server:
|
||||
|
||||
```console
|
||||
$ benchmark/server
|
||||
```
|
||||
|
||||
4. Point your browser to the [benchmark suite](http://localhost:8000/).
|
||||
|
||||
5. Click the **Run** button and wait for results.
|
@ -1,42 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let benchmarks = [
|
||||
{
|
||||
id: "json",
|
||||
title: "JSON",
|
||||
tests: [
|
||||
{ file: "example1.json", title: "Example 1" },
|
||||
{ file: "example2.json", title: "Example 2" },
|
||||
{ file: "example3.json", title: "Example 3" },
|
||||
{ file: "example4.json", title: "Example 4" },
|
||||
{ file: "example5.json", title: "Example 5" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "css",
|
||||
title: "CSS",
|
||||
tests: [
|
||||
{ file: "blueprint/src/reset.css", title: "Blueprint - reset.css (source)" },
|
||||
{ file: "blueprint/src/typography.css", title: "Blueprint - typography.css (source)" },
|
||||
{ file: "blueprint/src/forms.css", title: "Blueprint - forms.css (source)" },
|
||||
{ file: "blueprint/src/grid.css", title: "Blueprint - grid.css (source)" },
|
||||
{ file: "blueprint/src/print.css", title: "Blueprint - print.css (source)" },
|
||||
// Contains syntax errors.
|
||||
// { file: "blueprint/src/ie.css", title: "Blueprint - ie.css (source)" },
|
||||
{ file: "blueprint/min/screen.css", title: "Blueprint - screen.css (minified)" },
|
||||
{ file: "blueprint/min/print.css", title: "Blueprint - print.css (minified)" },
|
||||
// Contains syntax errors.
|
||||
// { file: "blueprint/min/ie.css", title: "Blueprint - ie.css (minified)" },
|
||||
{ file: "960.gs/src/reset.css", title: "960.gs - reset.css (source)" },
|
||||
{ file: "960.gs/src/text.css", title: "960.gs - text.css (source)" },
|
||||
{ file: "960.gs/src/960.css", title: "960.gs - 960.css (source)" },
|
||||
{ file: "960.gs/src/960_24_col.css", title: "960.gs - 960_24_col.css (source)" },
|
||||
{ file: "960.gs/min/reset.css", title: "960.gs - reset.css (minified)" },
|
||||
{ file: "960.gs/min/text.css", title: "960.gs - text.css (minified)" },
|
||||
{ file: "960.gs/min/960.css", title: "960.gs - 960.css (minified)" },
|
||||
{ file: "960.gs/min/960_24_col.css", title: "960.gs - 960_24_col.css (minified)" }
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = benchmarks;
|
@ -1,35 +0,0 @@
|
||||
/* -----------------------------------------------------------------------
|
||||
|
||||
|
||||
Blueprint CSS Framework 0.9
|
||||
http://blueprintcss.org
|
||||
|
||||
* Copyright (c) 2007-Present. See LICENSE for more info.
|
||||
* See README for instructions on how to use Blueprint.
|
||||
* For credits and origins, see AUTHORS.
|
||||
* This is a compressed file. See the sources in the 'src' directory.
|
||||
|
||||
----------------------------------------------------------------------- */
|
||||
|
||||
/* ie.css */
|
||||
body {text-align:center;}
|
||||
.container {text-align:left;}
|
||||
* html .column, * html .span-1, * html .span-2, * html .span-3, * html .span-4, * html .span-5, * html .span-6, * html .span-7, * html .span-8, * html .span-9, * html .span-10, * html .span-11, * html .span-12, * html .span-13, * html .span-14, * html .span-15, * html .span-16, * html .span-17, * html .span-18, * html .span-19, * html .span-20, * html .span-21, * html .span-22, * html .span-23, * html .span-24 {display:inline;overflow-x:hidden;}
|
||||
* html legend {margin:0px -8px 16px 0;padding:0;}
|
||||
sup {vertical-align:text-top;}
|
||||
sub {vertical-align:text-bottom;}
|
||||
html>body p code {*white-space:normal;}
|
||||
hr {margin:-8px auto 11px;}
|
||||
img {-ms-interpolation-mode:bicubic;}
|
||||
.clearfix, .container {display:inline-block;}
|
||||
* html .clearfix, * html .container {height:1%;}
|
||||
fieldset {padding-top:0;}
|
||||
textarea {overflow:auto;}
|
||||
input.text, input.title, textarea {background-color:#fff;border:1px solid #bbb;}
|
||||
input.text:focus, input.title:focus {border-color:#666;}
|
||||
input.text, input.title, textarea, select {margin:0.5em 0;}
|
||||
input.checkbox, input.radio {position:relative;top:.25em;}
|
||||
form.inline div, form.inline p {vertical-align:middle;}
|
||||
form.inline label {position:relative;top:-0.25em;}
|
||||
form.inline input.checkbox, form.inline input.radio, form.inline input.button, form.inline button {margin:0.5em 0;}
|
||||
button, input.button {position:relative;top:0.25em;}
|
@ -1,76 +0,0 @@
|
||||
/* --------------------------------------------------------------
|
||||
|
||||
ie.css
|
||||
|
||||
Contains every hack for Internet Explorer,
|
||||
so that our core files stay sweet and nimble.
|
||||
|
||||
-------------------------------------------------------------- */
|
||||
|
||||
/* Make sure the layout is centered in IE5 */
|
||||
body { text-align: center; }
|
||||
.container { text-align: left; }
|
||||
|
||||
/* Fixes IE margin bugs */
|
||||
* html .column, * html .span-1, * html .span-2,
|
||||
* html .span-3, * html .span-4, * html .span-5,
|
||||
* html .span-6, * html .span-7, * html .span-8,
|
||||
* html .span-9, * html .span-10, * html .span-11,
|
||||
* html .span-12, * html .span-13, * html .span-14,
|
||||
* html .span-15, * html .span-16, * html .span-17,
|
||||
* html .span-18, * html .span-19, * html .span-20,
|
||||
* html .span-21, * html .span-22, * html .span-23,
|
||||
* html .span-24 { display:inline; overflow-x: hidden; }
|
||||
|
||||
|
||||
/* Elements
|
||||
-------------------------------------------------------------- */
|
||||
|
||||
/* Fixes incorrect styling of legend in IE6. */
|
||||
* html legend { margin:0px -8px 16px 0; padding:0; }
|
||||
|
||||
/* Fixes wrong line-height on sup/sub in IE. */
|
||||
sup { vertical-align:text-top; }
|
||||
sub { vertical-align:text-bottom; }
|
||||
|
||||
/* Fixes IE7 missing wrapping of code elements. */
|
||||
html>body p code { *white-space: normal; }
|
||||
|
||||
/* IE 6&7 has problems with setting proper <hr> margins. */
|
||||
hr { margin:-8px auto 11px; }
|
||||
|
||||
/* Explicitly set interpolation, allowing dynamically resized images to not look horrible */
|
||||
img { -ms-interpolation-mode:bicubic; }
|
||||
|
||||
/* Clearing
|
||||
-------------------------------------------------------------- */
|
||||
|
||||
/* Makes clearfix actually work in IE */
|
||||
.clearfix, .container { display:inline-block; }
|
||||
* html .clearfix,
|
||||
* html .container { height:1%; }
|
||||
|
||||
|
||||
/* Forms
|
||||
-------------------------------------------------------------- */
|
||||
|
||||
/* Fixes padding on fieldset */
|
||||
fieldset { padding-top:0; }
|
||||
|
||||
/* Makes classic textareas in IE 6 resemble other browsers */
|
||||
textarea { overflow:auto; }
|
||||
|
||||
/* Fixes rule that IE 6 ignores */
|
||||
input.text, input.title, textarea { background-color:#fff; border:1px solid #bbb; }
|
||||
input.text:focus, input.title:focus { border-color:#666; }
|
||||
input.text, input.title, textarea, select { margin:0.5em 0; }
|
||||
input.checkbox, input.radio { position:relative; top:.25em; }
|
||||
|
||||
/* Fixes alignment of inline form elements */
|
||||
form.inline div, form.inline p { vertical-align:middle; }
|
||||
form.inline label { position:relative;top:-0.25em; }
|
||||
form.inline input.checkbox, form.inline input.radio,
|
||||
form.inline input.button, form.inline button {
|
||||
margin:0.5em 0;
|
||||
}
|
||||
button, input.button { position:relative;top:0.25em; }
|
@ -1,40 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>PEG.js Benchmark Suite</title>
|
||||
<link rel="stylesheet" href="index.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>PEG.js Benchmark Suite</h1>
|
||||
|
||||
<div id="options">
|
||||
<label for="run-count">Run each test</label>
|
||||
<input type="text" id="run-count" value="10"> times
|
||||
<input type="checkbox" id="cache">
|
||||
<label for="cache">Use results cache</label>
|
||||
<label for="optimize">Optimize:</label>
|
||||
<select id="optimize">
|
||||
<option value="speed">Speed</option>
|
||||
<option value="size">Size</option>
|
||||
</select>
|
||||
<input type="button" id="run" value="Run">
|
||||
</div>
|
||||
|
||||
<table id="results-table">
|
||||
<tr class="columns">
|
||||
<th>Test</th>
|
||||
<th>Input Size</th>
|
||||
<th>Average Parse Time</th>
|
||||
<th>Average Parse Speed</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="no-results" colspan="4">No results available yet.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<script src="vendor/jquery/jquery.js"></script>
|
||||
<script src="vendor/jquery.scrollto/jquery.scrollTo.js"></script>
|
||||
<script src="bundle.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,138 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/* eslint-env browser, jquery */
|
||||
|
||||
let Runner = require("./runner.js");
|
||||
let benchmarks = require("./benchmarks.js");
|
||||
|
||||
$("#run").click(() => {
|
||||
// Results Table Manipulation
|
||||
|
||||
let resultsTable = $("#results-table");
|
||||
|
||||
function appendHeading(heading) {
|
||||
resultsTable.append(
|
||||
"<tr class='heading'><th colspan='4'>" + heading + "</th></tr>"
|
||||
);
|
||||
}
|
||||
|
||||
function appendResult(klass, title, url, inputSize, parseTime) {
|
||||
const KB = 1024;
|
||||
const MS_IN_S = 1000;
|
||||
|
||||
resultsTable.append(
|
||||
"<tr class='" + klass + "'>"
|
||||
+ "<td class='title'>"
|
||||
+ (url !== null ? "<a href='" + url + "'>" : "")
|
||||
+ title
|
||||
+ (url !== null ? "</a>" : "")
|
||||
+ "</td>"
|
||||
+ "<td class='input-size'>"
|
||||
+ "<span class='value'>"
|
||||
+ (inputSize / KB).toFixed(2)
|
||||
+ "</span>"
|
||||
+ " <span class='unit'>kB</span>"
|
||||
+ "</td>"
|
||||
+ "<td class='parse-time'>"
|
||||
+ "<span class='value'>"
|
||||
+ parseTime.toFixed(2)
|
||||
+ "</span>"
|
||||
+ " <span class='unit'>ms</span>"
|
||||
+ "</td>"
|
||||
+ "<td class='parse-speed'>"
|
||||
+ "<span class='value'>"
|
||||
+ ((inputSize / KB) / (parseTime / MS_IN_S)).toFixed(2)
|
||||
+ "</span>"
|
||||
+ " <span class='unit'>kB/s</span>"
|
||||
+ "</td>"
|
||||
+ "</tr>"
|
||||
);
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
// Each input is parsed multiple times and the results are averaged. We
|
||||
// do this for two reasons:
|
||||
//
|
||||
// 1. To warm up the interpreter (PEG.js-generated parsers will be
|
||||
// most likely used repeatedly, so it makes sense to measure
|
||||
// performance after warming up).
|
||||
//
|
||||
// 2. To minimize random errors.
|
||||
|
||||
let runCount = parseInt($("#run-count").val(), 10);
|
||||
let options = {
|
||||
cache: $("#cache").is(":checked"),
|
||||
optimize: $("#optimize").val()
|
||||
};
|
||||
|
||||
if (isNaN(runCount) || runCount <= 0) {
|
||||
alert("Number of runs must be a positive integer.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Runner.run(benchmarks, runCount, options, {
|
||||
readFile(file) {
|
||||
return $.ajax({
|
||||
type: "GET",
|
||||
url: file,
|
||||
dataType: "text",
|
||||
async: false
|
||||
}).responseText;
|
||||
},
|
||||
|
||||
testStart() {
|
||||
// Nothing to do.
|
||||
},
|
||||
|
||||
testFinish(benchmark, test, inputSize, parseTime) {
|
||||
appendResult(
|
||||
"individual",
|
||||
test.title,
|
||||
benchmark.id + "/" + test.file,
|
||||
inputSize,
|
||||
parseTime
|
||||
);
|
||||
},
|
||||
|
||||
benchmarkStart(benchmark) {
|
||||
appendHeading(benchmark.title);
|
||||
},
|
||||
|
||||
benchmarkFinish(benchmark, inputSize, parseTime) {
|
||||
appendResult(
|
||||
"benchmark-total",
|
||||
benchmark.title + " total",
|
||||
null,
|
||||
inputSize,
|
||||
parseTime
|
||||
);
|
||||
},
|
||||
|
||||
start() {
|
||||
$("#run-count, #cache, #run").attr("disabled", "disabled");
|
||||
|
||||
resultsTable.show();
|
||||
$("#results-table tr").slice(1).remove();
|
||||
},
|
||||
|
||||
finish(inputSize, parseTime) {
|
||||
appendResult(
|
||||
"total",
|
||||
"Total",
|
||||
null,
|
||||
inputSize,
|
||||
parseTime
|
||||
);
|
||||
|
||||
$.scrollTo("max", { axis: "y", duration: 500 });
|
||||
|
||||
$("#run-count, #cache, #run").removeAttr("disabled");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$(document).ready(() => {
|
||||
$("#run").focus();
|
||||
});
|
@ -1,198 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
/* eslint-env node */
|
||||
|
||||
let Runner = require("./runner.js");
|
||||
let benchmarks = require("./benchmarks.js");
|
||||
let fs = require("fs");
|
||||
|
||||
// Results Table Manipulation
|
||||
|
||||
function dup(text, count) {
|
||||
let result = "";
|
||||
|
||||
for (let i = 1; i <= count; i++) {
|
||||
result += text;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function padLeft(text, length) {
|
||||
return dup(" ", length - text.length) + text;
|
||||
}
|
||||
|
||||
function padRight(text, length) {
|
||||
return text + dup(" ", length - text.length);
|
||||
}
|
||||
|
||||
function center(text, length) {
|
||||
let padLength = (length - text.length) / 2;
|
||||
|
||||
return dup(" ", Math.floor(padLength))
|
||||
+ text
|
||||
+ dup(" ", Math.ceil(padLength));
|
||||
}
|
||||
|
||||
function writeTableHeader() {
|
||||
console.log("┌─────────────────────────────────────┬───────────┬────────────┬──────────────┐");
|
||||
console.log("│ Test │ Inp. size │ Avg. time │ Avg. speed │");
|
||||
}
|
||||
|
||||
function writeHeading(heading) {
|
||||
console.log("├─────────────────────────────────────┴───────────┴────────────┴──────────────┤");
|
||||
console.log("│ " + center(heading, 75) + " │");
|
||||
console.log("├─────────────────────────────────────┬───────────┬────────────┬──────────────┤");
|
||||
}
|
||||
|
||||
function writeResult(title, inputSize, parseTime) {
|
||||
const KB = 1024;
|
||||
const MS_IN_S = 1000;
|
||||
|
||||
console.log("│ "
|
||||
+ padRight(title, 35)
|
||||
+ " │ "
|
||||
+ padLeft((inputSize / KB).toFixed(2), 6)
|
||||
+ " kB │ "
|
||||
+ padLeft(parseTime.toFixed(2), 7)
|
||||
+ " ms │ "
|
||||
+ padLeft(((inputSize / KB) / (parseTime / MS_IN_S)).toFixed(2), 7)
|
||||
+ " kB/s │"
|
||||
);
|
||||
}
|
||||
|
||||
function writeSeparator() {
|
||||
console.log("├─────────────────────────────────────┼───────────┼────────────┼──────────────┤");
|
||||
}
|
||||
|
||||
function writeTableFooter() {
|
||||
console.log("└─────────────────────────────────────┴───────────┴────────────┴──────────────┘");
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
function printHelp() {
|
||||
console.log("Usage: run [options]");
|
||||
console.log("");
|
||||
console.log("Runs PEG.js benchmark suite.");
|
||||
console.log("");
|
||||
console.log("Options:");
|
||||
console.log(" -n, --run-count <n> number of runs (default: 10)");
|
||||
console.log(" --cache make tested parsers cache results");
|
||||
console.log(" -o, --optimize <goal> select optimization for speed or size (default:");
|
||||
console.log(" speed)");
|
||||
}
|
||||
|
||||
function exitSuccess() {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function exitFailure() {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function abort(message) {
|
||||
console.error(message);
|
||||
exitFailure();
|
||||
}
|
||||
|
||||
// Arguments
|
||||
|
||||
let args = process.argv.slice(2); // Trim "node" and the script path.
|
||||
|
||||
function isOption(arg) {
|
||||
return (/^-/).test(arg);
|
||||
}
|
||||
|
||||
function nextArg() {
|
||||
args.shift();
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
let runCount = 10;
|
||||
let options = {
|
||||
cache: false,
|
||||
optimize: "speed"
|
||||
};
|
||||
|
||||
while (args.length > 0 && isOption(args[0])) {
|
||||
switch (args[0]) {
|
||||
case "-n":
|
||||
case "--run-count":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -n/--run-count option.");
|
||||
}
|
||||
runCount = parseInt(args[0], 10);
|
||||
if (isNaN(runCount) || runCount <= 0) {
|
||||
abort("Number of runs must be a positive integer.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "--cache":
|
||||
options.cache = true;
|
||||
break;
|
||||
|
||||
case "-o":
|
||||
case "--optimize":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -o/--optimize option.");
|
||||
}
|
||||
if (args[0] !== "speed" && args[0] !== "size") {
|
||||
abort("Optimization goal must be either \"speed\" or \"size\".");
|
||||
}
|
||||
options.optimize = args[0];
|
||||
break;
|
||||
|
||||
case "-h":
|
||||
case "--help":
|
||||
printHelp();
|
||||
exitSuccess();
|
||||
break;
|
||||
|
||||
default:
|
||||
abort("Unknown option: " + args[0] + ".");
|
||||
}
|
||||
nextArg();
|
||||
}
|
||||
|
||||
if (args.length > 0) {
|
||||
abort("No arguments are allowed.");
|
||||
}
|
||||
|
||||
Runner.run(benchmarks, runCount, options, {
|
||||
readFile(file) {
|
||||
return fs.readFileSync(__dirname + "/" + file, "utf8");
|
||||
},
|
||||
|
||||
testStart() {
|
||||
// Nothing to do.
|
||||
},
|
||||
|
||||
testFinish(benchmark, test, inputSize, parseTime) {
|
||||
writeResult(test.title, inputSize, parseTime);
|
||||
},
|
||||
|
||||
benchmarkStart(benchmark) {
|
||||
writeHeading(benchmark.title);
|
||||
},
|
||||
|
||||
benchmarkFinish(benchmark, inputSize, parseTime) {
|
||||
writeSeparator();
|
||||
writeResult(benchmark.title + " total", inputSize, parseTime);
|
||||
},
|
||||
|
||||
start() {
|
||||
writeTableHeader();
|
||||
},
|
||||
|
||||
finish(inputSize, parseTime) {
|
||||
writeSeparator();
|
||||
writeResult("Total", inputSize, parseTime);
|
||||
writeTableFooter();
|
||||
}
|
||||
});
|
@ -1,118 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/* global setTimeout */
|
||||
|
||||
let peg = require("../lib/peg");
|
||||
|
||||
let Runner = {
|
||||
run(benchmarks, runCount, options, callbacks) {
|
||||
// Queue
|
||||
|
||||
let Q = {
|
||||
functions: [],
|
||||
|
||||
add(f) {
|
||||
this.functions.push(f);
|
||||
},
|
||||
|
||||
run() {
|
||||
if (this.functions.length > 0) {
|
||||
this.functions.shift()();
|
||||
|
||||
// We can't use |arguments.callee| here because |this| would get
|
||||
// messed-up in that case.
|
||||
setTimeout(() => { Q.run(); }, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// The benchmark itself is factored out into several functions (some of them
|
||||
// generated), which are enqueued and run one by one using |setTimeout|. We
|
||||
// do this for two reasons:
|
||||
//
|
||||
// 1. To avoid bowser mechanism for interrupting long-running scripts to
|
||||
// kick-in (or at least to not kick-in that often).
|
||||
//
|
||||
// 2. To ensure progressive rendering of results in the browser (some
|
||||
// browsers do not render at all when running JavaScript code).
|
||||
//
|
||||
// The enqueued functions share state, which is all stored in the properties
|
||||
// of the |state| object.
|
||||
|
||||
let state = {};
|
||||
|
||||
function initialize() {
|
||||
callbacks.start();
|
||||
|
||||
state.totalInputSize = 0;
|
||||
state.totalParseTime = 0;
|
||||
}
|
||||
|
||||
function benchmarkInitializer(benchmark) {
|
||||
return function() {
|
||||
callbacks.benchmarkStart(benchmark);
|
||||
|
||||
state.parser = peg.generate(
|
||||
callbacks.readFile("../examples/" + benchmark.id + ".pegjs"),
|
||||
options
|
||||
);
|
||||
state.benchmarkInputSize = 0;
|
||||
state.benchmarkParseTime = 0;
|
||||
};
|
||||
}
|
||||
|
||||
function testRunner(benchmark, test) {
|
||||
return function() {
|
||||
callbacks.testStart(benchmark, test);
|
||||
|
||||
let input = callbacks.readFile(benchmark.id + "/" + test.file);
|
||||
|
||||
let parseTime = 0;
|
||||
for (let i = 0; i < runCount; i++) {
|
||||
let t = (new Date()).getTime();
|
||||
state.parser.parse(input);
|
||||
parseTime += (new Date()).getTime() - t;
|
||||
}
|
||||
let averageParseTime = parseTime / runCount;
|
||||
|
||||
callbacks.testFinish(benchmark, test, input.length, averageParseTime);
|
||||
|
||||
state.benchmarkInputSize += input.length;
|
||||
state.benchmarkParseTime += averageParseTime;
|
||||
};
|
||||
}
|
||||
|
||||
function benchmarkFinalizer(benchmark) {
|
||||
return function() {
|
||||
callbacks.benchmarkFinish(
|
||||
benchmark,
|
||||
state.benchmarkInputSize,
|
||||
state.benchmarkParseTime
|
||||
);
|
||||
|
||||
state.totalInputSize += state.benchmarkInputSize;
|
||||
state.totalParseTime += state.benchmarkParseTime;
|
||||
};
|
||||
}
|
||||
|
||||
function finalize() {
|
||||
callbacks.finish(state.totalInputSize, state.totalParseTime);
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
Q.add(initialize);
|
||||
benchmarks.forEach(benchmark => {
|
||||
Q.add(benchmarkInitializer(benchmark));
|
||||
benchmark.tests.forEach(test => {
|
||||
Q.add(testRunner(benchmark, test));
|
||||
});
|
||||
Q.add(benchmarkFinalizer(benchmark));
|
||||
});
|
||||
Q.add(finalize);
|
||||
|
||||
Q.run();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Runner;
|
@ -1,36 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
/* eslint-env node */
|
||||
|
||||
// Small server whose main purpose is to ensure that both the benchmarked code
|
||||
// and the benchmark get passed through Babel & Browserify before they are
|
||||
// served to the browser.
|
||||
|
||||
let babelify = require("babelify");
|
||||
let browserify = require("browserify");
|
||||
let express = require("express");
|
||||
let glob = require("glob");
|
||||
let logger = require("morgan");
|
||||
|
||||
let app = express();
|
||||
|
||||
app.use(logger("dev"));
|
||||
app.use(express.static(__dirname));
|
||||
app.use("/examples", express.static(`${__dirname}/../examples`));
|
||||
|
||||
app.get("/bundle.js", (req, res) => {
|
||||
let files = glob.sync(`${__dirname}/**/*.js`, {
|
||||
ignore: `${__dirname}/vendor/**/*`
|
||||
});
|
||||
|
||||
browserify(files)
|
||||
.transform(babelify, { presets: "es2015", compact: false })
|
||||
.bundle()
|
||||
.pipe(res);
|
||||
});
|
||||
|
||||
app.listen(8000, () => {
|
||||
console.log("Benchmark server running at http://localhost:8000...");
|
||||
});
|
@ -1,22 +0,0 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2007-2015 Ariel Flesler <aflesler@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2007-2015 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
|
||||
* Licensed under MIT
|
||||
* @author Ariel Flesler
|
||||
* @version 2.1.2
|
||||
*/
|
||||
;(function(f){"use strict";"function"===typeof define&&define.amd?define(["jquery"],f):"undefined"!==typeof module&&module.exports?module.exports=f(require("jquery")):f(jQuery)})(function($){"use strict";function n(a){return!a.nodeName||-1!==$.inArray(a.nodeName.toLowerCase(),["iframe","#document","html","body"])}function h(a){return $.isFunction(a)||$.isPlainObject(a)?a:{top:a,left:a}}var p=$.scrollTo=function(a,d,b){return $(window).scrollTo(a,d,b)};p.defaults={axis:"xy",duration:0,limit:!0};$.fn.scrollTo=function(a,d,b){"object"=== typeof d&&(b=d,d=0);"function"===typeof b&&(b={onAfter:b});"max"===a&&(a=9E9);b=$.extend({},p.defaults,b);d=d||b.duration;var u=b.queue&&1<b.axis.length;u&&(d/=2);b.offset=h(b.offset);b.over=h(b.over);return this.each(function(){function k(a){var k=$.extend({},b,{queue:!0,duration:d,complete:a&&function(){a.call(q,e,b)}});r.animate(f,k)}if(null!==a){var l=n(this),q=l?this.contentWindow||window:this,r=$(q),e=a,f={},t;switch(typeof e){case "number":case "string":if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(e)){e= h(e);break}e=l?$(e):$(e,q);case "object":if(e.length===0)return;if(e.is||e.style)t=(e=$(e)).offset()}var v=$.isFunction(b.offset)&&b.offset(q,e)||b.offset;$.each(b.axis.split(""),function(a,c){var d="x"===c?"Left":"Top",m=d.toLowerCase(),g="scroll"+d,h=r[g](),n=p.max(q,c);t?(f[g]=t[m]+(l?0:h-r.offset()[m]),b.margin&&(f[g]-=parseInt(e.css("margin"+d),10)||0,f[g]-=parseInt(e.css("border"+d+"Width"),10)||0),f[g]+=v[m]||0,b.over[m]&&(f[g]+=e["x"===c?"width":"height"]()*b.over[m])):(d=e[m],f[g]=d.slice&& "%"===d.slice(-1)?parseFloat(d)/100*n:d);b.limit&&/^\d+$/.test(f[g])&&(f[g]=0>=f[g]?0:Math.min(f[g],n));!a&&1<b.axis.length&&(h===f[g]?f={}:u&&(k(b.onAfterFirst),f={}))});k(b.onAfter)}})};p.max=function(a,d){var b="x"===d?"Width":"Height",h="scroll"+b;if(!n(a))return a[h]-$(a)[b.toLowerCase()]();var b="client"+b,k=a.ownerDocument||a.document,l=k.documentElement,k=k.body;return Math.max(l[h],k[h])-Math.min(l[b],k[b])};$.Tween.propHooks.scrollLeft=$.Tween.propHooks.scrollTop={get:function(a){return $(a.elem)[a.prop]()}, set:function(a){var d=this.get(a);if(a.options.interrupt&&a._last&&a._last!==d)return $(a.elem).stop();var b=Math.round(a.now);d!==b&&($(a.elem)[a.prop](b),a._last=this.get(a))}};return p});
|
@ -1,36 +0,0 @@
|
||||
Copyright jQuery Foundation and other contributors, https://jquery.org/
|
||||
|
||||
This software consists of voluntary contributions made by many
|
||||
individuals. For exact contribution history, see the revision history
|
||||
available at https://github.com/jquery/jquery
|
||||
|
||||
The following license applies to all parts of this software except as
|
||||
documented below:
|
||||
|
||||
====
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
====
|
||||
|
||||
All files located in the node_modules and external directories are
|
||||
externally maintained libraries used by this software which have their
|
||||
own licenses; we recommend you read them, as their terms may differ from
|
||||
the terms above.
|
File diff suppressed because one or more lines are too long
@ -1,8 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"rules": {
|
||||
"no-console": 0
|
||||
}
|
||||
}
|
@ -1,326 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
let fs = require("fs");
|
||||
let path = require("path");
|
||||
let peg = require("../lib/peg");
|
||||
|
||||
// Helpers
|
||||
|
||||
function printVersion() {
|
||||
console.log("PEG.js " + peg.VERSION);
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log("Usage: pegjs [options] [--] [<input_file>]");
|
||||
console.log("");
|
||||
console.log("Options:");
|
||||
console.log(" --allowed-start-rules <rules> comma-separated list of rules the generated");
|
||||
console.log(" parser will be allowed to start parsing");
|
||||
console.log(" from (default: the first rule in the");
|
||||
console.log(" grammar)");
|
||||
console.log(" --cache make generated parser cache results");
|
||||
console.log(" -d, --dependency <dependency> use specified dependency (can be specified");
|
||||
console.log(" multiple times)");
|
||||
console.log(" -e, --export-var <variable> name of a global variable into which the");
|
||||
console.log(" parser object is assigned to when no module");
|
||||
console.log(" loader is detected");
|
||||
console.log(" --extra-options <options> additional options (in JSON format) to pass");
|
||||
console.log(" to peg.generate");
|
||||
console.log(" --extra-options-file <file> file with additional options (in JSON");
|
||||
console.log(" format) to pass to peg.generate");
|
||||
console.log(" --format <format> format of the generated parser: amd,");
|
||||
console.log(" commonjs, globals, umd (default: commonjs)");
|
||||
console.log(" -h, --help print help and exit");
|
||||
console.log(" -O, --optimize <goal> select optimization for speed or size");
|
||||
console.log(" (default: speed)");
|
||||
console.log(" -o, --output <file> output file");
|
||||
console.log(" --plugin <plugin> use a specified plugin (can be specified");
|
||||
console.log(" multiple times)");
|
||||
console.log(" --trace enable tracing in generated parser");
|
||||
console.log(" -v, --version print version information and exit");
|
||||
}
|
||||
|
||||
function exitSuccess() {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function exitFailure() {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function abort(message) {
|
||||
console.error(message);
|
||||
exitFailure();
|
||||
}
|
||||
|
||||
function addExtraOptions(options, json) {
|
||||
let extraOptions;
|
||||
|
||||
try {
|
||||
extraOptions = JSON.parse(json);
|
||||
} catch (e) {
|
||||
if (!(e instanceof SyntaxError)) { throw e; }
|
||||
|
||||
abort("Error parsing JSON: " + e.message);
|
||||
}
|
||||
if (typeof extraOptions !== "object") {
|
||||
abort("The JSON with extra options has to represent an object.");
|
||||
}
|
||||
|
||||
Object.keys(extraOptions).forEach(key => {
|
||||
options[key] = extraOptions[key];
|
||||
});
|
||||
}
|
||||
|
||||
// Extracted into a function just to silence JSHint complaining about creating
|
||||
// functions in a loop.
|
||||
function trim(s) {
|
||||
return s.trim();
|
||||
}
|
||||
|
||||
// Arguments
|
||||
|
||||
let args = process.argv.slice(2); // Trim "node" and the script path.
|
||||
|
||||
function isOption(arg) {
|
||||
return (/^-.+/).test(arg);
|
||||
}
|
||||
|
||||
function nextArg() {
|
||||
args.shift();
|
||||
}
|
||||
|
||||
// Files
|
||||
|
||||
function readStream(inputStream, callback) {
|
||||
let input = "";
|
||||
inputStream.on("data", data => { input += data; });
|
||||
inputStream.on("end", () => { callback(input); });
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
let inputFile = null;
|
||||
let outputFile = null;
|
||||
|
||||
let options = {
|
||||
cache: false,
|
||||
dependencies: {},
|
||||
exportVar: null,
|
||||
format: "commonjs",
|
||||
optimize: "speed",
|
||||
output: "source",
|
||||
plugins: [],
|
||||
trace: false
|
||||
};
|
||||
|
||||
while (args.length > 0 && isOption(args[0])) {
|
||||
let json, id, mod;
|
||||
|
||||
switch (args[0]) {
|
||||
case "--allowed-start-rules":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -e/--allowed-start-rules option.");
|
||||
}
|
||||
options.allowedStartRules = args[0]
|
||||
.split(",")
|
||||
.map(trim);
|
||||
break;
|
||||
|
||||
case "--cache":
|
||||
options.cache = true;
|
||||
break;
|
||||
|
||||
case "-d":
|
||||
case "--dependency":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -d/--dependency option.");
|
||||
}
|
||||
if (args[0].indexOf(":") !== -1) {
|
||||
let parts = args[0].split(":");
|
||||
options.dependencies[parts[0]] = parts[1];
|
||||
} else {
|
||||
options.dependencies[args[0]] = args[0];
|
||||
}
|
||||
break;
|
||||
|
||||
case "-e":
|
||||
case "--export-var":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -e/--export-var option.");
|
||||
}
|
||||
options.exportVar = args[0];
|
||||
break;
|
||||
|
||||
case "--extra-options":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the --extra-options option.");
|
||||
}
|
||||
addExtraOptions(options, args[0]);
|
||||
break;
|
||||
|
||||
case "--extra-options-file":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the --extra-options-file option.");
|
||||
}
|
||||
try {
|
||||
json = fs.readFileSync(args[0]);
|
||||
} catch (e) {
|
||||
abort("Can't read from file \"" + args[0] + "\".");
|
||||
}
|
||||
addExtraOptions(options, json);
|
||||
break;
|
||||
|
||||
case "--format":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the --format option.");
|
||||
}
|
||||
if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "globals" && args[0] !== "umd") {
|
||||
abort("Module format must be one of \"amd\", \"commonjs\", \"globals\", and \"umd\".");
|
||||
}
|
||||
options.format = args[0];
|
||||
break;
|
||||
|
||||
case "-h":
|
||||
case "--help":
|
||||
printHelp();
|
||||
exitSuccess();
|
||||
break;
|
||||
|
||||
case "-O":
|
||||
case "--optimize":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -O/--optimize option.");
|
||||
}
|
||||
if (args[0] !== "speed" && args[0] !== "size") {
|
||||
abort("Optimization goal must be either \"speed\" or \"size\".");
|
||||
}
|
||||
options.optimize = args[0];
|
||||
break;
|
||||
|
||||
case "-o":
|
||||
case "--output":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the -o/--output option.");
|
||||
}
|
||||
outputFile = args[0];
|
||||
break;
|
||||
|
||||
case "--plugin":
|
||||
nextArg();
|
||||
if (args.length === 0) {
|
||||
abort("Missing parameter of the --plugin option.");
|
||||
}
|
||||
id = /^(\.\/|\.\.\/)/.test(args[0]) ? path.resolve(args[0]) : args[0];
|
||||
try {
|
||||
mod = require(id);
|
||||
} catch (e) {
|
||||
if (e.code !== "MODULE_NOT_FOUND") { throw e; }
|
||||
|
||||
abort("Can't load module \"" + id + "\".");
|
||||
}
|
||||
options.plugins.push(mod);
|
||||
break;
|
||||
|
||||
case "--trace":
|
||||
options.trace = true;
|
||||
break;
|
||||
|
||||
case "-v":
|
||||
case "--version":
|
||||
printVersion();
|
||||
exitSuccess();
|
||||
break;
|
||||
|
||||
case "--":
|
||||
nextArg();
|
||||
break;
|
||||
|
||||
default:
|
||||
abort("Unknown option: " + args[0] + ".");
|
||||
}
|
||||
nextArg();
|
||||
}
|
||||
|
||||
if (Object.keys(options.dependencies).length > 0) {
|
||||
if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "umd") {
|
||||
abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format.");
|
||||
}
|
||||
}
|
||||
|
||||
if (options.exportVar !== null) {
|
||||
if (options.format !== "globals" && options.format !== "umd") {
|
||||
abort("Can't use the -e/--export-var option with the \"" + options.format + "\" module format.");
|
||||
}
|
||||
}
|
||||
|
||||
let inputStream, outputStream;
|
||||
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
inputFile = "-";
|
||||
break;
|
||||
|
||||
case 1:
|
||||
inputFile = args[0];
|
||||
break;
|
||||
|
||||
default:
|
||||
abort("Too many arguments.");
|
||||
}
|
||||
|
||||
if (outputFile === null) {
|
||||
if (inputFile === "-") {
|
||||
outputFile = "-";
|
||||
} else {
|
||||
outputFile = inputFile.substr(0, inputFile.length - path.extname(inputFile).length) + ".js";
|
||||
}
|
||||
}
|
||||
|
||||
if (inputFile === "-") {
|
||||
process.stdin.resume();
|
||||
inputStream = process.stdin;
|
||||
inputStream.on("error", () => {
|
||||
abort("Can't read from file \"" + inputFile + "\".");
|
||||
});
|
||||
} else {
|
||||
inputStream = fs.createReadStream(inputFile);
|
||||
}
|
||||
|
||||
if (outputFile === "-") {
|
||||
outputStream = process.stdout;
|
||||
} else {
|
||||
outputStream = fs.createWriteStream(outputFile);
|
||||
outputStream.on("error", () => {
|
||||
abort("Can't write to file \"" + outputFile + "\".");
|
||||
});
|
||||
}
|
||||
|
||||
readStream(inputStream, input => {
|
||||
let source;
|
||||
|
||||
try {
|
||||
source = peg.generate(input, options);
|
||||
} catch (e) {
|
||||
if (e.location !== undefined) {
|
||||
abort(e.location.start.line + ":" + e.location.start.column + ": " + e.message);
|
||||
} else {
|
||||
abort(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
outputStream.write(source);
|
||||
if (outputStream !== process.stdout) {
|
||||
outputStream.end();
|
||||
}
|
||||
});
|
@ -0,0 +1,14 @@
|
||||
* [v0.10](./v0.10.md) _(August 19, 2016)_
|
||||
* [v0.9](./v0.9.md) _(August 30, 2015)_
|
||||
* [v0.8](./v0.8.md) _(December 24, 2013)_
|
||||
* [v0.7](./v0.7.md) _(April 18, 2012)_
|
||||
* [v0.6.2](./v0.6.md) _(April 20, 2011)_
|
||||
* [v0.6.1](./v0.6.md) _(April 14, 2011)_
|
||||
* [v0.6](./v0.6.md) _(April 14, 2011)_
|
||||
* [v0.5.1](./v0.5.md) _(November 28, 2010)_
|
||||
* [v0.5](./v0.5.md) _(June 10, 2010)_
|
||||
* [v0.4](./v0.4.md) _(April 17, 2010)_
|
||||
* [v0.3](./v0.3.md) _(March 14, 2010)_
|
||||
* [v0.2.1](./v0.2.md) _(March 8, 2010)_
|
||||
* [v0.2](./v0.2.md) _(March 8, 2010)_
|
||||
* [v0.1](./v0.1.md) _(March 8, 2010)_
|
@ -0,0 +1,5 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/commits/0.1) for a full list of changes.
|
||||
|
||||
## v0.1 _(March 8, 2010)_
|
||||
|
||||
* Initial release.
|
@ -0,0 +1,10 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.1...0.2.1) for a full list of changes.
|
||||
|
||||
## v0.2.1 _(March 8, 2010)_
|
||||
|
||||
* Added `pegjs-` prefix to the name of the minified runtime file.
|
||||
|
||||
## v0.2 _(March 8, 2010)_
|
||||
|
||||
* Added `Rakefile` that builds minified runtime using Google Closure Compiler API.
|
||||
* Removed trailing commas in object initializers (Google Closure does not like them).
|
@ -0,0 +1,14 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.2.1...0.3) for a full list of changes.
|
||||
|
||||
### v0.3 _(March 14, 2010)_
|
||||
|
||||
* Wrote `README`.
|
||||
* Bootstrapped the grammar parser.
|
||||
* Metagrammar recognizes JavaScript-like comments.
|
||||
* Changed standard grammar extension from `.peg` to `.pegjs` (it is more specific).
|
||||
* Simplified the example arithmetic's grammar + added comment.
|
||||
* Fixed a bug with reporting of invalid ranges such as `[b-a]` in the metagrammar.
|
||||
* Fixed `--start` vs. `--start-rule` inconsistency between help and actual option processing code.
|
||||
* Avoided ugliness in QUnit output.
|
||||
* Fixed typo in help: `parserVar` → `parser_var`.
|
||||
* Internal code improvements.
|
@ -0,0 +1,24 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.3...0.4) for a full list of changes.
|
||||
|
||||
## v0.4 _(April 17, 2010)_
|
||||
|
||||
### Big Changes
|
||||
|
||||
* Improved IE compatibility — IE6+ is now fully supported.
|
||||
* Generated parsers are now standalone (no runtime is required).
|
||||
* Added example grammars for JavaScript, CSS and JSON.
|
||||
* Added a benchmark suite.
|
||||
* Implemented negative character classes (e.g. `[^a-z]`).
|
||||
* Project moved from BitBucket to GitHub.
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Code generated for the character classes is now regexp-based (= simpler and more scalable).
|
||||
* Added `\uFEFF` (BOM) to the definition of whitespace in the metagrammar.
|
||||
* When building a parser, left-recursive rules (both direct and indirect) are reported as errors.
|
||||
* When building a parser, missing rules are reported as errors.
|
||||
* Expected items in the error messages do not contain duplicates and they are sorted.
|
||||
* Fixed several bugs in the example arithmetic's grammar.
|
||||
* Converted `README` to GitHub Flavored Markdown and improved it.
|
||||
* Added `CHANGELOG`.
|
||||
* Internal code improvements.
|
@ -0,0 +1,38 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.4...0.5.1) for a full list of changes.
|
||||
|
||||
## v0.5.1 _(November 28, 2010)_
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Fixed a problem where “SyntaxError: Invalid range in character class.” error appeared when using command-line version on Windows ([GH-13](https://github.com/dmajda/pegjs/issues/13)).
|
||||
* Fixed wrong version reported by `bin/pegjs --version`.
|
||||
* Removed two unused variables in the code.
|
||||
* Fixed incorrect variable name on two places.
|
||||
|
||||
## v0.5 _(June 10, 2010)_
|
||||
|
||||
### Big Changes
|
||||
|
||||
* Syntax change: Use labeled expressions and variables instead of `$1`, `$2`, etc.
|
||||
* Syntax change: Replaced `:` after a rule name with `=`.
|
||||
* Syntax change: Allow trailing semicolon (`;`) for rules
|
||||
* Semantic change: Start rule of the grammar is now implicitly its first rule.
|
||||
* Implemented semantic predicates.
|
||||
* Implemented initializers.
|
||||
* Removed ability to change the start rule when generating the parser.
|
||||
* Added several compiler optimizations — 0.5 is ~11% faster than 0.4 in the benchmark on V8.
|
||||
|
||||
### Small Changes
|
||||
|
||||
* `PEG.buildParser` now accepts grammars only in string format.
|
||||
* Added “Generated by ...” message to the generated parsers.
|
||||
* Formatted all grammars more consistently and transparently.
|
||||
* Added notes about ECMA-262, 5th ed. compatibility to the JSON example grammar.
|
||||
* Guarded against redefinition of `undefined`.
|
||||
* Made `bin/pegjs` work when called via a symlink ([issue #1](https://github.com/dmajda/pegjs/issues/1)).
|
||||
* Fixed bug causing incorrect error messages ([issue #2](https://github.com/dmajda/pegjs/issues/2)).
|
||||
* Fixed error message for invalid character range.
|
||||
* Fixed string literal parsing in the JavaScript grammar.
|
||||
* Generated code improvements and fixes.
|
||||
* Internal code improvements and fixes.
|
||||
* Improved `README.md`.
|
@ -0,0 +1,37 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.5.1...0.6.2) for a full list of changes.
|
||||
|
||||
## v0.6.2 _(April 20, 2011)_
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Reset parser position when action returns `null`.
|
||||
* Fixed typo in JavaScript example grammar.
|
||||
|
||||
## v0.6.1 _(April 14, 2011)_
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Use `--ascii` option when generating a minified version.
|
||||
|
||||
## v0.6.0 _(April 14, 2011)_
|
||||
|
||||
### Big Changes
|
||||
|
||||
* Rewrote the command-line mode to be based on Node.js instead of Rhino — no more Java dependency. This also means that PEG.js is available as a Node.js package and can be required as a module.
|
||||
* Version for the browser is built separately from the command-line one in two flavors (normal and minified).
|
||||
* Parser variable name is no longer required argument of `bin/pegjs` — it is `module.exports` by default and can be set using the `-e`/`--export-var` option. This makes parsers generated by `/bin/pegjs` Node.js modules by default.
|
||||
* Added ability to start parsing from any grammar rule.
|
||||
* Added several compiler optimizations — 0.6 is ~12% faster than 0.5.1 in the benchmark on V8.
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Split the source code into multiple files combined together using a build system.
|
||||
* Jake is now used instead of Rake for build scripts — no more Ruby dependency.
|
||||
* Test suite can be run from the command-line.
|
||||
* Benchmark suite can be run from the command-line.
|
||||
* Benchmark browser runner improvements (users can specify number of runs, benchmarks are run using `setTimeout`, table is centered and fixed-width).
|
||||
* Added PEG.js version to “Generated by...” line in generated parsers.
|
||||
* Added PEG.js version information and homepage header to `peg.js`.
|
||||
* Generated code improvements and fixes.
|
||||
* Internal code improvements and fixes.
|
||||
* Rewrote `README.md`.
|
@ -0,0 +1,36 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/0.6.2...v0.7.0) for a full list of changes.
|
||||
|
||||
## v0.7.0 _(April 18, 2012)_
|
||||
|
||||
### Big Changes
|
||||
|
||||
* Added ability to pass options to `PEG.buildParser`.
|
||||
* Implemented the `trackLineAndColumn` option for `PEG.buildParser` (together with the `--track-line-and-column` command-line option). It makes the generated parser track line and column during parsing. These are made available inside actions and predicates as `line` and `column` variables.
|
||||
* Implemented the `cache` option for `PEG.buildParser` (together with the `--cache` command-line option). This option enables/disables the results cache in generated parsers, resulting in dramatic speedup when the cache is disabled (the default now). The cost is breaking the linear parsing time guarantee.
|
||||
* The current parse position is visible inside actions and predicates as the `offset` variable.
|
||||
* Exceptions thrown by the parser have `offset`, `expected` and `found` properties containing machine-readable information about the parse failure (based on a patch by Marcin Stefaniuk).
|
||||
* Semantic predicates have access to preceding labels. [[GH-69](https://github.com/dmajda/pegjs/issues/69)]
|
||||
* Implemented case-insensitive literal and class matching. [[GH-34](https://github.com/dmajda/pegjs/issues/34)]
|
||||
* Rewrote the code generator — split some computations into separate passes and based it on a proper templating system (Codie).
|
||||
* Rewrote variable handling in generated parsers in a stack-like fashion, simplifying the code and making the parsers smaller and faster.
|
||||
* Adapted to Node.js 0.6.6+ (no longer supported in older versions).
|
||||
* Dropped support for IE < 8.
|
||||
* As a result of several optimizations, parsers generated by 0.7.0 are ~6.4 times faster and ~19% smaller than those generated by 0.6.2 (as reported by `/tools/impact`).
|
||||
|
||||
### Small Changes
|
||||
|
||||
* Fixed reported error position when part of the input is not consumed. [[GH-48](https://github.com/dmajda/pegjs/issues/48)]
|
||||
* Fixed incorrect disjunction operator in `computeErrorPosition` (original patch by Wolfgang Kluge).
|
||||
* Fixed regexp for detecting command-line options in `/bin/pegjs`. [[GH-51](https://github.com/dmajda/pegjs/issues/51)]
|
||||
* Generate more efficient code for empty literals (original patch by Wolfgang Kluge).
|
||||
* Fixed comment typos (patches by Wolfgang Kluge and Jason Davies). [[GH-59](https://github.com/dmajda/pegjs/issues/59)]
|
||||
* Fixed a typo in JavaScript example grammar. [[GH-62](https://github.com/dmajda/pegjs/issues/62)]
|
||||
* Made copy & paste inclusion of the PEG.js library into another code easier by changing how the library is exported.
|
||||
* Improved the copyright comment and the “Generated by...” header.
|
||||
* Replaced `Jakefile` with `Makefile`.
|
||||
* Added `make hint` task that checks all JavaScript files using JSHint and resolved all issues it reported. All JavaScript files and also generated parsers are JSHint-clean now.
|
||||
* Fixed output printed during test failures (expected value was being printed instead of the actual one). Original patch by Wolfgang Kluge.
|
||||
* Added a `/tools/impact` script to measure speed and size impact of commits.
|
||||
* Various generated code improvements and fixes.
|
||||
* Various internal code improvements and fixes.
|
||||
* Improved `README.md`.
|
@ -0,0 +1,38 @@
|
||||
> See [commit history](https://github.com/pegjs/pegjs/compare/v0.7.0...v0.8.0) for a full list of changes.
|
||||
|
||||
## v0.8.0 _(December 24, 2013)_
|
||||
|
||||
### Big Changes
|
||||
|
||||
* Completely rewrote the code generator. Among other things, it allows optimizing generated parsers for parsing speed or code size using the `optimize` option of the `PEG.buildParser` method or the `--optimize`/`-o` option on the command-line. All internal identifiers in generated code now also have a `peg$` prefix to discourage their use and avoid conflicts. [[#35](https://github.com/dmajda/pegjs/issues/35), [#92](https://github.com/dmajda/pegjs/issues/92)]
|
||||
* Completely redesigned error handling. Instead of returning `null` inside actions to indicate match failure, new `expected` and `error` functions can be called to trigger an error. Also, expectation inside the `SyntaxError` exceptions are now structured to allow easier machine processing. [[#198](https://github.com/dmajda/pegjs/issues/198)]
|
||||
* Implemented a plugin API. The list of plugins to use can be specified using the `plugins` option of the `PEG.buildParser` method or the `--plugin` option on the command-line. Also implemented the `--extra-options` and `--extra-options-file` command-line options, which are mainly useful to pass additional options to plugins. [[#106](https://github.com/dmajda/pegjs/issues/106)]
|
||||
* Made `offset`, `line` and `column` functions, not variables. They are now available in all parsers and return lazily-computed position data. Removed now useless `trackLineAndColumn` option of the `PEG.buildParser` method and the `--track-line-and-column` option on the command-line.
|
||||
* Added a new `text` function. When called inside an action, it returns the text matched by action's expression. [[#131](https://github.com/dmajda/pegjs/issues/131)]
|
||||
* Added a new `$` operator. It extracts matched strings from expressions.
|
||||
* The `?` operator now returns `null` on unsuccessful match.
|
||||
* Predicates now always return `undefined`.
|
||||
* Replaced the `startRule` parameter of the `parse` method in generated parsers with more generic `options` parameter. The start rule can now be specified as the `startRule` option. The `options` parameter can be also used to pass custom options to the parser because it is visible as the `options` variable inside parser code. [[#37](https://github.com/dmajda/pegjs/issues/37)]
|
||||
* The list of allowed start rules of a generated parser now has to be specified explicitly using the `allowedStartRules` option of the `PEG.buildParser` method or the `--allowed-start-rule` option on the command-line. This will make certain optimizations like rule inlining easier in the future.
|
||||
* Removed the `toSource` method of generated parsers and introduced a new `output` option of the `PEG.buildParser` method. It allows callers to specify whether they want to get back the parser object or its source code.
|
||||
* The source code is now a valid npm package. This makes using development versions easier. [[#32](https://github.com/dmajda/pegjs/issues/32)]
|
||||
* Generated parsers are now ~25% faster and ~62%/~3% smaller (when optimized for size/speed) than those generated by 0.7.0.
|
||||
* Requires Node.js 0.8.0+.
|
||||
|
||||
### Small Changes
|
||||
|
||||
* `bin/pegjs` now outputs just the parser source if the value of the `--export-var` option is empty. This makes embedding generated parsers into other files easier. [[#143](https://github.com/dmajda/pegjs/issues/143)]
|
||||
* Changed the value of the `name` property of `PEG.GrammarError` instances from “PEG.GrammarError” to just “GrammarError”. This better reflects the fact that PEG.js can get required with different variable name than `PEG`.
|
||||
* Setup prototype chain for `PEG.GrammarError` correctly.
|
||||
* Setup prototype chain for `SyntaxError` in generated parsers correctly.
|
||||
* Fixed error messages in certain cases with trailing input [[#119](https://github.com/dmajda/pegjs/issues/119)]
|
||||
* Fixed code generated for classes starting with `\^`. [[#125](https://github.com/dmajda/pegjs/issues/125)]
|
||||
* Fixed too eager proxy rules removal. [[#137](https://github.com/dmajda/pegjs/issues/137)]
|
||||
* Added a license to all vendored libraries. [[#207](https://github.com/dmajda/pegjs/issues/207)]
|
||||
* Converted the test suite from QUnit to Jasmine, cleaning it up on the way.
|
||||
* Travis CI integration.
|
||||
* Various internal code improvements and fixes.
|
||||
* Various generated code improvements and fixes.
|
||||
* Various example grammar improvements and fixes.
|
||||
* Improved `README.md`.
|
||||
* Converted `CHANGELOG` to Markdown.
|
@ -0,0 +1,10 @@
|
||||
## Compatibility
|
||||
|
||||
Both the parser generator and generated parsers should run well in the following environments:
|
||||
|
||||
* Node.js 8+
|
||||
* Internet Explorer 11
|
||||
* Microsoft Edge
|
||||
* Firefox
|
||||
* Chrome
|
||||
* Opera
|
@ -0,0 +1,32 @@
|
||||
PEG.js is currently maintained by [Futago-za Ryuu](https://github.com/futagoza) ([@futagozaryuu](https://twitter.com/futagozaryuu)).
|
||||
|
||||
Since it's [inception](https://www.google.com/search?q=inception+meaning) in 2010, PEG.js was maintained by [David Majda](https://majda.cz/) ([@dmajda](http://twitter.com/dmajda)), until [May 2017](https://github.com/pegjs/pegjs/issues/503).
|
||||
|
||||
::: warning
|
||||
The [Bower package](https://github.com/pegjs/bower) (which was maintained by [Michel Krämer](http://www.michel-kraemer.com/) ([@michelkraemer](https://twitter.com/michelkraemer))) has been deprecated, and will no longer be updated.
|
||||
:::
|
||||
|
||||
## Contribute
|
||||
|
||||
You are welcome to contribute code using [GitHub pull requests](https://github.com/pegjs/pegjs/pulls). Unless your contribution is really trivial you should get in touch with me first (preferably by creating a new issue on the [issue tracker](https://github.com/pegjs/pegjs/issues)) - this can prevent wasted effort on both sides.
|
||||
|
||||
> Before submitting a pull request, please make sure you've checked out the [Contribution Guidelines](https://github.com/pegjs/pegjs/blob/master/.github/CONTRIBUTING.md).
|
||||
|
||||
_Ensure you have Node.js 8.6+ and Yarn installed._
|
||||
|
||||
1. Create a fork of [https://github.com/pegjs/pegjs](https://github.com/pegjs/pegjs)
|
||||
2. Clone your fork, and optionally create a new branch
|
||||
3. Run the command `yarn install` from the root of your clone
|
||||
4. Add and commit your changes
|
||||
5. Validate your changes:
|
||||
- Lint the JavaScript changes (command line only, run `yarn lint`)
|
||||
- Run tests to ensure nothing's broken: `yarn test` from the root of the repository
|
||||
- Run benchmarks to check performance: `yarn benchmark`
|
||||
- Check the impact of your commits: [see separate documentation](https://github.com/pegjs/pegjs/blob/master/tools/impact/README.md)
|
||||
6. If validation fails: reverse your commit, fix the problem and then add/commit again
|
||||
7. Push the commits from your clone to the fork
|
||||
8. From your fork, start a new pull request
|
||||
|
||||
It's also a good idea to check out the [package.json](https://github.com/pegjs/pegjs/blob/master/package.json) that defines various scripts.
|
||||
|
||||
To see the list of contributors check out the [repository's contributors page](https://github.com/pegjs/pegjs/graphs/contributors).
|
@ -0,0 +1,72 @@
|
||||
## Grammar Syntax and Semantics
|
||||
|
||||
The grammar syntax is similar to JavaScript in that it is not line-oriented and ignores whitespace between tokens. You can also use JavaScript-style line comments (`// ...`) and block comments (`/* ... */`).
|
||||
|
||||
Let's look at an example grammar that recognizes simple arithmetic expressions like `2*(3+4)`. A parser generated from this grammar computes their values.
|
||||
|
||||
```pegjs
|
||||
start
|
||||
= additive
|
||||
|
||||
additive
|
||||
= left:multiplicative "+" right:additive { return left + right; }
|
||||
/ multiplicative
|
||||
|
||||
multiplicative
|
||||
= left:primary "*" right:multiplicative { return left * right; }
|
||||
/ primary
|
||||
|
||||
primary
|
||||
= integer
|
||||
/ "(" additive:additive ")" { return additive; }
|
||||
|
||||
integer "integer"
|
||||
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
|
||||
```
|
||||
|
||||
On the top level, the grammar consists of *rules* (in our example, there are five of them). Each rule has a *name* (e.g. `integer`) that identifies the rule, and a *parsing expression* (e.g. `digits:[0-9]+ { return parseInt(digits.join(""), 10); }`) that defines a pattern to match against the input text and possibly contains some JavaScript code that determines what happens when the pattern matches successfully. A rule can also contain a *human-readable name* that is used in error messages (in our example, only the `integer` rule has a human-readable name). The parsing starts at the first rule, which is also called the *start rule*.
|
||||
|
||||
A rule name must be a JavaScript identifier. It is followed by an equality sign (“=”) and a parsing expression. If the rule has a human-readable name, it is written as a JavaScript string between the name and separating equality sign. Rules need to be separated only by whitespace (their beginning is easily recognizable), but a semicolon (“;”) after the parsing expression is allowed.
|
||||
|
||||
The first rule can be preceded by an *initializer* - a piece of JavaScript code in curly braces (“{” and “}”). This code is executed before the generated parser starts parsing. All variables and functions defined in the initializer are accessible in rule actions and semantic predicates. The code inside the initializer can access options passed to the parser using the `options` variable. Curly braces in the initializer code must be [balanced](./balanced-braces.md).
|
||||
|
||||
Let's look at the example grammar from above using a simple initializer:
|
||||
|
||||
```pegjs
|
||||
{
|
||||
function makeInteger(o) {
|
||||
return parseInt(o.join(""), 10);
|
||||
}
|
||||
}
|
||||
|
||||
start
|
||||
= additive
|
||||
|
||||
additive
|
||||
= left:multiplicative "+" right:additive { return left + right; }
|
||||
/ multiplicative
|
||||
|
||||
multiplicative
|
||||
= left:primary "*" right:multiplicative { return left * right; }
|
||||
/ primary
|
||||
|
||||
primary
|
||||
= integer
|
||||
/ "(" additive:additive ")" { return additive; }
|
||||
|
||||
integer "integer"
|
||||
= digits:[0-9]+ { return makeInteger(digits); }
|
||||
```
|
||||
|
||||
The parsing expressions of the rules are used to match the input text to the grammar. There are various types of expressions — matching characters or character classes, indicating optional parts and repetition, etc. Expressions can also contain references to other rules. For a more detailed description, check out the [Parsing Expression Types](./parsing-expression-types.md) document.
|
||||
|
||||
If an expression successfully matches a part of the text when running the generated parser, it produces a *match result*, which is a JavaScript value. For example:
|
||||
|
||||
* An expression matching a literal string produces a JavaScript string containing matched text.
|
||||
* An expression matching repeated occurrence of some subexpression produces a JavaScript array with all the matches.
|
||||
|
||||
The match results propagate through the rules when the rule names are used in expressions, up to the start rule. The generated parser returns start rule's match result when parsing is successful.
|
||||
|
||||
One special case of parser expression is a *parser action* - a piece of JavaScript code inside curly braces (“{” and “}”) that takes match results of some of the preceding expressions and returns a JavaScript value. This value is considered a match result of the preceding expression (in other words, the parser action is a match result transformer).
|
||||
|
||||
In our arithmetic's example, there are many parser actions. Consider the action in expression `digits:[0-9]+ { return parseInt(digits.join(""), 10); }`. It takes the match result of the expression [0-9]+, which is an array of strings containing digits, as its parameter. It joins the digits together to form a number and converts it to a JavaScript `number` object.
|
@ -0,0 +1,49 @@
|
||||
### Action Execution Environment
|
||||
|
||||
Actions and predicates have these variables and functions available to them.
|
||||
|
||||
* All variables and functions defined in the initializer at the beginning of the grammar are available.
|
||||
|
||||
* Labels from preceding expressions are available as local variables, which will have the match result of the labelled expressions.
|
||||
|
||||
A label is only available after its labelled expression is matched:
|
||||
|
||||
```pegjs
|
||||
rule = A:('a' B:'b' { /* B is available, A is not */ } )
|
||||
```
|
||||
|
||||
A label in a sub-expression is only valid within the sub-expression:
|
||||
|
||||
```pegjs
|
||||
rule = A:'a' (B: 'b') (C: 'b' { /* A and C are available, B is not */ })
|
||||
```
|
||||
|
||||
* `options` is a variable that contains the parser options.
|
||||
|
||||
* `error(message, where)` will report an error and throw an exception. `where` is optional; the default is the value of `location()`.
|
||||
|
||||
* `expected(message, where)` is similar to `error`, but reports: Expected _message_ but "_other_" found
|
||||
|
||||
* `location()` returns an object like this:
|
||||
|
||||
```js
|
||||
{
|
||||
start: { offset: 23, line: 5, column: 6 },
|
||||
end: { offset: 25, line: 5, column: 8 }
|
||||
}
|
||||
```
|
||||
|
||||
For actions, `start` refers to the position at the beginning of the preceding expression, and `end` refers to the position after the end of the preceding expression.
|
||||
|
||||
For predicates, `start` and `end` are the same, the location where the predicate is evaluated.
|
||||
|
||||
`offset` is a 0-based character index within the source text.
|
||||
`line` and `column` are 1-based indices.
|
||||
|
||||
Note that `line` and `column` are somewhat expensive to compute, so if you need location frequently, you might want to use `offset()` or `range()` instead.
|
||||
|
||||
* `offset()` returns the start offset.
|
||||
|
||||
* `range()` returns an array containing the start and end offsets, such as `[23, 25]`.
|
||||
|
||||
* `text()` returns the source text between `start` and `end` (which will be "" for predicates).
|
@ -0,0 +1,20 @@
|
||||
### Backtracking
|
||||
|
||||
Unlike in regular expressions, there is no backtracking in PEG.js expressions.
|
||||
|
||||
For example, using the input "hi!":
|
||||
|
||||
```pegjs
|
||||
|
||||
// This will fail
|
||||
HI = "hi" / "hi!"
|
||||
|
||||
// This will pass
|
||||
HI = "hi!" / "hi"
|
||||
|
||||
// This will also pass
|
||||
HI = w:"hi" !"!" { return w } / "hi!"
|
||||
|
||||
```
|
||||
|
||||
For more information on backtracking in PEG.js, [checkout this excellent answer on Stack Overflow](https://stackoverflow.com/a/24809596/1518408).
|
@ -0,0 +1,12 @@
|
||||
### Balanced Braces
|
||||
|
||||
Code fragments such as actions and predicates must have balanced curly braces, because PEG.js doesn't parse the contents. It only looks at balanced braces to find the end of the code fragment.
|
||||
|
||||
If your code fragment needs an unbalanced brace in a string literal, you can balance it in a comment. For example:
|
||||
|
||||
```pegjs
|
||||
brace = [{}] {
|
||||
return text() === "{" ? 1 : -1;
|
||||
// } for balance
|
||||
}
|
||||
```
|
@ -0,0 +1,13 @@
|
||||
### Case-insensitivity
|
||||
|
||||
Appending `i` right after either [a literal](./parsing-expression-types.md#literalliteral) or a [a character set](./parsing-expression-types.md#characters) makes the match case-insensitive. The rules shown in the following example all produce the same result:
|
||||
|
||||
```pegjs
|
||||
// without `i`
|
||||
a1 = "a" / "b" / "c" / "A" / "B" / "C"
|
||||
b1 = [a-cA-C]
|
||||
|
||||
// with `i`
|
||||
a2 = "a"i / "b"i / "c"i
|
||||
b2 = [a-c]i
|
||||
```
|
@ -0,0 +1,42 @@
|
||||
### Error Messages
|
||||
|
||||
When parsing input with a generated parser, there are times when the parser will throw an error (on failure or when you call [expected()](./action-execution-environment.md) from one of the action blocks) with a message that contains a comma separated list of expressions that where expected or found. This is usually fine, but sometimes it is preferred to have a more human-readable name.
|
||||
|
||||
You can annotate your grammar rules with human-readable names that will be used in the error messages. For example, this:
|
||||
|
||||
```pegjs
|
||||
integer "integer" = $[0-9]+
|
||||
```
|
||||
|
||||
will produce an error message like:
|
||||
|
||||
> Expected integer but "a" found.
|
||||
|
||||
when parsing a non-number, referencing the human-readable name "integer."
|
||||
|
||||
Without the human-readable name, PEG.js instead uses a description of the expression that failed to match:
|
||||
|
||||
> Expected [0-9] but "a" found.
|
||||
|
||||
Aside from the text content of messages, human-readable names also have a subtler effect on *where* errors are reported. PEG.js prefers to match named rules completely or not at all, but not partially. Unnamed rules, on the other hand, can produce an error in the middle of their subexpressions.
|
||||
|
||||
For example, for this rule matching a comma-separated list of integers:
|
||||
|
||||
```pegjs
|
||||
seq = integer ("," integer)*
|
||||
```
|
||||
|
||||
an input like `1,2,a` produces this error message:
|
||||
|
||||
> Expected integer but "a" found.
|
||||
|
||||
But if we add a human-readable name to the `seq` production:
|
||||
|
||||
```pegjs
|
||||
seq "list of numbers"
|
||||
= integer ("," integer)*
|
||||
```
|
||||
|
||||
then PEG.js prefers an error message that implies a smaller attempted parse tree:
|
||||
|
||||
> Expected end of input but "," found.
|
@ -0,0 +1,136 @@
|
||||
### Parsing Expression Types
|
||||
|
||||
There are several types of parsing expressions, some of them containing subexpressions and thus forming a recursive structure:
|
||||
|
||||
* ["literal"](#literalliteral)
|
||||
* [. (dot character)](#-dot-character)
|
||||
* [[characters]](#characters)
|
||||
* [rule](#rule)
|
||||
* [( expression )](#-expression-)
|
||||
* [expression *](#expression-)
|
||||
* [expression +](#expression--1)
|
||||
* [expression ?](#expression--2)
|
||||
* [& expression](#-expression)
|
||||
* [! expression](#-expression-1)
|
||||
* [& { predicate }](#--predicate-)
|
||||
* [! { predicate }](#--predicate--1)
|
||||
* [$ expression](#-expression-2)
|
||||
* [label : expression](#label--expression)
|
||||
* [expression<sub>1</sub> expression<sub>2</sub> ... expression<sub>n</sub>](#expression1-expression2---expressionn)
|
||||
* [expression { action }](#expression--action-)
|
||||
* [expression<sub>1</sub> / expression<sub>2</sub> / ... / expression<sub>n</sub>](#expression1--expression2----expressionn)
|
||||
* [expression<sub>1</sub> @expression<sub>2</sub> ... expression<sub>n</sub>](#expression1-expression2---expressionn-1)
|
||||
|
||||
#### "*literal*"<br>'*literal*'
|
||||
|
||||
Match exact literal string and return it. The string syntax is the same as in JavaScript.
|
||||
|
||||
Appending `i` right after the literal makes the match case-insensitive.
|
||||
|
||||
#### . *(dot character)*
|
||||
|
||||
Match exactly one character and return it as a string.
|
||||
|
||||
#### [*characters*]
|
||||
|
||||
Match one character from a set and return it as a string. The characters in the list can be escaped in exactly the same way they are escaped in JavaScript strings. The list of characters can also contain ranges (e.g. `[a-f]` means “any character between (and including) _a_ and _f_, all lowercase letters”).
|
||||
|
||||
Preceding the characters with `^` inverts the matched set (e.g. `[^a-z]` means “all character but lowercase letters”).
|
||||
|
||||
Appending `i` right after the right bracket makes the match case-insensitive.
|
||||
|
||||
#### *rule*
|
||||
|
||||
Match a parsing expression of a rule recursively and return its match result.
|
||||
|
||||
#### ( *expression* )
|
||||
|
||||
Match a subexpression and return its match result.
|
||||
|
||||
#### *expression* \*
|
||||
|
||||
Match zero or more repetitions of the expression and return their match results in an array. The matching is greedy, i.e. the parser tries to match the expression as many times as possible. Unlike in regular expressions, [there is no backtracking](./backtracking.md).
|
||||
|
||||
#### *expression* +
|
||||
|
||||
Match one or more repetitions of the expression and return their match results in an array. The matching is greedy, i.e. the parser tries to match the expression as many times as possible. Unlike in regular expressions, [there is no backtracking](./backtracking.md).
|
||||
|
||||
#### *expression* ?
|
||||
|
||||
Try to match the expression. If the match succeeds, return its match result, otherwise return `null`. Unlike in regular expressions, [there is no backtracking](./backtracking.md).
|
||||
|
||||
#### & *expression*
|
||||
|
||||
Try to match the expression. If the match succeeds, just return `undefined` and do not consume any input, otherwise consider the match failed.
|
||||
|
||||
#### ! *expression*
|
||||
|
||||
Try to match the expression. If the match does not succeed, just return `undefined` and do not consume any input, otherwise consider the match failed (the expression was matched, when it shouldn't have).
|
||||
|
||||
#### & { *predicate* }
|
||||
|
||||
This is a positive assertion. No input is consumed.
|
||||
|
||||
The predicate should be JavaScript code, and it's executed as a function. Curly braces in the predicate must be [balanced](./balanced-braces.md).
|
||||
|
||||
The predicate should `return` a boolean value. If the result is _truthy_, the match result is `undefined`, otherwise the match is considered failed.
|
||||
|
||||
The predicate has access to all variables and functions in the [Action Execution Environment](./action-execution-environment.md).
|
||||
|
||||
#### ! { *predicate* }
|
||||
|
||||
This is a negative assertion. No input is consumed.
|
||||
|
||||
The predicate should be JavaScript code, and it's executed as a function. Curly braces in the predicate must be [balanced](./balanced-braces.md).
|
||||
|
||||
The predicate should `return` a boolean value. If the result is _falsy_, the match result is `undefined`, otherwise the match is considered failed.
|
||||
|
||||
The predicate has access to all variables and functions in the [Action Execution Environment](./action-execution-environment.md).
|
||||
|
||||
#### $ *expression*
|
||||
|
||||
Try to match the expression. If the match succeeds, return the matched text instead of the match result.
|
||||
|
||||
#### *label* : *expression*
|
||||
|
||||
Match the expression and remember its match result under given label. The label must be a JavaScript identifier.
|
||||
|
||||
Labeled expressions are useful together with actions, where saved match results can be accessed by action's JavaScript code.
|
||||
|
||||
#### *expression<sub>1</sub>* *expression<sub>2</sub>* ... *expression<sub>n</sub>*
|
||||
|
||||
Match a sequence of expressions and return their match results in an array.
|
||||
|
||||
#### *expression* { *action* }
|
||||
|
||||
If the expression matches successfully, run the action, otherwise consider the match failed.
|
||||
|
||||
The action should be JavaScript code, and it's executed as a function. Curly braces in the action must be [balanced](./balanced-braces.md).
|
||||
|
||||
The action should `return` some value, which will be used as the match result of the expression.
|
||||
|
||||
The action has access to all variables and functions in the [Action Execution Environment](./action-execution-environment.md).
|
||||
|
||||
#### *expression<sub>1</sub>* / *expression<sub>2</sub>* / ... / *expression<sub>n</sub>*
|
||||
|
||||
Try to match the first expression, if it does not succeed, try the second one, etc. Return the match result of the first successfully matched expression. If no expression matches, consider the match failed.
|
||||
|
||||
#### *expression<sub>1</sub>* @*expression<sub>2</sub>* ... *expression<sub>n</sub>*
|
||||
|
||||
Only returns the expression(s) following `@`
|
||||
|
||||
> WARNING: You cannot use this on predicate's, and cannot use it alongside an action.
|
||||
|
||||
```js
|
||||
start = MultiPluck
|
||||
/ SinglePluck
|
||||
|
||||
SinglePluck = "0"? @integer
|
||||
MultiPluck = @integer "." @integer
|
||||
|
||||
integer = $[0-9]+
|
||||
```
|
||||
|
||||
When `SinglePluck` finds `011`, it returns `"11"`
|
||||
|
||||
When `MultiPluck` finds `0.11`, it returns `["0", "11"]`
|
@ -0,0 +1,20 @@
|
||||
### tracer#trace()
|
||||
|
||||
Whether you are using the CLI or the API, as long as you pass the `trace` option, the generated parser can trace it's progress by calling `tracer.trace( event )`, which can then be overwritten by passing an alternative tracer in the options to the generated parser.
|
||||
|
||||
You can also optionally disable the default tracer by passing `features: { DefaultTracer: false }` to `peg.generate` or `peg.compiler.compile`, in which case a empty function will be called by default unless a tracer is passed to the parser.
|
||||
|
||||
The `event` passed to the `trace` method contains the following properties:
|
||||
|
||||
property | description
|
||||
--- | ---
|
||||
type | name of the event
|
||||
rule | rule that this event was emitted from
|
||||
result | the result of the match
|
||||
location | location in the grammar when the event was emitted
|
||||
|
||||
The `type` property will only be 1 of 3 values:
|
||||
|
||||
1. `rule.enter` when entering a rule
|
||||
2. `rule.match` when matching a rule was successful
|
||||
3. `rule.fail` when matching a rule failed
|
@ -0,0 +1,70 @@
|
||||
## Command Line
|
||||
|
||||
The CLI `pegjs` can be used from terminals to generate a parser from the given grammar, without accessing the PEG.js API.
|
||||
|
||||
It's usage signature is: `pegjs [options] [<input_file>] [--]`
|
||||
|
||||
### Configuration file
|
||||
|
||||
If you provide a configuration file (via `-c` or `--config`) then you can use all the options used by the [PEG.js API](./javascript-api.md), as well as:
|
||||
|
||||
* `input` — The grammar used as the input file
|
||||
* `output` — Output's generated parser to the provided destination
|
||||
|
||||
> 1. Will throw if neither are strings, or the input has already been provided
|
||||
> 2. the `output` option for the compiler is not usable from the config
|
||||
|
||||
### Input File
|
||||
|
||||
Only one grammar is accepted as the given input file, otherwise the CLI aborts with an error.
|
||||
|
||||
### Options and their values
|
||||
|
||||
Usually CLI options are passed like so
|
||||
|
||||
pegjs grammar.pegjs -o parser.js -c pegjs.config.js
|
||||
|
||||
With the PEG.js CLI, you can also use the assignment operator, so the above can be used like so:
|
||||
|
||||
pegjs grammar.pegjs -o=parser.js -c=pegjs.config.js
|
||||
|
||||
### Command Line Options
|
||||
|
||||
When using the CLI `pegjs`, you can pass the following options:
|
||||
|
||||
* `-a`, `--allowed-start-rules` — comma-separated list of rules the parser will be allowed to start parsing from (default: the first rule in the grammar)
|
||||
* `--cache` — makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower
|
||||
* `-c`, `--config` — file with additional options (in JSON or JavaScript) to pass to `peg.generate`
|
||||
* `-d`, `--dependency` — makes the parser require a specified dependency (can be specified multiple times)
|
||||
* `-e`, `--export-var` — name of a global variable into which the parser object is assigned to when no module loader is detected
|
||||
* `--extra-options` — additional options (in JSON format) to pass to `peg.generate`
|
||||
* `--extra-options-file` — same as `--config` (was the only cli option for config files before PEG.js v0.11)
|
||||
* `-f`, `--format` — format of the generated parser: `amd`, `bare`, `commonjs`, `es`, `globals`, `umd` (default: `commonjs`)
|
||||
* `--no-cache` — opposite of `--cache` (default behavior)
|
||||
* `--no-trace` — opposite of `--trace` (default behavior)
|
||||
* `-O`, `--optimize` — selects between optimizing the generated parser for parsing speed (`speed`) or code size (`size`) (default: `speed`)
|
||||
* `-o`, `--output` — Output file
|
||||
* `-p`, `--plugin` — makes PEG.js use a specified plugin (can be specified multiple times)
|
||||
* `--trace` — makes the parser trace its progress
|
||||
|
||||
The following options only print to the console before exiting:
|
||||
|
||||
* `-h`, `--help` — Print help and exit
|
||||
* `-v`, `--version` — Print version information and exit
|
||||
|
||||
### Repeatable and non-repeatable options
|
||||
|
||||
Repeatable options on the cli are handled by pushing the values into a list (which is used as the value for the option):
|
||||
|
||||
- `pegjs -a start,Rule -a Rule,Template` will set `options.allowedStartRules` to `[ "start", "Rule", "Template" ]`
|
||||
|
||||
Unless it's a repeatable option, any option on the right side will take priority over either the same option mentioned before or it's counter part:
|
||||
|
||||
- `pegjs -f es -f bare` will set `options.format` to `bare`
|
||||
- `pegjs --no-trace --trace` will set `options.trace` to `true`
|
||||
|
||||
### Extra options
|
||||
|
||||
Need to pass a custom set of arguments to your plugin? The PEG.js CLI will pass all arguments after `--` to `options["--"]`, which is passed to all plugins.
|
||||
|
||||
> NOTE: You will need to use your own argument parser, as the PEG.js CLI does not process these arguments.
|
@ -0,0 +1,27 @@
|
||||
## Generating a Parser
|
||||
|
||||
PEG.js generates a parser from grammar that describes expected input and can specify what the parser returns (using semantic actions on matched parts of the input). The generated parser itself is a JavaScript object with a simple API.
|
||||
|
||||
### Command Line
|
||||
|
||||
To generate a parser from your grammar, use the `pegjs` command:
|
||||
|
||||
```console
|
||||
$ pegjs arithmetics.pegjs
|
||||
```
|
||||
|
||||
This writes parser source code into a file with the same name as the grammar file but with “.js” extension. You can also specify the output file explicitly:
|
||||
|
||||
```console
|
||||
$ pegjs -o arithmetics-parser.js arithmetics.pegjs
|
||||
```
|
||||
|
||||
If you omit both input and output files, the standard input and output are used (REPL mode).
|
||||
|
||||
By default, the generated parser is in the Node.js module format. You can override this using the `--format` option.
|
||||
|
||||
You can tweak the generated parser with [several options](./command-line.md).
|
||||
|
||||
### Module
|
||||
|
||||
To generate a parser using the module, take a look at the section about the method `peg.generate()` described in the [JavaScript API documentation](./javascript-api.md). This method is the API alternative to the command line tool as it does everything for it, the command line tool just takes care of validating and handling input, output and options.
|
@ -0,0 +1,10 @@
|
||||
## Getting Started
|
||||
|
||||
The [online editor](https://pegjs.org/online) is the easiest way to generate a parser. Just enter your grammar, try parsing some input, and download the generated parser code.
|
||||
|
||||
If you want a more flexible way to generate a parser, check out the following:
|
||||
|
||||
- [Installing PEG.js](./installation.md)
|
||||
- [Generating a Parser](./generating-a-parser.md)
|
||||
- [JavaScript API](./javascript-api.md)
|
||||
- [Plugins](./plugins.md)
|
@ -0,0 +1,41 @@
|
||||
## Installation
|
||||
|
||||
### Node.js
|
||||
|
||||
To use the `pegjs` command, install PEG.js globally:
|
||||
|
||||
```console
|
||||
$ npm install -g pegjs
|
||||
```
|
||||
|
||||
To use the JavaScript API, install PEG.js locally:
|
||||
|
||||
```console
|
||||
$ npm install pegjs
|
||||
```
|
||||
|
||||
If you need both the `pegjs` command and the JavaScript API, install PEG.js both ways.
|
||||
|
||||
If you plan to use the `pegjs` command only in your `package.json`, it is suggested you install it locally only, as all locally installed packages that contain command line tools are available to it.
|
||||
|
||||
### Browser
|
||||
|
||||
[Download](https://pegjs.org/#download) the PEG.js library (regular or minified version) or use it directly using UNPKG:
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/pegjs/dist/peg.min.js"></script>
|
||||
```
|
||||
|
||||
### Latest
|
||||
|
||||
To use the latest features, fixes and changes of PEG.js, install the packaged dev release:
|
||||
|
||||
```console
|
||||
$ npm install pegjs@dev
|
||||
```
|
||||
|
||||
On the browser you can use UNPKG again:
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/pegjs@dev/dist/peg.min.js"></script>
|
||||
```
|
@ -0,0 +1,135 @@
|
||||
## JavaScript API
|
||||
|
||||
In Node.js, require the PEG.js module:
|
||||
|
||||
```js
|
||||
// ES2015+ Module System
|
||||
import peg from "pegjs";
|
||||
|
||||
// CommonJS
|
||||
var peg = require("pegjs");
|
||||
```
|
||||
|
||||
In browsers, include the PEG.js library in your web page or application using the `<script>` tag. If PEG.js detects an AMD loader, it will define itself as a module, otherwise the API will be available in the `peg` global object.
|
||||
|
||||
After this there are 3 methods and 1 class that you will mainly use:
|
||||
|
||||
```js
|
||||
var grammar = "start = ('a' / 'b')+";
|
||||
|
||||
// Parse grammar, generating AST
|
||||
var ast = peg.parser.parse(grammar);
|
||||
|
||||
// Create a new compiler session
|
||||
var session = new peg.compiler.Session( { warn: customLogger } );
|
||||
|
||||
// Generate parser from PEG.js AST using an exisiting session
|
||||
var parserA = peg.compiler.compile(ast, session);
|
||||
|
||||
// Generate parser from the grammar source using a new session
|
||||
var parserB = peg.generate(grammar);
|
||||
```
|
||||
|
||||
The most common method you will use is `peg.generate()`, which is the API alternative to the [PEG.js CLI](./generating-a-parser.md). More about each method is explained below, along with their respective options.
|
||||
|
||||
#### peg.parser.parse(input[, options])
|
||||
|
||||
You can simply parse a grammar and get it's AST by using the parser:
|
||||
|
||||
```js
|
||||
var ast = peg.parser.parse("start = ('a' / 'b')+");
|
||||
```
|
||||
|
||||
The following option's are used by the PEG.js parser:
|
||||
|
||||
* `extractComments` - If `true`, the parser will collect all comments in the grammar (default: `false`).
|
||||
* `reservedWords` - An array of words that the parser wont allow as labels for rules (default: [ES5](http://es5.github.io/#x7.6.1)).
|
||||
|
||||
When `extractComments` is set to `true`, the parser will collect all comments in the grammar and return them on the `comments` property (as an object map) on the `grammar` AST node (the AST directly returned by the parser). Each comment property has it's offset as the property key, while the property value has the following structure:
|
||||
|
||||
```js
|
||||
{
|
||||
text: 'all text between /* or */, or // and end of line',
|
||||
multiline: true|false,
|
||||
location: location()
|
||||
}
|
||||
```
|
||||
|
||||
When set to `false`, the `comments` property will be set to `null`.
|
||||
|
||||
For more information about `location()`, as well more methods you can use in JavaScript actions within your grammar, see the helper's description in [Action Execution Environment](../grammar/action-execution-environment.md).
|
||||
|
||||
#### new peg.compiler.Session([options])
|
||||
|
||||
An instance of this class holds helpers (methods, objects with methods, constants, etc) that are shared between multiple calls to `peg.compiler.compile()`.
|
||||
|
||||
> This approach is used rather then a traditional compiler class because it provides the plugin developer with an easier way to replace certain components rather then creating their own version of PEG.js to override one or more internal methods.
|
||||
|
||||
The following option's are used by the Session API, but are currently all optional:
|
||||
|
||||
* `opcodes` - An `enum` like hashmap (plain object) that is used by the bytecode and parser generators.
|
||||
* `parser` - A pre-generated PEG.js grammar parser that should return an instance of the PEG.js AST's Grammar class. Can be replaced to add additional syntax features, or allow an alternative syntax for the grammar.
|
||||
* `passes` - An object with each property being an array of methods that will check or alter the AST passed to them.
|
||||
* `visitor` - An object that should contain the `ASTVisitor` class for the PEG.js AST, as well as the `build()` helper.
|
||||
* `vm` - An object that should contain `evalModule()`, a wrapper for `eval` based on Node's `vm.runInContext()` method.
|
||||
* `warn` - A method called only when PEG.js encounters an error that doesn't stop the parser from being generated.
|
||||
* `error` - A method that will be called when PEG.js encounters an error that will stop the parser from being generated.
|
||||
|
||||
This class will also return some helper methods:
|
||||
|
||||
* `parse( input, options )` - an alias for `this.parser.parse( input, options )`
|
||||
* `buildVisitor( functions )` - an alias for `this.visitor.build( functions )`
|
||||
* `fatal( message, location )` - Will always throw an error (used by default for `this.error()` if not overridden).
|
||||
|
||||
#### peg.compiler.compile(ast, session[, options])
|
||||
|
||||
This method takes a parsed grammar (the PEG.js AST), sets default options, executes each pass currently within the session (passing the _ast_, _session_ and _options_ arguments to each one), then returns the result, which depends on the value of the `output` option.
|
||||
|
||||
```js
|
||||
var grammar = "start = ('a' / 'b')+";
|
||||
var ast = peg.parser.parse(grammar);
|
||||
var passes = peg.util.convertPasses( peg.compiler.passes );
|
||||
var session = new peg.compiler.Session( { passes } );
|
||||
var parser = peg.compiler.compile(ast, session);
|
||||
```
|
||||
|
||||
You can tweak the generated parser by passing a third parameter with an options object. The following options are supported:
|
||||
|
||||
Option | default value | description
|
||||
--- | --- | ---
|
||||
allowedStartRules | first rule | rules the generated parser is allowed to start parsing from
|
||||
cache | `false` | makes the generated parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower
|
||||
context | `{}` | contains a map of variables used by `peg.util.vm.evalModule()` when the `output` option is set to `"parser"`
|
||||
dependencies<sub>1</sub> | `{}` | parser dependencies, the value is an object which maps variables used to access the dependencies to module IDs used to load them
|
||||
exportVar<sub>2</sub> | `null` | name of an optional global variable into which the generated parser object is assigned to when no module loader is detected
|
||||
features | `null` | map of optional features that are set to `true` by default: `"text"`, `"offset"`, `"range"`, `"location"`, `"expected"`, `"error"`, `"filename"` and `"DefaultTracer"`
|
||||
format<sub>3</sub> | `"bare"` | format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, `"es"`, `"globals"`, or `"umd"`)
|
||||
header | `null` | adds additional comments or content after the `Generated by ...` comment; this option is only handled if it's an array or a string:
|
||||
optimize | `"speed"` | selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`)
|
||||
output | `"parser"` | if set to `"parser"`, the method will return generated parser object; if set to `"source"`, it will return generated parser source code as a string
|
||||
trace | `false` | makes the generated parser trace its progress
|
||||
|
||||
1. The `dependencies` option is only used when `format` is set to `"amd"`, `"commonjs"`, `"es"`, or `"umd"`
|
||||
2. The `exportVar` option is only used when `format` is set to `"globals"` or `"umd"`
|
||||
3. The `format` option is is always set to `"umd"` when `output` is set to `"parser"`
|
||||
|
||||
The `header` options behavior will change depending on the option type:
|
||||
|
||||
* `[ string1, string2, ... ]` will add each element (all expected to be strings) as a separate line comment
|
||||
* `string` will simply append the string (e.g. `"/* eslint-disable */"`) after the `Generated by ...` comment
|
||||
|
||||
#### peg.generate(grammar[, options])
|
||||
|
||||
Will generate a parser from the given grammar (the _input_ passed to `peg.parser.parse()`):
|
||||
|
||||
```js
|
||||
var parser = peg.generate("start = ('a' / 'b')+");
|
||||
```
|
||||
|
||||
This method will return a generated parser object or its source code as a string (depending on the value of the `output` option - see above). It will throw an exception if the grammar is invalid. The exception will contain the usual `message` property with more details about the error, along with a `location` property to track the location of the error. You can easily tell if the exception was thrown by PEG.js because it will always have the `name` property set to `GrammarError`.
|
||||
|
||||
You can tweak the generated parser by passing a second parameter with an options object to `peg.generate()`. The following options are supported:
|
||||
|
||||
* All options that you pass to `peg.compiler.compile()`
|
||||
* `parser` - an optional object with option's passed to the PEG.js parser (`peg.parser.parse()`)
|
||||
* `plugins` - plugins to use _(their `use()` method is executed by `peg.generate()`)_
|
@ -0,0 +1,34 @@
|
||||
## Plugins
|
||||
|
||||
You can use plugins by simply passing them to the generate method (e.g. [`peg.generate(grammar, { plugins: [plugin1, plugin2, etc] })`](./javascript-api.md#peggenerategrammar-options)).
|
||||
|
||||
|
||||
#### Creating a Plugin
|
||||
|
||||
Plugins are expected to be an object that contains at least one method: `use( session, options )`
|
||||
|
||||
- `session` - See the docs for [peg.compiler.Session](./javascript-api.md#new-pegcompilersessionoptions)
|
||||
- `options` - see the docs for [`peg.generate`](./javascript-api.md#peggenerategrammar-options) and [`peg.compiler.compile`](./javascript-api.md#pegcompilercompileast-session-options)
|
||||
|
||||
Here is a simple example:
|
||||
|
||||
```js
|
||||
import customGrammarParser from "./parser";
|
||||
|
||||
export function use( session, options ) {
|
||||
|
||||
// Replace the grammar parser
|
||||
session.parser = customGrammarParser;
|
||||
|
||||
// always output the source
|
||||
options.output = "source";
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
#### Resources
|
||||
|
||||
You can find some plugins to use on
|
||||
|
||||
- the [PEG.js wiki](https://github.com/pegjs/pegjs/wiki/Plugins) (or add yours to the list for other's to find)
|
||||
- NPM: https://www.npmjs.com/browse/keyword/pegjs
|
@ -0,0 +1,19 @@
|
||||
## Using the Parser
|
||||
|
||||
> parser { DefaultTracer?, SyntaxError, parse(input[, options]) }
|
||||
|
||||
Using the generated parser is simple; just call its `parse` method and pass an input string as a parameter. The method will return a parse result (the exact value depends on the grammar used to generate the parser) or throw an exception if the input is invalid. The exception will contain `location`, `expected`, `found`, and `message` properties with more details about the error.
|
||||
|
||||
```js
|
||||
parser.parse("abba"); // returns ["a", "b", "b", "a"]
|
||||
|
||||
parser.parse("abcd"); // throws an exception
|
||||
```
|
||||
|
||||
You can tweak parser behavior by passing a second parameter with an `options` object to the `parse` method. The following options are supported:
|
||||
|
||||
* `filename` — the name of the source passed to the generated parser; will be used by the `location()` helper
|
||||
* `startRule` — name of the rule to start parsing from (depends on the rules the grammar supports as starting rules)
|
||||
* `tracer` — tracer to use (only if the parser was generated with `"trace": true` option passed to the compiler)
|
||||
|
||||
Parsers can also support their own custom options on the `options` object passed to them.
|
@ -1,108 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/* eslint-env node */
|
||||
|
||||
let babelify = require("babelify");
|
||||
let browserify = require("browserify");
|
||||
let buffer = require("vinyl-buffer");
|
||||
let del = require("del");
|
||||
let eslint = require("gulp-eslint");
|
||||
let gulp = require("gulp");
|
||||
let header = require("gulp-header");
|
||||
let mocha = require("gulp-mocha");
|
||||
let package_ = require("./package");
|
||||
let peg = require("./lib/peg");
|
||||
let rename = require("gulp-rename");
|
||||
let runSequence = require("run-sequence");
|
||||
let source = require("vinyl-source-stream");
|
||||
let spawn = require("child_process").spawn;
|
||||
let transform = require("gulp-transform");
|
||||
let uglify = require("gulp-uglify");
|
||||
|
||||
const HEADER = [
|
||||
"// PEG.js " + package_.version,
|
||||
"//",
|
||||
"// https://pegjs.org/",
|
||||
"//",
|
||||
"// Copyright (c) 2010-2016 David Majda",
|
||||
"// Licensed under the MIT License.",
|
||||
""
|
||||
].map(line => `${line}\n`).join("");
|
||||
|
||||
const JS_FILES = [
|
||||
"lib/**/*.js",
|
||||
"!lib/parser.js",
|
||||
"test/**/*.js",
|
||||
"test/server",
|
||||
"!test/vendor/**/*",
|
||||
"benchmark/**/*.js",
|
||||
"benchmark/run",
|
||||
"benchmark/server",
|
||||
"!benchmark/vendor/**/*",
|
||||
"bin/pegjs",
|
||||
"gulpfile.js"
|
||||
];
|
||||
|
||||
const TEST_FILES = [
|
||||
"test/**/*.js",
|
||||
"!test/vendor/**/*"
|
||||
];
|
||||
|
||||
function generate(contents) {
|
||||
return peg.generate(contents.toString(), {
|
||||
output: "source",
|
||||
format: "commonjs"
|
||||
});
|
||||
}
|
||||
|
||||
// Run ESLint on all JavaScript files.
|
||||
gulp.task("lint", () =>
|
||||
gulp.src(JS_FILES)
|
||||
.pipe(eslint())
|
||||
.pipe(eslint.format())
|
||||
.pipe(eslint.failAfterError())
|
||||
);
|
||||
|
||||
// Run tests.
|
||||
gulp.task("test", () =>
|
||||
gulp.src(TEST_FILES, { read: false })
|
||||
.pipe(mocha())
|
||||
);
|
||||
|
||||
// Run benchmarks.
|
||||
gulp.task("benchmark", () =>
|
||||
spawn("benchmark/run", { stdio: "inherit" })
|
||||
);
|
||||
|
||||
// Create the browser build.
|
||||
gulp.task("browser:build", () =>
|
||||
browserify("lib/peg.js", { standalone: "peg" })
|
||||
.transform(babelify, { presets: "es2015", compact: false })
|
||||
.bundle()
|
||||
.pipe(source("peg.js"))
|
||||
.pipe(header(HEADER))
|
||||
.pipe(gulp.dest("browser"))
|
||||
.pipe(rename({ suffix: ".min" }))
|
||||
.pipe(buffer())
|
||||
.pipe(uglify())
|
||||
.pipe(header(HEADER))
|
||||
.pipe(gulp.dest("browser"))
|
||||
);
|
||||
|
||||
// Delete the browser build.
|
||||
gulp.task("browser:clean", () =>
|
||||
del("browser")
|
||||
);
|
||||
|
||||
// Generate the grammar parser.
|
||||
gulp.task("parser", () =>
|
||||
gulp.src("src/parser.pegjs")
|
||||
.pipe(transform(generate))
|
||||
.pipe(rename({ extname: ".js" }))
|
||||
.pipe(gulp.dest("lib"))
|
||||
);
|
||||
|
||||
// Default task.
|
||||
gulp.task("default", cb =>
|
||||
runSequence("lint", "test", cb)
|
||||
);
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"commonjs": true
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let visitor = require("./visitor");
|
||||
|
||||
// AST utilities.
|
||||
let asts = {
|
||||
findRule(ast, name) {
|
||||
for (let i = 0; i < ast.rules.length; i++) {
|
||||
if (ast.rules[i].name === name) {
|
||||
return ast.rules[i];
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
|
||||
indexOfRule(ast, name) {
|
||||
for (let i = 0; i < ast.rules.length; i++) {
|
||||
if (ast.rules[i].name === name) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
},
|
||||
|
||||
alwaysConsumesOnSuccess(ast, node) {
|
||||
function consumesTrue() { return true; }
|
||||
function consumesFalse() { return false; }
|
||||
|
||||
function consumesExpression(node) {
|
||||
return consumes(node.expression);
|
||||
}
|
||||
|
||||
let consumes = visitor.build({
|
||||
rule: consumesExpression,
|
||||
named: consumesExpression,
|
||||
|
||||
choice(node) {
|
||||
return node.alternatives.every(consumes);
|
||||
},
|
||||
|
||||
action: consumesExpression,
|
||||
|
||||
sequence(node) {
|
||||
return node.elements.some(consumes);
|
||||
},
|
||||
|
||||
labeled: consumesExpression,
|
||||
text: consumesExpression,
|
||||
simple_and: consumesFalse,
|
||||
simple_not: consumesFalse,
|
||||
optional: consumesFalse,
|
||||
zero_or_more: consumesFalse,
|
||||
one_or_more: consumesExpression,
|
||||
group: consumesExpression,
|
||||
semantic_and: consumesFalse,
|
||||
semantic_not: consumesFalse,
|
||||
|
||||
rule_ref(node) {
|
||||
return consumes(asts.findRule(ast, node.name));
|
||||
},
|
||||
|
||||
literal(node) {
|
||||
return node.value !== "";
|
||||
},
|
||||
|
||||
class: consumesTrue,
|
||||
any: consumesTrue
|
||||
});
|
||||
|
||||
return consumes(node);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = asts;
|
@ -1,91 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let generateBytecode = require("./passes/generate-bytecode");
|
||||
let generateJS = require("./passes/generate-js");
|
||||
let removeProxyRules = require("./passes/remove-proxy-rules");
|
||||
let reportDuplicateLabels = require("./passes/report-duplicate-labels");
|
||||
let reportDuplicateRules = require("./passes/report-duplicate-rules");
|
||||
let reportInfiniteRecursion = require("./passes/report-infinite-recursion");
|
||||
let reportInfiniteRepetition = require("./passes/report-infinite-repetition");
|
||||
let reportUndefinedRules = require("./passes/report-undefined-rules");
|
||||
let visitor = require("./visitor");
|
||||
|
||||
function processOptions(options, defaults) {
|
||||
let processedOptions = {};
|
||||
|
||||
Object.keys(options).forEach(name => {
|
||||
processedOptions[name] = options[name];
|
||||
});
|
||||
|
||||
Object.keys(defaults).forEach(name => {
|
||||
if (!Object.prototype.hasOwnProperty.call(processedOptions, name)) {
|
||||
processedOptions[name] = defaults[name];
|
||||
}
|
||||
});
|
||||
|
||||
return processedOptions;
|
||||
}
|
||||
|
||||
let compiler = {
|
||||
// AST node visitor builder. Useful mainly for plugins which manipulate the
|
||||
// AST.
|
||||
visitor: visitor,
|
||||
|
||||
// Compiler passes.
|
||||
//
|
||||
// Each pass is a function that is passed the AST. It can perform checks on it
|
||||
// or modify it as needed. If the pass encounters a semantic error, it throws
|
||||
// |peg.GrammarError|.
|
||||
passes: {
|
||||
check: {
|
||||
reportUndefinedRules: reportUndefinedRules,
|
||||
reportDuplicateRules: reportDuplicateRules,
|
||||
reportDuplicateLabels: reportDuplicateLabels,
|
||||
reportInfiniteRecursion: reportInfiniteRecursion,
|
||||
reportInfiniteRepetition: reportInfiniteRepetition
|
||||
},
|
||||
transform: {
|
||||
removeProxyRules: removeProxyRules
|
||||
},
|
||||
generate: {
|
||||
generateBytecode: generateBytecode,
|
||||
generateJS: generateJS
|
||||
}
|
||||
},
|
||||
|
||||
// Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
|
||||
// if the AST contains a semantic error. Note that not all errors are detected
|
||||
// during the generation and some may protrude to the generated parser and
|
||||
// cause its malfunction.
|
||||
compile(ast, passes, options) {
|
||||
options = options !== undefined ? options : {};
|
||||
|
||||
options = processOptions(options, {
|
||||
allowedStartRules: [ast.rules[0].name],
|
||||
cache: false,
|
||||
dependencies: {},
|
||||
exportVar: null,
|
||||
format: "bare",
|
||||
optimize: "speed",
|
||||
output: "parser",
|
||||
trace: false
|
||||
});
|
||||
|
||||
Object.keys(passes).forEach(stage => {
|
||||
passes[stage].forEach(p => { p(ast, options); });
|
||||
});
|
||||
|
||||
switch (options.output) {
|
||||
case "parser":
|
||||
return eval(ast.code);
|
||||
|
||||
case "source":
|
||||
return ast.code;
|
||||
|
||||
default:
|
||||
throw new Error("Invalid output format: " + options.output + ".");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = compiler;
|
@ -1,54 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
|
||||
|
||||
// JavaScript code generation helpers.
|
||||
let js = {
|
||||
stringEscape(s) {
|
||||
// ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
||||
// literal except for the closing quote character, backslash, carriage
|
||||
// return, line separator, paragraph separator, and line feed. Any character
|
||||
// may appear in the form of an escape sequence.
|
||||
//
|
||||
// For portability, we also escape all control and non-ASCII characters.
|
||||
return s
|
||||
.replace(/\\/g, "\\\\") // backslash
|
||||
.replace(/"/g, "\\\"") // closing double quote
|
||||
.replace(/\0/g, "\\0") // null
|
||||
.replace(/\x08/g, "\\b") // backspace
|
||||
.replace(/\t/g, "\\t") // horizontal tab
|
||||
.replace(/\n/g, "\\n") // line feed
|
||||
.replace(/\v/g, "\\v") // vertical tab
|
||||
.replace(/\f/g, "\\f") // form feed
|
||||
.replace(/\r/g, "\\r") // carriage return
|
||||
.replace(/[\x00-\x0F]/g, ch => "\\x0" + hex(ch))
|
||||
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex(ch))
|
||||
.replace(/[\u0100-\u0FFF]/g, ch => "\\u0" + hex(ch))
|
||||
.replace(/[\u1000-\uFFFF]/g, ch => "\\u" + hex(ch));
|
||||
},
|
||||
|
||||
regexpClassEscape(s) {
|
||||
// Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
||||
//
|
||||
// For portability, we also escape all control and non-ASCII characters.
|
||||
return s
|
||||
.replace(/\\/g, "\\\\") // backslash
|
||||
.replace(/\//g, "\\/") // closing slash
|
||||
.replace(/]/g, "\\]") // closing bracket
|
||||
.replace(/\^/g, "\\^") // caret
|
||||
.replace(/-/g, "\\-") // dash
|
||||
.replace(/\0/g, "\\0") // null
|
||||
.replace(/\x08/g, "\\b") // backspace
|
||||
.replace(/\t/g, "\\t") // horizontal tab
|
||||
.replace(/\n/g, "\\n") // line feed
|
||||
.replace(/\v/g, "\\v") // vertical tab
|
||||
.replace(/\f/g, "\\f") // form feed
|
||||
.replace(/\r/g, "\\r") // carriage return
|
||||
.replace(/[\x00-\x0F]/g, ch => "\\x0" + hex(ch))
|
||||
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex(ch))
|
||||
.replace(/[\u0100-\u0FFF]/g, ch => "\\u0" + hex(ch))
|
||||
.replace(/[\u1000-\uFFFF]/g, ch => "\\u" + hex(ch));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = js;
|
@ -1,54 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
// Bytecode instruction opcodes.
|
||||
let opcodes = {
|
||||
// Stack Manipulation
|
||||
|
||||
PUSH: 0, // PUSH c
|
||||
PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
|
||||
PUSH_NULL: 2, // PUSH_NULL
|
||||
PUSH_FAILED: 3, // PUSH_FAILED
|
||||
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
|
||||
PUSH_CURR_POS: 5, // PUSH_CURR_POS
|
||||
POP: 6, // POP
|
||||
POP_CURR_POS: 7, // POP_CURR_POS
|
||||
POP_N: 8, // POP_N n
|
||||
NIP: 9, // NIP
|
||||
APPEND: 10, // APPEND
|
||||
WRAP: 11, // WRAP n
|
||||
TEXT: 12, // TEXT
|
||||
|
||||
// Conditions and Loops
|
||||
|
||||
IF: 13, // IF t, f
|
||||
IF_ERROR: 14, // IF_ERROR t, f
|
||||
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
|
||||
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b
|
||||
|
||||
// Matching
|
||||
|
||||
MATCH_ANY: 17, // MATCH_ANY a, f, ...
|
||||
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
|
||||
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
|
||||
MATCH_REGEXP: 20, // MATCH_REGEXP r, a, f, ...
|
||||
ACCEPT_N: 21, // ACCEPT_N n
|
||||
ACCEPT_STRING: 22, // ACCEPT_STRING s
|
||||
FAIL: 23, // FAIL e
|
||||
|
||||
// Calls
|
||||
|
||||
LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
|
||||
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
|
||||
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN
|
||||
|
||||
// Rules
|
||||
|
||||
RULE: 27, // RULE r
|
||||
|
||||
// Failure Reporting
|
||||
|
||||
SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
|
||||
SILENT_FAILS_OFF: 29 // SILENT_FAILS_OFF
|
||||
};
|
||||
|
||||
module.exports = opcodes;
|
@ -1,618 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let asts = require("../asts");
|
||||
let js = require("../js");
|
||||
let op = require("../opcodes");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Generates bytecode.
|
||||
//
|
||||
// Instructions
|
||||
// ============
|
||||
//
|
||||
// Stack Manipulation
|
||||
// ------------------
|
||||
//
|
||||
// [0] PUSH c
|
||||
//
|
||||
// stack.push(consts[c]);
|
||||
//
|
||||
// [1] PUSH_UNDEFINED
|
||||
//
|
||||
// stack.push(undefined);
|
||||
//
|
||||
// [2] PUSH_NULL
|
||||
//
|
||||
// stack.push(null);
|
||||
//
|
||||
// [3] PUSH_FAILED
|
||||
//
|
||||
// stack.push(FAILED);
|
||||
//
|
||||
// [4] PUSH_EMPTY_ARRAY
|
||||
//
|
||||
// stack.push([]);
|
||||
//
|
||||
// [5] PUSH_CURR_POS
|
||||
//
|
||||
// stack.push(currPos);
|
||||
//
|
||||
// [6] POP
|
||||
//
|
||||
// stack.pop();
|
||||
//
|
||||
// [7] POP_CURR_POS
|
||||
//
|
||||
// currPos = stack.pop();
|
||||
//
|
||||
// [8] POP_N n
|
||||
//
|
||||
// stack.pop(n);
|
||||
//
|
||||
// [9] NIP
|
||||
//
|
||||
// value = stack.pop();
|
||||
// stack.pop();
|
||||
// stack.push(value);
|
||||
//
|
||||
// [10] APPEND
|
||||
//
|
||||
// value = stack.pop();
|
||||
// array = stack.pop();
|
||||
// array.push(value);
|
||||
// stack.push(array);
|
||||
//
|
||||
// [11] WRAP n
|
||||
//
|
||||
// stack.push(stack.pop(n));
|
||||
//
|
||||
// [12] TEXT
|
||||
//
|
||||
// stack.push(input.substring(stack.pop(), currPos));
|
||||
//
|
||||
// Conditions and Loops
|
||||
// --------------------
|
||||
//
|
||||
// [13] IF t, f
|
||||
//
|
||||
// if (stack.top()) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [14] IF_ERROR t, f
|
||||
//
|
||||
// if (stack.top() === FAILED) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [15] IF_NOT_ERROR t, f
|
||||
//
|
||||
// if (stack.top() !== FAILED) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [16] WHILE_NOT_ERROR b
|
||||
//
|
||||
// while(stack.top() !== FAILED) {
|
||||
// interpret(ip + 2, ip + 2 + b);
|
||||
// }
|
||||
//
|
||||
// Matching
|
||||
// --------
|
||||
//
|
||||
// [17] MATCH_ANY a, f, ...
|
||||
//
|
||||
// if (input.length > currPos) {
|
||||
// interpret(ip + 3, ip + 3 + a);
|
||||
// } else {
|
||||
// interpret(ip + 3 + a, ip + 3 + a + f);
|
||||
// }
|
||||
//
|
||||
// [18] MATCH_STRING s, a, f, ...
|
||||
//
|
||||
// if (input.substr(currPos, consts[s].length) === consts[s]) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [19] MATCH_STRING_IC s, a, f, ...
|
||||
//
|
||||
// if (input.substr(currPos, consts[s].length).toLowerCase() === consts[s]) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [20] MATCH_REGEXP r, a, f, ...
|
||||
//
|
||||
// if (consts[r].test(input.charAt(currPos))) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [21] ACCEPT_N n
|
||||
//
|
||||
// stack.push(input.substring(currPos, n));
|
||||
// currPos += n;
|
||||
//
|
||||
// [22] ACCEPT_STRING s
|
||||
//
|
||||
// stack.push(consts[s]);
|
||||
// currPos += consts[s].length;
|
||||
//
|
||||
// [23] FAIL e
|
||||
//
|
||||
// stack.push(FAILED);
|
||||
// fail(consts[e]);
|
||||
//
|
||||
// Calls
|
||||
// -----
|
||||
//
|
||||
// [24] LOAD_SAVED_POS p
|
||||
//
|
||||
// savedPos = stack[p];
|
||||
//
|
||||
// [25] UPDATE_SAVED_POS
|
||||
//
|
||||
// savedPos = currPos;
|
||||
//
|
||||
// [26] CALL f, n, pc, p1, p2, ..., pN
|
||||
//
|
||||
// value = consts[f](stack[p1], ..., stack[pN]);
|
||||
// stack.pop(n);
|
||||
// stack.push(value);
|
||||
//
|
||||
// Rules
|
||||
// -----
|
||||
//
|
||||
// [27] RULE r
|
||||
//
|
||||
// stack.push(parseRule(r));
|
||||
//
|
||||
// Failure Reporting
|
||||
// -----------------
|
||||
//
|
||||
// [28] SILENT_FAILS_ON
|
||||
//
|
||||
// silentFails++;
|
||||
//
|
||||
// [29] SILENT_FAILS_OFF
|
||||
//
|
||||
// silentFails--;
|
||||
function generateBytecode(ast) {
|
||||
let consts = [];
|
||||
|
||||
function addConst(value) {
|
||||
let index = consts.indexOf(value);
|
||||
|
||||
return index === -1 ? consts.push(value) - 1 : index;
|
||||
}
|
||||
|
||||
function addFunctionConst(params, code) {
|
||||
return addConst(
|
||||
"function(" + params.join(", ") + ") {" + code + "}"
|
||||
);
|
||||
}
|
||||
|
||||
function cloneEnv(env) {
|
||||
let clone = {};
|
||||
|
||||
Object.keys(env).forEach(name => {
|
||||
clone[name] = env[name];
|
||||
});
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
function buildSequence() {
|
||||
return Array.prototype.concat.apply([], arguments);
|
||||
}
|
||||
|
||||
function buildCondition(condCode, thenCode, elseCode) {
|
||||
return condCode.concat(
|
||||
[thenCode.length, elseCode.length],
|
||||
thenCode,
|
||||
elseCode
|
||||
);
|
||||
}
|
||||
|
||||
function buildLoop(condCode, bodyCode) {
|
||||
return condCode.concat([bodyCode.length], bodyCode);
|
||||
}
|
||||
|
||||
function buildCall(functionIndex, delta, env, sp) {
|
||||
let params = Object.keys(env).map(name => sp - env[name]);
|
||||
|
||||
return [op.CALL, functionIndex, delta, params.length].concat(params);
|
||||
}
|
||||
|
||||
function buildSimplePredicate(expression, negative, context) {
|
||||
return buildSequence(
|
||||
[op.PUSH_CURR_POS],
|
||||
[op.SILENT_FAILS_ON],
|
||||
generate(expression, {
|
||||
sp: context.sp + 1,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
}),
|
||||
[op.SILENT_FAILS_OFF],
|
||||
buildCondition(
|
||||
[negative ? op.IF_ERROR : op.IF_NOT_ERROR],
|
||||
buildSequence(
|
||||
[op.POP],
|
||||
[negative ? op.POP : op.POP_CURR_POS],
|
||||
[op.PUSH_UNDEFINED]
|
||||
),
|
||||
buildSequence(
|
||||
[op.POP],
|
||||
[negative ? op.POP_CURR_POS : op.POP],
|
||||
[op.PUSH_FAILED]
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function buildSemanticPredicate(code, negative, context) {
|
||||
let functionIndex = addFunctionConst(Object.keys(context.env), code);
|
||||
|
||||
return buildSequence(
|
||||
[op.UPDATE_SAVED_POS],
|
||||
buildCall(functionIndex, 0, context.env, context.sp),
|
||||
buildCondition(
|
||||
[op.IF],
|
||||
buildSequence(
|
||||
[op.POP],
|
||||
negative ? [op.PUSH_FAILED] : [op.PUSH_UNDEFINED]
|
||||
),
|
||||
buildSequence(
|
||||
[op.POP],
|
||||
negative ? [op.PUSH_UNDEFINED] : [op.PUSH_FAILED]
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function buildAppendLoop(expressionCode) {
|
||||
return buildLoop(
|
||||
[op.WHILE_NOT_ERROR],
|
||||
buildSequence([op.APPEND], expressionCode)
|
||||
);
|
||||
}
|
||||
|
||||
let generate = visitor.build({
|
||||
grammar(node) {
|
||||
node.rules.forEach(generate);
|
||||
|
||||
node.consts = consts;
|
||||
},
|
||||
|
||||
rule(node) {
|
||||
node.bytecode = generate(node.expression, {
|
||||
sp: -1, // stack pointer
|
||||
env: { }, // mapping of label names to stack positions
|
||||
action: null // action nodes pass themselves to children here
|
||||
});
|
||||
},
|
||||
|
||||
named(node, context) {
|
||||
let nameIndex = addConst(
|
||||
"peg$otherExpectation(\"" + js.stringEscape(node.name) + "\")"
|
||||
);
|
||||
|
||||
// The code generated below is slightly suboptimal because |FAIL| pushes
|
||||
// to the stack, so we need to stick a |POP| in front of it. We lack a
|
||||
// dedicated instruction that would just report the failure and not touch
|
||||
// the stack.
|
||||
return buildSequence(
|
||||
[op.SILENT_FAILS_ON],
|
||||
generate(node.expression, context),
|
||||
[op.SILENT_FAILS_OFF],
|
||||
buildCondition([op.IF_ERROR], [op.FAIL, nameIndex], [])
|
||||
);
|
||||
},
|
||||
|
||||
choice(node, context) {
|
||||
function buildAlternativesCode(alternatives, context) {
|
||||
return buildSequence(
|
||||
generate(alternatives[0], {
|
||||
sp: context.sp,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
}),
|
||||
alternatives.length > 1
|
||||
? buildCondition(
|
||||
[op.IF_ERROR],
|
||||
buildSequence(
|
||||
[op.POP],
|
||||
buildAlternativesCode(alternatives.slice(1), context)
|
||||
),
|
||||
[]
|
||||
)
|
||||
: []
|
||||
);
|
||||
}
|
||||
|
||||
return buildAlternativesCode(node.alternatives, context);
|
||||
},
|
||||
|
||||
action(node, context) {
|
||||
let env = cloneEnv(context.env);
|
||||
let emitCall = node.expression.type !== "sequence"
|
||||
|| node.expression.elements.length === 0;
|
||||
let expressionCode = generate(node.expression, {
|
||||
sp: context.sp + (emitCall ? 1 : 0),
|
||||
env: env,
|
||||
action: node
|
||||
});
|
||||
let functionIndex = addFunctionConst(Object.keys(env), node.code);
|
||||
|
||||
return emitCall
|
||||
? buildSequence(
|
||||
[op.PUSH_CURR_POS],
|
||||
expressionCode,
|
||||
buildCondition(
|
||||
[op.IF_NOT_ERROR],
|
||||
buildSequence(
|
||||
[op.LOAD_SAVED_POS, 1],
|
||||
buildCall(functionIndex, 1, env, context.sp + 2)
|
||||
),
|
||||
[]
|
||||
),
|
||||
[op.NIP]
|
||||
)
|
||||
: expressionCode;
|
||||
},
|
||||
|
||||
sequence(node, context) {
|
||||
function buildElementsCode(elements, context) {
|
||||
if (elements.length > 0) {
|
||||
let processedCount = node.elements.length - elements.slice(1).length;
|
||||
|
||||
return buildSequence(
|
||||
generate(elements[0], {
|
||||
sp: context.sp,
|
||||
env: context.env,
|
||||
action: null
|
||||
}),
|
||||
buildCondition(
|
||||
[op.IF_NOT_ERROR],
|
||||
buildElementsCode(elements.slice(1), {
|
||||
sp: context.sp + 1,
|
||||
env: context.env,
|
||||
action: context.action
|
||||
}),
|
||||
buildSequence(
|
||||
processedCount > 1 ? [op.POP_N, processedCount] : [op.POP],
|
||||
[op.POP_CURR_POS],
|
||||
[op.PUSH_FAILED]
|
||||
)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
if (context.action) {
|
||||
let functionIndex = addFunctionConst(
|
||||
Object.keys(context.env),
|
||||
context.action.code
|
||||
);
|
||||
|
||||
return buildSequence(
|
||||
[op.LOAD_SAVED_POS, node.elements.length],
|
||||
buildCall(
|
||||
functionIndex,
|
||||
node.elements.length,
|
||||
context.env,
|
||||
context.sp
|
||||
),
|
||||
[op.NIP]
|
||||
);
|
||||
} else {
|
||||
return buildSequence([op.WRAP, node.elements.length], [op.NIP]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buildSequence(
|
||||
[op.PUSH_CURR_POS],
|
||||
buildElementsCode(node.elements, {
|
||||
sp: context.sp + 1,
|
||||
env: context.env,
|
||||
action: context.action
|
||||
})
|
||||
);
|
||||
},
|
||||
|
||||
labeled(node, context) {
|
||||
let env = cloneEnv(context.env);
|
||||
|
||||
context.env[node.label] = context.sp + 1;
|
||||
|
||||
return generate(node.expression, {
|
||||
sp: context.sp,
|
||||
env: env,
|
||||
action: null
|
||||
});
|
||||
},
|
||||
|
||||
text(node, context) {
|
||||
return buildSequence(
|
||||
[op.PUSH_CURR_POS],
|
||||
generate(node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
}),
|
||||
buildCondition(
|
||||
[op.IF_NOT_ERROR],
|
||||
buildSequence([op.POP], [op.TEXT]),
|
||||
[op.NIP]
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
simple_and(node, context) {
|
||||
return buildSimplePredicate(node.expression, false, context);
|
||||
},
|
||||
|
||||
simple_not(node, context) {
|
||||
return buildSimplePredicate(node.expression, true, context);
|
||||
},
|
||||
|
||||
optional(node, context) {
|
||||
return buildSequence(
|
||||
generate(node.expression, {
|
||||
sp: context.sp,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
}),
|
||||
buildCondition(
|
||||
[op.IF_ERROR],
|
||||
buildSequence([op.POP], [op.PUSH_NULL]),
|
||||
[]
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
zero_or_more(node, context) {
|
||||
let expressionCode = generate(node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
});
|
||||
|
||||
return buildSequence(
|
||||
[op.PUSH_EMPTY_ARRAY],
|
||||
expressionCode,
|
||||
buildAppendLoop(expressionCode),
|
||||
[op.POP]
|
||||
);
|
||||
},
|
||||
|
||||
one_or_more(node, context) {
|
||||
let expressionCode = generate(node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
});
|
||||
|
||||
return buildSequence(
|
||||
[op.PUSH_EMPTY_ARRAY],
|
||||
expressionCode,
|
||||
buildCondition(
|
||||
[op.IF_NOT_ERROR],
|
||||
buildSequence(buildAppendLoop(expressionCode), [op.POP]),
|
||||
buildSequence([op.POP], [op.POP], [op.PUSH_FAILED])
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
group(node, context) {
|
||||
return generate(node.expression, {
|
||||
sp: context.sp,
|
||||
env: cloneEnv(context.env),
|
||||
action: null
|
||||
});
|
||||
},
|
||||
|
||||
semantic_and(node, context) {
|
||||
return buildSemanticPredicate(node.code, false, context);
|
||||
},
|
||||
|
||||
semantic_not(node, context) {
|
||||
return buildSemanticPredicate(node.code, true, context);
|
||||
},
|
||||
|
||||
rule_ref(node) {
|
||||
return [op.RULE, asts.indexOfRule(ast, node.name)];
|
||||
},
|
||||
|
||||
literal(node) {
|
||||
if (node.value.length > 0) {
|
||||
let stringIndex = addConst("\""
|
||||
+ js.stringEscape(
|
||||
node.ignoreCase ? node.value.toLowerCase() : node.value
|
||||
)
|
||||
+ "\""
|
||||
);
|
||||
let expectedIndex = addConst(
|
||||
"peg$literalExpectation("
|
||||
+ "\"" + js.stringEscape(node.value) + "\", "
|
||||
+ node.ignoreCase
|
||||
+ ")"
|
||||
);
|
||||
|
||||
// For case-sensitive strings the value must match the beginning of the
|
||||
// remaining input exactly. As a result, we can use |ACCEPT_STRING| and
|
||||
// save one |substr| call that would be needed if we used |ACCEPT_N|.
|
||||
return buildCondition(
|
||||
node.ignoreCase
|
||||
? [op.MATCH_STRING_IC, stringIndex]
|
||||
: [op.MATCH_STRING, stringIndex],
|
||||
node.ignoreCase
|
||||
? [op.ACCEPT_N, node.value.length]
|
||||
: [op.ACCEPT_STRING, stringIndex],
|
||||
[op.FAIL, expectedIndex]
|
||||
);
|
||||
} else {
|
||||
let stringIndex = addConst("\"\"");
|
||||
|
||||
return [op.PUSH, stringIndex];
|
||||
}
|
||||
},
|
||||
|
||||
class(node) {
|
||||
let regexp = "/^["
|
||||
+ (node.inverted ? "^" : "")
|
||||
+ node.parts.map(part =>
|
||||
Array.isArray(part)
|
||||
? js.regexpClassEscape(part[0])
|
||||
+ "-"
|
||||
+ js.regexpClassEscape(part[1])
|
||||
: js.regexpClassEscape(part)
|
||||
).join("")
|
||||
+ "]/" + (node.ignoreCase ? "i" : "");
|
||||
let parts = "["
|
||||
+ node.parts.map(part =>
|
||||
Array.isArray(part)
|
||||
? "[\"" + js.stringEscape(part[0]) + "\", \"" + js.stringEscape(part[1]) + "\"]"
|
||||
: "\"" + js.stringEscape(part) + "\""
|
||||
).join(", ")
|
||||
+ "]";
|
||||
let regexpIndex = addConst(regexp);
|
||||
let expectedIndex = addConst(
|
||||
"peg$classExpectation("
|
||||
+ parts + ", "
|
||||
+ node.inverted + ", "
|
||||
+ node.ignoreCase
|
||||
+ ")"
|
||||
);
|
||||
|
||||
return buildCondition(
|
||||
[op.MATCH_REGEXP, regexpIndex],
|
||||
[op.ACCEPT_N, 1],
|
||||
[op.FAIL, expectedIndex]
|
||||
);
|
||||
},
|
||||
|
||||
any() {
|
||||
let expectedIndex = addConst("peg$anyExpectation()");
|
||||
|
||||
return buildCondition(
|
||||
[op.MATCH_ANY],
|
||||
[op.ACCEPT_N, 1],
|
||||
[op.FAIL, expectedIndex]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
generate(ast);
|
||||
}
|
||||
|
||||
module.exports = generateBytecode;
|
File diff suppressed because it is too large
Load Diff
@ -1,39 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Removes proxy rules -- that is, rules that only delegate to other rule.
|
||||
function removeProxyRules(ast, options) {
|
||||
function isProxyRule(node) {
|
||||
return node.type === "rule" && node.expression.type === "rule_ref";
|
||||
}
|
||||
|
||||
function replaceRuleRefs(ast, from, to) {
|
||||
let replace = visitor.build({
|
||||
rule_ref(node) {
|
||||
if (node.name === from) {
|
||||
node.name = to;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
replace(ast);
|
||||
}
|
||||
|
||||
let indices = [];
|
||||
|
||||
ast.rules.forEach((rule, i) => {
|
||||
if (isProxyRule(rule)) {
|
||||
replaceRuleRefs(ast, rule.name, rule.expression.name);
|
||||
if (options.allowedStartRules.indexOf(rule.name) === -1) {
|
||||
indices.push(i);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
indices.reverse();
|
||||
|
||||
indices.forEach(i => { ast.rules.splice(i, 1); });
|
||||
}
|
||||
|
||||
module.exports = removeProxyRules;
|
@ -1,62 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("../../grammar-error");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Checks that each label is defined only once within each scope.
|
||||
function reportDuplicateLabels(ast) {
|
||||
function cloneEnv(env) {
|
||||
let clone = {};
|
||||
|
||||
Object.keys(env).forEach(name => {
|
||||
clone[name] = env[name];
|
||||
});
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
function checkExpressionWithClonedEnv(node, env) {
|
||||
check(node.expression, cloneEnv(env));
|
||||
}
|
||||
|
||||
let check = visitor.build({
|
||||
rule(node) {
|
||||
check(node.expression, { });
|
||||
},
|
||||
|
||||
choice(node, env) {
|
||||
node.alternatives.forEach(alternative => {
|
||||
check(alternative, cloneEnv(env));
|
||||
});
|
||||
},
|
||||
|
||||
action: checkExpressionWithClonedEnv,
|
||||
|
||||
labeled(node, env) {
|
||||
if (Object.prototype.hasOwnProperty.call(env, node.label)) {
|
||||
throw new GrammarError(
|
||||
"Label \"" + node.label + "\" is already defined "
|
||||
+ "at line " + env[node.label].start.line + ", "
|
||||
+ "column " + env[node.label].start.column + ".",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
|
||||
check(node.expression, env);
|
||||
|
||||
env[node.label] = node.location;
|
||||
},
|
||||
|
||||
text: checkExpressionWithClonedEnv,
|
||||
simple_and: checkExpressionWithClonedEnv,
|
||||
simple_not: checkExpressionWithClonedEnv,
|
||||
optional: checkExpressionWithClonedEnv,
|
||||
zero_or_more: checkExpressionWithClonedEnv,
|
||||
one_or_more: checkExpressionWithClonedEnv,
|
||||
group: checkExpressionWithClonedEnv
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportDuplicateLabels;
|
@ -1,28 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("../../grammar-error");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Checks that each rule is defined only once.
|
||||
function reportDuplicateRules(ast) {
|
||||
let rules = {};
|
||||
|
||||
let check = visitor.build({
|
||||
rule(node) {
|
||||
if (Object.prototype.hasOwnProperty.call(rules, node.name)) {
|
||||
throw new GrammarError(
|
||||
"Rule \"" + node.name + "\" is already defined "
|
||||
+ "at line " + rules[node.name].start.line + ", "
|
||||
+ "column " + rules[node.name].start.column + ".",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
|
||||
rules[node.name] = node.location;
|
||||
}
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportDuplicateRules;
|
@ -1,54 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("../../grammar-error");
|
||||
let asts = require("../asts");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Reports left recursion in the grammar, which prevents infinite recursion in
|
||||
// the generated parser.
|
||||
//
|
||||
// Both direct and indirect recursion is detected. The pass also correctly
|
||||
// reports cases like this:
|
||||
//
|
||||
// start = "a"? start
|
||||
//
|
||||
// In general, if a rule reference can be reached without consuming any input,
|
||||
// it can lead to left recursion.
|
||||
function reportInfiniteRecursion(ast) {
|
||||
let visitedRules = [];
|
||||
|
||||
let check = visitor.build({
|
||||
rule(node) {
|
||||
visitedRules.push(node.name);
|
||||
check(node.expression);
|
||||
visitedRules.pop(node.name);
|
||||
},
|
||||
|
||||
sequence(node) {
|
||||
node.elements.every(element => {
|
||||
check(element);
|
||||
|
||||
return !asts.alwaysConsumesOnSuccess(ast, element);
|
||||
});
|
||||
},
|
||||
|
||||
rule_ref(node) {
|
||||
if (visitedRules.indexOf(node.name) !== -1) {
|
||||
visitedRules.push(node.name);
|
||||
|
||||
throw new GrammarError(
|
||||
"Possible infinite loop when parsing (left recursion: "
|
||||
+ visitedRules.join(" -> ")
|
||||
+ ").",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
|
||||
check(asts.findRule(ast, node.name));
|
||||
}
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportInfiniteRecursion;
|
@ -1,33 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("../../grammar-error");
|
||||
let asts = require("../asts");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Reports expressions that don't consume any input inside |*| or |+| in the
|
||||
// grammar, which prevents infinite loops in the generated parser.
|
||||
function reportInfiniteRepetition(ast) {
|
||||
let check = visitor.build({
|
||||
zero_or_more(node) {
|
||||
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
|
||||
throw new GrammarError(
|
||||
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
one_or_more(node) {
|
||||
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
|
||||
throw new GrammarError(
|
||||
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportInfiniteRepetition;
|
@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("../../grammar-error");
|
||||
let asts = require("../asts");
|
||||
let visitor = require("../visitor");
|
||||
|
||||
// Checks that all referenced rules exist.
|
||||
function reportUndefinedRules(ast) {
|
||||
let check = visitor.build({
|
||||
rule_ref(node) {
|
||||
if (!asts.findRule(ast, node.name)) {
|
||||
throw new GrammarError(
|
||||
"Rule \"" + node.name + "\" is not defined.",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportUndefinedRules;
|
@ -1,75 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
// Simple AST node visitor builder.
|
||||
let visitor = {
|
||||
build(functions) {
|
||||
function visit(node) {
|
||||
return functions[node.type].apply(null, arguments);
|
||||
}
|
||||
|
||||
function visitNop() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
function visitExpression(node) {
|
||||
let extraArgs = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
visit.apply(null, [node.expression].concat(extraArgs));
|
||||
}
|
||||
|
||||
function visitChildren(property) {
|
||||
return function(node) {
|
||||
let extraArgs = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
node[property].forEach(child => {
|
||||
visit.apply(null, [child].concat(extraArgs));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
const DEFAULT_FUNCTIONS = {
|
||||
grammar(node) {
|
||||
let extraArgs = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
if (node.initializer) {
|
||||
visit.apply(null, [node.initializer].concat(extraArgs));
|
||||
}
|
||||
|
||||
node.rules.forEach(rule => {
|
||||
visit.apply(null, [rule].concat(extraArgs));
|
||||
});
|
||||
},
|
||||
|
||||
initializer: visitNop,
|
||||
rule: visitExpression,
|
||||
named: visitExpression,
|
||||
choice: visitChildren("alternatives"),
|
||||
action: visitExpression,
|
||||
sequence: visitChildren("elements"),
|
||||
labeled: visitExpression,
|
||||
text: visitExpression,
|
||||
simple_and: visitExpression,
|
||||
simple_not: visitExpression,
|
||||
optional: visitExpression,
|
||||
zero_or_more: visitExpression,
|
||||
one_or_more: visitExpression,
|
||||
group: visitExpression,
|
||||
semantic_and: visitNop,
|
||||
semantic_not: visitNop,
|
||||
rule_ref: visitNop,
|
||||
literal: visitNop,
|
||||
class: visitNop,
|
||||
any: visitNop
|
||||
};
|
||||
|
||||
Object.keys(DEFAULT_FUNCTIONS).forEach(type => {
|
||||
if (!Object.prototype.hasOwnProperty.call(functions, type)) {
|
||||
functions[type] = DEFAULT_FUNCTIONS[type];
|
||||
}
|
||||
});
|
||||
|
||||
return visit;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = visitor;
|
@ -1,16 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
// Thrown when the grammar contains an error.
|
||||
class GrammarError {
|
||||
constructor(message, location) {
|
||||
this.name = "GrammarError";
|
||||
this.message = message;
|
||||
this.location = location;
|
||||
|
||||
if (typeof Error.captureStackTrace === "function") {
|
||||
Error.captureStackTrace(this, GrammarError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GrammarError;
|
File diff suppressed because one or more lines are too long
@ -1,54 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
let GrammarError = require("./grammar-error");
|
||||
let compiler = require("./compiler");
|
||||
let parser = require("./parser");
|
||||
|
||||
let peg = {
|
||||
// PEG.js version (uses semantic versioning).
|
||||
VERSION: "0.10.0",
|
||||
|
||||
GrammarError: GrammarError,
|
||||
parser: parser,
|
||||
compiler: compiler,
|
||||
|
||||
// Generates a parser from a specified grammar and returns it.
|
||||
//
|
||||
// The grammar must be a string in the format described by the metagramar in
|
||||
// the parser.pegjs file.
|
||||
//
|
||||
// Throws |peg.parser.SyntaxError| if the grammar contains a syntax error or
|
||||
// |peg.GrammarError| if it contains a semantic error. Note that not all
|
||||
// errors are detected during the generation and some may protrude to the
|
||||
// generated parser and cause its malfunction.
|
||||
generate(grammar, options) {
|
||||
options = options !== undefined ? options : {};
|
||||
|
||||
function convertPasses(passes) {
|
||||
let converted = {};
|
||||
|
||||
Object.keys(passes).forEach(stage => {
|
||||
converted[stage] = Object.keys(passes[stage])
|
||||
.map(name => passes[stage][name]);
|
||||
});
|
||||
|
||||
return converted;
|
||||
}
|
||||
|
||||
let plugins = "plugins" in options ? options.plugins : [];
|
||||
let config = {
|
||||
parser: peg.parser,
|
||||
passes: convertPasses(peg.compiler.passes)
|
||||
};
|
||||
|
||||
plugins.forEach(p => { p.use(config, options); });
|
||||
|
||||
return peg.compiler.compile(
|
||||
config.parser.parse(grammar),
|
||||
config.passes,
|
||||
options
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = peg;
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
"version": 2,
|
||||
"name": "pegjs",
|
||||
"alias": [
|
||||
"pegjs.org",
|
||||
"www.pegjs.org"
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"handle": "filesystem"
|
||||
},
|
||||
{
|
||||
"src": "/",
|
||||
"dest": "/index.html"
|
||||
},
|
||||
{
|
||||
"src": "/([^\\.*]+)",
|
||||
"dest": "/$1.html"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,71 +1,35 @@
|
||||
{
|
||||
"name": "pegjs",
|
||||
"version": "0.10.0",
|
||||
"description": "Parser generator for JavaScript",
|
||||
"keywords": [
|
||||
"parser generator",
|
||||
"PEG"
|
||||
],
|
||||
"homepage": "https://pegjs.org/",
|
||||
"bugs": "https://github.com/pegjs/pegjs/issues",
|
||||
"license": "MIT",
|
||||
"author": "David Majda <david@majda.cz> (https://majda.cz/)",
|
||||
"files": [
|
||||
"CHANGELOG.md",
|
||||
"LICENSE",
|
||||
"README.md",
|
||||
"bin/pegjs",
|
||||
"examples/arithmetics.pegjs",
|
||||
"examples/css.pegjs",
|
||||
"examples/javascript.pegjs",
|
||||
"examples/json.pegjs",
|
||||
"lib/compiler/asts.js",
|
||||
"lib/compiler/index.js",
|
||||
"lib/compiler/js.js",
|
||||
"lib/compiler/opcodes.js",
|
||||
"lib/compiler/passes/generate-bytecode.js",
|
||||
"lib/compiler/passes/generate-js.js",
|
||||
"lib/compiler/passes/remove-proxy-rules.js",
|
||||
"lib/compiler/passes/report-duplicate-labels.js",
|
||||
"lib/compiler/passes/report-duplicate-rules.js",
|
||||
"lib/compiler/passes/report-infinite-recursion.js",
|
||||
"lib/compiler/passes/report-infinite-repetition.js",
|
||||
"lib/compiler/passes/report-undefined-rules.js",
|
||||
"lib/compiler/visitor.js",
|
||||
"lib/grammar-error.js",
|
||||
"lib/parser.js",
|
||||
"lib/peg.js",
|
||||
"package.json"
|
||||
],
|
||||
"main": "lib/peg",
|
||||
"bin": "bin/pegjs",
|
||||
"repository": "pegjs/pegjs",
|
||||
"scripts": {
|
||||
"test": "gulp"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-preset-es2015": "6.14.0",
|
||||
"babelify": "7.3.0",
|
||||
"browserify": "13.1.0",
|
||||
"chai": "3.5.0",
|
||||
"del": "2.2.2",
|
||||
"eslint-config-dmajda": "1.0.0",
|
||||
"express": "4.14.0",
|
||||
"glob": "7.0.6",
|
||||
"gulp": "3.9.1",
|
||||
"gulp-eslint": "3.0.1",
|
||||
"gulp-header": "1.8.8",
|
||||
"gulp-mocha": "3.0.1",
|
||||
"gulp-rename": "1.2.2",
|
||||
"gulp-transform": "1.0.8",
|
||||
"gulp-uglify": "2.0.0",
|
||||
"morgan": "1.7.0",
|
||||
"run-sequence": "1.2.2",
|
||||
"sinon": "1.17.6",
|
||||
"vinyl-buffer": "1.0.0",
|
||||
"vinyl-source-stream": "1.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
"name": "pegjs-dev",
|
||||
"private": true,
|
||||
"workspaces": [
|
||||
"packages/*",
|
||||
"tools/*",
|
||||
"test",
|
||||
"website"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "eslint --ext .js .",
|
||||
"spec": "mocha --reporter progress",
|
||||
"benchmark": "node tools/benchmark",
|
||||
"build-parser": "pegjs -c src/pegjs.config.js",
|
||||
"build-dist": "bundle",
|
||||
"build-website": "node website/export",
|
||||
"clean": "rimraf public packages/pegjs/dist examples/*.js",
|
||||
"impact": "node tools/impact master",
|
||||
"build": "node website/export --fresh",
|
||||
"test": "nyc mocha",
|
||||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
|
||||
},
|
||||
"dependencies": {
|
||||
"@futagoza/eslint-config": "11.2.0",
|
||||
"@types/node": "8",
|
||||
"codecov": "3.6.1",
|
||||
"eslint": "6.6.0",
|
||||
"mocha": "6.2.2",
|
||||
"nyc": "14.1.1",
|
||||
"rimraf": "3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2010-2016 David Majda
|
||||
Copyright (c) 2017+ Futago-za Ryuu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,35 @@
|
||||
![GitHub Actions](https://github.com/pegjs/pegjs/workflows/Github%20Actions/badge.svg)
|
||||
[![Codecov](https://codecov.io/gh/pegjs/pegjs/branch/master/graph/badge.svg)](https://codecov.io/gh/pegjs/pegjs)
|
||||
[![CodeFactor](https://www.codefactor.io/repository/github/pegjs/pegjs/badge)](https://www.codefactor.io/repository/github/pegjs/pegjs)
|
||||
[![license](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)
|
||||
|
||||
PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily.
|
||||
|
||||
> PEG.js is still very much work in progress. There are no compatibility guarantees until version 1.0
|
||||
|
||||
## features
|
||||
|
||||
* Simple and expressive grammar syntax
|
||||
* Integrates both lexical and syntactical analysis
|
||||
* Parsers have excellent error reporting out of the box
|
||||
* Based on [parsing expression grammar](http://en.wikipedia.org/wiki/Parsing_expression_grammar) formalism - more powerful than traditional LL(*k*) and LR(*k*) parsers
|
||||
* Usable [from your browser](https://pegjs.org/online), from the command line, or via JavaScript API
|
||||
|
||||
## documentation
|
||||
|
||||
- [Getting Started](https://github.com/pegjs/pegjs/blob/master/docs/guides/getting-started.md)
|
||||
- [Installation](https://github.com/pegjs/pegjs/blob/master/docs/guides/installation.md)
|
||||
- [Generating a Parser](https://github.com/pegjs/pegjs/blob/master/docs/guides/generating-a-parser.md)
|
||||
- [Using the Parser](https://github.com/pegjs/pegjs/blob/master/docs/guides/using-the-parser.md)
|
||||
- [Grammar Syntax and Semantics](https://github.com/pegjs/pegjs/tree/master/docs/grammar)
|
||||
- [JavaScript API](https://github.com/pegjs/pegjs/blob/master/docs/guides/javascript-api.md)
|
||||
- [Plugins](https://github.com/pegjs/pegjs/blob/master/docs/guides/plugins.md)
|
||||
- [Compatibility](https://github.com/pegjs/pegjs/blob/master/docs/compatibility.md)
|
||||
- [Development](https://github.com/pegjs/pegjs/blob/master/docs/development.md)
|
||||
|
||||
## license
|
||||
|
||||
Released under the [MIT License](https://opensource.org/licenses/MIT).
|
||||
|
||||
Copyright (c) 2010-2016 [David Majda](https://majda.cz/)<br>
|
||||
Copyright (c) 2017+ [Futago-za Ryuu](https://github.com/futagoza)
|
@ -0,0 +1,350 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require( "fs" );
|
||||
const path = require( "path" );
|
||||
const peg = require( "../lib/peg" );
|
||||
const util = peg.util;
|
||||
|
||||
// Options
|
||||
|
||||
let inputFile = null;
|
||||
let outputFile = null;
|
||||
let options = {
|
||||
"--": [],
|
||||
"cache": false,
|
||||
"dependencies": {},
|
||||
"exportVar": null,
|
||||
"format": "commonjs",
|
||||
"optimize": "speed",
|
||||
"output": "source",
|
||||
"parser": {},
|
||||
"plugins": [],
|
||||
"trace": false,
|
||||
};
|
||||
|
||||
const EXPORT_VAR_FORMATS = [ "globals", "umd" ];
|
||||
const DEPENDENCY_FORMATS = [ "amd", "commonjs", "es", "umd" ];
|
||||
const MODULE_FORMATS = [ "amd", "bare", "commonjs", "es", "globals", "umd" ];
|
||||
const OPTIMIZATION_GOALS = [ "size", "speed" ];
|
||||
|
||||
// Helpers
|
||||
|
||||
function abort( message ) {
|
||||
|
||||
console.error( message );
|
||||
process.exit( 1 );
|
||||
|
||||
}
|
||||
|
||||
function addExtraOptions( config ) {
|
||||
|
||||
if ( typeof config === "string" ) {
|
||||
|
||||
try {
|
||||
|
||||
config = JSON.parse( config );
|
||||
|
||||
} catch ( e ) {
|
||||
|
||||
if ( ! ( e instanceof SyntaxError ) ) throw e;
|
||||
abort( "Error parsing JSON: " + e.message );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if ( typeof config !== "object" ) {
|
||||
|
||||
abort( "The JSON with extra options has to represent an object." );
|
||||
|
||||
}
|
||||
|
||||
if ( config.input !== null || config.output !== null ) {
|
||||
|
||||
// We don't want to touch the orignal config, just in case it comes from
|
||||
// a javascript file, in which case its possible the same object is used
|
||||
// for something else, somewhere else.
|
||||
config = util.clone( config );
|
||||
const { input, output } = config;
|
||||
|
||||
if ( input !== null ) {
|
||||
|
||||
if ( typeof input !== "string" )
|
||||
|
||||
abort( "The option `input` must be a string." );
|
||||
|
||||
if ( inputFile !== null )
|
||||
|
||||
abort( `The input file is already set, cannot use: "${ input }".` );
|
||||
|
||||
inputFile = input;
|
||||
delete config.input;
|
||||
|
||||
}
|
||||
|
||||
if ( output !== null ) {
|
||||
|
||||
if ( typeof output !== "string" )
|
||||
|
||||
abort( "The option `output` must be a string." );
|
||||
|
||||
outputFile = output;
|
||||
delete config.output;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
options = util.processOptions( config, options );
|
||||
|
||||
}
|
||||
|
||||
function formatChoicesList( list ) {
|
||||
|
||||
list = list.map( entry => `"${ entry }"` );
|
||||
const lastOption = list.pop();
|
||||
|
||||
return list.length === 0
|
||||
? lastOption
|
||||
: list.join( ", " ) + " or " + lastOption;
|
||||
|
||||
}
|
||||
|
||||
function updateList( list, string ) {
|
||||
|
||||
string
|
||||
.split( "," )
|
||||
.forEach( entry => {
|
||||
|
||||
entry = entry.trim();
|
||||
if ( list.indexOf( entry ) === -1 ) {
|
||||
|
||||
list.push( entry );
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
// Arguments
|
||||
|
||||
let args = process.argv.slice( 2 );
|
||||
|
||||
function nextArg( option ) {
|
||||
|
||||
if ( args.length === 0 ) {
|
||||
|
||||
abort( `Missing parameter of the ${ option } option.` );
|
||||
|
||||
}
|
||||
return args.shift();
|
||||
|
||||
}
|
||||
|
||||
// Parse Arguments
|
||||
|
||||
while ( args.length > 0 ) {
|
||||
|
||||
let config, mod;
|
||||
let argument = args.shift();
|
||||
|
||||
if ( argument.indexOf( "-" ) === 0 && argument.indexOf( "=" ) > 1 ) {
|
||||
|
||||
argument = argument.split( "=" );
|
||||
args.unshift( argument.length > 2 ? argument.slice( 1 ) : argument[ 1 ] );
|
||||
argument = argument[ 0 ];
|
||||
|
||||
}
|
||||
|
||||
switch ( argument ) {
|
||||
|
||||
case "--":
|
||||
options[ "--" ] = args;
|
||||
args = [];
|
||||
break;
|
||||
|
||||
case "-a":
|
||||
case "--allowed-start-rules":
|
||||
if ( ! options.allowedStartRules ) options.allowedStartRules = [];
|
||||
updateList( options.allowedStartRules, nextArg( "--allowed-start-rules" ) );
|
||||
break;
|
||||
|
||||
case "--cache":
|
||||
options.cache = true;
|
||||
break;
|
||||
|
||||
case "--no-cache":
|
||||
options.cache = false;
|
||||
break;
|
||||
|
||||
case "-d":
|
||||
case "--dependency":
|
||||
argument = nextArg( "-d/--dependency" );
|
||||
mod = argument.split( ":" );
|
||||
|
||||
if ( mod.length === 1 ) mod = [ argument, argument ];
|
||||
else if ( mod.length > 2 ) mod[ 1 ] = mod.slice( 1 );
|
||||
|
||||
options.dependencies[ mod[ 0 ] ] = mod[ 1 ];
|
||||
break;
|
||||
|
||||
case "-e":
|
||||
case "--export-var":
|
||||
options.exportVar = nextArg( "-e/--export-var" );
|
||||
break;
|
||||
|
||||
case "--extra-options":
|
||||
addExtraOptions( nextArg( "--extra-options" ) );
|
||||
break;
|
||||
|
||||
case "-c":
|
||||
case "--config":
|
||||
case "--extra-options-file":
|
||||
argument = nextArg( "-c/--config/--extra-options-file" );
|
||||
if ( path.extname( argument ) === ".js" ) {
|
||||
|
||||
config = require( path.resolve( argument ) );
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
|
||||
config = fs.readFileSync( argument, "utf8" );
|
||||
|
||||
} catch ( e ) {
|
||||
|
||||
abort( `Can't read from file "${ argument }".` );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
addExtraOptions( config );
|
||||
break;
|
||||
|
||||
case "-f":
|
||||
case "--format":
|
||||
argument = nextArg( "-f/--format" );
|
||||
if ( MODULE_FORMATS.indexOf( argument ) === -1 ) {
|
||||
|
||||
abort( `Module format must be either ${ formatChoicesList( MODULE_FORMATS ) }.` );
|
||||
|
||||
}
|
||||
options.format = argument;
|
||||
break;
|
||||
|
||||
case "-h":
|
||||
case "--help":
|
||||
console.log( require( "./usage" ) );
|
||||
process.exit();
|
||||
break;
|
||||
|
||||
case "-O":
|
||||
case "--optimize":
|
||||
argument = nextArg( "-O/--optimize" );
|
||||
if ( OPTIMIZATION_GOALS.indexOf( argument ) === -1 ) {
|
||||
|
||||
abort( `Optimization goal must be either ${ formatChoicesList( OPTIMIZATION_GOALS ) }.` );
|
||||
|
||||
}
|
||||
options.optimize = argument;
|
||||
break;
|
||||
|
||||
case "-o":
|
||||
case "--output":
|
||||
outputFile = nextArg( "-o/--output" );
|
||||
break;
|
||||
|
||||
case "-p":
|
||||
case "--plugin":
|
||||
argument = nextArg( "-p/--plugin" );
|
||||
try {
|
||||
|
||||
mod = require( argument );
|
||||
|
||||
} catch ( ex1 ) {
|
||||
|
||||
if ( ex1.code !== "MODULE_NOT_FOUND" ) throw ex1;
|
||||
try {
|
||||
|
||||
mod = require( path.resolve( argument ) );
|
||||
|
||||
} catch ( ex2 ) {
|
||||
|
||||
if ( ex2.code !== "MODULE_NOT_FOUND" ) throw ex2;
|
||||
abort( `Can't load module "${ argument }".` );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
options.plugins.push( mod );
|
||||
break;
|
||||
|
||||
case "--trace":
|
||||
options.trace = true;
|
||||
break;
|
||||
|
||||
case "--no-trace":
|
||||
options.trace = false;
|
||||
break;
|
||||
|
||||
case "-v":
|
||||
case "--version":
|
||||
console.log( "PEG.js v" + peg.VERSION );
|
||||
process.exit();
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( inputFile !== null ) {
|
||||
|
||||
abort( `Unknown option: "${ argument }".` );
|
||||
|
||||
}
|
||||
inputFile = argument;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Validation and defaults
|
||||
|
||||
if ( Object.keys( options.dependencies ).length > 0 ) {
|
||||
|
||||
if ( DEPENDENCY_FORMATS.indexOf( options.format ) === -1 ) {
|
||||
|
||||
abort( `Can't use the -d/--dependency option with the "${ options.format }" module format.` );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( options.exportVar !== null ) {
|
||||
|
||||
if ( EXPORT_VAR_FORMATS.indexOf( options.format ) === -1 ) {
|
||||
|
||||
abort( `Can't use the -e/--export-var option with the "${ options.format }" module format.` );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( inputFile === null ) inputFile = "-";
|
||||
|
||||
if ( outputFile === null ) {
|
||||
|
||||
if ( inputFile === "-" ) outputFile = "-";
|
||||
else if ( inputFile ) {
|
||||
|
||||
outputFile = inputFile
|
||||
.substr( 0, inputFile.length - path.extname( inputFile ).length )
|
||||
+ ".js";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Export
|
||||
|
||||
options.inputFile = inputFile;
|
||||
options.outputFile = outputFile;
|
||||
|
||||
module.exports = options;
|
@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
const fs = require( "fs" );
|
||||
const peg = require( "../lib/peg" );
|
||||
const options = require( "./options" );
|
||||
|
||||
// Helpers
|
||||
|
||||
function readStream( inputStream, callback ) {
|
||||
|
||||
let input = "";
|
||||
inputStream.on( "data", data => {
|
||||
|
||||
input += data;
|
||||
|
||||
} );
|
||||
inputStream.on( "end", () => {
|
||||
|
||||
callback( input );
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
function closeStream( stream ) {
|
||||
|
||||
if ( stream !== process.stdin || stream !== process.stdout ) stream.end();
|
||||
|
||||
}
|
||||
|
||||
function abort( message ) {
|
||||
|
||||
console.error( message );
|
||||
process.exit( 1 );
|
||||
|
||||
}
|
||||
|
||||
// Main
|
||||
|
||||
let inputStream, outputStream, orignalContent;
|
||||
|
||||
const inputFile = options.inputFile;
|
||||
const outputFile = options.outputFile;
|
||||
options.parser = options.parser || {};
|
||||
|
||||
if ( inputFile === "-" ) {
|
||||
|
||||
process.stdin.resume();
|
||||
inputStream = process.stdin;
|
||||
inputStream.on( "error", () => {
|
||||
|
||||
abort( `Can't read from file "${ inputFile }".` );
|
||||
|
||||
} );
|
||||
|
||||
} else {
|
||||
|
||||
inputStream = fs.createReadStream( inputFile );
|
||||
options.parser.filename = inputFile;
|
||||
|
||||
}
|
||||
|
||||
if ( outputFile === "-" ) {
|
||||
|
||||
outputStream = process.stdout;
|
||||
|
||||
} else {
|
||||
|
||||
if ( fs.existsSync( outputFile ) ) {
|
||||
|
||||
orignalContent = fs.readFileSync( outputFile, "utf8" );
|
||||
|
||||
}
|
||||
|
||||
outputStream = fs.createWriteStream( outputFile );
|
||||
outputStream.on( "error", () => {
|
||||
|
||||
abort( `Can't write to file "${ outputFile }".` );
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
readStream( inputStream, input => {
|
||||
|
||||
let location, source;
|
||||
|
||||
try {
|
||||
|
||||
source = peg.generate( input, options );
|
||||
|
||||
} catch ( e ) {
|
||||
|
||||
if ( typeof e.location === "object" ) {
|
||||
|
||||
location = e.location.start;
|
||||
if ( typeof location === "object" ) {
|
||||
|
||||
return abort( location.line + ":" + location.column + ": " + e.message );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( orignalContent ) {
|
||||
|
||||
closeStream( outputStream );
|
||||
fs.writeFileSync( outputFile, orignalContent, "utf8" );
|
||||
|
||||
}
|
||||
|
||||
return abort( e.message );
|
||||
|
||||
}
|
||||
|
||||
outputStream.write( source );
|
||||
closeStream( outputStream );
|
||||
|
||||
} );
|
@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = `
|
||||
Usage: pegjs [options] [<input_file>] [--]
|
||||
|
||||
Options:
|
||||
|
||||
-a, --allowed-start-rules <rules> Comma-separated list of rules the generated
|
||||
parser will be allowed to start parsing from
|
||||
(default: first rule in the grammar)
|
||||
(note: repeatable option)
|
||||
|
||||
--cache Make generated parser cache results
|
||||
|
||||
-c, --config <file> Aliases for "--extra-options-file"
|
||||
|
||||
-d, --dependency <dependency> Use specified dependency
|
||||
(note: repeatable option)
|
||||
|
||||
-e, --export-var <variable> Name of a global variable into which the
|
||||
parser object is assigned to when no
|
||||
module loader is detected
|
||||
|
||||
--extra-options <options> A string with additional options (in JSON
|
||||
format) to pass to peg.generate
|
||||
(note: repeatable option)
|
||||
|
||||
--extra-options-file <file> File with additional options (in JSON
|
||||
or JavaScript) to pass to peg.generate
|
||||
(note: repeatable option)
|
||||
|
||||
-f, --format <format> Format of the generated parser:
|
||||
amd, bare, commonjs, es, globals, umd
|
||||
(default: commonjs)
|
||||
|
||||
-h, --help Print help and exit
|
||||
|
||||
--no-cache Generated parser doesn't cache results
|
||||
(default behavior)
|
||||
|
||||
--no-trace Disable tracing in generated parser
|
||||
(default behavior)
|
||||
|
||||
-O, --optimize <goal> Select optimization for speed or size
|
||||
(default: speed)
|
||||
|
||||
-o, --output <file> Output file
|
||||
|
||||
-p, --plugin <plugin> Use a specified plugin
|
||||
(note: repeatable option)
|
||||
|
||||
--trace Enable tracing in generated parser
|
||||
|
||||
-v, --version Print version information and exit
|
||||
`;
|
@ -0,0 +1,118 @@
|
||||
"use strict";
|
||||
|
||||
const Node = require( "./Node" );
|
||||
const visitor = require( "./visitor" );
|
||||
const util = require( "../util" );
|
||||
|
||||
class Grammar extends Node {
|
||||
|
||||
// Creates a new AST
|
||||
constructor( initializer, rules, comments, location ) {
|
||||
|
||||
super( "grammar", location );
|
||||
|
||||
this.initializer = initializer;
|
||||
this.comments = comments;
|
||||
this.rules = rules;
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
this._alwaysConsumesOnSuccess = new AlwaysConsumesOnSuccess( this );
|
||||
|
||||
}
|
||||
|
||||
alwaysConsumesOnSuccess( node ) {
|
||||
|
||||
return this._alwaysConsumesOnSuccess.visit( node );
|
||||
|
||||
}
|
||||
|
||||
findRule( name ) {
|
||||
|
||||
return util.find( this.rules, rule => rule.name === name );
|
||||
|
||||
}
|
||||
|
||||
indexOfRule( name ) {
|
||||
|
||||
return util.findIndex( this.rules, rule => rule.name === name );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Grammar;
|
||||
|
||||
/* ***************************** @private ***************************** */
|
||||
|
||||
class AlwaysConsumesOnSuccess extends visitor.ASTVisitor {
|
||||
|
||||
constructor( ast ) {
|
||||
|
||||
super();
|
||||
this.ast = ast;
|
||||
|
||||
}
|
||||
|
||||
choice( node ) {
|
||||
|
||||
return node.alternatives.every( this.visit, this );
|
||||
|
||||
}
|
||||
|
||||
sequence( node ) {
|
||||
|
||||
return node.elements.some( this.visit, this );
|
||||
|
||||
}
|
||||
|
||||
rule_ref( node ) {
|
||||
|
||||
return this.visit( this.ast.findRule( node.name ) );
|
||||
|
||||
}
|
||||
|
||||
literal( node ) {
|
||||
|
||||
return node.value !== "";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function consumesTrue() {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
function consumesFalse() {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
function consumesExpression( node ) {
|
||||
|
||||
return this.visit( node.expression );
|
||||
|
||||
}
|
||||
|
||||
util.extend( AlwaysConsumesOnSuccess.prototype, {
|
||||
|
||||
rule: consumesExpression,
|
||||
named: consumesExpression,
|
||||
action: consumesExpression,
|
||||
labeled: consumesExpression,
|
||||
text: consumesExpression,
|
||||
simple_and: consumesFalse,
|
||||
simple_not: consumesFalse,
|
||||
optional: consumesFalse,
|
||||
zero_or_more: consumesFalse,
|
||||
one_or_more: consumesExpression,
|
||||
group: consumesExpression,
|
||||
semantic_and: consumesFalse,
|
||||
semantic_not: consumesFalse,
|
||||
class: consumesTrue,
|
||||
any: consumesTrue,
|
||||
|
||||
} );
|
@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
class Node {
|
||||
|
||||
constructor( type, location ) {
|
||||
|
||||
this.type = type;
|
||||
this.location = location;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Node;
|
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
exports.visitor = require( "./visitor" );
|
||||
exports.Node = require( "./Node" );
|
||||
exports.Grammar = require( "./Grammar" );
|
@ -0,0 +1,141 @@
|
||||
"use strict";
|
||||
|
||||
const util = require( "../util" );
|
||||
|
||||
// Abstract syntax tree visitor for PEG.js
|
||||
class ASTVisitor {
|
||||
|
||||
// Will traverse the node, strictly assuming the visitor can handle the node type.
|
||||
visit( node ) {
|
||||
|
||||
// istanbul ignore next
|
||||
if ( ! node ) throw new Error( "Visitor function called with no arguments or a `falsy` node" );
|
||||
|
||||
const fn = this[ node.type ];
|
||||
|
||||
// istanbul ignore next
|
||||
if ( ! fn ) throw new Error( `Visitor function for node type "${ node.type }" not defined` );
|
||||
|
||||
return fn.apply( this, arguments ); // eslint-disable-line prefer-rest-params
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
ASTVisitor,
|
||||
|
||||
// Simple AST node visitor builder for PEG.js
|
||||
build( functions ) {
|
||||
|
||||
let visitor = new ASTVisitor();
|
||||
util.extend( visitor, functions );
|
||||
visitor = util.enforceFastProperties( visitor );
|
||||
return visitor.visit.bind( visitor );
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
// Helper's to create visitor's for use with the ASTVisitor class
|
||||
const on = ASTVisitor.on = {
|
||||
|
||||
// Visit a node that is defined as a property on another node
|
||||
property( name ) {
|
||||
|
||||
return function visitProperty( node, ...extraArgs ) {
|
||||
|
||||
const value = node[ name ];
|
||||
|
||||
if ( extraArgs.length )
|
||||
|
||||
this.visit( value, ...extraArgs );
|
||||
|
||||
else
|
||||
|
||||
this.visit( value );
|
||||
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
// Visit an array of nodes that are defined as a property on another node
|
||||
children( name ) {
|
||||
|
||||
return function visitProperty( node, ...extraArgs ) {
|
||||
|
||||
const children = node[ name ];
|
||||
const visitor = this;
|
||||
|
||||
const cb = extraArgs.length < 1
|
||||
? function withoutArgs( child ) {
|
||||
|
||||
visitor.visit( child );
|
||||
|
||||
}
|
||||
: function withArgs( child ) {
|
||||
|
||||
visitor.visit( child, ...extraArgs );
|
||||
|
||||
};
|
||||
|
||||
children.forEach( cb );
|
||||
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
// Build the default ast visitor functions
|
||||
|
||||
const visitNop = util.noop;
|
||||
const visitExpression = on.property( "expression" );
|
||||
|
||||
const DEFAULT_FUNCTIONS = {
|
||||
|
||||
grammar( node, ...extraArgs ) {
|
||||
|
||||
if ( node.initializer ) {
|
||||
|
||||
this.visit( node.initializer, ...extraArgs );
|
||||
|
||||
}
|
||||
|
||||
node.rules.forEach( rule => {
|
||||
|
||||
this.visit( rule, ...extraArgs );
|
||||
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
initializer: visitNop,
|
||||
rule: visitExpression,
|
||||
named: visitExpression,
|
||||
choice: on.children( "alternatives" ),
|
||||
action: visitExpression,
|
||||
sequence: on.children( "elements" ),
|
||||
labeled: visitExpression,
|
||||
text: visitExpression,
|
||||
simple_and: visitExpression,
|
||||
simple_not: visitExpression,
|
||||
optional: visitExpression,
|
||||
zero_or_more: visitExpression,
|
||||
one_or_more: visitExpression,
|
||||
group: visitExpression,
|
||||
semantic_and: visitNop,
|
||||
semantic_not: visitNop,
|
||||
rule_ref: visitNop,
|
||||
literal: visitNop,
|
||||
class: visitNop,
|
||||
any: visitNop,
|
||||
|
||||
};
|
||||
|
||||
util.each( DEFAULT_FUNCTIONS, ( fn, name ) => {
|
||||
|
||||
ASTVisitor.prototype[ name ] = fn;
|
||||
|
||||
} );
|
@ -0,0 +1,98 @@
|
||||
"use strict";
|
||||
|
||||
const calcReportFailures = require( "./passes/calc-report-failures" );
|
||||
const generateBytecode = require( "./passes/generate-bytecode" );
|
||||
const generateJS = require( "./passes/generate-js" );
|
||||
const removeProxyRules = require( "./passes/remove-proxy-rules" );
|
||||
const reportDuplicateLabels = require( "./passes/report-duplicate-labels" );
|
||||
const reportDuplicateRules = require( "./passes/report-duplicate-rules" );
|
||||
const reportUnusedRules = require( "./passes/report-unused-rules" );
|
||||
const reportInfiniteRecursion = require( "./passes/report-infinite-recursion" );
|
||||
const reportInfiniteRepetition = require( "./passes/report-infinite-repetition" );
|
||||
const reportUndefinedRules = require( "./passes/report-undefined-rules" );
|
||||
const inferenceMatchResult = require( "./passes/inference-match-result" );
|
||||
const reportIncorrectPlucking = require( "./passes/report-incorrect-plucking" );
|
||||
const Session = require( "./session" );
|
||||
const util = require( "../util" );
|
||||
|
||||
const compiler = {
|
||||
|
||||
Session: Session,
|
||||
|
||||
// Compiler passes.
|
||||
//
|
||||
// Each pass is a function that is passed the AST. It can perform checks on it
|
||||
// or modify it as needed. If the pass encounters a semantic error, it throws
|
||||
// |peg.GrammarError|.
|
||||
passes: {
|
||||
check: {
|
||||
reportUndefinedRules: reportUndefinedRules,
|
||||
reportDuplicateRules: reportDuplicateRules,
|
||||
reportUnusedRules: reportUnusedRules,
|
||||
reportDuplicateLabels: reportDuplicateLabels,
|
||||
reportInfiniteRecursion: reportInfiniteRecursion,
|
||||
reportInfiniteRepetition: reportInfiniteRepetition,
|
||||
reportIncorrectPlucking: reportIncorrectPlucking,
|
||||
},
|
||||
transform: {
|
||||
removeProxyRules: removeProxyRules,
|
||||
},
|
||||
generate: {
|
||||
calcReportFailures: calcReportFailures,
|
||||
inferenceMatchResult: inferenceMatchResult,
|
||||
generateBytecode: generateBytecode,
|
||||
generateJS: generateJS,
|
||||
},
|
||||
},
|
||||
|
||||
// Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
|
||||
// if the AST contains a semantic error. Note that not all errors are detected
|
||||
// during the generation and some may protrude to the generated parser and
|
||||
// cause its malfunction.
|
||||
compile( ast, session, options = {} ) {
|
||||
|
||||
options = util.processOptions( options, {
|
||||
allowedStartRules: [ ast.rules[ 0 ].name ],
|
||||
cache: false,
|
||||
context: {},
|
||||
dependencies: {},
|
||||
exportVar: null,
|
||||
features: null,
|
||||
format: "bare",
|
||||
header: null,
|
||||
optimize: "speed",
|
||||
output: "parser",
|
||||
trace: false,
|
||||
} );
|
||||
|
||||
// We want `session.vm.evalModule` to return the parser
|
||||
if ( options.output === "parser" ) options.format = "umd";
|
||||
|
||||
util.each( session.passes, stage => {
|
||||
|
||||
stage.forEach( pass => {
|
||||
|
||||
pass( ast, session, options );
|
||||
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
switch ( options.output ) {
|
||||
|
||||
case "parser":
|
||||
return session.vm.evalModule( ast.code, options.context );
|
||||
|
||||
case "source":
|
||||
return ast.code;
|
||||
|
||||
default:
|
||||
session.error( `Invalid output format: ${ options.output }.` );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
module.exports = compiler;
|
@ -0,0 +1,60 @@
|
||||
"use strict";
|
||||
|
||||
// Bytecode instruction opcodes.
|
||||
const opcodes = {
|
||||
|
||||
// Stack Manipulation
|
||||
|
||||
PUSH_EMPTY_STRING: 0, // PUSH_EMPTY_STRING
|
||||
PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
|
||||
PUSH_NULL: 2, // PUSH_NULL
|
||||
PUSH_FAILED: 3, // PUSH_FAILED
|
||||
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
|
||||
PUSH_CURR_POS: 5, // PUSH_CURR_POS
|
||||
POP: 6, // POP
|
||||
POP_CURR_POS: 7, // POP_CURR_POS
|
||||
POP_N: 8, // POP_N n
|
||||
NIP: 9, // NIP
|
||||
APPEND: 10, // APPEND
|
||||
WRAP: 11, // WRAP n
|
||||
TEXT: 12, // TEXT
|
||||
PLUCK: 41, // PLUCK n, k, p1, ..., pK
|
||||
|
||||
// Conditions and Loops
|
||||
|
||||
IF: 13, // IF t, f
|
||||
IF_ERROR: 14, // IF_ERROR t, f
|
||||
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
|
||||
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b
|
||||
|
||||
// Matching
|
||||
|
||||
MATCH_ANY: 17, // MATCH_ANY a, f, ...
|
||||
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
|
||||
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
|
||||
MATCH_CLASS: 20, // MATCH_CLASS c, a, f, ...
|
||||
ACCEPT_N: 21, // ACCEPT_N n
|
||||
ACCEPT_STRING: 22, // ACCEPT_STRING s
|
||||
EXPECT: 23, // EXPECT e
|
||||
|
||||
// Calls
|
||||
|
||||
LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
|
||||
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
|
||||
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN
|
||||
|
||||
// Rules
|
||||
|
||||
RULE: 27, // RULE r
|
||||
|
||||
// Failure Reporting
|
||||
|
||||
SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
|
||||
SILENT_FAILS_OFF: 29, // SILENT_FAILS_OFF
|
||||
|
||||
EXPECT_NS_BEGIN: 38, // EXPECT_NS_BEGIN
|
||||
EXPECT_NS_END: 39, // EXPECT_NS_END invert
|
||||
|
||||
};
|
||||
|
||||
module.exports = opcodes;
|
@ -0,0 +1,65 @@
|
||||
"use strict";
|
||||
|
||||
// Determines if rule always used in disabled report failure context,
|
||||
// that means, that any failures, reported within it, are never will be
|
||||
// visible, so the no need to report it.
|
||||
function calcReportFailures( ast, session, options ) {
|
||||
|
||||
// By default, not report failures for rules...
|
||||
ast.rules.forEach( rule => {
|
||||
|
||||
rule.reportFailures = false;
|
||||
|
||||
} );
|
||||
|
||||
// ...but report for start rules, because in that context report failures
|
||||
// always enabled
|
||||
const changedRules = options.allowedStartRules.map( name => {
|
||||
|
||||
const rule = ast.findRule( name );
|
||||
|
||||
rule.reportFailures = true;
|
||||
|
||||
return rule;
|
||||
|
||||
} );
|
||||
|
||||
const calc = session.buildVisitor( {
|
||||
rule( node ) {
|
||||
|
||||
calc( node.expression );
|
||||
|
||||
},
|
||||
|
||||
// Because all rules already by default marked as not report any failures
|
||||
// just break AST traversing when we need mark all referenced rules from
|
||||
// this sub-AST as always not report anything (of course if it not be
|
||||
// already marked as report failures).
|
||||
named() {},
|
||||
|
||||
rule_ref( node ) {
|
||||
|
||||
const rule = ast.findRule( node.name );
|
||||
|
||||
// This function only called when rule can report failures. If so, we
|
||||
// need recalculate all rules that referenced from it. But do not do
|
||||
// this twice - if rule is already marked, it was in `changedRules`.
|
||||
if ( ! rule.reportFailures ) {
|
||||
|
||||
rule.reportFailures = true;
|
||||
changedRules.push( rule );
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
} );
|
||||
|
||||
while ( changedRules.length > 0 ) {
|
||||
|
||||
calc( changedRules.pop() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = calcReportFailures;
|
@ -0,0 +1,754 @@
|
||||
"use strict";
|
||||
|
||||
const util = require( "../../util" );
|
||||
|
||||
// Generates bytecode.
|
||||
//
|
||||
// Instructions
|
||||
// ============
|
||||
//
|
||||
// Stack Manipulation
|
||||
// ------------------
|
||||
//
|
||||
// [0] PUSH_EMPTY_STRING
|
||||
//
|
||||
// stack.push("");
|
||||
//
|
||||
// [1] PUSH_UNDEFINED
|
||||
//
|
||||
// stack.push(undefined);
|
||||
//
|
||||
// [2] PUSH_NULL
|
||||
//
|
||||
// stack.push(null);
|
||||
//
|
||||
// [3] PUSH_FAILED
|
||||
//
|
||||
// stack.push(FAILED);
|
||||
//
|
||||
// [4] PUSH_EMPTY_ARRAY
|
||||
//
|
||||
// stack.push([]);
|
||||
//
|
||||
// [5] PUSH_CURR_POS
|
||||
//
|
||||
// stack.push(currPos);
|
||||
//
|
||||
// [6] POP
|
||||
//
|
||||
// stack.pop();
|
||||
//
|
||||
// [7] POP_CURR_POS
|
||||
//
|
||||
// currPos = stack.pop();
|
||||
//
|
||||
// [8] POP_N n
|
||||
//
|
||||
// stack.pop(n);
|
||||
//
|
||||
// [9] NIP
|
||||
//
|
||||
// value = stack.pop();
|
||||
// stack.pop();
|
||||
// stack.push(value);
|
||||
//
|
||||
// [10] APPEND
|
||||
//
|
||||
// value = stack.pop();
|
||||
// array = stack.pop();
|
||||
// array.push(value);
|
||||
// stack.push(array);
|
||||
//
|
||||
// [11] WRAP n
|
||||
//
|
||||
// stack.push(stack.pop(n));
|
||||
//
|
||||
// [12] TEXT
|
||||
//
|
||||
// stack.push(input.substring(stack.pop(), currPos));
|
||||
//
|
||||
// Conditions and Loops
|
||||
// --------------------
|
||||
//
|
||||
// [13] IF t, f
|
||||
//
|
||||
// if (stack.top()) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [14] IF_ERROR t, f
|
||||
//
|
||||
// if (stack.top() === FAILED) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [15] IF_NOT_ERROR t, f
|
||||
//
|
||||
// if (stack.top() !== FAILED) {
|
||||
// interpret(ip + 3, ip + 3 + t);
|
||||
// } else {
|
||||
// interpret(ip + 3 + t, ip + 3 + t + f);
|
||||
// }
|
||||
//
|
||||
// [16] WHILE_NOT_ERROR b
|
||||
//
|
||||
// while(stack.top() !== FAILED) {
|
||||
// interpret(ip + 2, ip + 2 + b);
|
||||
// }
|
||||
//
|
||||
// Matching
|
||||
// --------
|
||||
//
|
||||
// [17] MATCH_ANY a, f, ...
|
||||
//
|
||||
// if (input.length > currPos) {
|
||||
// interpret(ip + 3, ip + 3 + a);
|
||||
// } else {
|
||||
// interpret(ip + 3 + a, ip + 3 + a + f);
|
||||
// }
|
||||
//
|
||||
// [18] MATCH_STRING s, a, f, ...
|
||||
//
|
||||
// if (input.substr(currPos, literals[s].length) === literals[s]) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [19] MATCH_STRING_IC s, a, f, ...
|
||||
//
|
||||
// if (input.substr(currPos, literals[s].length).toLowerCase() === literals[s]) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [20] MATCH_CLASS c, a, f, ...
|
||||
//
|
||||
// if (classes[c].test(input.charAt(currPos))) {
|
||||
// interpret(ip + 4, ip + 4 + a);
|
||||
// } else {
|
||||
// interpret(ip + 4 + a, ip + 4 + a + f);
|
||||
// }
|
||||
//
|
||||
// [21] ACCEPT_N n
|
||||
//
|
||||
// stack.push(input.substring(currPos, n));
|
||||
// currPos += n;
|
||||
//
|
||||
// [22] ACCEPT_STRING s
|
||||
//
|
||||
// stack.push(literals[s]);
|
||||
// currPos += literals[s].length;
|
||||
//
|
||||
// [23] EXPECT e
|
||||
//
|
||||
// expect(expectations[e]);
|
||||
//
|
||||
// Calls
|
||||
// -----
|
||||
//
|
||||
// [24] LOAD_SAVED_POS p
|
||||
//
|
||||
// savedPos = stack[p];
|
||||
//
|
||||
// [25] UPDATE_SAVED_POS
|
||||
//
|
||||
// savedPos = currPos;
|
||||
//
|
||||
// [26] CALL f, n, pc, p1, p2, ..., pN
|
||||
//
|
||||
// value = functions[f](stack[p1], ..., stack[pN]);
|
||||
// stack.pop(n);
|
||||
// stack.push(value);
|
||||
//
|
||||
// Rules
|
||||
// -----
|
||||
//
|
||||
// [27] RULE r
|
||||
//
|
||||
// stack.push(parseRule(r));
|
||||
//
|
||||
// Failure Reporting
|
||||
// -----------------
|
||||
//
|
||||
// [28] SILENT_FAILS_ON
|
||||
//
|
||||
// silentFails++;
|
||||
//
|
||||
// [29] SILENT_FAILS_OFF
|
||||
//
|
||||
// silentFails--;
|
||||
//
|
||||
// [38] EXPECT_NS_BEGIN
|
||||
//
|
||||
// expected.push({ pos: curPos, variants: [] });
|
||||
//
|
||||
// [39] EXPECT_NS_END invert
|
||||
//
|
||||
// value = expected.pop();
|
||||
// if (value.pos === expected.top().pos) {
|
||||
// if (invert) {
|
||||
// value.variants.forEach(e => { e.not = !e.not; });
|
||||
// }
|
||||
// expected.top().variants.pushAll(value.variants);
|
||||
// }
|
||||
function generateBytecode( ast, session ) {
|
||||
|
||||
const op = session.opcodes;
|
||||
|
||||
const literals = [];
|
||||
const classes = [];
|
||||
const expectations = [];
|
||||
const functions = [];
|
||||
let generate;
|
||||
|
||||
function addLiteralConst( value ) {
|
||||
|
||||
const index = literals.indexOf( value );
|
||||
return index === -1 ? literals.push( value ) - 1 : index;
|
||||
|
||||
}
|
||||
|
||||
function addClassConst( node ) {
|
||||
|
||||
const cls = {
|
||||
value: node.parts,
|
||||
inverted: node.inverted,
|
||||
ignoreCase: node.ignoreCase,
|
||||
};
|
||||
const pattern = JSON.stringify( cls );
|
||||
const index = util.findIndex( classes, c => JSON.stringify( c ) === pattern );
|
||||
return index === -1 ? classes.push( cls ) - 1 : index;
|
||||
|
||||
}
|
||||
|
||||
function addExpectedConst( expected ) {
|
||||
|
||||
const pattern = JSON.stringify( expected );
|
||||
const index = util.findIndex( expectations, e => JSON.stringify( e ) === pattern );
|
||||
return index === -1 ? expectations.push( expected ) - 1 : index;
|
||||
|
||||
}
|
||||
|
||||
function addFunctionConst( predicate, params, code ) {
|
||||
|
||||
const func = { predicate: predicate, params: params, body: code };
|
||||
const pattern = JSON.stringify( func );
|
||||
const index = util.findIndex( functions, f => JSON.stringify( f ) === pattern );
|
||||
return index === -1 ? functions.push( func ) - 1 : index;
|
||||
|
||||
}
|
||||
|
||||
const buildSequence = ( ...parts ) => [].concat( ...parts );
|
||||
|
||||
function buildCondition( match, condCode, thenCode, elseCode ) {
|
||||
|
||||
if ( match > 0 ) return thenCode;
|
||||
if ( match < 0 ) return elseCode;
|
||||
|
||||
return condCode.concat(
|
||||
[ thenCode.length, elseCode.length ],
|
||||
thenCode,
|
||||
elseCode,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function buildLoop( condCode, bodyCode ) {
|
||||
|
||||
return condCode.concat( [ bodyCode.length ], bodyCode );
|
||||
|
||||
}
|
||||
|
||||
function buildCall( functionIndex, delta, env, sp ) {
|
||||
|
||||
const params = util.values( env, value => sp - value );
|
||||
return [ op.CALL, functionIndex, delta, params.length ].concat( params );
|
||||
|
||||
}
|
||||
|
||||
function buildSimplePredicate( expression, negative, context ) {
|
||||
|
||||
const match = expression.match|0;
|
||||
return buildSequence(
|
||||
[ op.PUSH_CURR_POS ],
|
||||
[ op.EXPECT_NS_BEGIN ],
|
||||
generate( expression, {
|
||||
sp: context.sp + 1,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
[ op.EXPECT_NS_END, negative ? 1 : 0 ],
|
||||
buildCondition(
|
||||
negative ? -match : match,
|
||||
[ negative ? op.IF_ERROR : op.IF_NOT_ERROR ],
|
||||
buildSequence(
|
||||
[ op.POP ],
|
||||
[ negative ? op.POP : op.POP_CURR_POS ],
|
||||
[ op.PUSH_UNDEFINED ],
|
||||
),
|
||||
buildSequence(
|
||||
[ op.POP ],
|
||||
[ negative ? op.POP_CURR_POS : op.POP ],
|
||||
[ op.PUSH_FAILED ],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function buildSemanticPredicate( node, negative, context ) {
|
||||
|
||||
const functionIndex = addFunctionConst( true, Object.keys( context.env ), node.code );
|
||||
|
||||
return buildSequence(
|
||||
[ op.UPDATE_SAVED_POS ],
|
||||
buildCall( functionIndex, 0, context.env, context.sp ),
|
||||
buildCondition(
|
||||
node.match|0,
|
||||
[ op.IF ],
|
||||
buildSequence( [ op.POP ], negative ? [ op.PUSH_FAILED ] : [ op.PUSH_UNDEFINED ] ),
|
||||
buildSequence( [ op.POP ], negative ? [ op.PUSH_UNDEFINED ] : [ op.PUSH_FAILED ] ),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function buildAppendLoop( expressionCode ) {
|
||||
|
||||
return buildLoop(
|
||||
[ op.WHILE_NOT_ERROR ],
|
||||
buildSequence( [ op.APPEND ], expressionCode ),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
generate = session.buildVisitor( {
|
||||
grammar( node ) {
|
||||
|
||||
node.rules.forEach( generate );
|
||||
node.literals = literals;
|
||||
node.classes = classes;
|
||||
node.expectations = expectations;
|
||||
node.functions = functions;
|
||||
|
||||
},
|
||||
|
||||
rule( node ) {
|
||||
|
||||
node.bytecode = generate( node.expression, {
|
||||
sp: -1, // stack pointer
|
||||
env: { }, // mapping of label names to stack positions
|
||||
action: null, // action nodes pass themselves to children here
|
||||
reportFailures: node.reportFailures, // if `false`, suppress generation of EXPECT opcodes
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
named( node, context ) {
|
||||
|
||||
// Do not generate unused constant, if no need it
|
||||
const nameIndex = context.reportFailures ? addExpectedConst(
|
||||
{ type: "rule", value: node.name },
|
||||
) : null;
|
||||
const expressionCode = generate( node.expression, {
|
||||
sp: context.sp,
|
||||
env: context.env,
|
||||
action: context.action,
|
||||
reportFailures: false,
|
||||
} );
|
||||
|
||||
// No need to disable report failures if it already disabled
|
||||
return context.reportFailures ? buildSequence(
|
||||
[ op.EXPECT, nameIndex ],
|
||||
[ op.SILENT_FAILS_ON ],
|
||||
expressionCode,
|
||||
[ op.SILENT_FAILS_OFF ],
|
||||
) : expressionCode;
|
||||
|
||||
},
|
||||
|
||||
choice( node, context ) {
|
||||
|
||||
function buildAlternativesCode( alternatives, context ) {
|
||||
|
||||
return buildSequence(
|
||||
generate( alternatives[ 0 ], {
|
||||
sp: context.sp,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
alternatives.length < 2
|
||||
? []
|
||||
: buildCondition(
|
||||
// If alternative always match no need generate code for next alternatives
|
||||
-( alternatives[ 0 ].match|0 ),
|
||||
[ op.IF_ERROR ],
|
||||
buildSequence(
|
||||
[ op.POP ],
|
||||
buildAlternativesCode( alternatives.slice( 1 ), context ),
|
||||
),
|
||||
[],
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return buildAlternativesCode( node.alternatives, context );
|
||||
|
||||
},
|
||||
|
||||
action( node, context ) {
|
||||
|
||||
const env = util.clone( context.env );
|
||||
const emitCall = node.expression.type !== "sequence" || node.expression.elements.length === 0;
|
||||
const expressionCode = generate( node.expression, {
|
||||
sp: context.sp + ( emitCall ? 1 : 0 ),
|
||||
env: env,
|
||||
action: node,
|
||||
reportFailures: context.reportFailures,
|
||||
} );
|
||||
const match = node.expression.match|0;
|
||||
const functionIndex = emitCall && match >= 0
|
||||
? addFunctionConst( false, Object.keys( env ), node.code )
|
||||
: null;
|
||||
|
||||
return emitCall === false
|
||||
? expressionCode
|
||||
: buildSequence(
|
||||
[ op.PUSH_CURR_POS ],
|
||||
expressionCode,
|
||||
buildCondition(
|
||||
match,
|
||||
[ op.IF_NOT_ERROR ],
|
||||
buildSequence(
|
||||
[ op.LOAD_SAVED_POS, 1 ],
|
||||
buildCall( functionIndex, 1, env, context.sp + 2 ),
|
||||
),
|
||||
[],
|
||||
),
|
||||
[ op.NIP ],
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
sequence( node, context ) {
|
||||
|
||||
const TOTAL_ELEMENTS = node.elements.length;
|
||||
|
||||
function buildElementsCode( elements, context ) {
|
||||
|
||||
if ( elements.length > 0 ) {
|
||||
|
||||
const processedCount = TOTAL_ELEMENTS - elements.slice( 1 ).length;
|
||||
|
||||
return buildSequence(
|
||||
generate( elements[ 0 ], {
|
||||
sp: context.sp,
|
||||
env: context.env,
|
||||
pluck: context.pluck,
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
buildCondition(
|
||||
elements[ 0 ].match|0,
|
||||
[ op.IF_NOT_ERROR ],
|
||||
buildElementsCode( elements.slice( 1 ), {
|
||||
sp: context.sp + 1,
|
||||
env: context.env,
|
||||
pluck: context.pluck,
|
||||
action: context.action,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
buildSequence(
|
||||
processedCount > 1 ? [ op.POP_N, processedCount ] : [ op.POP ],
|
||||
[ op.POP_CURR_POS ],
|
||||
[ op.PUSH_FAILED ],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if ( context.pluck.length > 0 )
|
||||
|
||||
return buildSequence(
|
||||
[ op.PLUCK, TOTAL_ELEMENTS + 1, context.pluck.length ],
|
||||
context.pluck.map( eSP => context.sp - eSP ),
|
||||
);
|
||||
|
||||
if ( context.action )
|
||||
|
||||
return buildSequence(
|
||||
[ op.LOAD_SAVED_POS, TOTAL_ELEMENTS ],
|
||||
buildCall(
|
||||
addFunctionConst( // functionIndex
|
||||
false,
|
||||
Object.keys( context.env ),
|
||||
context.action.code,
|
||||
),
|
||||
TOTAL_ELEMENTS + 1,
|
||||
context.env,
|
||||
context.sp,
|
||||
),
|
||||
);
|
||||
|
||||
return buildSequence( [ op.WRAP, TOTAL_ELEMENTS ], [ op.NIP ] );
|
||||
|
||||
}
|
||||
|
||||
return buildSequence(
|
||||
[ op.PUSH_CURR_POS ],
|
||||
buildElementsCode( node.elements, {
|
||||
sp: context.sp + 1,
|
||||
env: context.env,
|
||||
pluck: [],
|
||||
action: context.action,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
labeled( node, context ) {
|
||||
|
||||
let env = context.env;
|
||||
const label = node.label;
|
||||
const sp = context.sp + 1;
|
||||
|
||||
if ( label !== null ) {
|
||||
|
||||
env = util.clone( context.env );
|
||||
context.env[ label ] = sp;
|
||||
|
||||
}
|
||||
|
||||
if ( context.pluck && node.pick )
|
||||
|
||||
context.pluck.push( sp );
|
||||
|
||||
return generate( node.expression, {
|
||||
sp: context.sp,
|
||||
env: env,
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
text( node, context ) {
|
||||
|
||||
return buildSequence(
|
||||
[ op.PUSH_CURR_POS ],
|
||||
generate( node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
buildCondition(
|
||||
node.expression.match|0,
|
||||
[ op.IF_NOT_ERROR ],
|
||||
buildSequence( [ op.POP ], [ op.TEXT ] ),
|
||||
[ op.NIP ],
|
||||
),
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
simple_and( node, context ) {
|
||||
|
||||
return buildSimplePredicate( node.expression, false, context );
|
||||
|
||||
},
|
||||
|
||||
simple_not( node, context ) {
|
||||
|
||||
return buildSimplePredicate( node.expression, true, context );
|
||||
|
||||
},
|
||||
|
||||
optional( node, context ) {
|
||||
|
||||
return buildSequence(
|
||||
generate( node.expression, {
|
||||
sp: context.sp,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} ),
|
||||
buildCondition(
|
||||
// If expression always match no need replace FAILED to NULL
|
||||
-( node.expression.match|0 ),
|
||||
[ op.IF_ERROR ],
|
||||
buildSequence( [ op.POP ], [ op.PUSH_NULL ] ),
|
||||
[],
|
||||
),
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
zero_or_more( node, context ) {
|
||||
|
||||
const expressionCode = generate( node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} );
|
||||
|
||||
return buildSequence(
|
||||
[ op.PUSH_EMPTY_ARRAY ],
|
||||
expressionCode,
|
||||
buildAppendLoop( expressionCode ),
|
||||
[ op.POP ],
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
one_or_more( node, context ) {
|
||||
|
||||
const expressionCode = generate( node.expression, {
|
||||
sp: context.sp + 1,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} );
|
||||
|
||||
return buildSequence(
|
||||
[ op.PUSH_EMPTY_ARRAY ],
|
||||
expressionCode,
|
||||
buildCondition(
|
||||
node.expression.match|0,
|
||||
[ op.IF_NOT_ERROR ],
|
||||
buildSequence( buildAppendLoop( expressionCode ), [ op.POP ] ),
|
||||
buildSequence( [ op.POP ], [ op.POP ], [ op.PUSH_FAILED ] ),
|
||||
),
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
group( node, context ) {
|
||||
|
||||
return generate( node.expression, {
|
||||
sp: context.sp,
|
||||
env: util.clone( context.env ),
|
||||
action: null,
|
||||
reportFailures: context.reportFailures,
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
semantic_and( node, context ) {
|
||||
|
||||
return buildSemanticPredicate( node, false, context );
|
||||
|
||||
},
|
||||
|
||||
semantic_not( node, context ) {
|
||||
|
||||
return buildSemanticPredicate( node, true, context );
|
||||
|
||||
},
|
||||
|
||||
rule_ref( node ) {
|
||||
|
||||
return [ op.RULE, ast.indexOfRule( node.name ) ];
|
||||
|
||||
},
|
||||
|
||||
literal( node, context ) {
|
||||
|
||||
if ( node.value.length > 0 ) {
|
||||
|
||||
const match = node.match|0;
|
||||
const needConst = match === 0 || ( match > 0 && ! node.ignoreCase );
|
||||
const stringIndex = needConst ? addLiteralConst(
|
||||
node.ignoreCase ? node.value.toLowerCase() : node.value,
|
||||
) : null;
|
||||
// Do not generate unused constant, if no need it
|
||||
const expectedIndex = context.reportFailures ? addExpectedConst( {
|
||||
type: "literal",
|
||||
value: node.value,
|
||||
ignoreCase: node.ignoreCase,
|
||||
} ) : null;
|
||||
|
||||
// For case-sensitive strings the value must match the beginning of the
|
||||
// remaining input exactly. As a result, we can use |ACCEPT_STRING| and
|
||||
// save one |substr| call that would be needed if we used |ACCEPT_N|.
|
||||
return buildSequence(
|
||||
context.reportFailures ? [ op.EXPECT, expectedIndex ] : [],
|
||||
buildCondition(
|
||||
match,
|
||||
node.ignoreCase
|
||||
? [ op.MATCH_STRING_IC, stringIndex ]
|
||||
: [ op.MATCH_STRING, stringIndex ],
|
||||
node.ignoreCase
|
||||
? [ op.ACCEPT_N, node.value.length ]
|
||||
: [ op.ACCEPT_STRING, stringIndex ],
|
||||
[ op.PUSH_FAILED ],
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return [ op.PUSH_EMPTY_STRING ];
|
||||
|
||||
},
|
||||
|
||||
class( node, context ) {
|
||||
|
||||
const match = node.match|0;
|
||||
const classIndex = match === 0 ? addClassConst( node ) : null;
|
||||
// Do not generate unused constant, if no need it
|
||||
const expectedIndex = context.reportFailures ? addExpectedConst( {
|
||||
type: "class",
|
||||
value: node.parts,
|
||||
inverted: node.inverted,
|
||||
ignoreCase: node.ignoreCase,
|
||||
} ) : null;
|
||||
|
||||
return buildSequence(
|
||||
context.reportFailures ? [ op.EXPECT, expectedIndex ] : [],
|
||||
buildCondition(
|
||||
match,
|
||||
[ op.MATCH_CLASS, classIndex ],
|
||||
[ op.ACCEPT_N, 1 ],
|
||||
[ op.PUSH_FAILED ],
|
||||
),
|
||||
);
|
||||
|
||||
},
|
||||
|
||||
any( node, context ) {
|
||||
|
||||
// Do not generate unused constant, if no need it
|
||||
const expectedIndex = context.reportFailures
|
||||
? addExpectedConst( { type: "any" } )
|
||||
: null;
|
||||
|
||||
return buildSequence(
|
||||
context.reportFailures ? [ op.EXPECT, expectedIndex ] : [],
|
||||
buildCondition(
|
||||
node.match|0,
|
||||
[ op.MATCH_ANY ],
|
||||
[ op.ACCEPT_N, 1 ],
|
||||
[ op.PUSH_FAILED ],
|
||||
),
|
||||
);
|
||||
|
||||
},
|
||||
} );
|
||||
|
||||
generate( ast );
|
||||
|
||||
}
|
||||
|
||||
module.exports = generateBytecode;
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,169 @@
|
||||
"use strict";
|
||||
|
||||
// Inference match result of the rule. Can be:
|
||||
// -1: negative result, always fails
|
||||
// 0: neutral result, may be fail, may be match
|
||||
// 1: positive result, always match
|
||||
function inferenceMatchResult( ast, session ) {
|
||||
|
||||
let inference;
|
||||
function sometimesMatch( node ) {
|
||||
|
||||
node.match = 0;
|
||||
|
||||
return node.match;
|
||||
|
||||
}
|
||||
function alwaysMatch( node ) {
|
||||
|
||||
inference( node.expression );
|
||||
|
||||
node.match = 1;
|
||||
|
||||
return node.match;
|
||||
|
||||
}
|
||||
|
||||
function inferenceExpression( node ) {
|
||||
|
||||
node.match = inference( node.expression );
|
||||
|
||||
return node.match;
|
||||
|
||||
}
|
||||
function inferenceElements( elements, forChoice ) {
|
||||
|
||||
const length = elements.length;
|
||||
let always = 0;
|
||||
let never = 0;
|
||||
|
||||
for ( let i = 0; i < length; ++i ) {
|
||||
|
||||
const result = inference( elements[ i ] );
|
||||
|
||||
if ( result > 0 ) {
|
||||
|
||||
++always;
|
||||
|
||||
}
|
||||
if ( result < 0 ) {
|
||||
|
||||
++never;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( always === length ) {
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
if ( forChoice ) {
|
||||
|
||||
return never === length ? -1 : 0;
|
||||
|
||||
}
|
||||
|
||||
return never > 0 ? -1 : 0;
|
||||
|
||||
}
|
||||
|
||||
inference = session.buildVisitor( {
|
||||
rule( node ) {
|
||||
|
||||
let oldResult;
|
||||
let count = 0;
|
||||
|
||||
if ( typeof node.match === "undefined" ) {
|
||||
|
||||
node.match = 0;
|
||||
do {
|
||||
|
||||
oldResult = node.match;
|
||||
node.match = inference( node.expression );
|
||||
// 6 == 3! -- permutations count for all transitions from one match
|
||||
// state to another.
|
||||
// After 6 iterations the cycle with guarantee begins
|
||||
// istanbul ignore next
|
||||
if ( ++count > 6 ) {
|
||||
|
||||
session.error(
|
||||
"Infinity cycle detected when trying to evaluate node match result",
|
||||
node.location,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
} while ( oldResult !== node.match );
|
||||
|
||||
}
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
named: inferenceExpression,
|
||||
choice( node ) {
|
||||
|
||||
node.match = inferenceElements( node.alternatives, true );
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
action: inferenceExpression,
|
||||
sequence( node ) {
|
||||
|
||||
node.match = inferenceElements( node.elements, false );
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
labeled: inferenceExpression,
|
||||
text: inferenceExpression,
|
||||
simple_and: inferenceExpression,
|
||||
simple_not( node ) {
|
||||
|
||||
node.match = -inference( node.expression );
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
optional: alwaysMatch,
|
||||
zero_or_more: alwaysMatch,
|
||||
one_or_more: inferenceExpression,
|
||||
group: inferenceExpression,
|
||||
semantic_and: sometimesMatch,
|
||||
semantic_not: sometimesMatch,
|
||||
rule_ref( node ) {
|
||||
|
||||
const rule = ast.findRule( node.name );
|
||||
node.match = inference( rule );
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
literal( node ) {
|
||||
|
||||
// Empty literal always match on any input
|
||||
node.match = node.value.length === 0 ? 1 : 0;
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
class( node ) {
|
||||
|
||||
// Empty character class never match on any input
|
||||
node.match = node.parts.length === 0 ? -1 : 0;
|
||||
|
||||
return node.match;
|
||||
|
||||
},
|
||||
// |any| not match on empty input
|
||||
any: sometimesMatch,
|
||||
} );
|
||||
|
||||
inference( ast );
|
||||
|
||||
}
|
||||
|
||||
module.exports = inferenceMatchResult;
|
@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
|
||||
// Removes proxy rules -- that is, rules that only delegate to other rule.
|
||||
function removeProxyRules( ast, session, options ) {
|
||||
|
||||
function isProxyRule( node ) {
|
||||
|
||||
return node.type === "rule" && node.expression.type === "rule_ref";
|
||||
|
||||
}
|
||||
|
||||
const replaceRuleRefs = session.buildVisitor( {
|
||||
|
||||
rule_ref( node, proxy, real ) {
|
||||
|
||||
if ( node.name === proxy ) node.name = real;
|
||||
|
||||
},
|
||||
|
||||
} );
|
||||
|
||||
const allowedStartRules = options.allowedStartRules;
|
||||
const rules = [];
|
||||
|
||||
ast.rules.forEach( rule => {
|
||||
|
||||
if ( isProxyRule( rule ) ) {
|
||||
|
||||
replaceRuleRefs( ast, rule.name, rule.expression.name );
|
||||
if ( allowedStartRules.indexOf( rule.name ) < 0 ) return;
|
||||
|
||||
}
|
||||
|
||||
rules.push( rule );
|
||||
|
||||
} );
|
||||
|
||||
ast.rules = rules;
|
||||
|
||||
}
|
||||
|
||||
module.exports = removeProxyRules;
|
@ -0,0 +1,70 @@
|
||||
"use strict";
|
||||
|
||||
const util = require( "../../util" );
|
||||
const __hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
|
||||
// Checks that each label is defined only once within each scope.
|
||||
function reportDuplicateLabels( ast, session ) {
|
||||
|
||||
let check;
|
||||
|
||||
function checkExpressionWithClonedEnv( node, env ) {
|
||||
|
||||
check( node.expression, util.clone( env ) );
|
||||
|
||||
}
|
||||
|
||||
check = session.buildVisitor( {
|
||||
rule( node ) {
|
||||
|
||||
check( node.expression, {} );
|
||||
|
||||
},
|
||||
|
||||
choice( node, env ) {
|
||||
|
||||
node.alternatives.forEach( alternative => {
|
||||
|
||||
check( alternative, util.clone( env ) );
|
||||
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
action: checkExpressionWithClonedEnv,
|
||||
|
||||
labeled( node, env ) {
|
||||
|
||||
const label = node.label;
|
||||
|
||||
if ( label && __hasOwnProperty.call( env, label ) ) {
|
||||
|
||||
const start = env[ label ].start;
|
||||
|
||||
session.error(
|
||||
`Label "${ label }" is already defined at line ${ start.line }, column ${ start.column }.`,
|
||||
node.location,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
check( node.expression, env );
|
||||
|
||||
if ( label ) env[ label ] = node.location;
|
||||
|
||||
},
|
||||
|
||||
text: checkExpressionWithClonedEnv,
|
||||
simple_and: checkExpressionWithClonedEnv,
|
||||
simple_not: checkExpressionWithClonedEnv,
|
||||
optional: checkExpressionWithClonedEnv,
|
||||
zero_or_more: checkExpressionWithClonedEnv,
|
||||
one_or_more: checkExpressionWithClonedEnv,
|
||||
group: checkExpressionWithClonedEnv,
|
||||
} );
|
||||
|
||||
check( ast );
|
||||
|
||||
}
|
||||
|
||||
module.exports = reportDuplicateLabels;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue