Skip to content

Instantly share code, notes, and snippets.

@pmuston
Created April 8, 2017 08:05
Show Gist options
  • Save pmuston/48766ce29e977d5685782f5a1821d84f to your computer and use it in GitHub Desktop.
Save pmuston/48766ce29e977d5685782f5a1821d84f to your computer and use it in GitHub Desktop.
Parser Notebook from More Adventures in Python Talk
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/paulmuston/anaconda/envs/ipython/bin/python'"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sys\n",
"sys.executable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"reference https://fdik.org/pyPEG/"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
"from pypeg2 import *"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"example = \"\"\"int f(int a, long b)\n",
"{\n",
" do_this;\n",
" do_that;\n",
"}\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Type(Keyword):\n",
" grammar = Enum( K(\"int\"), K(\"long\") )\n",
"\n",
"class Parameter:\n",
" grammar = attr(\"typing\", Type), name()"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<__main__.Parameter at 0x107f79278>"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = parse(\"int a\", Parameter)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'grammar',\n",
" 'name',\n",
" 'position_in_text',\n",
" 'typing']"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(a)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(Symbol('a'), Type('int'))"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.name, a.typing"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'__add__,__class__,__contains__,__delattr__,__dict__,__dir__,__doc__,__eq__,__format__,__ge__,__getattribute__,__getitem__,__getnewargs__,__gt__,__hash__,__init__,__init_subclass__,__iter__,__le__,__len__,__lt__,__mod__,__module__,__mul__,__ne__,__new__,__reduce__,__reduce_ex__,__repr__,__rmod__,__rmul__,__setattr__,__sizeof__,__str__,__subclasshook__,__weakref__,capitalize,casefold,center,check_keywords,count,encode,endswith,expandtabs,find,format,format_map,index,isalnum,isalpha,isdecimal,isdigit,isidentifier,islower,isnumeric,isprintable,isspace,istitle,isupper,join,ljust,lower,lstrip,maketrans,name,namespace,partition,position_in_text,regex,replace,rfind,rindex,rjust,rpartition,rsplit,rstrip,split,splitlines,startswith,strip,swapcase,title,translate,upper,zfill'"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\",\".join(dir(a.name))"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(Symbol('b'), Type('long'))"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = parse(\"long b\", Parameter)\n",
"b.name, b.typing"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
"class Parameters(Namespace):\n",
" grammar = optional(csl(Parameter))"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"odict_keys([Symbol('a'), Symbol('b')])"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"parms = parse(\"int a, long b\", Parameters)\n",
"parms.keys()"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a isa int', 'b isa long']\n"
]
}
],
"source": [
"print([\"{} isa {}\".format(x.name,x.typing) for x in parms.values()])"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Function(List):\n",
" grammar = attr(\"typing\", Type), name(), \\\n",
" \"(\", attr(\"parms\", Parameters), \")\""
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(Symbol('f'),\n",
" Type('int'),\n",
" Parameters([(Symbol('a'), <__main__.Parameter object at 0x1087aafd0>), (Symbol('b'), <__main__.Parameter object at 0x1087b2128>), ]))"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = parse(\"int f(int a, long b)\", Function)\n",
"f.name, f.typing, f.parms"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'int f(int a, long b)\\n{\\n do_this;\\n do_that;\\n}'"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"example"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Instruction(str):\n",
" grammar = word, \";\""
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"block = \"{\", maybe_some(Instruction), \"}\""
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"b = parse(\"{\\n do_this;\\n do_that;\\n}\", block)"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['do_this', 'do_that']"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"class Function(List):\n",
" grammar = attr(\"typing\", Type), name(), \\\n",
" \"(\", attr(\"parms\", Parameters), \")\", \\\n",
" block"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Function(['do_this', 'do_that'], name=Symbol('f'))"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = parse(example,Function)\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(Symbol('f'), Type('int'), 'do_this', 'do_that')"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.name, f.typing, f[0], f[1]"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a isa int', 'b isa long']\n"
]
}
],
"source": [
"print([\"{} isa {}\".format(x.name,x.typing) for x in f.parms.values()])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment