Skip to content

thm-mni-ii/PHP-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP-Parser Build Status Coverage Status

This project contains a PHP-Parser based on the current php language specification. It supports PHP version 7.

The parser is written in Scala, but can be used in Java as well as in Scala projects. Based on FastParse it transforms a valid PHP-Script into an abstract syntax tree.

Getting Started

Import the artifacts from Maven Central.

<dependency>
  <groupId>de.thm.mni.ii</groupId>
  <artifactId>phpparser</artifactId>
  <version>1.0.0</version>
</dependency>

Usage

These simple examples present the basic usage of the parser.

Java

import de.thm.mni.ii.phpparser.PHPParser;
import de.thm.mni.ii.phpparser.ast.Basic;

public class Main {
    public static void main(String[] args) {
        PHPParser.Result res = (PHPParser.Result) PHPParser.parse("<?php $value = 5;");
        if (res instanceof PHPParser.Success) {
            Basic.Script s = ((PHPParser.Success) res).script();
            System.out.println(s);
        } else if (res instanceof PHPParser.Failure) {
            String msg = ((PHPParser.Failure) res).fullMsg();
            System.out.println(msg);
        }
    }
}

Scala

object Main extends App {
  import de.thm.mni.ii.phpparser.PHPParser

  PHPParser.parse("<?php $value = 5;") match {
    case s: PHPParser.Success => println(s.script)
    case f: PHPParser.Failure => println(f.fullMsg)
  }
}

The top-level PHPParser parses a whole PHP-Script. The result is an instance of PHPParser.Success or PHPParser.Failure.

PHPParser.Success contains the abstract syntax tree. PHPParser.Failure contains additional information about the error. If you need further error information, take a look at the failure-member. FastParse provides additional methods to present the origin of the parse-error.

Project Structure

.
├── ...
├── src/main/scala/de/thm/mni/ii/phpparser
│   ├── PHPParser.scala    # top-level parser
│   ├── ast/               # case classes of abstract syntax tree
│   └── parser/            # specific parsers for all different elements
└── ...

Contribute

Feel free to send Pull-Requests to improve this parser. If you find a bug, please report it as an issue on github.

License

This project is licensed under the MIT License - see the LICENSE file for details

Releases

No releases published

Packages

No packages published

Languages