|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# rule_engine/parser/base.py |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are |
| 8 | +# met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above |
| 13 | +# copyright notice, this list of conditions and the following disclaimer |
| 14 | +# in the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# * Neither the name of the project nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +# |
| 32 | + |
| 33 | +import threading |
| 34 | + |
| 35 | +import ply.lex as lex |
| 36 | +import ply.yacc as yacc |
| 37 | + |
| 38 | +class ParserBase(object): |
| 39 | + """ |
| 40 | + A base class for parser objects to inherit from. This does not provide any |
| 41 | + grammar related definitions. |
| 42 | + """ |
| 43 | + precedence = () |
| 44 | + """The precedence for operators.""" |
| 45 | + tokens = () |
| 46 | + reserved_words = {} |
| 47 | + """ |
| 48 | + A mapping of literal words which are reserved to their corresponding grammar |
| 49 | + names. |
| 50 | + """ |
| 51 | + __mutex = threading.Lock() |
| 52 | + def __init__(self, debug=False): |
| 53 | + """ |
| 54 | + :param bool debug: Whether or not to enable debugging features when |
| 55 | + using the ply API. |
| 56 | + """ |
| 57 | + self.debug = debug |
| 58 | + self.context = None |
| 59 | + # Build the lexer and parser |
| 60 | + self._lexer = lex.lex(module=self, debug=self.debug) |
| 61 | + self._parser = yacc.yacc(module=self, debug=self.debug, write_tables=self.debug) |
| 62 | + |
| 63 | + def parse(self, text, context, **kwargs): |
| 64 | + """ |
| 65 | + Parse the specified text in an abstract syntax tree of nodes that can later be evaluated. This is done in two |
| 66 | + phases. First, the syntax is parsed and a tree of deferred / uninitialized AST nodes are constructed. Next each |
| 67 | + node is built recursively using it's respective :py:meth:`rule_engine.ast.ASTNodeBase.build`. |
| 68 | +
|
| 69 | + :param str text: The grammar text to parse into an AST. |
| 70 | + :param context: A context for specifying parsing and evaluation options. |
| 71 | + :type context: :py:class:`~rule_engine.engine.Context` |
| 72 | + :return: The parsed AST statement. |
| 73 | + :rtype: :py:class:`~rule_engine.ast.Statement` |
| 74 | + """ |
| 75 | + kwargs['lexer'] = kwargs.pop('lexer', self._lexer) |
| 76 | + with self.__mutex: |
| 77 | + self.context = context |
| 78 | + # phase 1: parse the string into a tree of deferred nodes |
| 79 | + result = self._parser.parse(text, **kwargs) |
| 80 | + self.context = None |
| 81 | + # phase 2: initialize each AST node recursively, providing them with an opportunity to define assignments |
| 82 | + return result.build() |
0 commit comments