Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from __future__ import absolute_import, unicode_literals, print_function
- from arpeggio.cleanpeg import ParserPEG
- import sys
- # Semantic actions
- from calc import to_floatSA, factorSA, termSA, exprSA
- input = """\
- <?php
- class Enum {
- protected $self = array();
- public function __construct( $fun ) {
- $args = func_get_args();
- for( $i=0, $n=count($args); $i<$n; $i++ )
- $this->add($args[$i]);
- }
- public function __get( $name = null ) {
- return $this->self[$name];
- }
- public function add( $name = null, $enum = null ) {
- if( isset($enum) )
- $this->self[$name] = $enum;
- else
- $this->self[$name] = end($this->self) + 1;
- }
- """
- # Grammar is defined using textual specification based on PEG language.
- calc_grammar = """
- calc = test
- test = visibility ws* function_keyword ws* word ws* arguments* ws*
- function = visibility "function" word arguments block
- block = "{" ws* r'[^}]*' ws* "}"
- arguments = "(" ws* argument* ws* ")"
- #$types = array("cappuccino")
- #arguments end with optional comma
- argument = ( byvalue / byreference ) ("=" value )* ","*
- byreference = "&" byvalue
- byvalue = variable
- #value may be variable or array or string or any php type
- value = variable
- visibility = "public" / "protected" / "private"
- function_keyword = "function"
- variable = "$" literal r'[a-zA-Z0-9_]*'
- word = r'[a-zA-Z0-9_]+'
- literal = r'[a-zA-Z]+'
- comment = r'("//.*")|("/\*.*\*/")'
- symbol = r'[\W]+'
- anyword = r'[\w]*' ws*
- ws = r'[\s]+'
- """
- def argument(parser, node, children):
- """
- Removes parenthesis if exists and returns what was contained inside.
- """
- print ("chi")
- print(children)
- if len(children) == 1:
- print(children[0])
- return children[0]
- sign = -1 if children[0] == '-' else 1
- return sign * children[-1]
- # Rules are mapped to semantic actions
- sem_actions = {
- "argument" : argument,
- }
- def main(debug=False):
- # First we will make a parser - an instance of the calc parser model.
- # Parser model is given in the form of PEG notation therefore we
- # are using ParserPEG class. Root rule name (parsing expression) is "calc".
- parser = ParserPEG(calc_grammar, "calc", True)
- # filename = sys.argv[1]
- # with open(filename) as file:
- # contents = file.read()
- #
- # # An expression we want to evaluate
- input_expr = """public function __construct( )"""
- # input_expr = contents
- # input_expr = input
- # Then parse tree is created out of the input_expr expression.
- parse_tree = parser.parse(input_expr)
- #result = parser.getASG(sem_actions)
- # print( result )
- print( parse_tree )
- if debug:
- # getASG will start semantic analysis.
- # In this case semantic analysis will evaluate expression and
- # returned value will be evaluated result of the input_expr expression.
- # Semantic actions are supplied to the getASG function.
- print("{} = {}".format(input_expr, result))
- if __name__ == "__main__":
- # In debug mode dot (graphviz) files for parser model
- # and parse tree will be created for visualization.
- # Checkout current folder for .dot files.
- main(debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement