pharmpy.parse_utils package¶
Submodules¶
Module contents¶
- class pharmpy.parse_utils.AttrToken(type_, value, pos_in_stream=None, line=None, column=None, end_line=None, end_column=None, end_pos=None)[source]¶
Bases:
lark.lexer.Token
Token with attribute access.
Created by
AttrTree.transform()
fromlark.lexer.Token
.- Variables
Can be instantiated with
__init__`(rule, content), via :class:`lark.lexer.Token()
, or alternative constructortransform()
(transform object of classlark.lexer.Token
).- property eval¶
Evaluated value (str, int, float).
- property rule¶
Rule name (synonymous with ‘type’)
- class pharmpy.parse_utils.AttrTree(data, children, meta=None)[source]¶
Bases:
lark.tree.Tree
Tree with attribute access.
Created in
GenericParser.parse()
bytransform()
, fromlark.Tree
.- Variables
- Can be instantiated with
__init__()
, vialark.Tree
, or alternative constructors: transform()
(transform recursively object of classlark.Tree
).create()
(create from nested iterators).
- class AttrToken(type_, value, pos_in_stream=None, line=None, column=None, end_line=None, end_column=None, end_pos=None)¶
Bases:
lark.lexer.Token
Token with attribute access.
Created by
AttrTree.transform()
fromlark.lexer.Token
.- Variables
Can be instantiated with
__init__`(rule, content), via :class:`lark.lexer.Token()
, or alternative constructortransform()
(transform object of classlark.lexer.Token
).- property eval¶
Evaluated value (str, int, float).
- replace(value)¶
Returns copy (same rule), but with content replaced.
- property rule¶
Rule name (synonymous with ‘type’)
- classmethod transform(token, **kwargs)¶
Alternative constructor: From Token (with optional overrides).
- classmethod create(rule, items, _anon_count=0, _list=False)[source]¶
Alternative constructor: Creates new tree from (possibly nested) iterables.
Only non-iterable items become leaves (i.e. content of token nodes). All others are trees.
- Handling of missing names (e.g. lists):
Tree: Children are moved to parent.
Token: Native naming scheme, __ANON_%d, of Lark is used.
- Parameters
items – Child (tree) nodes or content (token) nodes.
rule – Name of root tree (__ANON_0 if False).
_anon_count – Internal. Anonymous numbering offset.
_list – Internal. Recursion state. Drop ‘rule’ & return list of children, which are orphaned if name is False.
- Raises
TypeError – ‘items’ not iterable or instance of ‘str’ (only leaves shall contain ‘str’).
ValueError – ‘items’ empty (trees can’t be empty).
Note
Please follow convention of all lower/upper case for trees/tokens.
- property debug¶
Debug formatted tree structure.
- property eval¶
Evaluated value (self).
- partition(rule)[source]¶
Partition children into (head, item, tail).
Search for child item ‘rule’ and return the part before it (head), the item, and the part after it (tail). If ‘rule’ is not found, return (children, [], []).
- property rule¶
Rule name (synonymous with ‘data’).
- property rules¶
All rules of (immediate) children.
- property tokens¶
All tokens as flattened list.
- class pharmpy.parse_utils.GenericParser(buf=None, **lark_options)[source]¶
Bases:
object
Generic parser using lark-parser.
Inherit to define a parser, say ThetaRecordParser for NONMEM, with the workflow:
Lex and parse a ‘buffer’ using Lark (from
grammar
file). Builds AST.Shape AST by options (see
non_empty
).Convert to
AttrTree
for convenient traversal (attribute access).
- Variables
self.non_empty – Insert empty placeholders if missing. Dict of rule -> (pos, name), where a Tree or Token (if uppercase) will be inserted at ‘pos’ of the children of ‘rule’, if none exists.
self.buffer – Buffer parsed by
parse()
.self.grammar – Path to grammar file.
self.root – Root of final tree. Instance of
AttrTree
.
- class AttrTree(data, children, meta=None)¶
Bases:
lark.tree.Tree
Tree with attribute access.
Created in
GenericParser.parse()
bytransform()
, fromlark.Tree
.- Variables
- Can be instantiated with
__init__()
, vialark.Tree
, or alternative constructors: transform()
(transform recursively object of classlark.Tree
).create()
(create from nested iterators).
- class AttrToken(type_, value, pos_in_stream=None, line=None, column=None, end_line=None, end_column=None, end_pos=None)¶
Bases:
lark.lexer.Token
Token with attribute access.
Created by
AttrTree.transform()
fromlark.lexer.Token
.- Variables
Can be instantiated with
__init__`(rule, content), via :class:`lark.lexer.Token()
, or alternative constructortransform()
(transform object of classlark.lexer.Token
).- property eval¶
Evaluated value (str, int, float).
- replace(value)¶
Returns copy (same rule), but with content replaced.
- property rule¶
Rule name (synonymous with ‘type’)
- classmethod transform(token, **kwargs)¶
Alternative constructor: From Token (with optional overrides).
- add_comment_node(comment, adjacent_node=None)¶
- add_newline_node()¶
- add_node(node, following_node=None, comment=False)¶
- all(rule)¶
Returns all children matching rule, or [].
- classmethod create(rule, items, _anon_count=0, _list=False)¶
Alternative constructor: Creates new tree from (possibly nested) iterables.
Only non-iterable items become leaves (i.e. content of token nodes). All others are trees.
- Handling of missing names (e.g. lists):
Tree: Children are moved to parent.
Token: Native naming scheme, __ANON_%d, of Lark is used.
- Parameters
items – Child (tree) nodes or content (token) nodes.
rule – Name of root tree (__ANON_0 if False).
_anon_count – Internal. Anonymous numbering offset.
_list – Internal. Recursion state. Drop ‘rule’ & return list of children, which are orphaned if name is False.
- Raises
TypeError – ‘items’ not iterable or instance of ‘str’ (only leaves shall contain ‘str’).
ValueError – ‘items’ empty (trees can’t be empty).
Note
Please follow convention of all lower/upper case for trees/tokens.
- property debug¶
Debug formatted tree structure.
- property eval¶
Evaluated value (self).
- find(rule)¶
Returns first child matching ‘rule’, or None.
- get_last_node()¶
- partition(rule)¶
Partition children into (head, item, tail).
Search for child item ‘rule’ and return the part before it (head), the item, and the part after it (tail). If ‘rule’ is not found, return (children, [], []).
- remove(rule)¶
Remove all children with rule. Not recursively
- remove_node(node)¶
- property rule¶
Rule name (synonymous with ‘data’).
- property rules¶
All rules of (immediate) children.
- set(rule, value)¶
Sets first child matching rule. Raises if none.
- set_child(attr, value)¶
- property tokens¶
All tokens as flattened list.
- classmethod transform(tree, **kwargs)¶
Alternative constructor: From Tree (with optional overrides).
- tree_walk()¶
Generator for iterating depth-first (i.e. parse order) over children.
- treeprint(indent='')¶
Prints debug formatted tree structure.
- classmethod insert(item, non_empty)[source]¶
Inserts missing Tree/Token amongst children (see
non_empty
).- Parameters
item – Tree to recurse.
non_empty – Dict of rule -> (pos, name) tuple.
- lark_options = {'ambiguity': 'resolve', 'debug': False, 'keep_all_tokens': True, 'lexer': 'dynamic', 'parser': 'earley', 'start': 'root'}¶