pyebnf/compiler.py
def _get_rule_definition(self, rule):
    """Generates the source code for a rule."""
    fmt = """def {rule_fxn_name}(self, text):
             {indent}\"\"\"{rule_source}\"\"\"
             {indent}self._attempting(text)
             {indent}return {rule_definition}(text){transform}
          """
    fmt = self._clean_fmt(fmt)
    source = self._indent(self._ast_to_code(rule.expression), skip_first_line=True)

    # All the primitives will accept a string x in place of terminal(x). This is terminal shorthand.
    # However, if a rule is only a wrapper around a single terminal, we have to actually make a
    # terminal call. This handles that situation.
    if self.use_terminal_shorthand and len(source) == 1 and source[0].startswith(("'", '"')):
      source = ["terminal({})".format(source[0])]

    rule_source = fmt.format(rule_fxn_name=self._get_rule_fxn_name(rule.name),
                             indent=self.indent,
                             rule_source=self._get_rule_source(rule),
                             rule_definition="\n".join(source),
                             transform=self._get_rule_transform(rule))
    return self._indent(rule_source, 1)
def _get_rule_definition(self, rule):
    fmt = """def {rule_fxn_name}(self, text):
             {indent}\"\"\"{rule_source}\"\"\"
             {indent}self._attempting(text)
             {indent}return {rule_definition}(text){transform}
          """
    fmt = self._clean_fmt(fmt)
    source = self._indent(self._ast_to_code(rule.expression), skip_first_line=True)

    # All the primitives will accept a string x in place of terminal(x). This is terminal shorthand.
    # However, if a rule is only a wrapper around a single terminal, we have to actually make a
    # terminal call. This handles that situation.
    if self.use_terminal_shorthand and len(source) == 1 and source[0].startswith(("'", '"')):
      source = ["terminal({})".format(source[0])]

    rule_source = fmt.format(rule_fxn_name=self._get_rule_fxn_name(rule.name),
                             indent=self.indent,
                             rule_source=self._get_rule_source(rule),
                             rule_definition="\n".join(source),
                             transform=self._get_rule_transform(rule))
    return self._indent(rule_source, 1)
def _get_rule_definition ( self , rule ) : fmt = """def {rule_fxn_name}(self, text):
             {indent}\"\"\"{rule_source}\"\"\"
             {indent}self._attempting(text)
             {indent}return {rule_definition}(text){transform}
          """ fmt = self . _clean_fmt ( fmt ) source = self . _indent ( self . _ast_to_code ( rule . expression ) , skip_first_line = True ) # All the primitives will accept a string x in place of terminal(x). This is terminal shorthand. # However, if a rule is only a wrapper around a single terminal, we have to actually make a # terminal call. This handles that situation. if self . use_terminal_shorthand and len ( source ) == 1 and source [ 0 ] . startswith ( ( "'" , '"' ) ) : source = [ "terminal({})" . format ( source [ 0 ] ) ] rule_source = fmt . format ( rule_fxn_name = self . _get_rule_fxn_name ( rule . name ) , indent = self . indent , rule_source = self . _get_rule_source ( rule ) , rule_definition = "\n" . join ( source ) , transform = self . _get_rule_transform ( rule ) ) return self . _indent ( rule_source , 1 )
Generates the source code for a rule.
Generates the source code for a rule .