Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

php7parser

1.0.8 • Public • Published

php7parser

A fast and forgiving PHP7 recursive descent parser implemented in Typescript.

The parser outputs a parse tree of phrases (branches) and tokens (leaves). The complete source code is represented by the tree including whitespace.

Design Goals

  • Modern browser and nodejs compatibility.
  • Error tolerant and high performance.
  • Output representative of full source code.
  • Adherance to the PHP language specifications.
  • Prefer error tolerance over enforcement of language constraints.

Usage

    import { Parser } from 'php7parser';
 
    let src = '<?php echo "Hello World!";';
    let tree = Parser.parse(src);

Interface

 
    export declare namespace Parser {
        function parse(text: string): Phrase;
    }
 
    export interface Phrase {
        /**
         * Phrase type
         */
        phraseType: PhraseType;
        /**
         * Phrase and token child nodes
         */
        children: (Phrase | Token)[];
    }
 
    export interface ParseError extends Phrase {
 
        /**
         * The token that prompted the parse error
         */
        unexpected: Token;
 
        /**
         * The expected token type
         */
        expected?: TokenType;
 
    }
 
    export interface Token {
        /**
         * Token type
         */
        tokenType: TokenType;
        /**
         * Offset within source where first char of token is found
         */
        offset: number;
        /**
         * Length of token string
         */
        length: number;
        /**
         * Lexer mode prior to this token being read.
         */
        modeStack: LexerMode[];
    }
 

Install

npm i php7parser

DownloadsWeekly Downloads

10

Version

1.0.8

License

ISC

Unpacked Size

368 kB

Total Files

21

Last publish

Collaborators

  • avatar