Created
January 21, 2019 04:41
-
-
Save darius/39b09e3731c25a9a3d47d744c58e86dd to your computer and use it in GitHub Desktop.
This file contains 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
# See https://gist.github.com/kragen/6bee5b4529cbde133050e9dc2e0b4b7e | |
from parson import Grammar | |
g = Grammar(r""" | |
_: {' ' | '\n' | '\t'}*. | |
digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'. | |
atom: :'<num>' {digit+ ('.' digit* | ) | '.' digit+} :'</num>' _ | {'('} _ a {')'} _. | |
e: :'<exp> <base>' atom :'</base>' {'**'} _ :'<pow>' atom :'</pow> </exp>' | atom. | |
m: :'<term>' e (:'<op>' {'/' | '*' | '%'} :'</op>' _ e)+ :'</term>' | e. | |
a: :'<sum>' m (:'<op>' {'+' | '-'} :'</op>' _ m)+ :'</sum>' | m. | |
""")() | |
## ''.join(g.a('2+(3 -4)')) | |
#. '<sum><num>2</num><op>+</op>(<sum><num>3</num> <op>-</op><num>4</num></sum>)</sum>' | |
r""" | |
Hm, if we changed the way capturing worked, we could instead write | |
_: (' ' | '\n' | '\t')*. | |
digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'. | |
atom: :'<num>' (digit+ ('.' digit* | ) | '.' digit+) :'</num>' _ | '(' _ a ')' _. | |
e: :'<exp> <base>'atom:'</base>' '**' _ :'<pow>'atom:'</pow> </exp>' | atom. | |
m: :'<term>' e (:'<op>'('/' | '*' | '%'):'</op>' _ e)+ :'</term>' | e. | |
a: :'<sum>' m (:'<op>'('+' | '-'):'</op>' _ m)+ :'</sum>' | m. | |
top: {a}. | |
The idea is that here {foo} would set a mode wherein 'x' echoes the x | |
on recognition, instead of skipping over it. This would allow you to | |
insert :'outputs' midstream. In Parson currently {foo} runs foo and | |
then appends the recognized text as a single monolithic value. | |
On the one hand, this could be nice. On the other, it's modeful. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment