Skip to content

Commit abe8f78

Browse files
committed
Move Processor docs to docs/
1 parent 4de7f43 commit abe8f78

File tree

4 files changed

+117
-61
lines changed

4 files changed

+117
-61
lines changed

fluent.syntax/docs/processing.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Processing
2+
==========
3+
4+
.. code-block:: kotlin
5+
6+
package org.projectfluent.syntax.processor
7+
8+
import org.projectfluent.syntax.ast.*
9+
10+
/**
11+
* Process patterns by returning new patterns with elements transformed.
12+
*/
13+
class Processor {
14+
/**
15+
* "Bake" the values of StringLiterals into TextElements. This is a lossy
16+
* transformation for literals which are not special in Fluent syntax.
17+
*/
18+
fun unescapeLiteralsToText(pattern: Pattern): Pattern
19+
20+
/**
21+
* "Un-bake" special characters into StringLiterals, which would otherwise
22+
* cause syntax errors with Fluent parsers.
23+
*/
24+
fun escapeTextToLiterals(pattern: Pattern): Pattern
25+
}

fluent.syntax/docs/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ provide more fine-grained control and detail.
1616
ast
1717
visitor
1818
serializing
19+
processing

fluent.syntax/docs/usage.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Using syntax
44
The ``org.projectfluent:syntax`` package provides a parser, a serializer, and libraries
55
for analysis and processing of Fluent files.
66

7+
78
Parsing
89
-------
910

@@ -29,6 +30,7 @@ To create Fluent syntax from AST objects, use
2930
serializer.serialize(resource)
3031
serializer.serialize(resource.body[0])
3132
33+
3234
Analysis (Visitor)
3335
------------------
3436

@@ -51,6 +53,7 @@ to continue iteration.
5153
}
5254
}
5355
56+
5457
Custom Traversal (childrenOf)
5558
-----------------------------
5659

@@ -69,3 +72,89 @@ iterate over children.
6972
listOf("default", "key", "span", "value"),
7073
variant_props.map { (name, _) -> name }.sorted().toList()
7174
)
75+
76+
77+
Pattern Processing
78+
------------------
79+
80+
The :py:class:`syntax.processor.Processor` class can be used to transform
81+
patterns in a way that is friendly to localization workflows which want to
82+
allow text characters which are special in Fluent to be written as regular
83+
text.
84+
85+
According to the Fluent syntax, characters like the curly braces must be
86+
enclosed in :py:class:`syntax.ast.StringLiteral` instances if they are
87+
supposed to be part of the translation content. Otherwise, an open curly
88+
brace would start a :py:class:`syntax.ast.Placeable` and likely lead to a
89+
syntax error.
90+
91+
Workflows in which the support for Fluent placeables is limited may choose to
92+
provide their own visual cues for them. This often comes in form of visual
93+
placeholders which can be rearranged within a translation segment by the
94+
translator, but whose contents cannot be modified.
95+
96+
In these workflows, the special meaning of the character like the curly
97+
braces is void; the translator is not able to insert new placeables by
98+
opening a curly brace anyways. Thus, for translators' convenience, the curly
99+
brace can be treated as a regular text character and part of the translation
100+
content.
101+
102+
The :py:class:`syntax.processor.Processor`'s methods allow baking
103+
:py:class:`syntax.ast.StringLiteral` instances into surrounding
104+
:py:class:`syntax.ast.TextElement` instances, and then "un-baking" them again
105+
if required by the Fluent syntax. Note that all string literals are baked,
106+
while only some are un-baked. The processing is a lossy transformation.
107+
108+
.. note::
109+
Processed patterns are not valid Fluent AST nodes anymore and must not be
110+
serialized without first un-processing them.
111+
112+
113+
Baking literals into text
114+
^^^^^^^^^^^^^^^^^^^^^^^^^
115+
116+
Use the :py:func:`unescapeLiteralsToText` method to bake the values of string
117+
literals into the surrounding text elements. This is a lossy transformation
118+
for literals which are not special in Fluent syntax.
119+
120+
Examples::
121+
122+
→Hello, {"{-_-}"}.
123+
←Hello, {-_-}.
124+
125+
→{" "}Hello, world!
126+
← Hello, world!
127+
128+
→A multiline pattern:
129+
{"*"} Asterisk is special
130+
←A multiline pattern:
131+
* Asterisk is special
132+
133+
→Copyright {"\u00A9"} 2020
134+
←Copyright © 2020
135+
136+
137+
Un-baking special characters into literals
138+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139+
140+
Use the :py:func:`escapeTextToLiterals` method to un-bake special characters
141+
into string literals, which would otherwise cause syntax errors with Fluent
142+
parsers. Character sequences which might have been previously enclosed in
143+
string literals will not be un-baked as long as they are valid text
144+
characters in Fluent syntax.
145+
146+
Examples::
147+
148+
→Hello, {-_-}.
149+
←Hello, {"{"}-_-{"}"}.
150+
151+
→ Hello, world!
152+
←{""} Hello, world!
153+
154+
→A multiline pattern:
155+
* Asterisk is special
156+
←A multiline pattern:
157+
{"*"} Asterisk is special
158+
159+
→Copyright © 2020
160+
←Copyright © 2020

