Advertisement
qtyash

Php Parser

Oct 11th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. from __future__ import absolute_import, unicode_literals, print_function
  6.  
  7. from arpeggio.cleanpeg import ParserPEG
  8. import sys
  9.  
  10. # Semantic actions
  11. from calc import to_floatSA, factorSA, termSA, exprSA
  12.  
  13.  
  14. input = """\
  15. <?php
  16. class Enum {
  17.    protected $self = array();
  18.    public function __construct( $fun ) {
  19.        $args = func_get_args();
  20.        for( $i=0, $n=count($args); $i<$n; $i++ )
  21.            $this->add($args[$i]);
  22.    }
  23.  
  24.    public function __get(  $name = null ) {
  25.        return $this->self[$name];
  26.    }
  27.  
  28.    public function add(  $name = null,  $enum = null ) {
  29.        if( isset($enum) )
  30.            $this->self[$name] = $enum;
  31.        else
  32.            $this->self[$name] = end($this->self) + 1;
  33.    }
  34.  
  35.  
  36. """
  37.  
  38.  
  39. # Grammar is defined using textual specification based on PEG language.
  40. calc_grammar = """
  41.  
  42.  
  43. calc =  test
  44.  
  45. test = visibility ws* function_keyword ws* word ws* arguments* ws*
  46. function = visibility "function" word arguments block
  47. block = "{" ws* r'[^}]*' ws* "}"
  48. arguments = "(" ws* argument* ws* ")"
  49.  
  50. #$types = array("cappuccino")
  51. #arguments end with optional comma
  52. argument = ( byvalue / byreference ) ("=" value )* ","*
  53. byreference = "&" byvalue
  54. byvalue = variable
  55.  
  56. #value may be variable or array or string or any php type
  57. value = variable
  58.  
  59. visibility = "public" / "protected" / "private"
  60. function_keyword = "function"
  61.  
  62. variable = "$" literal r'[a-zA-Z0-9_]*'
  63. word = r'[a-zA-Z0-9_]+'
  64. literal = r'[a-zA-Z]+'
  65.  
  66. comment = r'("//.*")|("/\*.*\*/")'
  67. symbol = r'[\W]+'
  68.  
  69. anyword = r'[\w]*' ws*
  70. ws = r'[\s]+'
  71.  
  72.  
  73. """
  74.  
  75.  
  76. def argument(parser, node, children):
  77.     """
  78.    Removes parenthesis if exists and returns what was contained inside.
  79.    """
  80.     print ("chi")
  81.     print(children)
  82.    
  83.     if len(children) == 1:
  84.         print(children[0])
  85.         return children[0]
  86.    
  87.     sign = -1 if children[0] == '-' else 1
  88.    
  89.     return sign * children[-1]
  90.  
  91. # Rules are mapped to semantic actions
  92. sem_actions = {
  93.     "argument" : argument,
  94. }
  95.  
  96.  
  97. def main(debug=False):
  98.  
  99.     # First we will make a parser - an instance of the calc parser model.
  100.     # Parser model is given in the form of PEG notation therefore we
  101.     # are using ParserPEG class. Root rule name (parsing expression) is "calc".
  102.     parser = ParserPEG(calc_grammar, "calc", True)
  103.  
  104.  
  105. #    filename = sys.argv[1]
  106. #    with open(filename) as file:
  107. #        contents = file.read()
  108. #    
  109. #    # An expression we want to evaluate
  110.     input_expr = """public function __construct( )"""
  111. #    input_expr = contents
  112. #    input_expr = input
  113.  
  114.     # Then parse tree is created out of the input_expr expression.
  115.     parse_tree = parser.parse(input_expr)
  116.     #result = parser.getASG(sem_actions)
  117.  
  118. #    print( result )
  119.     print( parse_tree )
  120.     if debug:
  121.         # getASG will start semantic analysis.
  122.         # In this case semantic analysis will evaluate expression and
  123.         # returned value will be evaluated result of the input_expr expression.
  124.         # Semantic actions are supplied to the getASG function.
  125.         print("{} = {}".format(input_expr, result))
  126.  
  127. if __name__ == "__main__":
  128.     # In debug mode dot (graphviz) files for parser model
  129.     # and parse tree will be created for visualization.
  130.     # Checkout current folder for .dot files.
  131.     main(debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement