"""
Source code generator.
The `srcgen` module contains generic helper routines and classes for generating
source code.
"""
# noqa
pass
"""
Source code formatter class.
- Collect source code to be written to a file.
- Keep track of indentation.
Indentation example:
>>> f = Formatter()
>>> f.line('Hello line 1')
>>> f.writelines()
Hello line 1
>>> f.indent_push()
>>> f.comment('Nested comment')
>>> f.indent_pop()
>>> f.format('Back {} again', 'home')
>>> f.writelines()
Hello line 1
// Nested comment
Back home again
"""
= 4
# type: () -> None
=
= # type: List[str]
# type: () -> None
"""Increase current indentation level by one."""
+= *
# type: () -> None
"""Decrease indentation by one level."""
assert != ,
=
# type: (str) -> None
"""Add an indented line."""
# type: (str) -> None
"""
Emit a line outdented one level.
This is used for '} else {' and similar things inside a single indented
block.
"""
# type: (Any) -> None
"""Write all lines to `f`."""
=
# type: (str, str) -> None
=
# type: (Formatter, str) -> None
=
=
# type: () -> None
# type: (object, object, object) -> None
# type: (str, str) -> Formatter._IndentedScope
"""
Return a scope object for use with a `with` statement:
>>> f = Formatter()
>>> with f.indented('prefix {', '} suffix'):
... f.line('hello')
>>> f.writelines()
prefix {
hello
} suffix
The optional `before` and `after` parameters are surrounding lines
which are *not* indented.
"""
return
# type: (str, *Any) -> None
# type: (str) -> None
"""Add one or more lines after stripping common indentation."""
# type: (str) -> None
"""Add a comment line."""
# type: (str) -> None
"""Add a (multi-line) documentation comment."""
# type: (Match) -> None
"""
Add a match expression.
Example:
>>> f = Formatter()
>>> m = Match('x')
>>> m.arm('Orange', ['a', 'b'], 'some body')
>>> m.arm('Yellow', ['a', 'b'], 'some body')
>>> m.arm('Green', ['a', 'b'], 'different body')
>>> m.arm('Blue', ['x', 'y'], 'some body')
>>> f.match(m)
>>> f.writelines()
match x {
Orange { a, b } |
Yellow { a, b } => {
some body
}
Green { a, b } => {
different body
}
Blue { x, y } => {
some body
}
}
"""
=
=
=
-= 1
=
=
# type: (str) -> int
"""
Compute the indentation of s, or None of an empty line.
Example:
>>> _indent("foo")
0
>>> _indent(" bar")
4
>>> _indent(" ")
>>> _indent("")
"""
=
return -
# type: (str) -> List[str]
"""
Given a multi-line string, split it into a sequence of lines after
stripping a common indentation, as described in the "trim" function
from PEP 257. This is useful for strings defined with doc strings:
>>> parse_multiline('\\n hello\\n world\\n')
['hello', 'world']
"""
return
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
=
# Determine minimum indentation (first line doesn't count):
=
=
=
# Remove indentation (first line is special):
=
# Strip off trailing and leading blank lines:
return
"""
Match formatting class.
Match objects collect all the information needed to emit a Rust `match`
expression, automatically deduplicating overlapping identical arms.
Example:
>>> m = Match('x')
>>> m.arm('Orange', ['a', 'b'], 'some body')
>>> m.arm('Yellow', ['a', 'b'], 'some body')
>>> m.arm('Green', ['a', 'b'], 'different body')
>>> m.arm('Blue', ['x', 'y'], 'some body')
>>> assert(len(m.arms) == 3)
Note that this class is ignorant of Rust types, and considers two fields
with the same name to be equivalent.
"""
# type: (str) -> None
=
= # type: OrderedDict[Tuple[Tuple[str, ...], str], OrderedDict[str, None]] # noqa
# type: (str, List[str], str) -> None
=
=
= None