Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
@@ -0,0 +1 @@
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"ebc10010f51b1cb0f10a80d5f86bacf0f420a3f996d57a6a63fc3b22050053af","Cargo.lock":"a0a9550a1f887592f8d3d1dce1d33dc7ed000701aeb788c40247a422b3207947","Cargo.toml":"b37c47c508559bcf1b7118c38f7bb0fe0b57701e4a7ed4ef6ff2327cd7ec72a7","Cargo.toml.orig":"75e18d0c0701c0fa8bc3739dcf253ff171fa330c56ed8f3b287bf944b40042a5","LICENSE-APACHE":"5db2b182453ff32ed40f7da63589c9667a3f8bd8b16b1471b152caae56f77e45","LICENSE-MIT":"49c0b000c03731d9e3970dc059ad4ca345d773681f4a612b0024435b663e0220","README.md":"db9b85bdb86eea38861e4311891263213a3c1cbe0a656037454725c1b21f1da4","benches/contexts/README.md":"562d317f507caedd62cbe00e6b2bb350cb970168e5ebd5b3c497e68696e21e8b","benches/parser.rs":"d3520b384cf84335f53bc25749df5292270173bdb8160bfbb3a916971fa842d1","benches/parser_iai.rs":"10253310d8abbe979ae0833df4bb0d1e2b770d1e94fe99416dec7fcd23aa3f16","src/ast/helper.rs":"09f1379141ba8ddd4c6bb5eded8001e864ddcc2b48c025d6fee4e123442b976d","src/ast/mod.rs":"59eb21d2d816a255d40ea624619d95b7584bf75f3342efe19454d18c0ae05315","src/bin/parser.rs":"eebe03dc02e657f31dc8c929c159ce603173b8be246e5e10e1759f89d631df70","src/bin/update_fixtures.rs":"c27019b33f31f2039594dee998f762b4956f27a71bb37024267c0a75203f33da","src/lib.rs":"f3dc661b97f5d6b832d4e62da6fcdc6d2121b153239e5b909be0e24c5c257669","src/parser/comment.rs":"12a60470ae0ef8be864e446e43a9f2c3aba87d64b19c55b833446b12a4fcb564","src/parser/core.rs":"ec18804e148e575d60f12fa3d091d7c9237850ddf9ffb3ae123187d07726aff3","src/parser/errors.rs":"797f41af46248d65ff110c8a64192c79b534c2d7d712ee677a5b2704249e669b","src/parser/expression.rs":"737b6670e849e8f04b06146ebddb59b8da9f082660f427a564e7eb52ec674326","src/parser/helper.rs":"26ba1d0d64455220cc054a2c7c23051854b1915bf7d1028f4db7ec8a8dc9114b","src/parser/macros.rs":"712a0ae820251331795383eb62fedb200d314e74ff07930b8d0f92786748f1bd","src/parser/mod.rs":"8b9abc0ecd6bbbdf73c4c4d435f705da8831c807ab0060e81ce51111377c52fe","src/parser/pattern.rs":"5927d27f5092660bcdc8902594c1f9b0a0893c4867242e35c77d234758d154d6","src/parser/runtime.rs":"280aefcd960a4e1d402132c38c6764582e471853aa0ef143b099edbdd41f4b6c","src/parser/slice.rs":"20ed4cc2791c716d24a74a60f9989e303c0836ffafe8baf8bf290f2945a7bd40","src/serializer.rs":"95fba7ee88d29732fc3508dd460a3c577d6cdec111ab7486433d06a705b38c40","src/unicode.rs":"9fae798b9dc3d82748786d47eae21fbca3c896fd6b5259c96f8045dfdaa570ec"},"package":"54f0d287c53ffd184d04d8677f590f4ac5379785529e5e08b1c8083acdd5c198"}
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "f22da4ea48328b4c617b7666c482634c49fbe0a7"
},
"path_in_vcs": "fluent-syntax"
}
+643
View File
@@ -0,0 +1,643 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstyle"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15c4c2c83f81532e5845a733998b6971faca23490340a418e9b72a3ec9de12ea"
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb690e81c7840c0d7aade59f242ea3b41b9bc27bcd5997890e7702ae4b32e487"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ed2e96bc16d8d740f6f48d663eddf4b8a0983e79210fd55479b7bcd0a69860e"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "fluent-syntax"
version = "0.12.0"
dependencies = [
"criterion",
"glob",
"iai",
"memchr",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "glob"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "half"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0"
dependencies = [
"crunchy",
]
[[package]]
name = "hermit-abi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f154ce46856750ed433c8649605bf7ed2de3bc35fd9d2a9f30cddd873c80cb08"
[[package]]
name = "iai"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678"
[[package]]
name = "is-terminal"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]]
name = "plotters"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]]
name = "plotters-svg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "web-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+118
View File
@@ -0,0 +1,118 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.64.0"
name = "fluent-syntax"
version = "0.12.0"
authors = [
"Caleb Maclennan <caleb@alerque.com>",
"Bruce Mitchener <bruce.mitchener@gmail.com",
"Zibi Braniecki <zibi@unicode.org>",
"Staś Małolepszy <stas@mozilla.com>",
]
build = false
include = [
"src/**/*",
"benches/*.rs",
"Cargo.toml",
"README.md",
"LICENSE-APACHE",
"LICENSE-MIT",
]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = """
A low-level parser, AST, and serializer API for the syntax used by Project Fluent,
a localization system designed to unleash the entire expressive power of natural language translations.
"""
homepage = "https://www.projectfluent.org"
readme = "README.md"
keywords = [
"localization",
"l10n",
"i18n",
"intl",
"internationalization",
]
categories = [
"localization",
"internationalization",
]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/projectfluent/fluent-rs"
[features]
all-benchmarks = []
default = []
json = [
"serde",
"dep:serde_json",
]
serde = ["dep:serde"]
[lib]
name = "fluent_syntax"
path = "src/lib.rs"
[[bin]]
name = "parser"
path = "src/bin/parser.rs"
[[bin]]
name = "update_fixtures"
path = "src/bin/update_fixtures.rs"
required-features = ["json"]
[[bench]]
name = "parser"
path = "benches/parser.rs"
harness = false
[[bench]]
name = "parser_iai"
path = "benches/parser_iai.rs"
harness = false
[dependencies.memchr]
version = "2.0"
[dependencies.serde]
version = "1.0"
features = ["derive"]
optional = true
[dependencies.serde_json]
version = "1.0"
optional = true
[dependencies.thiserror]
version = "2.0"
[dev-dependencies.criterion]
version = "0.5"
[dev-dependencies.glob]
version = "0.3"
[dev-dependencies.iai]
version = "0.1"
[dev-dependencies.serde]
version = "1.0"
features = ["derive"]
[dev-dependencies.serde_json]
version = "1.0"
+65
View File
@@ -0,0 +1,65 @@
[package]
name = "fluent-syntax"
description = """
A low-level parser, AST, and serializer API for the syntax used by Project Fluent,
a localization system designed to unleash the entire expressive power of natural language translations.
"""
version = "0.12.0"
edition.workspace = true
rust-version = "1.64.0"
homepage.workspace = true
repository.workspace = true
license.workspace = true
authors.workspace = true
categories.workspace = true
keywords.workspace = true
readme = "README.md"
include = [
"src/**/*",
"benches/*.rs",
"Cargo.toml",
"README.md",
"LICENSE-APACHE",
"LICENSE-MIT",
]
[dependencies]
memchr = "2.0"
serde = { workspace = true, optional = true, features = ["derive"] }
serde_json = { workspace = true, optional = true }
thiserror.workspace = true
[dev-dependencies]
criterion.workspace = true
iai.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
glob = "0.3"
[features]
default = []
serde = ["dep:serde"]
json = ["serde", "dep:serde_json"]
all-benchmarks = []
[[bench]]
name = "parser"
harness = false
[[bench]]
name = "parser_iai"
harness = false
[[bin]]
name = "parser"
path = "src/bin/parser.rs"
[[bin]]
name = "update_fixtures"
path = "src/bin/update_fixtures.rs"
required-features = ["json"]
[[test]]
name = "parser_fixtures"
path = "tests/parser_fixtures.rs"
required-features = ["json"]
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 Mozilla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+19
View File
@@ -0,0 +1,19 @@
Copyright 2017 Mozilla
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.
+32
View File
@@ -0,0 +1,32 @@
# Fluent Syntax
[![crates.io](https://img.shields.io/crates/v/fluent-syntax.svg)](https://crates.io/crates/fluent-syntax)
[![docs.rs](https://img.shields.io/docsrs/fluent-syntax)](https://docs.rs/fluent-syntax)
[![Build](https://github.com/projectfluent/fluent-rs/actions/workflows/test.yaml/badge.svg)](https://github.com/projectfluent/fluent-rs/actions/workflows/test.yaml)
[![Coverage Status](https://coveralls.io/repos/github/projectfluent/fluent-rs/badge.svg?branch=main)](https://coveralls.io/github/projectfluent/fluent-rs?branch=main)
The `fluent-rs` workspace is a collection of Rust crates implementing [Project Fluent][],
a localization system designed to unleash the entire expressive power of natural language translations.
This crate is a low-level parser, AST, and serializer API for the Fluent Syntax.
[Project Fluent]: https://projectfluent.org
Get Involved
------------
`fluent-rs` is open-source, licensed under both the Apache 2.0 and MIT licenses. We
encourage everyone to take a look at our code and we'll listen to your
feedback.
Discuss
-------
We'd love to hear your thoughts on Project Fluent! Whether you're a localizer
looking for a better way to express yourself in your language, or a developer
trying to make your app localizable and multilingual, or a hacker looking for
a project to contribute to, please do get in touch on discourse and the IRC channel.
- Discourse: https://discourse.mozilla.org/c/fluent
- Matrix channel: <a href="https://chat.mozilla.org/#/room/#fluent:mozilla.org">#fluent:mozilla.org</a>
@@ -0,0 +1,4 @@
The following context is extracted from
the `browser.xhtml` localization context
from mozilla-central rev 51efc4b931f7
from 2020-03-03.
+141
View File
@@ -0,0 +1,141 @@
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::Criterion;
use std::collections::HashMap;
use std::fs;
use std::io;
use fluent_syntax::parser::parse_runtime;
fn read_file(path: &str) -> Result<String, io::Error> {
fs::read_to_string(path)
}
#[cfg(feature = "all-benchmarks")]
fn get_resources(tests: &[&'static str]) -> HashMap<&'static str, String> {
let mut ftl_strings = HashMap::new();
for test in tests {
let path = format!("./benches/{}", test);
ftl_strings.insert(*test, read_file(&path).expect("Couldn't load file"));
}
ftl_strings
}
fn get_ctxs(tests: &[&'static str]) -> HashMap<&'static str, Vec<String>> {
let mut ftl_strings = HashMap::new();
for test in tests {
let paths = fs::read_dir(format!("./benches/contexts/{}", test)).unwrap();
let strings = paths
.into_iter()
.map(|p| {
let p = p.unwrap().path();
let path = p.to_str().unwrap();
read_file(path).unwrap()
})
.collect::<Vec<_>>();
ftl_strings.insert(*test, strings);
}
ftl_strings
}
fn parse_bench(c: &mut Criterion) {
#[cfg(feature = "all-benchmarks")]
{
let tests = &["simple.ftl", "preferences.ftl", "menubar.ftl"];
let mut group = c.benchmark_group("parse_resource");
for (name, resource) in get_resources(tests) {
group.bench_with_input(name, &resource, |b, source| {
b.iter(|| parse_runtime(source.as_str()).expect("Parsing of the FTL failed."))
});
}
group.finish();
}
let ctx_names = &["browser", "preferences"];
#[cfg(feature = "all-benchmarks")]
{
use fluent_syntax::parser::parse;
let mut group = c.benchmark_group("parse_ctx");
for (name, ctx) in get_ctxs(ctx_names) {
group.bench_with_input(name, &ctx, |b, ctx| {
b.iter(|| {
for source in ctx {
parse(source.as_str()).expect("Parsing of the FTL failed.");
}
})
});
}
group.finish();
}
{
let mut group = c.benchmark_group("parse_ctx_runtime");
for (name, ctx) in get_ctxs(ctx_names) {
group.bench_with_input(name, &ctx, |b, ctx| {
b.iter(|| {
for source in ctx {
parse_runtime(source.as_str()).expect("Parsing of the FTL failed.");
}
});
});
}
group.finish();
}
#[cfg(feature = "all-benchmarks")]
{
use fluent_syntax::unicode::{unescape_unicode, unescape_unicode_to_string};
let strings = &[
"foo",
"This is an example value",
"Hello \\u00e3\\u00e9 World",
"\\u004c\\u006f\\u0072\\u0065\\u006d \\u0069\\u0070\\u0073\\u0075\\u006d \\u0064\\u006f\\u006c\\u006f\\u0072 \\u0073\\u0069\\u0074 \\u0061\\u006d\\u0065\\u0074",
"Let me introduce \\\"The\\\" Fluent",
"And here's an example of \\\\ a character to be escaped",
"But this message is completely unescape free",
"And so is this one",
"Maybe this one is as well completely escape free",
"Welcome to Mozilla Firefox",
"\\u0054\\u0068\\u0065\\u0073\\u0065 \\u0073\\u0065\\u0074\\u0074\\u0069\\u006e\\u0067\\u0073 \\u0061\\u0072\\u0065 \\u0074\\u0061\\u0069\\u006c\\u006f\\u0072\\u0065\\u0064 \\u0074\\u006f \\u0079\\u006f\\u0075\\u0072 \\u0063\\u006f\\u006d\\u0070\\u0075\\u0074\\u0065\\u0072\\u2019\\u0073 \\u0068\\u0061\\u0072\\u0064\\u0077\\u0061\\u0072\\u0065 \\u0061\\u006e\\u0064 \\u006f\\u0070\\u0065\\u0072\\u0061\\u0074\\u0069\\u006e\\u0067 \\u0073\\u0079\\u0073\\u0074\\u0065\\u006d\\u002e",
"These settings are tailored to your computers hardware and operating system",
"Use recommended performance settings",
"\\u0041\\u0064\\u0064\\u0069\\u0074\\u0069\\u006f\\u006e\\u0061\\u006c \\u0063\\u006f\\u006e\\u0074\\u0065\\u006e\\u0074 \\u0070\\u0072\\u006f\\u0063\\u0065\\u0073\\u0073\\u0065\\u0073 \\u0063\\u0061\\u006e \\u0069\\u006d\\u0070\\u0072\\u006f\\u0076\\u0065 \\u0070\\u0065\\u0072\\u0066\\u006f\\u0072\\u006d\\u0061\\u006e\\u0063\\u0065 \\u0077\\u0068\\u0065\\u006e \\u0075\\u0073\\u0069\\u006e\\u0067 \\u006d\\u0075\\u006c\\u0074\\u0069\\u0070\\u006c\\u0065 \\u0074\\u0061\\u0062\\u0073\\u002c \\u0062\\u0075\\u0074 \\u0077\\u0069\\u006c\\u006c \\u0061\\u006c\\u0073\\u006f \\u0075\\u0073\\u0065 \\u006d\\u006f\\u0072\\u0065 \\u006d\\u0065\\u006d\\u006f\\u0072\\u0079\\u002e",
"Additional content processes can improve performance when using multiple tabs, but will also use more memory.",
];
let mut group = c.benchmark_group("unicode");
group.bench_function("writer", |b| {
b.iter(|| {
let mut result = String::new();
for s in strings {
unescape_unicode(&mut result, s).unwrap();
result.clear();
}
})
});
group.bench_function("to_string", |b| {
b.iter(|| {
for s in strings {
let _ = unescape_unicode_to_string(s);
}
})
});
group.finish();
}
}
criterion_group!(benches, parse_bench,);
criterion_main!(benches);
@@ -0,0 +1,15 @@
use fluent_syntax::parser::parse_runtime;
fn iai_parse_ctx_runtime() {
let files = &[
include_str!("contexts/browser/appmenu.ftl"),
include_str!("contexts/browser/browser.ftl"),
include_str!("contexts/browser/menubar.ftl"),
include_str!("contexts/preferences/preferences.ftl"),
];
for source in files {
parse_runtime(*source).expect("Parsing of the FTL failed.");
}
}
iai::main!(iai_parse_ctx_runtime);
+25
View File
@@ -0,0 +1,25 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::Comment;
// This is a helper struct used to properly deserialize referential
// JSON comments which are single continuous String, into a vec of
// content slices.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum CommentDef<S> {
Single { content: S },
Multi { content: Vec<S> },
}
impl<S> From<CommentDef<S>> for Comment<S> {
fn from(input: CommentDef<S>) -> Self {
match input {
CommentDef::Single { content } => Self {
content: vec![content],
},
CommentDef::Multi { content } => Self { content },
}
}
}
File diff suppressed because it is too large Load Diff
+59
View File
@@ -0,0 +1,59 @@
//! This is a simple CLI utility to take in an FTL file and output the AST.
//!
//! ## View the `Debug` representation:
//!
//! From the root directory of the `fluent-rs` repo:
//!
//! ```sh
//! cargo run --bin parser -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl
//! ```
//!
//! ## View the `json` representation:
//!
//! ```sh
//! cargo run --bin parser --features json -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl
//! ```
use fluent_syntax::parser::parse;
use std::env;
use std::fs::File;
use std::io;
use std::io::Read;
fn read_file(path: &str) -> Result<String, io::Error> {
let mut f = File::open(path)?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
fn main() {
let args: Vec<String> = env::args().collect();
let source = read_file(args.get(1).expect("Pass a file path as the first argument"))
.expect("Failed to fetch file");
let (ast, errors) = match parse(source.as_str()) {
Ok(ast) => (ast, None),
Err((ast, err)) => (ast, Some(err)),
};
#[cfg(feature = "json")]
{
let target_json = serde_json::to_string_pretty(&ast).unwrap();
println!("{}", target_json);
}
#[cfg(not(feature = "json"))]
{
use std::fmt::Write;
let mut result = String::new();
write!(result, "{:#?}", ast).unwrap();
println!("{}", result);
}
if let Some(errors) = errors {
println!("\n======== Errors ========== \n");
for err in errors {
println!("Err: {:#?}", err);
}
}
}
@@ -0,0 +1,54 @@
//! Run the `update_fixtures` binary after updating any of the `benches` `.ftl` fixtures.
//! This will update the `.json` files used in reference tests.
//!
//! This file must be run from `{PROJECT_ROOT}/fluent-syntax`
//!
//! ```sh
//! cargo run --bin update_fixtures --features="json"
//! ```
use std::fs;
use std::io;
use fluent_syntax::parser::parse;
fn read_file(path: &str) -> Result<String, io::Error> {
fs::read_to_string(path)
}
fn write_file(path: &str, source: &str) -> Result<(), io::Error> {
fs::write(path, source)
}
fn main() {
let samples = &["menubar", "preferences", "simple"];
let contexts = &["browser", "preferences"];
for sample in samples {
let path = format!("./benches/{}.ftl", sample);
let source = read_file(&path).expect(
"Could not read the benches file. Are you running this from the correct directory? It must be run from `{PROJECT_ROOT}/fluent-syntax`",
);
let ast = parse(source).unwrap();
let target_json = serde_json::to_string_pretty(&ast).unwrap();
let new_path = format!("./tests/fixtures/benches/{}.json", sample);
write_file(&new_path, &target_json).unwrap();
}
for test in contexts {
let paths = fs::read_dir(format!("./benches/contexts/{}", test)).unwrap();
for path in paths.into_iter() {
let p = path.unwrap().path();
let file_name = p.file_name().unwrap().to_str().unwrap();
let path = p.to_str().unwrap();
let source = read_file(path).unwrap();
let ast = parse(source).unwrap();
let target_json = serde_json::to_string_pretty(&ast).unwrap();
let new_path = format!(
"./tests/fixtures/benches/contexts/{}/{}",
test,
file_name.replace(".ftl", ".json")
);
write_file(&new_path, &target_json).unwrap();
}
}
}
+52
View File
@@ -0,0 +1,52 @@
//! Fluent is a modern localization system designed to improve how software is translated.
//!
//! `fluent-syntax` is the lowest level component of the [Fluent Localization
//! System](https://www.projectfluent.org).
//!
//! It exposes components necessary for parsing and tooling operations on Fluent Translation Lists ("FTL").
//!
//! The crate provides a [`parser`] module which allows for parsing of an
//! input string to an Abstract Syntax Tree defined in the [`ast`] module.
//!
//! The [`unicode`] module exposes a set of helper functions used to decode
//! escaped unicode literals according to Fluent specification.
//!
//! # Example
//!
//! ```
//! use fluent_syntax::parser;
//! use fluent_syntax::ast;
//!
//! let ftl = r#"
//!
//! hello-world = Hello World!
//!
//! "#;
//!
//! let resource = parser::parse(ftl)
//! .expect("Failed to parse an FTL resource.");
//!
//! assert_eq!(
//! resource.body[0],
//! ast::Entry::Message(
//! ast::Message {
//! id: ast::Identifier {
//! name: "hello-world"
//! },
//! value: Some(ast::Pattern {
//! elements: vec![
//! ast::PatternElement::TextElement {
//! value: "Hello World!"
//! },
//! ]
//! }),
//! attributes: vec![],
//! comment: None,
//! }
//! ),
//! );
//! ```
pub mod ast;
pub mod parser;
pub mod serializer;
pub mod unicode;
@@ -0,0 +1,89 @@
use super::{core::Parser, core::Result, Slice};
use crate::ast;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Level {
None = 0,
Regular = 1,
Group = 2,
Resource = 3,
}
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub(super) fn get_comment(&mut self) -> Result<(ast::Comment<S>, Level)> {
let mut level = Level::None;
let mut content = vec![];
while self.ptr < self.length {
let line_level = self.get_comment_level();
if line_level == Level::None {
self.ptr -= 1;
break;
} else if level != Level::None && line_level != level {
self.ptr -= line_level as usize;
break;
}
level = line_level;
if self.ptr == self.length {
break;
} else if self.is_eol() {
content.push(self.get_comment_line());
} else {
if let Err(e) = self.expect_byte(b' ') {
if content.is_empty() {
return Err(e);
} else {
self.ptr -= line_level as usize;
break;
}
}
content.push(self.get_comment_line());
}
self.skip_eol();
}
Ok((ast::Comment { content }, level))
}
pub(super) fn skip_comment(&mut self) {
loop {
while self.ptr < self.length && !self.is_eol() {
self.ptr += 1;
}
self.ptr += 1;
if self.is_current_byte(b'#') {
self.ptr += 1;
} else {
break;
}
}
}
fn get_comment_level(&mut self) -> Level {
if self.take_byte_if(b'#') {
if self.take_byte_if(b'#') {
if self.take_byte_if(b'#') {
return Level::Resource;
}
return Level::Group;
}
return Level::Regular;
}
Level::None
}
fn get_comment_line(&mut self) -> S {
let start_pos = self.ptr;
while !self.is_eol() {
self.ptr += 1;
}
self.source.slice(start_pos..self.ptr)
}
}
+307
View File
@@ -0,0 +1,307 @@
use super::{
comment,
errors::{ErrorKind, ParserError},
slice::Slice,
};
use crate::ast;
pub type Result<T> = std::result::Result<T, ParserError>;
pub struct Parser<S> {
pub(super) source: S,
pub(super) ptr: usize,
pub(super) length: usize,
}
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub fn new(source: S) -> Self {
let length = source.as_ref().len();
Self {
source,
ptr: 0,
length,
}
}
pub fn parse(
mut self,
) -> std::result::Result<ast::Resource<S>, (ast::Resource<S>, Vec<ParserError>)> {
let mut errors = vec![];
let mut body = vec![];
self.skip_blank_block();
let mut last_comment = None;
let mut last_blank_count = 0;
while self.ptr < self.length {
let entry_start = self.ptr;
let mut entry = self.get_entry(entry_start);
if let Some(comment) = last_comment.take() {
match entry {
Ok(ast::Entry::Message(ref mut msg)) if last_blank_count < 2 => {
msg.comment = Some(comment);
}
Ok(ast::Entry::Term(ref mut term)) if last_blank_count < 2 => {
term.comment = Some(comment);
}
_ => {
body.push(ast::Entry::Comment(comment));
}
}
}
match entry {
Ok(ast::Entry::Comment(comment)) => {
last_comment = Some(comment);
}
Ok(entry) => {
body.push(entry);
}
Err(mut err) => {
self.skip_to_next_entry_start();
err.slice = Some(entry_start..self.ptr);
errors.push(err);
let content = self.source.slice(entry_start..self.ptr);
body.push(ast::Entry::Junk { content });
}
}
last_blank_count = self.skip_blank_block();
}
if let Some(last_comment) = last_comment.take() {
body.push(ast::Entry::Comment(last_comment));
}
if errors.is_empty() {
Ok(ast::Resource { body })
} else {
Err((ast::Resource { body }, errors))
}
}
fn get_entry(&mut self, entry_start: usize) -> Result<ast::Entry<S>> {
let entry = match get_current_byte!(self) {
Some(b'#') => {
let (comment, level) = self.get_comment()?;
match level {
comment::Level::Regular => ast::Entry::Comment(comment),
comment::Level::Group => ast::Entry::GroupComment(comment),
comment::Level::Resource => ast::Entry::ResourceComment(comment),
comment::Level::None => unreachable!(),
}
}
Some(b'-') => ast::Entry::Term(self.get_term(entry_start)?),
_ => ast::Entry::Message(self.get_message(entry_start)?),
};
Ok(entry)
}
pub fn get_message(&mut self, entry_start: usize) -> Result<ast::Message<S>> {
let id = self.get_identifier()?;
self.skip_blank_inline();
self.expect_byte(b'=')?;
let pattern = self.get_pattern()?;
self.skip_blank_block();
let attributes = self.get_attributes();
if pattern.is_none() && attributes.is_empty() {
let entry_id = id.name.as_ref().to_owned();
return error!(
ErrorKind::ExpectedMessageField { entry_id },
entry_start, self.ptr
);
}
Ok(ast::Message {
id,
value: pattern,
attributes,
comment: None,
})
}
pub fn get_term(&mut self, entry_start: usize) -> Result<ast::Term<S>> {
self.expect_byte(b'-')?;
let id = self.get_identifier()?;
self.skip_blank_inline();
self.expect_byte(b'=')?;
self.skip_blank_inline();
let value = self.get_pattern()?;
self.skip_blank_block();
let attributes = self.get_attributes();
if let Some(value) = value {
Ok(ast::Term {
id,
value,
attributes,
comment: None,
})
} else {
error!(
ErrorKind::ExpectedTermField {
entry_id: id.name.as_ref().to_owned()
},
entry_start, self.ptr
)
}
}
fn get_attributes(&mut self) -> Vec<ast::Attribute<S>> {
let mut attributes = vec![];
loop {
let line_start = self.ptr;
self.skip_blank_inline();
if !self.take_byte_if(b'.') {
self.ptr = line_start;
break;
}
if let Ok(attr) = self.get_attribute() {
attributes.push(attr);
} else {
self.ptr = line_start;
break;
}
}
attributes
}
fn get_attribute(&mut self) -> Result<ast::Attribute<S>> {
let id = self.get_identifier()?;
self.skip_blank_inline();
self.expect_byte(b'=')?;
let pattern = self.get_pattern()?;
match pattern {
Some(pattern) => Ok(ast::Attribute { id, value: pattern }),
None => error!(ErrorKind::MissingValue, self.ptr),
}
}
pub(super) fn get_identifier_unchecked(&mut self) -> ast::Identifier<S> {
let mut ptr = self.ptr;
while matches!(get_byte!(self, ptr), Some(b) if b.is_ascii_alphanumeric() || *b == b'-' || *b == b'_')
{
ptr += 1;
}
let name = self.source.slice(self.ptr - 1..ptr);
self.ptr = ptr;
ast::Identifier { name }
}
pub(super) fn get_identifier(&mut self) -> Result<ast::Identifier<S>> {
if !self.is_identifier_start() {
return error!(
ErrorKind::ExpectedCharRange {
range: "a-zA-Z".to_string()
},
self.ptr
);
}
self.ptr += 1;
Ok(self.get_identifier_unchecked())
}
pub(super) fn get_attribute_accessor(&mut self) -> Result<Option<ast::Identifier<S>>> {
if self.take_byte_if(b'.') {
let ident = self.get_identifier()?;
Ok(Some(ident))
} else {
Ok(None)
}
}
fn get_variant_key(&mut self) -> Result<ast::VariantKey<S>> {
self.skip_blank();
let key = if self.is_number_start() {
ast::VariantKey::NumberLiteral {
value: self.get_number_literal()?,
}
} else {
ast::VariantKey::Identifier {
name: self.get_identifier()?.name,
}
};
self.skip_blank();
self.expect_byte(b']')?;
Ok(key)
}
pub(super) fn get_variants(&mut self) -> Result<Vec<ast::Variant<S>>> {
let mut variants = Vec::with_capacity(2);
let mut has_default = false;
loop {
let default = self.take_byte_if(b'*');
if default {
if has_default {
return error!(ErrorKind::MultipleDefaultVariants, self.ptr);
} else {
has_default = true;
}
}
if !self.take_byte_if(b'[') {
break;
}
let key = self.get_variant_key()?;
let value = self.get_pattern()?;
if let Some(value) = value {
variants.push(ast::Variant {
key,
value,
default,
});
self.skip_blank();
} else {
return error!(ErrorKind::MissingValue, self.ptr);
}
}
if has_default {
Ok(variants)
} else {
error!(ErrorKind::MissingDefaultVariant, self.ptr)
}
}
pub(super) fn get_placeable(&mut self) -> Result<ast::Expression<S>> {
self.skip_blank();
let exp = self.get_expression()?;
self.skip_blank_inline();
self.expect_byte(b'}')?;
let invalid_expression_found = match &exp {
ast::Expression::Inline(ast::InlineExpression::TermReference {
ref attribute, ..
}) => attribute.is_some(),
_ => false,
};
if invalid_expression_found {
return error!(ErrorKind::TermAttributeAsPlaceable, self.ptr);
}
Ok(exp)
}
}
+169
View File
@@ -0,0 +1,169 @@
use std::ops::Range;
use thiserror::Error;
/// Error containing information about an error encountered by the Fluent Parser.
///
/// Errors in Fluent Parser are non-fatal, and the syntax has been
/// designed to allow for strong recovery.
///
/// In result [`ParserError`] is designed to point at the slice of
/// the input that is most likely to be a complete fragment from after
/// the end of a valid entry, to the start of the next valid entry, with
/// the invalid syntax in the middle.
///
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::ast;
///
/// let ftl = r#"
/// key1 = Value 1
///
/// g@Rb@ge = #2y ds
///
/// key2 = Value 2
///
/// "#;
///
/// let (resource, errors) = parser::parse_runtime(ftl)
/// .expect_err("Resource should contain errors.");
///
/// assert_eq!(
/// errors,
/// vec![
/// parser::ParserError {
/// pos: 18..19,
/// slice: Some(17..35),
/// kind: parser::ErrorKind::ExpectedToken('=')
/// }
/// ]
/// );
///
/// assert_eq!(
/// resource.body[0],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key1"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 1"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
///
/// assert_eq!(
/// resource.body[1],
/// ast::Entry::Junk {
/// content: "g@Rb@ge = #2y ds\n\n"
/// }
/// );
///
/// assert_eq!(
/// resource.body[2],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key2"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 2"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
/// ```
///
/// The information contained in the `ParserError` should allow the tooling
/// to display rich contextual annotations of the error slice, using
/// crates such as `annotate-snippers`.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("{}", self.kind)]
pub struct ParserError {
/// Precise location of where the parser encountered the error.
pub pos: Range<usize>,
/// Slice of the input from the end of the last valid entry to the beginning
/// of the next valid entry with the invalid syntax in the middle.
pub slice: Option<Range<usize>>,
/// The type of the error that the parser encountered.
pub kind: ErrorKind,
}
macro_rules! error {
($kind:expr, $start:expr) => {{
Err(ParserError {
pos: $start..$start + 1,
slice: None,
kind: $kind,
})
}};
($kind:expr, $start:expr, $end:expr) => {{
Err(ParserError {
pos: $start..$end,
slice: None,
kind: $kind,
})
}};
}
/// Kind of an error associated with the [`ParserError`].
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum ErrorKind {
#[error("Expected a token starting with \"{0}\"")]
ExpectedToken(char),
#[error("Expected one of \"{range}\"")]
ExpectedCharRange { range: String },
#[error("Expected a message field for \"{entry_id}\"")]
ExpectedMessageField { entry_id: String },
#[error("Expected a term field for \"{entry_id}\"")]
ExpectedTermField { entry_id: String },
#[error("Callee is not allowed here")]
ForbiddenCallee,
#[error("The select expression must have a default variant")]
MissingDefaultVariant,
#[error("Expected a value")]
MissingValue,
#[error("A select expression can only have one default variant")]
MultipleDefaultVariants,
#[error("Message references can't be used as a selector")]
MessageReferenceAsSelector,
#[error("Term references can't be used as a selector")]
TermReferenceAsSelector,
#[error("Message attributes can't be used as a selector")]
MessageAttributeAsSelector,
#[error("Term attributes can't be used as a selector")]
TermAttributeAsPlaceable,
#[error("Unterminated string literal")]
UnterminatedStringLiteral,
#[error("Positional arguments must come before named arguments")]
PositionalArgumentFollowsNamed,
#[error("The \"{0}\" argument appears twice")]
DuplicatedNamedArgument(String),
#[error("Unknown escape sequence")]
UnknownEscapeSequence(String),
#[error("Invalid unicode escape sequence, \"{0}\"")]
InvalidUnicodeEscapeSequence(String),
#[error("Unbalanced closing brace")]
UnbalancedClosingBrace,
#[error("Expected an inline expression")]
ExpectedInlineExpression,
#[error("Expected a simple expression as selector")]
ExpectedSimpleExpressionAsSelector,
#[error("Expected a string or number literal")]
ExpectedLiteral,
}
@@ -0,0 +1,224 @@
use super::errors::{ErrorKind, ParserError};
use super::{core::Parser, core::Result, slice::Slice};
use crate::ast;
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub(super) fn get_expression(&mut self) -> Result<ast::Expression<S>> {
let exp = self.get_inline_expression(false)?;
self.skip_blank();
if !self.is_current_byte(b'-') || !self.is_byte_at(b'>', self.ptr + 1) {
if let ast::InlineExpression::TermReference { ref attribute, .. } = exp {
if attribute.is_some() {
return error!(ErrorKind::TermAttributeAsPlaceable, self.ptr);
}
}
return Ok(ast::Expression::Inline(exp));
}
match exp {
ast::InlineExpression::MessageReference { ref attribute, .. } => {
if attribute.is_none() {
return error!(ErrorKind::MessageReferenceAsSelector, self.ptr);
} else {
return error!(ErrorKind::MessageAttributeAsSelector, self.ptr);
}
}
ast::InlineExpression::TermReference { ref attribute, .. } => {
if attribute.is_none() {
return error!(ErrorKind::TermReferenceAsSelector, self.ptr);
}
}
ast::InlineExpression::StringLiteral { .. }
| ast::InlineExpression::NumberLiteral { .. }
| ast::InlineExpression::VariableReference { .. }
| ast::InlineExpression::FunctionReference { .. } => {}
_ => {
return error!(ErrorKind::ExpectedSimpleExpressionAsSelector, self.ptr);
}
};
self.ptr += 2; // ->
self.skip_blank_inline();
if !self.skip_eol() {
return error!(
ErrorKind::ExpectedCharRange {
range: "\n | \r\n".to_string()
},
self.ptr
);
}
self.skip_blank();
let variants = self.get_variants()?;
Ok(ast::Expression::Select {
selector: exp,
variants,
})
}
pub(super) fn get_inline_expression(
&mut self,
only_literal: bool,
) -> Result<ast::InlineExpression<S>> {
match get_current_byte!(self) {
Some(b'"') => {
self.ptr += 1; // "
let start = self.ptr;
while let Some(b) = get_current_byte!(self) {
match b {
b'\\' => match get_byte!(self, self.ptr + 1) {
Some(b'\\') | Some(b'{') | Some(b'"') => self.ptr += 2,
Some(b'u') => {
self.ptr += 2;
self.skip_unicode_escape_sequence(4)?;
}
Some(b'U') => {
self.ptr += 2;
self.skip_unicode_escape_sequence(6)?;
}
b => {
let seq = b.unwrap_or(&b' ').to_string();
return error!(ErrorKind::UnknownEscapeSequence(seq), self.ptr);
}
},
b'"' => {
break;
}
b'\n' => {
return error!(ErrorKind::UnterminatedStringLiteral, self.ptr);
}
_ => self.ptr += 1,
}
}
self.expect_byte(b'"')?;
let slice = self.source.slice(start..self.ptr - 1);
Ok(ast::InlineExpression::StringLiteral { value: slice })
}
Some(b) if b.is_ascii_digit() => {
let num = self.get_number_literal()?;
Ok(ast::InlineExpression::NumberLiteral { value: num })
}
Some(b'-') if !only_literal => {
self.ptr += 1; // -
if self.is_identifier_start() {
self.ptr += 1;
let id = self.get_identifier_unchecked();
let attribute = self.get_attribute_accessor()?;
let arguments = self.get_call_arguments()?;
Ok(ast::InlineExpression::TermReference {
id,
attribute,
arguments,
})
} else {
self.ptr -= 1;
let num = self.get_number_literal()?;
Ok(ast::InlineExpression::NumberLiteral { value: num })
}
}
Some(b'$') if !only_literal => {
self.ptr += 1; // $
let id = self.get_identifier()?;
Ok(ast::InlineExpression::VariableReference { id })
}
Some(b) if b.is_ascii_alphabetic() => {
self.ptr += 1;
let id = self.get_identifier_unchecked();
let arguments = self.get_call_arguments()?;
if let Some(arguments) = arguments {
if !Self::is_callee(&id.name) {
return error!(ErrorKind::ForbiddenCallee, self.ptr);
}
Ok(ast::InlineExpression::FunctionReference { id, arguments })
} else {
let attribute = self.get_attribute_accessor()?;
Ok(ast::InlineExpression::MessageReference { id, attribute })
}
}
Some(b'{') if !only_literal => {
self.ptr += 1; // {
let exp = self.get_placeable()?;
Ok(ast::InlineExpression::Placeable {
expression: Box::new(exp),
})
}
_ if only_literal => error!(ErrorKind::ExpectedLiteral, self.ptr),
_ => error!(ErrorKind::ExpectedInlineExpression, self.ptr),
}
}
pub fn get_call_arguments(&mut self) -> Result<Option<ast::CallArguments<S>>> {
self.skip_blank();
if !self.take_byte_if(b'(') {
return Ok(None);
}
let mut positional = vec![];
let mut named = vec![];
let mut argument_names = vec![];
self.skip_blank();
while self.ptr < self.length {
if self.is_current_byte(b')') {
break;
}
let expr = self.get_inline_expression(false)?;
if let ast::InlineExpression::MessageReference {
ref id,
attribute: None,
} = expr
{
self.skip_blank();
if self.is_current_byte(b':') {
if argument_names.contains(&id.name) {
return error!(
ErrorKind::DuplicatedNamedArgument(id.name.as_ref().to_owned()),
self.ptr
);
}
self.ptr += 1;
self.skip_blank();
let val = self.get_inline_expression(true)?;
argument_names.push(id.name.clone());
named.push(ast::NamedArgument {
name: ast::Identifier {
name: id.name.clone(),
},
value: val,
});
} else {
if !argument_names.is_empty() {
return error!(ErrorKind::PositionalArgumentFollowsNamed, self.ptr);
}
positional.push(expr);
}
} else {
if !argument_names.is_empty() {
return error!(ErrorKind::PositionalArgumentFollowsNamed, self.ptr);
}
positional.push(expr);
}
self.skip_blank();
self.take_byte_if(b',');
self.skip_blank();
}
self.expect_byte(b')')?;
Ok(Some(ast::CallArguments { positional, named }))
}
}
+169
View File
@@ -0,0 +1,169 @@
use super::errors::{ErrorKind, ParserError};
use super::{core::Parser, core::Result, slice::Slice};
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub(super) fn is_current_byte(&self, b: u8) -> bool {
get_current_byte!(self) == Some(&b)
}
pub(super) fn is_byte_at(&self, b: u8, pos: usize) -> bool {
get_byte!(self, pos) == Some(&b)
}
pub(super) fn skip_to_next_entry_start(&mut self) {
while let Some(b) = get_current_byte!(self) {
let new_line = self.ptr == 0 || get_byte!(self, self.ptr - 1) == Some(&b'\n');
if new_line && (b.is_ascii_alphabetic() || [b'-', b'#'].contains(b)) {
break;
}
self.ptr += 1;
}
}
pub(super) fn skip_eol(&mut self) -> bool {
match get_current_byte!(self) {
Some(b'\n') => {
self.ptr += 1;
true
}
Some(b'\r') if self.is_byte_at(b'\n', self.ptr + 1) => {
self.ptr += 2;
true
}
_ => false,
}
}
pub(super) fn skip_unicode_escape_sequence(&mut self, length: usize) -> Result<()> {
let start = self.ptr;
for _ in 0..length {
match get_current_byte!(self) {
Some(b) if b.is_ascii_hexdigit() => self.ptr += 1,
_ => break,
}
}
if self.ptr - start != length {
let end = if self.ptr >= self.length {
self.ptr
} else {
self.ptr + 1
};
let seq = self.source.slice(start..end).as_ref().to_owned();
return error!(ErrorKind::InvalidUnicodeEscapeSequence(seq), self.ptr);
}
Ok(())
}
pub(super) fn is_identifier_start(&self) -> bool {
matches!(get_current_byte!(self), Some(b) if b.is_ascii_alphabetic())
}
pub(super) fn take_byte_if(&mut self, b: u8) -> bool {
if self.is_current_byte(b) {
self.ptr += 1;
true
} else {
false
}
}
pub(super) fn skip_blank_block(&mut self) -> usize {
let mut count = 0;
loop {
let start = self.ptr;
self.skip_blank_inline();
if !self.skip_eol() {
self.ptr = start;
break;
}
count += 1;
}
count
}
pub(super) fn skip_blank(&mut self) {
loop {
match get_current_byte!(self) {
Some(b' ') | Some(b'\n') => self.ptr += 1,
Some(b'\r') if get_byte!(self, self.ptr + 1) == Some(&b'\n') => self.ptr += 2,
_ => break,
}
}
}
pub(super) fn skip_blank_inline(&mut self) -> usize {
let start = self.ptr;
while let Some(b' ') = get_current_byte!(self) {
self.ptr += 1;
}
self.ptr - start
}
pub(super) fn is_byte_pattern_continuation(b: u8) -> bool {
!matches!(b, b'.' | b'}' | b'[' | b'*')
}
pub(super) fn is_callee(name: &S) -> bool {
name.as_ref()
.as_bytes()
.iter()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || *c == b'_' || *c == b'-')
}
pub(super) fn expect_byte(&mut self, b: u8) -> Result<()> {
if !self.is_current_byte(b) {
return error!(ErrorKind::ExpectedToken(b as char), self.ptr);
}
self.ptr += 1;
Ok(())
}
pub(super) fn is_number_start(&self) -> bool {
matches!(get_current_byte!(self), Some(b) if b.is_ascii_digit() || b == &b'-')
}
pub(super) fn is_eol(&self) -> bool {
match get_current_byte!(self) {
Some(b'\n') => true,
Some(b'\r') if self.is_byte_at(b'\n', self.ptr + 1) => true,
None => true,
_ => false,
}
}
pub(super) fn skip_digits(&mut self) -> Result<()> {
let start = self.ptr;
loop {
match get_current_byte!(self) {
Some(b) if b.is_ascii_digit() => self.ptr += 1,
_ => break,
}
}
if start == self.ptr {
error!(
ErrorKind::ExpectedCharRange {
range: "0-9".to_string()
},
self.ptr
)
} else {
Ok(())
}
}
pub(super) fn get_number_literal(&mut self) -> Result<S> {
let start = self.ptr;
self.take_byte_if(b'-');
self.skip_digits()?;
if self.take_byte_if(b'.') {
self.skip_digits()?;
}
Ok(self.source.slice(start..self.ptr))
}
}
+17
View File
@@ -0,0 +1,17 @@
macro_rules! get_byte {
($s:expr, $idx:expr) => {
$s.source.as_ref().as_bytes().get($idx)
};
}
macro_rules! get_current_byte {
($s:expr) => {
$s.source.as_ref().as_bytes().get($s.ptr)
};
}
macro_rules! get_remaining_bytes {
($s:expr) => {
$s.source.as_ref().as_bytes().get($s.ptr..)
};
}
+279
View File
@@ -0,0 +1,279 @@
//! Fluent Translation List parsing utilities
//!
//! FTL resources can be parsed using one of two methods:
//! * [`parse`] - parses an input into a complete Abstract Syntax Tree representation with all source information preserved.
//! * [`parse_runtime`] - parses an input into a runtime optimized Abstract Syntax Tree
//! representation with comments stripped.
//!
//! # Example
//!
//! ```
//! use fluent_syntax::parser;
//! use fluent_syntax::ast;
//!
//! let ftl = r#"
//! #### Resource Level Comment
//!
//! ## This is a message comment
//! hello-world = Hello World!
//!
//! "#;
//!
//! let resource = parser::parse(ftl)
//! .expect("Failed to parse an FTL resource.");
//!
//! assert_eq!(
//! resource.body[0],
//! ast::Entry::ResourceComment(
//! ast::Comment {
//! content: vec![
//! "Resource Level Comment"
//! ]
//! }
//! )
//! );
//! assert_eq!(
//! resource.body[1],
//! ast::Entry::Message(
//! ast::Message {
//! id: ast::Identifier {
//! name: "hello-world"
//! },
//! value: Some(ast::Pattern {
//! elements: vec![
//! ast::PatternElement::TextElement {
//! value: "Hello World!"
//! },
//! ]
//! }),
//! attributes: vec![],
//! comment: Some(
//! ast::Comment {
//! content: vec!["This is a message comment"]
//! }
//! )
//! }
//! ),
//! );
//! ```
//!
//! # Error Recovery
//!
//! In both modes the parser is lenient, attempting to recover from errors.
//!
//! The [`Result`] return the resulting AST in both scenarios, and in the
//! error scenario a vector of [`ParserError`] elements is returned as well.
//!
//! Any unparsed parts of the input are returned as [`ast::Entry::Junk`] elements.
#[macro_use]
mod errors;
#[macro_use]
mod macros;
mod comment;
mod core;
mod expression;
mod helper;
mod pattern;
mod runtime;
mod slice;
use crate::ast;
pub use errors::{ErrorKind, ParserError};
pub(crate) use slice::matches_fluent_ws;
pub use slice::Slice;
/// Parser result always returns an AST representation of the input,
/// and if parsing errors were encountered, a list of [`ParserError`] elements
/// is also returned.
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::ast;
///
/// let ftl = r#"
/// key1 = Value 1
///
/// g@Rb@ge = #2y ds
///
/// key2 = Value 2
///
/// "#;
///
/// let (resource, errors) = parser::parse_runtime(ftl)
/// .expect_err("Resource should contain errors.");
///
/// assert_eq!(
/// errors,
/// vec![
/// parser::ParserError {
/// pos: 18..19,
/// slice: Some(17..35),
/// kind: parser::ErrorKind::ExpectedToken('=')
/// }
/// ]
/// );
///
/// assert_eq!(
/// resource.body[0],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key1"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 1"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
///
/// assert_eq!(
/// resource.body[1],
/// ast::Entry::Junk {
/// content: "g@Rb@ge = #2y ds\n\n"
/// }
/// );
///
/// assert_eq!(
/// resource.body[2],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key2"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 2"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
/// ```
pub type Result<S> = std::result::Result<ast::Resource<S>, (ast::Resource<S>, Vec<ParserError>)>;
/// Parses an input into a complete Abstract Syntax Tree representation with
/// all source information preserved.
///
/// This mode is intended for tooling, linters and other scenarios where
/// complete representation, with comments, is preferred over speed and memory
/// utilization.
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::ast;
///
/// let ftl = r#"
/// #### Resource Level Comment
///
/// ## This is a message comment
/// hello-world = Hello World!
///
/// "#;
///
/// let resource = parser::parse(ftl)
/// .expect("Failed to parse an FTL resource.");
///
/// assert_eq!(
/// resource.body[0],
/// ast::Entry::ResourceComment(
/// ast::Comment {
/// content: vec![
/// "Resource Level Comment"
/// ]
/// }
/// )
/// );
/// assert_eq!(
/// resource.body[1],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "hello-world"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Hello World!"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: Some(
/// ast::Comment {
/// content: vec!["This is a message comment"]
/// }
/// )
/// }
/// ),
/// );
/// ```
pub fn parse<'s, S>(input: S) -> Result<S>
where
S: Slice<'s>,
{
core::Parser::new(input).parse()
}
/// Parses an input into an Abstract Syntax Tree representation with comments stripped.
///
/// This mode is intended for runtime use of Fluent. It currently strips all
/// comments improving parsing performance and reducing the size of the AST tree.
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::ast;
///
/// let ftl = r#"
/// #### Resource Level Comment
///
/// ## This is a message comment
/// hello-world = Hello World!
///
/// "#;
///
/// let resource = parser::parse_runtime(ftl)
/// .expect("Failed to parse an FTL resource.");
///
/// assert_eq!(
/// resource.body[0],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "hello-world"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Hello World!"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
/// ```
pub fn parse_runtime<'s, S>(input: S) -> Result<S>
where
S: Slice<'s>,
{
core::Parser::new(input).parse_runtime()
}
+220
View File
@@ -0,0 +1,220 @@
use super::errors::{ErrorKind, ParserError};
use super::{core::Parser, core::Result, slice::Slice};
use crate::ast;
#[derive(Debug, PartialEq)]
enum TextElementTermination {
LineFeed,
Crlf,
PlaceableStart,
Eof,
}
// This enum tracks the placement of the text element in the pattern, which is needed for
// dedentation logic.
#[derive(Debug, PartialEq)]
enum TextElementPosition {
InitialLineStart,
LineStart,
Continuation,
}
// This enum allows us to mark pointers in the source which will later become text elements
// but without slicing them out of the source string. This makes the indentation adjustments
// cheaper since they'll happen on the pointers, rather than extracted slices.
#[derive(Debug)]
enum PatternElementPlaceholders<S> {
Placeable(ast::Expression<S>),
// (start, end, indent, position)
TextElement(usize, usize, usize, TextElementPosition),
}
// This enum tracks whether the text element is blank or not.
// This is important to identify text elements which should not be taken into account
// when calculating common indent.
#[derive(Debug, PartialEq)]
enum TextElementType {
Blank,
NonBlank,
}
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub(super) fn get_pattern(&mut self) -> Result<Option<ast::Pattern<S>>> {
let mut elements = vec![];
let mut last_non_blank = None;
let mut common_indent = None;
self.skip_blank_inline();
let mut text_element_role = if self.skip_eol() {
self.skip_blank_block();
TextElementPosition::LineStart
} else {
TextElementPosition::InitialLineStart
};
while self.ptr < self.length {
if self.take_byte_if(b'{') {
if text_element_role == TextElementPosition::LineStart {
common_indent = Some(0);
}
let exp = self.get_placeable()?;
last_non_blank = Some(elements.len());
elements.push(PatternElementPlaceholders::Placeable(exp));
text_element_role = TextElementPosition::Continuation;
} else {
let slice_start = self.ptr;
let mut indent = 0;
if text_element_role == TextElementPosition::LineStart {
indent = self.skip_blank_inline();
if let Some(b) = get_current_byte!(self) {
if indent == 0 {
if b != &b'\r' && b != &b'\n' {
break;
}
} else if !Self::is_byte_pattern_continuation(*b) {
self.ptr = slice_start;
break;
}
} else {
break;
}
}
let (start, end, text_element_type, termination_reason) = self.get_text_slice()?;
if start != end {
if text_element_role == TextElementPosition::LineStart
&& text_element_type == TextElementType::NonBlank
{
if let Some(common) = common_indent {
if indent < common {
common_indent = Some(indent);
}
} else {
common_indent = Some(indent);
}
}
if text_element_role != TextElementPosition::LineStart
|| text_element_type == TextElementType::NonBlank
|| termination_reason == TextElementTermination::LineFeed
{
if text_element_type == TextElementType::NonBlank {
last_non_blank = Some(elements.len());
}
elements.push(PatternElementPlaceholders::TextElement(
slice_start,
end,
indent,
text_element_role,
));
}
}
text_element_role = match termination_reason {
TextElementTermination::LineFeed => TextElementPosition::LineStart,
TextElementTermination::Crlf => TextElementPosition::LineStart,
TextElementTermination::PlaceableStart => TextElementPosition::Continuation,
TextElementTermination::Eof => TextElementPosition::Continuation,
};
}
}
if let Some(last_non_blank) = last_non_blank {
let elements = elements
.into_iter()
.take(last_non_blank + 1)
.enumerate()
.map(|(i, elem)| match elem {
PatternElementPlaceholders::Placeable(expression) => {
ast::PatternElement::Placeable { expression }
}
PatternElementPlaceholders::TextElement(start, end, indent, role) => {
let start = if role == TextElementPosition::LineStart {
common_indent.map_or_else(
|| start + indent,
|common_indent| start + std::cmp::min(indent, common_indent),
)
} else {
start
};
let mut value = self.source.slice(start..end);
if last_non_blank == i {
value.trim();
}
ast::PatternElement::TextElement { value }
}
})
.collect();
return Ok(Some(ast::Pattern { elements }));
}
Ok(None)
}
fn get_text_slice(
&mut self,
) -> Result<(usize, usize, TextElementType, TextElementTermination)> {
let start_pos = self.ptr;
let Some(rest) = get_remaining_bytes!(self) else {
return Ok((
start_pos,
self.ptr,
TextElementType::Blank,
TextElementTermination::Eof,
));
};
let end = memchr::memchr3(b'\n', b'{', b'}', rest);
let element_type = |text: &[u8]| {
if text.iter().any(|&c| c != b' ') {
TextElementType::NonBlank
} else {
TextElementType::Blank
}
};
match end.map(|p| &rest[..=p]) {
Some([text @ .., b'}']) => {
self.ptr += text.len();
error!(ErrorKind::UnbalancedClosingBrace, self.ptr)
}
Some([text @ .., b'\r', b'\n']) => {
self.ptr += text.len() + 1;
Ok((
start_pos,
self.ptr - 1,
element_type(text),
TextElementTermination::Crlf,
))
}
Some([text @ .., b'\n']) => {
self.ptr += text.len() + 1;
Ok((
start_pos,
self.ptr,
element_type(text),
TextElementTermination::LineFeed,
))
}
Some([text @ .., b'{']) => {
self.ptr += text.len();
Ok((
start_pos,
self.ptr,
element_type(text),
TextElementTermination::PlaceableStart,
))
}
None => {
self.ptr += rest.len();
Ok((
start_pos,
self.ptr,
element_type(rest),
TextElementTermination::Eof,
))
}
_ => unreachable!(),
}
}
}
@@ -0,0 +1,61 @@
use super::{
core::{Parser, Result},
errors::ParserError,
slice::Slice,
};
use crate::ast;
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub fn parse_runtime(
mut self,
) -> std::result::Result<ast::Resource<S>, (ast::Resource<S>, Vec<ParserError>)> {
let mut errors = vec![];
// That default allocation gives the lowest
// number of instructions and cycles in ioi.
let mut body = Vec::with_capacity(6);
self.skip_blank_block();
while self.ptr < self.length {
let entry_start = self.ptr;
let entry = self.get_entry_runtime(entry_start);
match entry {
Ok(Some(entry)) => {
body.push(entry);
}
Ok(None) => {}
Err(mut err) => {
self.skip_to_next_entry_start();
err.slice = Some(entry_start..self.ptr);
errors.push(err);
let content = self.source.slice(entry_start..self.ptr);
body.push(ast::Entry::Junk { content });
}
}
self.skip_blank_block();
}
if errors.is_empty() {
Ok(ast::Resource { body })
} else {
Err((ast::Resource { body }, errors))
}
}
fn get_entry_runtime(&mut self, entry_start: usize) -> Result<Option<ast::Entry<S>>> {
let entry = match get_current_byte!(self) {
Some(b'#') => {
self.skip_comment();
None
}
Some(b'-') => Some(ast::Entry::Term(self.get_term(entry_start)?)),
_ => Some(ast::Entry::Message(self.get_message(entry_start)?)),
};
Ok(entry)
}
}
+30
View File
@@ -0,0 +1,30 @@
use std::ops::Range;
pub(crate) fn matches_fluent_ws(c: char) -> bool {
c == ' ' || c == '\r' || c == '\n'
}
pub trait Slice<'s>: AsRef<str> + Clone + PartialEq {
fn slice(&self, range: Range<usize>) -> Self;
fn trim(&mut self);
}
impl Slice<'_> for String {
fn slice(&self, range: Range<usize>) -> Self {
self[range].to_string()
}
fn trim(&mut self) {
*self = self.trim_end_matches(matches_fluent_ws).to_string();
}
}
impl<'s> Slice<'s> for &'s str {
fn slice(&self, range: Range<usize>) -> Self {
&self[range]
}
fn trim(&mut self) {
*self = self.trim_end_matches(matches_fluent_ws);
}
}
+655
View File
@@ -0,0 +1,655 @@
//! Fluent Translation List serialization utilities
//!
//! This modules provides a way to serialize an abstract syntax tree representing a
//! Fluent Translation List. Use cases include normalization and programmatic
//! manipulation of a Fluent Translation List.
//!
//! # Example
//!
//! ```
//! use fluent_syntax::parser;
//! use fluent_syntax::serializer;
//!
//! let ftl = r#"# This is a message comment
//! hello-world = Hello World!
//! "#;
//!
//! let resource = parser::parse(ftl).expect("Failed to parse an FTL resource.");
//!
//! let serialized = serializer::serialize(&resource);
//!
//! assert_eq!(ftl, serialized);
//! ```
use crate::{ast::*, parser::matches_fluent_ws, parser::Slice};
use std::fmt::Write;
/// Serializes an abstract syntax tree representing a Fluent Translation List into a
/// String.
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::serializer;
///
/// let ftl = r#"
/// unnormalized-message=This message has
/// abnormal spacing and indentation"#;
///
/// let resource = parser::parse(ftl).expect("Failed to parse an FTL resource.");
///
/// let serialized = serializer::serialize(&resource);
///
/// let expected = r#"unnormalized-message =
/// This message has
/// abnormal spacing and indentation
/// "#;
///
/// assert_eq!(expected, serialized);
/// ```
pub fn serialize<'s, S: Slice<'s>>(resource: &Resource<S>) -> String {
serialize_with_options(resource, Options::default())
}
/// Serializes an abstract syntax tree representing a Fluent Translation List into a
/// String accepting custom options.
pub fn serialize_with_options<'s, S: Slice<'s>>(
resource: &Resource<S>,
options: Options,
) -> String {
let mut ser = Serializer::new(options);
ser.serialize_resource(resource);
ser.into_serialized_text()
}
#[derive(Debug)]
struct Serializer {
writer: TextWriter,
options: Options,
state: State,
}
impl Serializer {
fn new(options: Options) -> Self {
Serializer {
writer: TextWriter::default(),
options,
state: State::default(),
}
}
fn serialize_resource<'s, S: Slice<'s>>(&mut self, res: &Resource<S>) {
for entry in &res.body {
match entry {
Entry::Message(msg) => self.serialize_message(msg),
Entry::Term(term) => self.serialize_term(term),
Entry::Comment(comment) => self.serialize_free_comment(comment, "#"),
Entry::GroupComment(comment) => self.serialize_free_comment(comment, "##"),
Entry::ResourceComment(comment) => self.serialize_free_comment(comment, "###"),
Entry::Junk { content } => {
if self.options.with_junk {
self.serialize_junk(content.as_ref());
}
}
};
self.state.wrote_non_junk_entry = !matches!(entry, Entry::Junk { .. });
}
}
fn into_serialized_text(self) -> String {
self.writer.buffer
}
fn serialize_junk(&mut self, junk: &str) {
self.writer.write_literal(junk);
}
fn serialize_free_comment<'s, S: Slice<'s>>(&mut self, comment: &Comment<S>, prefix: &str) {
if self.state.wrote_non_junk_entry {
self.writer.newline();
}
self.serialize_comment(comment, prefix);
self.writer.newline();
}
fn serialize_comment<'s, S: Slice<'s>>(&mut self, comment: &Comment<S>, prefix: &str) {
for line in &comment.content {
self.writer.write_literal(prefix);
if !line.as_ref().trim_matches(matches_fluent_ws).is_empty() {
self.writer.write_literal(" ");
self.writer.write_literal(line.as_ref());
}
self.writer.newline();
}
}
fn serialize_message<'s, S: Slice<'s>>(&mut self, msg: &Message<S>) {
if let Some(comment) = msg.comment.as_ref() {
self.serialize_comment(comment, "#");
}
self.writer.write_literal(msg.id.name.as_ref());
self.writer.write_literal(" =");
if let Some(value) = msg.value.as_ref() {
self.serialize_pattern(value);
}
self.serialize_attributes(&msg.attributes);
self.writer.newline();
}
fn serialize_term<'s, S: Slice<'s>>(&mut self, term: &Term<S>) {
if let Some(comment) = term.comment.as_ref() {
self.serialize_comment(comment, "#");
}
self.writer.write_literal("-");
self.writer.write_literal(term.id.name.as_ref());
self.writer.write_literal(" =");
self.serialize_pattern(&term.value);
self.serialize_attributes(&term.attributes);
self.writer.newline();
}
fn serialize_pattern<'s, S: Slice<'s>>(&mut self, pattern: &Pattern<S>) {
let start_on_newline = pattern.starts_on_new_line();
if start_on_newline {
self.writer.newline();
self.writer.indent();
} else {
self.writer.write_literal(" ");
}
for element in &pattern.elements {
self.serialize_element(element);
}
if start_on_newline {
self.writer.dedent();
}
}
fn serialize_attributes<'s, S: Slice<'s>>(&mut self, attrs: &[Attribute<S>]) {
if attrs.is_empty() {
return;
}
self.writer.indent();
for attr in attrs {
self.writer.newline();
self.serialize_attribute(attr);
}
self.writer.dedent();
}
fn serialize_attribute<'s, S: Slice<'s>>(&mut self, attr: &Attribute<S>) {
self.writer.write_literal(".");
self.writer.write_literal(attr.id.name.as_ref());
self.writer.write_literal(" =");
self.serialize_pattern(&attr.value);
}
fn serialize_element<'s, S: Slice<'s>>(&mut self, elem: &PatternElement<S>) {
match elem {
PatternElement::TextElement { value } => self.writer.write_literal(value.as_ref()),
PatternElement::Placeable { expression } => match expression {
Expression::Inline(InlineExpression::Placeable { expression }) => {
// A placeable inside a placeable is a special case because we
// don't want the braces to look silly (e.g. "{ { Foo() } }").
self.writer.write_literal("{{ ");
self.serialize_expression(expression);
self.writer.write_literal(" }}");
}
Expression::Select { .. } => {
// select adds its own newline and indent, emit the brace
// *without* a space so we don't get 5 spaces instead of 4
self.writer.write_literal("{ ");
self.serialize_expression(expression);
self.writer.write_literal("}");
}
Expression::Inline(_) => {
self.writer.write_literal("{ ");
self.serialize_expression(expression);
self.writer.write_literal(" }");
}
},
}
}
fn serialize_expression<'s, S: Slice<'s>>(&mut self, expr: &Expression<S>) {
match expr {
Expression::Inline(inline) => self.serialize_inline_expression(inline),
Expression::Select { selector, variants } => {
self.serialize_select_expression(selector, variants);
}
}
}
fn serialize_inline_expression<'s, S: Slice<'s>>(&mut self, expr: &InlineExpression<S>) {
match expr {
InlineExpression::StringLiteral { value } => {
self.writer.write_literal("\"");
self.writer.write_literal(value.as_ref());
self.writer.write_literal("\"");
}
InlineExpression::NumberLiteral { value } => self.writer.write_literal(value.as_ref()),
InlineExpression::VariableReference {
id: Identifier { name: value },
} => {
self.writer.write_literal("$");
self.writer.write_literal(value.as_ref());
}
InlineExpression::FunctionReference { id, arguments } => {
self.writer.write_literal(id.name.as_ref());
self.serialize_call_arguments(arguments);
}
InlineExpression::MessageReference { id, attribute } => {
self.writer.write_literal(id.name.as_ref());
if let Some(attr) = attribute.as_ref() {
self.writer.write_literal(".");
self.writer.write_literal(attr.name.as_ref());
}
}
InlineExpression::TermReference {
id,
attribute,
arguments,
} => {
self.writer.write_literal("-");
self.writer.write_literal(id.name.as_ref());
if let Some(attr) = attribute.as_ref() {
self.writer.write_literal(".");
self.writer.write_literal(attr.name.as_ref());
}
if let Some(args) = arguments.as_ref() {
self.serialize_call_arguments(args);
}
}
InlineExpression::Placeable { expression } => {
self.writer.write_literal("{");
self.serialize_expression(expression);
self.writer.write_literal("}");
}
}
}
fn serialize_select_expression<'s, S: Slice<'s>>(
&mut self,
selector: &InlineExpression<S>,
variants: &[Variant<S>],
) {
self.serialize_inline_expression(selector);
self.writer.write_literal(" ->");
self.writer.newline();
self.writer.indent();
for variant in variants {
self.serialize_variant(variant);
self.writer.newline();
}
self.writer.dedent();
}
fn serialize_variant<'s, S: Slice<'s>>(&mut self, variant: &Variant<S>) {
if variant.default {
self.writer.write_char_into_indent('*');
}
self.writer.write_literal("[");
self.serialize_variant_key(&variant.key);
self.writer.write_literal("]");
self.serialize_pattern(&variant.value);
}
fn serialize_variant_key<'s, S: Slice<'s>>(&mut self, key: &VariantKey<S>) {
match key {
VariantKey::NumberLiteral { value } | VariantKey::Identifier { name: value } => {
self.writer.write_literal(value.as_ref());
}
}
}
fn serialize_call_arguments<'s, S: Slice<'s>>(&mut self, args: &CallArguments<S>) {
let mut argument_written = false;
self.writer.write_literal("(");
for positional in &args.positional {
if argument_written {
self.writer.write_literal(", ");
}
self.serialize_inline_expression(positional);
argument_written = true;
}
for named in &args.named {
if argument_written {
self.writer.write_literal(", ");
}
self.writer.write_literal(named.name.name.as_ref());
self.writer.write_literal(": ");
self.serialize_inline_expression(&named.value);
argument_written = true;
}
self.writer.write_literal(")");
}
}
impl<'s, S: Slice<'s>> Pattern<S> {
fn starts_on_new_line(&self) -> bool {
!self.has_leading_text_dot() && self.is_multiline()
}
fn is_multiline(&self) -> bool {
self.elements.iter().any(|elem| match elem {
PatternElement::TextElement { value } => value.as_ref().contains('\n'),
PatternElement::Placeable { expression } => is_select_expr(expression),
})
}
fn has_leading_text_dot(&self) -> bool {
if let Some(PatternElement::TextElement { value }) = self.elements.first() {
value.as_ref().starts_with('.')
} else {
false
}
}
}
fn is_select_expr<'s, S: Slice<'s>>(expr: &Expression<S>) -> bool {
match expr {
Expression::Select { .. } => true,
Expression::Inline(InlineExpression::Placeable { expression }) => {
is_select_expr(expression)
}
Expression::Inline(_) => false,
}
}
/// Options for serializing an abstract syntax tree.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Options {
/// Whether invalid text fragments should be serialized, too.
pub with_junk: bool,
}
#[derive(Debug, Default, PartialEq)]
struct State {
wrote_non_junk_entry: bool,
}
#[derive(Clone, Debug, Default)]
struct TextWriter {
buffer: String,
indent_level: usize,
}
impl TextWriter {
fn indent(&mut self) {
self.indent_level += 1;
}
fn dedent(&mut self) {
self.indent_level = self
.indent_level
.checked_sub(1)
.expect("Dedenting without a corresponding indent");
}
fn write_indent(&mut self) {
for _ in 0..self.indent_level {
self.buffer.push_str(" ");
}
}
fn newline(&mut self) {
if self.buffer.ends_with('\r') {
// handle rare edge case, where the trailing `\r` would get confused
// as part of the line ending
self.buffer.push('\r');
}
self.buffer.push('\n');
}
fn write_literal(&mut self, item: &str) {
if self.buffer.ends_with('\n') {
// we've just added a newline, make sure it's properly indented
self.write_indent();
}
write!(self.buffer, "{}", item).expect("Writing to an in-memory buffer never fails");
}
fn write_char_into_indent(&mut self, ch: char) {
if self.buffer.ends_with('\n') {
self.write_indent();
}
self.buffer.pop();
self.buffer.push(ch);
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::parser::parse;
#[test]
fn write_something_then_indent() {
let mut writer = TextWriter::default();
writer.write_literal("foo =");
writer.newline();
writer.indent();
writer.write_literal("first line");
writer.newline();
writer.write_literal("second line");
writer.newline();
writer.dedent();
writer.write_literal("not indented");
writer.newline();
let got = &writer.buffer;
assert_eq!(
got,
"foo =\n first line\n second line\nnot indented\n"
);
}
macro_rules! text_message {
($name:expr, $value:expr) => {
Entry::Message(Message {
id: Identifier { name: $name },
value: Some(Pattern {
elements: vec![PatternElement::TextElement { value: $value }],
}),
attributes: vec![],
comment: None,
})
};
}
impl<'a> Entry<&'a str> {
fn as_message(&mut self) -> &mut Message<&'a str> {
match self {
Self::Message(msg) => msg,
_ => panic!("Expected Message"),
}
}
}
impl<'a> Message<&'a str> {
fn as_pattern(&mut self) -> &mut Pattern<&'a str> {
self.value.as_mut().expect("Expected Pattern")
}
}
impl<'a> PatternElement<&'a str> {
fn as_text(&mut self) -> &mut &'a str {
match self {
Self::TextElement { value } => value,
_ => panic!("Expected TextElement"),
}
}
fn as_expression(&mut self) -> &mut Expression<&'a str> {
match self {
Self::Placeable { expression } => expression,
_ => panic!("Expected Placeable"),
}
}
}
impl<'a> Expression<&'a str> {
fn as_variants(&mut self) -> &mut Vec<Variant<&'a str>> {
match self {
Self::Select { variants, .. } => variants,
_ => panic!("Expected Select"),
}
}
fn as_inline_variable_id(&mut self) -> &mut Identifier<&'a str> {
match self {
Self::Inline(InlineExpression::VariableReference { id }) => id,
_ => panic!("Expected Inline"),
}
}
}
#[test]
fn change_id() {
let mut ast = parse("foo = bar\n").expect("failed to parse ftl resource");
ast.body[0].as_message().id.name = "baz";
assert_eq!(serialize(&ast), "baz = bar\n");
}
#[test]
fn change_value() {
let mut ast = parse("foo = bar\n").expect("failed to parse ftl resource");
*ast.body[0].as_message().as_pattern().elements[0].as_text() = "baz";
assert_eq!("foo = baz\n", serialize(&ast));
}
#[test]
fn add_expression_variant() {
let message = concat!(
"foo =\n",
" { $num ->\n",
" *[other] { $num } bars\n",
" }\n"
);
let mut ast = parse(message).expect("failed to parse ftl resource");
let one_variant = Variant {
key: VariantKey::Identifier { name: "one" },
value: Pattern {
elements: vec![
PatternElement::Placeable {
expression: Expression::Inline(InlineExpression::VariableReference {
id: Identifier { name: "num" },
}),
},
PatternElement::TextElement { value: " bar" },
],
},
default: false,
};
ast.body[0].as_message().as_pattern().elements[0]
.as_expression()
.as_variants()
.insert(0, one_variant);
let expected = concat!(
"foo =\n",
" { $num ->\n",
" [one] { $num } bar\n",
" *[other] { $num } bars\n",
" }\n"
);
assert_eq!(serialize(&ast), expected);
}
#[test]
fn change_variable_reference() {
let mut ast = parse("foo = { $bar }\n").expect("failed to parse ftl resource");
ast.body[0].as_message().as_pattern().elements[0]
.as_expression()
.as_inline_variable_id()
.name = "qux";
assert_eq!("foo = { $qux }\n", serialize(&ast));
}
#[test]
fn remove_message() {
let mut ast = parse("foo = bar\nbaz = qux\n").expect("failed to parse ftl resource");
ast.body.pop();
assert_eq!("foo = bar\n", serialize(&ast));
}
#[test]
fn add_message_at_top() {
let mut ast = parse("foo = bar\n").expect("failed to parse ftl resource");
ast.body.insert(0, text_message!("baz", "qux"));
assert_eq!("baz = qux\nfoo = bar\n", serialize(&ast));
}
#[test]
fn add_message_at_end() {
let mut ast = parse("foo = bar\n").expect("failed to parse ftl resource");
ast.body.push(text_message!("baz", "qux"));
assert_eq!("foo = bar\nbaz = qux\n", serialize(&ast));
}
#[test]
fn add_message_in_between() {
let mut ast = parse("foo = bar\nbaz = qux\n").expect("failed to parse ftl resource");
ast.body.insert(1, text_message!("hello", "there"));
assert_eq!("foo = bar\nhello = there\nbaz = qux\n", serialize(&ast));
}
#[test]
fn add_message_comment() {
let mut ast = parse("foo = bar\n").expect("failed to parse ftl resource");
ast.body[0].as_message().comment.replace(Comment {
content: vec!["great message!"],
});
assert_eq!("# great message!\nfoo = bar\n", serialize(&ast));
}
#[test]
fn remove_message_comment() {
let mut ast = parse("# great message!\nfoo = bar\n").expect("failed to parse ftl resource");
ast.body[0].as_message().comment.take();
assert_eq!("foo = bar\n", serialize(&ast));
}
#[test]
fn edit_message_comment() {
let mut ast = parse("# great message!\nfoo = bar\n").expect("failed to parse ftl resource");
ast.body[0]
.as_message()
.comment
.as_mut()
.expect("comment is missing")
.content[0] = "very original";
assert_eq!("# very original\nfoo = bar\n", serialize(&ast));
}
}
+144
View File
@@ -0,0 +1,144 @@
//! A set of helper functions for unescaping Fluent unicode escape sequences.
//!
//! # Unicode
//!
//! Fluent supports UTF-8 in all FTL resources, but it also allows
//! unicode sequences to be escaped in [`String
//! Literals`](super::ast::InlineExpression::StringLiteral).
//!
//! Four byte sequences are encoded with `\u` and six byte
//! sequences using `\U`.
//! ## Example
//!
//! ```
//! use fluent_syntax::unicode::unescape_unicode_to_string;
//!
//! assert_eq!(
//! unescape_unicode_to_string("Foo \\u5bd2 Bar"),
//! "Foo 寒 Bar"
//! );
//!
//! assert_eq!(
//! unescape_unicode_to_string("Foo \\U01F68A Bar"),
//! "Foo 🚊 Bar"
//! );
//! ```
//!
//! # Other unescapes
//!
//! This also allows for a char `"` to be present inside an FTL string literal,
//! and for `\` itself to be escaped.
//!
//! ## Example
//!
//! ```
//! use fluent_syntax::unicode::unescape_unicode_to_string;
//!
//! assert_eq!(
//! unescape_unicode_to_string("Foo \\\" Bar"),
//! "Foo \" Bar"
//! );
//! assert_eq!(
//! unescape_unicode_to_string("Foo \\\\ Bar"),
//! "Foo \\ Bar"
//! );
//! ```
use std::borrow::Cow;
use std::char;
use std::fmt;
const UNKNOWN_CHAR: char = '';
fn encode_unicode(s: Option<&str>) -> char {
s.and_then(|s| u32::from_str_radix(s, 16).ok().and_then(char::from_u32))
.unwrap_or(UNKNOWN_CHAR)
}
/// Unescapes to a writer without allocating.
///
/// ## Example
///
/// ```
/// use fluent_syntax::unicode::unescape_unicode;
///
/// let mut s = String::new();
/// unescape_unicode(&mut s, "Foo \\U01F60A Bar");
/// assert_eq!(s, "Foo 😊 Bar");
/// ```
pub fn unescape_unicode<W>(w: &mut W, input: &str) -> fmt::Result
where
W: fmt::Write,
{
if unescape(w, input)? {
return Ok(());
}
w.write_str(input)
}
fn unescape<W>(w: &mut W, input: &str) -> Result<bool, std::fmt::Error>
where
W: fmt::Write,
{
let bytes = input.as_bytes();
let mut start = 0;
let mut ptr = 0;
while let Some(b) = bytes.get(ptr) {
if b != &b'\\' {
ptr += 1;
continue;
}
if start != ptr {
w.write_str(&input[start..ptr])?;
}
ptr += 1;
let new_char = match bytes.get(ptr) {
Some(b'\\') => '\\',
Some(b'"') => '"',
Some(u @ b'u') | Some(u @ b'U') => {
let seq_start = ptr + 1;
let len = if u == &b'u' { 4 } else { 6 };
ptr += len;
encode_unicode(input.get(seq_start..seq_start + len))
}
_ => UNKNOWN_CHAR,
};
ptr += 1;
w.write_char(new_char)?;
start = ptr;
}
if start == 0 {
return Ok(false);
}
if start != ptr {
w.write_str(&input[start..ptr])?;
}
Ok(true)
}
/// Unescapes to a `Cow<str>` optionally allocating.
///
/// ## Example
///
/// ```
/// use fluent_syntax::unicode::unescape_unicode_to_string;
///
/// assert_eq!(
/// unescape_unicode_to_string("Foo \\U01F60A Bar"),
/// "Foo 😊 Bar"
/// );
/// ```
pub fn unescape_unicode_to_string(input: &str) -> Cow<str> {
let mut result = String::new();
let owned = unescape(&mut result, input).expect("String write methods don't Err");
if owned {
Cow::Owned(result)
} else {
Cow::Borrowed(input)
}
}