|
| 1 | +Using fluent.syntax |
| 2 | +=================== |
| 3 | + |
| 4 | +The ``fluent.syntax`` package provides a parser, a serializer, and libraries |
| 5 | +for analysis and processing of Fluent files. |
| 6 | + |
| 7 | +Parsing |
| 8 | +------- |
| 9 | + |
| 10 | +To parse a full resource, you can use the :py:func:`fluent.syntax.parse` |
| 11 | +shorthand: |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + from fluent.syntax import parse |
| 16 | + resource = parse(""" |
| 17 | + ### Fluent resource comment |
| 18 | +
|
| 19 | + first = creating a { $thing } |
| 20 | + second = more content |
| 21 | + """) |
| 22 | +
|
| 23 | +To parse a single :py:class:`fluent.syntax.ast.Message` or :py:class:`fluent.syntax.ast.Term`, use |
| 24 | +:py:meth:`fluent.syntax.parser.FluentParser.parse_entry`: |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + from fluent.syntax.parser import FluentParser |
| 29 | + parser = FluentParser() |
| 30 | + key_message = parser.parse_entry(""" |
| 31 | + ### Fluent resource comment |
| 32 | +
|
| 33 | + key = value |
| 34 | + """) |
| 35 | +
|
| 36 | +Serialization |
| 37 | +------------- |
| 38 | + |
| 39 | +To create Fluent syntax from AST objects, use :py:func:`fluent.syntax.serialize` or |
| 40 | +:py:class:`fluent.syntax.serializer.FluentSerializer`. |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + from fluent.syntax import serialize |
| 45 | + from fluent.syntax.serializer import FluentSerializer |
| 46 | + serialize(resource) |
| 47 | + serializer = FluentSerializer() |
| 48 | + serializer.serialize(resource) |
| 49 | + serializer.serialize_entry(key_message) |
| 50 | +
|
| 51 | +Analysis (Visitor) |
| 52 | +------------------ |
| 53 | + |
| 54 | +To analyze an AST tree in a read-only fashion, you can subclass |
| 55 | +:py:class:`fluent.syntax.visitor.Visitor`. |
| 56 | + |
| 57 | +You overload individual :py:func:`visit_NodeName` methods to |
| 58 | +handle nodes of that type, and then call into :py:func`self.generic_visit` |
| 59 | +to continue iteration. |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + from fluent.syntax import visitor |
| 64 | + import re |
| 65 | +
|
| 66 | + class WordCounter(visitor.Visitor): |
| 67 | + COUNTER = re.compile(r"[\w,.-]+") |
| 68 | + @classmethod |
| 69 | + def count(cls, node): |
| 70 | + wordcounter = cls() |
| 71 | + wordcounter.visit(node) |
| 72 | + return wordcounter.word_count |
| 73 | + def __init__(self): |
| 74 | + super() |
| 75 | + self.word_count = 0 |
| 76 | + def visit_TextElement(self, node): |
| 77 | + self.word_count += len(self.COUNTER.findall(node.value)) |
| 78 | + self.generic_visit(node) |
| 79 | +
|
| 80 | + WordCounter.count(resource) |
| 81 | + WordCounter.count(key_message) |
| 82 | +
|
| 83 | +In-place Modification (Transformer) |
| 84 | +----------------------------------- |
| 85 | + |
| 86 | +Manipulation of an AST tree can be done in-place with a subclass of |
| 87 | +:py:class:`fluent.syntax.visitor.Transformer`. The coding pattern matches that |
| 88 | +of :py:class:`visitor.Visitor`, but you can modify the node in-place. |
| 89 | +You can even return different types, or remove nodes alltogether. |
| 90 | + |
| 91 | +.. code-block:: python |
| 92 | +
|
| 93 | + class Skeleton(visitor.Transformer): |
| 94 | + def visit_SelectExpression(self, node): |
| 95 | + # This should do more checks, good enough for docs |
| 96 | + for variant in node.variants: |
| 97 | + if variant.default: |
| 98 | + default_variant = variant |
| 99 | + break |
| 100 | + template_variant = self.visit(default_variant) |
| 101 | + template_variant.default = False |
| 102 | + node.variants[:] = [] |
| 103 | + for key in ('one', 'few', 'many'): |
| 104 | + variant = template_variant.clone() |
| 105 | + variant.key.name = key |
| 106 | + node.variants.append(variant) |
| 107 | + node.variants[-1].default = True |
| 108 | + return node |
| 109 | + def visit_TextElement(self, node): |
| 110 | + return None |
| 111 | +
|
| 112 | + skeleton = Skeleton() |
| 113 | + skeleton.visit(key_message) |
| 114 | + WordCounter.count(key_message) |
| 115 | + # Returns 0. |
| 116 | + new_plural = skeleton.visit(parser.parse_entry(""" |
| 117 | + with-plural = { $num -> |
| 118 | + [one] Using { -one-term-reference } to hide |
| 119 | + *[other] Using { $num } {-term-reference} as template |
| 120 | + } |
| 121 | + """)) |
| 122 | + print(serializer.serialize_entry(new_plural)) |
| 123 | +
|
| 124 | +This returns |
| 125 | + |
| 126 | +.. code-block:: fluent |
| 127 | +
|
| 128 | + with-plural = |
| 129 | + { $num -> |
| 130 | + [one] { $num }{ -term-reference } |
| 131 | + [few] { $num }{ -term-reference } |
| 132 | + *[many] { $num }{ -term-reference } |
| 133 | + } |
| 134 | +
|
| 135 | +
|
| 136 | +.. warning:: |
| 137 | + |
| 138 | + Serializing an AST tree that was created like this might not produce |
| 139 | + valid Fluent content. |
| 140 | + |
0 commit comments