fluent.syntax/src/main/kotlin/org/projectfluent/syntax/processor/Processor.kt

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,12 @@ private val special =
77
"""\\(([\\"])|(u[0-9a-fA-F]{4}))""".toRegex()
88

99
/**
10-
* Processor for transforming patterns.
11-
*
12-
* This class can be used to transform patterns in a way that is friendly
13-
* to localization workflows which want to allow text characters which are
14-
* special in Fluent to be written as regular text.
15-
*
16-
* According to the Fluent syntax, characters like the curly braces must be
17-
* enclosed in StringLiterals if they are supposed to be part of the
18-
* translation content. Otherwise, an open curly brace would start a Placeable
19-
* and likely lead to a syntax error.
20-
*
21-
* Workflows in which the support for Fluent placeables is limited may choose
22-
* to provide their own visual cues for them. This often comes in form of
23-
* visual placeholders which can be rearranged within a translation
24-
* segment by the translator, but whose contents cannot be modified.
25-
*
26-
* In these workflows, the special meaning of the character like the curly
27-
* braces is void; the translator is not able to insert new placeables by
28-
* opening a curly brace anyways. Thus, for translators' convenience, the
29-
* curly brace can be treated as a regular text character and part of the
30-
* translation content.
31-
*
32-
* The Processor's methods allow baking StringLiterals into TextElements, and
33-
* then "un-baking" them again if required by the Fluent syntax. Note that
34-
* all StringLiterals are baked, while only some are un-baked. The Processor
35-
* is lossy.
10+
* Process patterns by returning new patterns with elements transformed.
3611
*/
3712
class Processor {
3813
/**
3914
* "Bake" the values of StringLiterals into TextElements. This is a lossy
4015
* transformation for literals which are not special in Fluent syntax.
41-
*
42-
* Examples:
43-
*
44-
* →Hello, {"{-_-}"}.
45-
* ←Hello, {-_-}.
46-
*
47-
* →{" "}Hello, world!
48-
* ← Hello, world!
49-
*
50-
* →A multiline pattern:
51-
* {"*"} Asterisk is special
52-
* ←A multiline pattern:
53-
* * Asterisk is special
54-
*
55-
* →Copyright {"\u00A9"} 2020
56-
* ←Copyright © 2020
5716
*/
5817
fun unescapeLiteralsToText(pattern: Pattern): Pattern {
5918
val result = Pattern()
@@ -65,25 +24,7 @@ class Processor {
6524

6625
/**
6726
* "Un-bake" special characters into StringLiterals, which would otherwise
68-
* cause syntax errors with Fluent parsers. Character sequences which might
69-
* have been previously enclosed in StringLiterals, will not be un-baked,
70-
* provided they are valid text characters in Fluent syntax.
71-
*
72-
* Examples:
73-
*
74-
* →Hello, {-_-}.
75-
* ←Hello, {"{"}-_-{"}"}.
76-
*
77-
* → Hello, world!
78-
* ←{""} Hello, world!
79-
*
80-
* →A multiline pattern:
81-
* * Asterisk is special
82-
* ←A multiline pattern:
83-
* {"*"} Asterisk is special
84-
*
85-
* →Copyright © 2020
86-
* ←Copyright © 2020
27+
* cause syntax errors with Fluent parsers.
8728
*/
8829
fun escapeTextToLiterals(pattern: Pattern): Pattern {
8930
val result = Pattern()

0 commit comments

Comments
 (0)