Skip to content

Commit

Permalink
Refactor the parser code
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Apr 5, 2024
1 parent 45fdc6a commit 53a3dec
Show file tree
Hide file tree
Showing 8 changed files with 9,504 additions and 57 deletions.
2 changes: 1 addition & 1 deletion lib/rule_engine/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import re

from . import errors
from ._utils import parse_datetime, parse_float, parse_timedelta
from .parser.utilities import parse_datetime, parse_float, parse_timedelta
from .suggestions import suggest_symbol
from .types import *

Expand Down
2 changes: 1 addition & 1 deletion lib/rule_engine/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import math
import random

from ._utils import parse_datetime, parse_float, parse_timedelta
from . import ast
from . import errors
from . import types
from .parser.utilities import parse_datetime, parse_float, parse_timedelta

import dateutil.tz

Expand Down
59 changes: 5 additions & 54 deletions lib/rule_engine/parser.py → lib/rule_engine/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# rule_engine/parser.py
# rule_engine/parser/__init__.py
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -32,15 +32,12 @@

import ast as pyast
import collections
import threading
import types as pytypes

from . import ast
from . import errors
from ._utils import timedelta_regex

import ply.lex as lex
import ply.yacc as yacc
from .. import ast
from .. import errors
from .base import ParserBase
from .utilities import timedelta_regex

literal_eval = pyast.literal_eval

Expand All @@ -58,52 +55,6 @@ def build(self):
constructor = getattr(self.cls, self.method)
return constructor(*self.args, **self.kwargs)

class ParserBase(object):
"""
A base class for parser objects to inherit from. This does not provide any
grammar related definitions.
"""
precedence = ()
"""The precedence for operators."""
tokens = ()
reserved_words = {}
"""
A mapping of literal words which are reserved to their corresponding grammar
names.
"""
__mutex = threading.Lock()
def __init__(self, debug=False):
"""
:param bool debug: Whether or not to enable debugging features when
using the ply API.
"""
self.debug = debug
self.context = None
# Build the lexer and parser
self._lexer = lex.lex(module=self, debug=self.debug)
self._parser = yacc.yacc(module=self, debug=self.debug, write_tables=self.debug)

def parse(self, text, context, **kwargs):
"""
Parse the specified text in an abstract syntax tree of nodes that can later be evaluated. This is done in two
phases. First, the syntax is parsed and a tree of deferred / uninitialized AST nodes are constructed. Next each
node is built recursively using it's respective :py:meth:`rule_engine.ast.ASTNodeBase.build`.
:param str text: The grammar text to parse into an AST.
:param context: A context for specifying parsing and evaluation options.
:type context: :py:class:`~rule_engine.engine.Context`
:return: The parsed AST statement.
:rtype: :py:class:`~rule_engine.ast.Statement`
"""
kwargs['lexer'] = kwargs.pop('lexer', self._lexer)
with self.__mutex:
self.context = context
# phase 1: parse the string into a tree of deferred nodes
result = self._parser.parse(text, **kwargs)
self.context = None
# phase 2: initialize each AST node recursively, providing them with an opportunity to define assignments
return result.build()

class Parser(ParserBase):
"""
The parser class for the rule grammar. This class contains many ply specific
Expand Down
82 changes: 82 additions & 0 deletions lib/rule_engine/parser/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# rule_engine/parser/base.py
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the project nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import threading

import ply.lex as lex
import ply.yacc as yacc

class ParserBase(object):
"""
A base class for parser objects to inherit from. This does not provide any
grammar related definitions.
"""
precedence = ()
"""The precedence for operators."""
tokens = ()
reserved_words = {}
"""
A mapping of literal words which are reserved to their corresponding grammar
names.
"""
__mutex = threading.Lock()
def __init__(self, debug=False):
"""
:param bool debug: Whether or not to enable debugging features when
using the ply API.
"""
self.debug = debug
self.context = None
# Build the lexer and parser
self._lexer = lex.lex(module=self, debug=self.debug)
self._parser = yacc.yacc(module=self, debug=self.debug, write_tables=self.debug)

def parse(self, text, context, **kwargs):
"""
Parse the specified text in an abstract syntax tree of nodes that can later be evaluated. This is done in two
phases. First, the syntax is parsed and a tree of deferred / uninitialized AST nodes are constructed. Next each
node is built recursively using it's respective :py:meth:`rule_engine.ast.ASTNodeBase.build`.
:param str text: The grammar text to parse into an AST.
:param context: A context for specifying parsing and evaluation options.
:type context: :py:class:`~rule_engine.engine.Context`
:return: The parsed AST statement.
:rtype: :py:class:`~rule_engine.ast.Statement`
"""
kwargs['lexer'] = kwargs.pop('lexer', self._lexer)
with self.__mutex:
self.context = context
# phase 1: parse the string into a tree of deferred nodes
result = self._parser.parse(text, **kwargs)
self.context = None
# phase 2: initialize each AST node recursively, providing them with an opportunity to define assignments
return result.build()
Loading

0 comments on commit 53a3dec

Please sign in to comment.