Created
October 1, 2018 23:38
-
-
Save jayrbolton/13f3438d0bcc643a1a2720b93d807bcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import parsec as p | |
whitespace = p.regex(r'\s+') | |
ignore = p.many(whitespace) | |
lexeme = lambda parser: parser.skip(ignore) | |
lparen = lexeme(p.string('(')) | |
rparen = lexeme(p.string(')')) | |
symbol = lexeme(p.regex(r'[\d\w_-]+')) | |
and_op = lexeme(p.string('and')) | |
or_op = lexeme(p.string('or')) | |
op = and_op | or_op | |
atom = op | symbol | |
@p.generate('binary expression') | |
def binary_expr(): | |
yield lparen | |
es = yield p.many(expr) | |
yield rparen | |
return es | |
expr = atom | binary_expr | |
program = p.many(expr).skip(ignore) | |
def parse_gpr(string): | |
"""Parse a gene boolean expression.""" | |
return program(string, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment