Package: pegomancy
pegomancyYet another parsing thingyInstallationThis project is written using Python 3.8. It can be installed using setuptools or pip.UsageAs a toolPegomancy provides apegomantexecutable that can be used to generate Python code to parse data according to a given grammar specification.usage: pegomant [-h] [-c CLASS_NAME] [-o OUTPUT_FILE] grammar_file

positional arguments:
  grammar_file

optional arguments:
  -h, --help            show this help message and exit
  -c CLASS_NAME, --class_name CLASS_NAME
  -o OUTPUT_FILE, --output-file OUTPUT_FILEAs a libraryThepegomancymodule can be used as a library to fully control grammars and how code is generated.Grammar syntaxPegomancy grammars look like regular PEG grammars, with a dash of sugar syntax. Here is an example grammar specification that can be used to parse arithmetic expressions:integer: r"[0-9]+"

expr: left:expr op:'+' right:term
    | left:expr op:'-' right:term
    | term

term: left:term op:'*' right:atom
    | left:term op:'/' right:atom
    | atom

atom: integer | '(' expr ')'In case the syntax isn't familiar, let's provide a bit of information.RulesA grammar is made of one or several rules. Each rule is specified with the following syntax:rule_name: expressionIn the grammar above,integer,expr,termandatomare the rules.ExpressionsAtomsAtoms are the most primitive constructs used in an expression: they match a simple portion of the source text.In the example grammar given above, we have different kinds of atoms:'(',')','+'(and others), each matching a raw string of textr"[0-9]+", matching a regular expressioninteger(in theatomrule), matching what theintegerrule matchesNote that atoms can be concatenated:'(' expr ')'will match an opening parenthesis, then what theexprrule matches, then a closing parenthesis.ItemsItems are more complex expressions and introduce modifiers to repeat or make expressions optional:the*operator can be used to allow repeating an expression zero or more timesthe+operator can be used to allow repeating an expression one or more timesthe?operator can be used to make an expression optionalItems are unnamed by default, but can be named using the:operator, as inop:'+', which gives the nameopto the'+'atom.AlternativesSome rules might allow multiple possibilities: for example, theatomrule in the above grammar can match either an integer or a parenthesized expression.
The notion of alternative is expressed in the grammar using the|operator.Parse resultsDefault ASTBy default, parsers generated withpegomantwill produce AST nodes that are either:a single value, if the matched expression has only one componenta list, if the matched expression has multiple componentsa dictionary, if the matched expression has named items (unnamed items are discarded from the result)Customizing the ASTThe default AST can be enough, but in some cases it is useful to transform it into a custom data structure.
When using a Pegomancy parser, you can specify an object whose methods will be invoked when a rule matches some input.That object must be given as therule_handlerparameter when initializing the parser.Here is a possible rule handler for the example grammar given in previous sections.classRuleHandler:definteger(self,node):returnint(node)defexpr(self,node):ifisinstance(node,dict):ifnode["op"]=="+":returnnode["left"]+node["right"]else:returnnode["left"]-node["right"]else:returnnodedefterm(self,node):ifisinstance(node,dict):ifnode["op"]=="*":returnnode["left"]*node["right"]else:returnnode["left"]/node["right"]else:returnnodedefatom(self,node):ifisinstance(node,list):returnnode[1]returnnodeThe methods can raise aParseErrorto indicate a parse failure for the rule being handled.