Package intermine :: Module constraints :: Class LogicParser
[hide private]
[frames] | no frames]

Class LogicParser

source code

object --+
         |
        LogicParser

Parses logic strings into logic groups

Instances of this class are used to parse logic strings into abstract syntax trees, and then logic groups. This aims to provide robust parsing of logic strings, with the ability to identify syntax errors in such strings.

Instance Methods [hide private]
 
__init__(self, query)
Parsers need access to the query they are parsing for, in order to reference the constraints on the query.
source code
intermine.constraints.CodedConstraint
get_constraint(self, code)
This method fetches the constraint from the parent query with the matching code.
source code
int
get_priority(self, op)
Operators have a specific precedence, from highest to lowest:
source code
LogicGroup
parse(self, logic_str)
Takes a string such as "A and B or C and D", and parses it into a structure which represents this logic as a binary abstract syntax tree.
source code
 
check_syntax(self, infix_tokens)
Syntax is checked before parsing to provide better errors, which should hopefully lead to more informative error messages.
source code
list
infix_to_postfix(self, infix_tokens)
Take in a set of infix tokens and return the set parsed to a postfix sequence.
source code
LogicGroup
postfix_to_tree(self, postfix_tokens)
Convert a set of tokens in postfix notation to a single LogicGroup object.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  ops = {'&': 'AND', '&&': 'AND', '(': '(', ')': ')', 'AND': 'AN...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, query)
(Constructor)

source code 

Constructor

Parsers need access to the query they are parsing for, in order to reference the constraints on the query.

Parameters:
  • query (intermine.query.Query) - The parent query object
Overrides: object.__init__

get_constraint(self, code)

source code 

Get the constraint with the given code

This method fetches the constraint from the parent query with the matching code.

Returns: intermine.constraints.CodedConstraint

See Also: intermine.query.Query.get_constraint

get_priority(self, op)

source code 

Get the priority for a given operator

Operators have a specific precedence, from highest to lowest:

  • ()
  • AND
  • OR

This method returns an integer which can be used to compare operator priorities.

Returns: int

parse(self, logic_str)

source code 

Parse a logic string into an abstract syntax tree

Takes a string such as "A and B or C and D", and parses it into a structure which represents this logic as a binary abstract syntax tree. The above string would parse to "(A and B) or (C and D)", as AND binds more tightly than OR.

Note that only singly rooted trees are parsed.

Parameters:
  • logic_str (string) - The logic defininition as a string
Returns: LogicGroup
Raises:

check_syntax(self, infix_tokens)

source code 

Check the syntax for errors before parsing

Syntax is checked before parsing to provide better errors, which should hopefully lead to more informative error messages.

This checks for:

  • correct operator positions (cannot put two codes next to each other without intervening operators)
  • correct grouping (all brackets are matched, and contain valid expressions)
Parameters:
  • infix_tokens (iterable) - The input parsed into a list of tokens.
Raises:

infix_to_postfix(self, infix_tokens)

source code 

Convert a list of infix tokens to postfix notation

Take in a set of infix tokens and return the set parsed to a postfix sequence.

Parameters:
  • infix_tokens (iterable) - The list of tokens
Returns: list

postfix_to_tree(self, postfix_tokens)

source code 

Convert a set of structured tokens to a single LogicGroup

Convert a set of tokens in postfix notation to a single LogicGroup object.

Parameters:
  • postfix_tokens (list) - A list of tokens in postfix notation.
Returns: LogicGroup
Raises:
  • AssertionError - is the tree doesn't have a unique root.

Class Variable Details [hide private]

ops

Value:
{'&': 'AND',
 '&&': 'AND',
 '(': '(',
 ')': ')',
 'AND': 'AND',
 'OR': 'OR',
 '|': 'OR',
 '||': 'OR'}