From 3bd8bfa367e1c489eb9abbe2c4134bc710e39832 Mon Sep 17 00:00:00 2001 From: neingeist Date: Thu, 21 Jul 2016 16:51:09 +0200 Subject: [PATCH] add pypeg2 example --- peg-parse.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 peg-parse.py diff --git a/peg-parse.py b/peg-parse.py new file mode 100755 index 0000000..dc428c7 --- /dev/null +++ b/peg-parse.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 +# https://fdik.org/pyPEG/ + +from __future__ import division, print_function +from pypeg2 import * + +class Type(Keyword): + grammar = Enum( K("int"), K("long") ) +class Parameter: + grammar = attr("typing", Type), name() +class Parameters(Namespace): + grammar = optional(csl(Parameter)) +class Instruction(str): + grammar = word, ";" + +block = "{", maybe_some(Instruction), "}" +class Function(List): + grammar = attr("typing", Type), name(), \ + "(", attr("parms", Parameters), ")", block + +f = parse("int f(int a, long b) { do_this; do_that; }", Function) +print(f.name) +print(f.typing) +print(f.parms["b"].typing) +print(f[0]) +print(f[1])