Skip to content

Commit a5d467e

Browse files
authored
Add docs to fluent.syntax (#139)
1 parent dd08ff4 commit a5d467e

10 files changed

Lines changed: 169 additions & 4 deletions

File tree

fluent.syntax/docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

fluent.syntax/docs/ast.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AST & Tooling
2+
=============
3+
4+
5+
.. automodule:: fluent.syntax.ast
6+
:members:
7+
:exclude-members: scalars_equal
8+
:show-inheritance:

fluent.syntax/docs/conf.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
13+
import os
14+
import sys
15+
sys.path.insert(0, os.path.abspath('..'))
16+
17+
18+
# -- Project information -----------------------------------------------------
19+
20+
project = 'Fluent Syntax'
21+
copyright = '2020'
22+
23+
24+
# -- General configuration ---------------------------------------------------
25+
26+
# Add any Sphinx extension module names here, as strings. They can be
27+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
28+
# ones.
29+
extensions = [
30+
'sphinx.ext.autodoc',
31+
'sphinx.ext.intersphinx',
32+
]
33+
34+
# Add any paths that contain templates here, relative to this directory.
35+
templates_path = ['_templates']
36+
37+
# List of patterns, relative to source directory, that match files and
38+
# directories to ignore when looking for source files.
39+
# This pattern also affects html_static_path and html_extra_path.
40+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
41+
42+
43+
# -- Options for HTML output -------------------------------------------------
44+
45+
# The theme to use for HTML and HTML Help pages. See the documentation for
46+
# a list of builtin themes.
47+
#
48+
html_theme = 'nature'
49+
50+
# Add any paths that contain custom static files (such as style sheets) here,
51+
# relative to this directory. They are copied after the builtin static files,
52+
# so a file named "default.css" will overwrite the builtin "default.css".
53+
html_static_path = ['_static']
54+
55+
56+
# -- Extension configuration -------------------------------------------------
57+
58+
# -- Options for intersphinx extension ---------------------------------------
59+
60+
# Example configuration for intersphinx: refer to the Python standard library.
61+
intersphinx_mapping = {'https://docs.python.org/3/': None}
62+
63+
# -- Options for autodoc extension -------------------------------------------
64+
65+
autodoc_member_order = 'bysource'

fluent.syntax/docs/index.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Fluent Syntax
2+
=============
3+
4+
The :py:mod:`fluent.syntax` library is built for tooling around Fluent. It's
5+
designed to parse, analyze, process, and serialize Fluent files.
6+
7+
The :py:mod:`fluent.syntax.ast` module implements ``Visitor`` and ``Transformer``
8+
patterns, which are the recommended interfaces to be used in analysis and
9+
post-processing of Fluent source files.
10+
11+
.. toctree::
12+
:maxdepth: 2
13+
:caption: Contents:
14+
15+
reference
16+
17+
18+
Indices and tables
19+
==================
20+
21+
* :ref:`genindex`
22+
* :ref:`modindex`
23+
* :ref:`search`

fluent.syntax/docs/parsing.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Parsing
2+
=======
3+
4+
.. py:module:: fluent.syntax.parser
5+
6+
.. autoclass:: fluent.syntax.parser.FluentParser
7+
:members: parse, parse_entry

fluent.syntax/docs/reference.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
API Reference
2+
=============
3+
4+
The :py:mod:`fluent.syntax` module contains two helper methods.
5+
6+
The :py:mod:`fluent.syntax.parser` and :py:mod:`fluent.syntax.serializer` modules
7+
provide more fine-grained control and detail.
8+
9+
10+
.. automodule:: fluent.syntax
11+
:members:
12+
13+
14+
.. toctree::
15+
:maxdepth: 2
16+
:caption: Contents:
17+
18+
parsing
19+
ast
20+
serializing

fluent.syntax/docs/serializing.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Serializing
2+
===========
3+
4+
.. automodule:: fluent.syntax.serializer
5+
:members:

fluent.syntax/fluent/syntax/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44

55
def parse(source, **kwargs):
6+
"""Create an ast.Resource from a Fluent Syntax source.
7+
"""
68
parser = FluentParser(**kwargs)
79
return parser.parse(source)
810

911

1012
def serialize(resource, **kwargs):
13+
"""Serialize an ast.Resource to a unicode string.
14+
"""
1115
serializer = FluentSerializer(**kwargs)
1216
return serializer.serialize(resource)

fluent.syntax/fluent/syntax/parser.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ def decorated(self, ps, *args, **kwargs):
2626

2727

2828
class FluentParser(object):
29+
"""This class is used to parse Fluent source content.
30+
31+
``with_spans`` enables source information in the form of
32+
:class:`.ast.Span` objects for each :class:`.ast.SyntaxNode`.
33+
"""
2934
def __init__(self, with_spans=True):
3035
self.with_spans = with_spans
3136

3237
def parse(self, source):
38+
"""Create a :class:`.ast.Resource` from a Fluent source.
39+
"""
3340
ps = FluentParserStream(source)
3441
ps.skip_blank_block()
3542

@@ -73,13 +80,13 @@ def parse(self, source):
7380
return res
7481

7582
def parse_entry(self, source):
76-
"""Parse the first Message or Term in source.
83+
"""Parse the first :class:`.ast.Entry` in source.
7784
78-
Skip all encountered comments and start parsing at the first Mesage
79-
or Term start. Return Junk if the parsing is not successful.
85+
Skip all encountered comments and start parsing at the first :class:`.ast.Message`
86+
or :class:`.ast.Term` start. Return :class:`.ast.Junk` if the parsing is not successful.
8087
8188
Preceding comments are ignored unless they contain syntax errors
82-
themselves, in which case Junk for the invalid comment is returned.
89+
themselves, in which case :class:`.ast.Junk` for the invalid comment is returned.
8390
"""
8491
ps = FluentParserStream(source)
8592
ps.skip_blank_block()

fluent.syntax/fluent/syntax/serializer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ def is_select_expr(elem):
1919

2020

2121
class FluentSerializer(object):
22+
"""FluentSerializer converts :class:`.ast.SyntaxNode` objects to unicode strings.
23+
24+
`with_junk` controls if parse errors are written back or not.
25+
"""
2226
HAS_ENTRIES = 1
2327

2428
def __init__(self, with_junk=False):
2529
self.with_junk = with_junk
2630

2731
def serialize(self, resource):
32+
"Serialize a :class:`.ast.Resource` to a string."
2833
if not isinstance(resource, ast.Resource):
2934
raise Exception('Unknown resource type: {}'.format(type(resource)))
3035

@@ -40,6 +45,7 @@ def serialize(self, resource):
4045
return "".join(parts)
4146

4247
def serialize_entry(self, entry, state=0):
48+
"Serialize an :class:`.ast.Entry` to a string."
4349
if isinstance(entry, ast.Message):
4450
return serialize_message(entry)
4551
if isinstance(entry, ast.Term):

0 commit comments

Comments
 (0)