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() from lark.lexer.Token.

Variables
  • self.rule – Name, in common with AttrTree.

  • self.eval – Transformed data type, in common with AttrTree.

Can be instantiated with __init__`(rule, content), via :class:`lark.lexer.Token(), or alternative constructor transform() (transform object of class lark.lexer.Token).

property eval

Evaluated value (str, int, float).

replace(value)[source]

Returns copy (same rule), but with content replaced.

property rule

Rule name (synonymous with ‘type’)

classmethod transform(token, **kwargs)[source]

Alternative constructor: From Token (with optional overrides).

class pharmpy.parse_utils.AttrTree(data, children, meta=None)[source]

Bases: lark.tree.Tree

Tree with attribute access.

Created in GenericParser.parse() by transform(), from lark.Tree.

Variables
  • self.rule – Name, in common with AttrToken.

  • self.rules – Names of children.

  • self.eval – Transformed data type, in common with AttrToken.

  • self.tokens – Recursive tokens as (flattened) list.

  • self.debug – Treeview str, formatted for debugging.

Can be instantiated with __init__(), via lark.Tree, or alternative constructors:
  1. transform() (transform recursively object of class lark.Tree).

  2. 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() from lark.lexer.Token.

Variables
  • self.rule – Name, in common with AttrTree.

  • self.eval – Transformed data type, in common with AttrTree.

Can be instantiated with __init__`(rule, content), via :class:`lark.lexer.Token(), or alternative constructor transform() (transform object of class lark.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)[source]
add_newline_node()[source]
add_node(node, following_node=None, comment=False)[source]
all(rule)[source]

Returns all children matching rule, or [].

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):
  1. Tree: Children are moved to parent.

  2. 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)[source]

Returns first child matching ‘rule’, or None.

get_last_node()[source]
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, [], []).

remove(rule)[source]

Remove all children with rule. Not recursively

remove_node(node)[source]
property rule

Rule name (synonymous with ‘data’).

property rules

All rules of (immediate) children.

set(rule, value)[source]

Sets first child matching rule. Raises if none.

set_child(attr, value)[source]
property tokens

All tokens as flattened list.

classmethod transform(tree, **kwargs)[source]

Alternative constructor: From Tree (with optional overrides).

tree_walk()[source]

Generator for iterating depth-first (i.e. parse order) over children.

treeprint(indent='')[source]

Prints debug formatted tree structure.

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:

  1. Lex and parse a ‘buffer’ using Lark (from grammar file). Builds AST.

  2. Shape AST by options (see non_empty).

  3. 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() by transform(), from lark.Tree.

Variables
  • self.rule – Name, in common with AttrToken.

  • self.rules – Names of children.

  • self.eval – Transformed data type, in common with AttrToken.

  • self.tokens – Recursive tokens as (flattened) list.

  • self.debug – Treeview str, formatted for debugging.

Can be instantiated with __init__(), via lark.Tree, or alternative constructors:
  1. transform() (transform recursively object of class lark.Tree).

  2. 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() from lark.lexer.Token.

Variables
  • self.rule – Name, in common with AttrTree.

  • self.eval – Transformed data type, in common with AttrTree.

Can be instantiated with __init__`(rule, content), via :class:`lark.lexer.Token(), or alternative constructor transform() (transform object of class lark.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):
  1. Tree: Children are moved to parent.

  2. 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'}
non_empty = []

AttrTree implementation.

parse(buf)[source]

Parses a buffer, transforms and constructs AttrTree object.

Parameters

buf – Buffer to parse.