From b352fe19fad55040d31cd4bd0508d19557845c37 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sun, 2 Aug 2020 19:47:23 -0700 Subject: [PATCH 01/12] Start overhaul of typehints and TS type definition --- index.d.ts | 365 --------- out/types.d.ts | 1342 ++++++++++++++++++++++++++++++++++ package-lock.json | 15 + package.json | 2 + src/ast.js | 3 +- src/ast/assignref.js | 2 +- src/ast/byref.js | 2 +- src/ast/call.js | 4 +- src/ast/classconstant.js | 2 +- src/ast/closure.js | 2 +- src/ast/continue.js | 2 +- src/ast/declaration.js | 4 +- src/ast/exit.js | 2 +- src/ast/goto.js | 2 +- src/ast/location.js | 2 +- src/ast/method.js | 8 +- src/ast/namespace.js | 4 +- src/ast/new.js | 2 +- src/ast/node.js | 8 +- src/ast/nowdoc.js | 4 +- src/ast/position.js | 6 +- src/ast/program.js | 4 +- src/ast/propertystatement.js | 2 +- src/ast/string.js | 2 +- src/ast/unary.js | 2 +- src/ast/usegroup.js | 4 +- src/ast/useitem.js | 4 +- src/ast/variable.js | 2 +- src/ast/yield.js | 4 +- src/index.js | 28 +- src/lexer.js | 104 ++- src/parser.js | 44 +- 32 files changed, 1506 insertions(+), 477 deletions(-) delete mode 100644 index.d.ts create mode 100644 out/types.d.ts diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b14669b6e..000000000 --- a/index.d.ts +++ /dev/null @@ -1,365 +0,0 @@ -/*! - * Copyright (C) 2017 Glayzzle (BSD3 License) - * @authors https://github.com/glayzzle/php-parser/graphs/contributors - * @url http://glayzzle.com - */ - -declare module "php-parser" { - /** - * Token items - */ - const enum TokenEnum { - T_HALT_COMPILER = 101, - T_USE = 102, - T_ENCAPSED_AND_WHITESPACE = 103, - T_OBJECT_OPERATOR = 104, - T_STRING = 105, - T_DOLLAR_OPEN_CURLY_BRACES = 106, - T_STRING_VARNAME = 107, - T_CURLY_OPEN = 108, - T_NUM_STRING = 109, - T_ISSET = 110, - T_EMPTY = 111, - T_INCLUDE = 112, - T_INCLUDE_ONCE = 113, - T_EVAL = 114, - T_REQUIRE = 115, - T_REQUIRE_ONCE = 116, - T_NAMESPACE = 117, - T_NS_SEPARATOR = 118, - T_AS = 119, - T_IF = 120, - T_ENDIF = 121, - T_WHILE = 122, - T_DO = 123, - T_FOR = 124, - T_SWITCH = 125, - T_BREAK = 126, - T_CONTINUE = 127, - T_RETURN = 128, - T_GLOBAL = 129, - T_STATIC = 130, - T_ECHO = 131, - T_INLINE_HTML = 132, - T_UNSET = 133, - T_FOREACH = 134, - T_DECLARE = 135, - T_TRY = 136, - T_THROW = 137, - T_GOTO = 138, - T_FINALLY = 139, - T_CATCH = 140, - T_ENDDECLARE = 141, - T_LIST = 142, - T_CLONE = 143, - T_PLUS_EQUAL = 144, - T_MINUS_EQUAL = 145, - T_MUL_EQUAL = 146, - T_DIV_EQUAL = 147, - T_CONCAT_EQUAL = 148, - T_MOD_EQUAL = 149, - T_AND_EQUAL = 150, - T_OR_EQUAL = 151, - T_XOR_EQUAL = 152, - T_SL_EQUAL = 153, - T_SR_EQUAL = 154, - T_INC = 155, - T_DEC = 156, - T_BOOLEAN_OR = 157, - T_BOOLEAN_AND = 158, - T_LOGICAL_OR = 159, - T_LOGICAL_AND = 160, - T_LOGICAL_XOR = 161, - T_SL = 162, - T_SR = 163, - T_IS_IDENTICAL = 164, - T_IS_NOT_IDENTICAL = 165, - T_IS_EQUAL = 166, - T_IS_NOT_EQUAL = 167, - T_IS_SMALLER_OR_EQUAL = 168, - T_IS_GREATER_OR_EQUAL = 169, - T_INSTANCEOF = 170, - T_INT_CAST = 171, - T_DOUBLE_CAST = 172, - T_STRING_CAST = 173, - T_ARRAY_CAST = 174, - T_OBJECT_CAST = 175, - T_BOOL_CAST = 176, - T_UNSET_CAST = 177, - T_EXIT = 178, - T_PRINT = 179, - T_YIELD = 180, - T_YIELD_FROM = 181, - T_FUNCTION = 182, - T_DOUBLE_ARROW = 183, - T_DOUBLE_COLON = 184, - T_ARRAY = 185, - T_CALLABLE = 186, - T_CLASS = 187, - T_ABSTRACT = 188, - T_TRAIT = 189, - T_FINAL = 190, - T_EXTENDS = 191, - T_INTERFACE = 192, - T_IMPLEMENTS = 193, - T_VAR = 194, - T_PUBLIC = 195, - T_PROTECTED = 196, - T_PRIVATE = 197, - T_CONST = 198, - T_NEW = 199, - T_INSTEADOF = 200, - T_ELSEIF = 201, - T_ELSE = 202, - T_ENDSWITCH = 203, - T_CASE = 204, - T_DEFAULT = 205, - T_ENDFOR = 206, - T_ENDFOREACH = 207, - T_ENDWHILE = 208, - T_CONSTANT_ENCAPSED_STRING = 209, - T_LNUMBER = 210, - T_DNUMBER = 211, - T_LINE = 212, - T_FILE = 213, - T_DIR = 214, - T_TRAIT_C = 215, - T_METHOD_C = 216, - T_FUNC_C = 217, - T_NS_C = 218, - T_START_HEREDOC = 219, - T_END_HEREDOC = 220, - T_CLASS_C = 221, - T_VARIABLE = 222, - T_OPEN_TAG = 223, - T_OPEN_TAG_WITH_ECHO = 224, - T_CLOSE_TAG = 225, - T_WHITESPACE = 226, - T_COMMENT = 227, - T_DOC_COMMENT = 228, - T_ELLIPSIS = 229, - T_COALESCE = 230, - T_POW = 231, - T_POW_EQUAL = 232, - T_SPACESHIP = 233, - T_COALESCE_EQUAL = 234 - } - - /** - * The tokens dictionnary - */ - interface TokenDefinition { - /** List of token names as texts */ - values: String[], - /** Define tokens */ - names: TokenEnum[] - } - - /** - * The token structure - */ - interface Token extends Array { - // token name - 0: String; - // the token value - 1: TokenEnum; - // the current line - 2: Number - } - - /** - * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): - */ - interface Position { - line: Number; - column: Number; - offset: Number; - } - - /** - * Defines the location of the node (with it's source contents as string) - */ - interface Location { - source: string; - start: Position; - end: Position; - } - - /** - * - */ - interface Node { - kind: String; - loc: Location; - } - - /** - * Error node - */ - interface ParserError extends Node { - message: String; - token: Token; - line: Number; - expected: any; - } - - /** - * A block statement, i.e., a sequence of statements surrounded by braces. - */ - interface Block extends Node { - children: Node[]; - } - - /** - * The main root node - */ - interface Program extends Block { - errors: ParserError[]; - } - - interface Parser { - lexer: Lexer; - ast: AST; - token: TokenEnum; - prev: TokenEnum; - debug: Boolean; - extractDoc: Boolean; - suppressErrors: Boolean; - getTokenName(token:TokenEnum): String; - parse(code: String, filename: String): Program; - raiseError(message: String, msgExpect: String, expect: any, token: TokenEnum): ParserError; - error(expect: String): ParserError; - node(kind:String): Node; - expectEndOfStatement(): Boolean; - showlog(): Parser; - expect(token:TokenEnum): Boolean; - expect(tokens:TokenEnum[]): Boolean; - text(): String; - next(): Parser; - ignoreComments(): Parser; - nextWithComments(): Parser; - is(type: String): Boolean; - // @todo other parsing functions ... - } - - interface KeywordsDictionnary { - [index: string]: TokenEnum - } - - interface yylloc { - first_offset: Number; - first_line: Number; - first_column: Number; - last_line: Number; - last_column: Number; - } - - interface LexerState { - yytext: String; - offset: Number; - yylineno: Number; - yyprevcol: Number; - yylloc: yylloc; - } - - interface Lexer { - debug: Boolean; - all_tokens: Boolean; - comment_tokens: Boolean; - mode_eval: Boolean; - asp_tags: Boolean; - short_tags: Boolean; - keywords: KeywordsDictionnary; - castKeywords: KeywordsDictionnary; - setInput(input:String): Lexer; - input(size:Number): String; - unput(size:Number): Lexer; - tryMatch(match:String): Boolean; - tryMatchCaseless(match:String): Boolean; - ahead(size:Number): String; - consume(size:Number): Lexer; - getState(): LexerState; - setState(state:LexerState): Lexer; - appendToken(value:TokenEnum, ahead:Number): Lexer; - lex(): TokenEnum; - begin(state:String): Lexer; - popState(): String; - next(): TokenEnum; - // @todo other lexer functions ... - } - - - interface AST { - /** - * - */ - withPositions: Boolean; - /** - * Option, if true extracts original source code attached to the node (by default false) - */ - withSource: Boolean; - /** - * Constructor - */ - constructor(withPositions:Boolean, withSource:Boolean): AST; - constructor(withPositions:Boolean): AST; - constructor(): AST; - /** - * Create a position node from specified parser - * including it's lexer current state - */ - position(parser:Parser): Position; - /** - * Prepares an AST node - */ - prepare(kind:String, parser:Parser): Function; - } - - /** - * List of options / extensions - */ - interface Options { - ast?: { - withPositions?: Boolean; - withSource?: Boolean; - }; - lexer?: { - debug?: Boolean; - all_tokens?: Boolean; - comment_tokens?: Boolean; - mode_eval?: Boolean; - asp_tags?: Boolean; - short_tags?: Boolean; - }; - parser?: { - debug?: Boolean; - extractDoc?: Boolean; - suppressErrors?: Boolean; - }; - } - - /** - * Initialise a new parser instance with the specified options - */ - export default class Engine { - // ----- STATIC HELPERS - static create(options?: Options) : Engine; - static parseEval(buffer: String, options: Options) : Program; - static parseEval(buffer: String) : Program; - static parseCode(buffer: String, filename: String, options: Options) : Program; - static parseCode(buffer: String, options: Options) : Program; - static parseCode(buffer: String) : Program; - static tokenGetAll(buffer: String, options: Options) : Token[]; - static tokenGetAll(buffer: String) : Token[]; - // ----- INSTANCE FUNCTIONS - ast: AST; - lexer: Lexer; - parser: Parser; - tokens: TokenDefinition; - constructor(options?: Options); - parseEval(buffer: String) : Program; - parseCode(buffer: String, filename: String) : Program; - parseCode(buffer: String) : Program; - tokenGetAll(buffer: String) : Token[]; - } -} diff --git a/out/types.d.ts b/out/types.d.ts new file mode 100644 index 000000000..41f991214 --- /dev/null +++ b/out/types.d.ts @@ -0,0 +1,1342 @@ +/** + * Defines an array structure + * @example + * // PHP code : + * [1, 'foo' => 'bar', 3] + * + * // AST structure : + * { + * "kind": "array", + * "shortForm": true + * "items": [ + * {"kind": "number", "value": "1"}, + * { + * "kind": "entry", + * "key": {"kind": "string", "value": "foo", "isDoubleQuote": false}, + * "value": {"kind": "string", "value": "bar", "isDoubleQuote": false} + * }, + * {"kind": "number", "value": "3"} + * ] + * } + * @property items - List of array items + * @property shortForm - Indicate if the short array syntax is used, ex `[]` instead `array()` + */ +declare class Array extends Expression { + /** + * List of array items + */ + items: Entry | Expr | Variable; + /** + * Indicate if the short array syntax is used, ex `[]` instead `array()` + */ + shortForm: boolean; +} + +/** + * Defines an arrow function (it's like a closure) + */ +declare class ArrowFunc extends Expression { + arguments: Parameter[]; + type: Identifier; + body: Expression; + byref: boolean; + nullable: boolean; + isStatic: boolean; +} + +/** + * Assigns a value to the specified target + */ +declare class Assign extends Expression { + left: Expression; + right: Expression; + operator: string; +} + +/** + * Assigns a value to the specified target + */ +declare class AssignRef extends Expression { + left: Expression; + right: Expression; + operator: string; +} + +/** + * Binary operations + */ +declare class Bin extends Operation { + type: string; + left: Expression; + right: Expression; +} + +/** + * A block statement, i.e., a sequence of statements surrounded by braces. + */ +declare class Block extends Statement { + children: Node[]; +} + +/** + * Defines a boolean value (true/false) + */ +declare class Boolean extends Literal { +} + +/** + * A break statement + */ +declare class Break extends Statement { + level: number | null; +} + +/** + * Passing by Reference - so the function can modify the variable + */ +declare class ByRef extends Expression { + what: ExpressionStatement; +} + +/** + * Executes a call statement + */ +declare class Call extends Expression { + what: Identifier | Variable; + arguments: Variable[]; +} + +/** + * A switch case statement + * @property test - if null, means that the default case + */ +declare class Case extends Statement { + /** + * if null, means that the default case + */ + test: Expression | null; + body: Block | null; +} + +/** + * Binary operations + */ +declare class Cast extends Operation { + type: string; + raw: string; + expr: Expression; +} + +/** + * Defines a catch statement + */ +declare class Catch extends Statement { + what: Identifier[]; + variable: Variable; + body: Statement; +} + +/** + * A class definition + */ +declare class Class extends Declaration { + extends: Identifier | null; + implements: Identifier[]; + body: Declaration[]; + isAnonymous: boolean; + isAbstract: boolean; + isFinal: boolean; +} + +/** + * Defines a class/interface/trait constant + */ +declare class ClassConstant extends ConstantStatement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; + visibility: string; +} + +/** + * Defines a clone call + */ +declare class Clone extends Expression { + what: Expression; +} + +/** + * Defines a closure + */ +declare class Closure extends Expression { + arguments: Parameter[]; + uses: Variable[]; + type: Identifier; + byref: boolean; + nullable: boolean; + body: Block | null; + isStatic: boolean; +} + +/** + * Abstract documentation node (ComentLine or CommentBlock) + */ +declare class Comment extends Node { + value: string; +} + +/** + * A comment block (multiline) + */ +declare class CommentBlock extends Comment { +} + +/** + * A single line comment + */ +declare class CommentLine extends Comment { +} + +/** + * Defines a constant + */ +declare class Constant extends Node { + name: string; + value: Node | string | number | boolean | null; +} + +/** + * Declares a constants into the current scope + */ +declare class ConstantStatement extends Statement { + constants: Constant[]; +} + +/** + * A continue statement + */ +declare class Continue extends Statement { + level: number | null; +} + +/** + * A declaration statement (function, class, interface...) + */ +declare class Declaration extends Statement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; + name: number | string; +} + +/** + * The declare construct is used to set execution directives for a block of code + */ +declare class Declare extends Block { + directives: any[][]; + mode: string; +} + +/** + * The node is declared as a short tag syntax : + * ```php + * bar_$baz; + * ``` + */ +declare const TYPE_OFFSET: string; + +/** + * Part of `Encapsed` node + */ +declare class EncapsedPart extends Expression { + expression: Expression; + syntax: string; + curly: boolean; +} + +/** + * An array entry - see [Array](#array) + * @property key - The entry key/offset + * @property value - The entry value + * @property byRef - By reference + * @property unpack - Argument unpacking + */ +declare class Entry extends Expression { + /** + * The entry key/offset + */ + key: Node | null; + /** + * The entry value + */ + value: Node; + /** + * By reference + */ + byRef: boolean; + /** + * Argument unpacking + */ + unpack: boolean; +} + +/** + * Defines an error node (used only on silentMode) + */ +declare class Error extends Node { + message: string; + line: number; + token: number | string; + expected: string | any[]; +} + +/** + * Defines an eval statement + */ +declare class Eval extends Expression { + source: Node; +} + +/** + * Defines an exit / die call + */ +declare class Exit extends Expression { + expression: Node | null; + useDie: boolean; +} + +/** + * Any expression node. Since the left-hand side of an assignment may + * be any expression in general, an expression can also be a pattern. + */ +declare class Expression extends Node { +} + +/** + * Defines an expression based statement + */ +declare class ExpressionStatement extends Statement { + expression: Expression; +} + +/** + * Defines a for iterator + */ +declare class For extends Statement { + init: Expression[]; + test: Expression[]; + increment: Expression[]; + body: Statement; + shortForm: boolean; +} + +/** + * Defines a foreach iterator + */ +declare class Foreach extends Statement { + source: Expression; + key: Expression | null; + value: Expression; + body: Statement; + shortForm: boolean; +} + +/** + * Defines a classic function + */ +declare class Function extends Declaration { + arguments: Parameter[]; + type: Identifier; + byref: boolean; + nullable: boolean; + body: Block | null; +} + +/** + * Imports a variable from the global scope + */ +declare class Global extends Statement { + items: Variable[]; +} + +/** + * Defines goto statement + */ +declare class Goto extends Statement { + label: string; +} + +/** + * Halts the compiler execution + * @property after - String after the halt statement + */ +declare class Halt extends Statement { + /** + * String after the halt statement + */ + after: string; +} + +/** + * Defines an identifier node + */ +declare class Identifier extends Node { + name: string; +} + +/** + * Defines a if statement + */ +declare class If extends Statement { + test: Expression; + body: Block; + alternate: Block | If | null; + shortForm: boolean; +} + +/** + * Defines system include call + */ +declare class Include extends Expression { + target: Node; + once: boolean; + require: boolean; +} + +/** + * Defines inline html output (treated as echo output) + */ +declare class Inline extends Literal { +} + +/** + * An interface definition + */ +declare class Interface extends Declaration { + extends: Identifier[]; + body: Declaration[]; +} + +/** + * Defines an isset call + */ +declare class Isset extends Expression { +} + +/** + * A label statement (referenced by goto) + */ +declare class Label extends Statement { + name: string; +} + +/** + * Defines list assignment + */ +declare class List extends Expression { + shortForm: boolean; +} + +/** + * Defines an array structure + */ +declare class Literal extends Expression { + raw: string; + value: Node | string | number | boolean | null; +} + +/** + * Defines the location of the node (with it's source contents as string) + */ +declare class Location { + source: string | null; + start: Position; + end: Position; +} + +/** + * Lookup on an offset in the specified object + */ +declare class Lookup extends Expression { + what: Expression; + offset: Expression; +} + +/** + * Defines magic constant + */ +declare class Magic extends Literal { +} + +/** + * Defines a class/interface/trait method + */ +declare class Method extends Function { + isAbstract: boolean; + isFinal: boolean; + isStatic: boolean; + visibility: string; +} + +/** + * Defines a class reference node + */ +declare class Name extends Reference { + name: string; + resolution: string; +} + +/** + * This is an identifier without a namespace separator, such as Foo + */ +declare const UNQUALIFIED_NAME: string; + +/** + * This is an identifier with a namespace separator, such as Foo\Bar + */ +declare const QUALIFIED_NAME: string; + +/** + * This is an identifier with a namespace separator that begins with + * a namespace separator, such as \Foo\Bar. The namespace \Foo is also + * a fully qualified name. + */ +declare const FULL_QUALIFIED_NAME: string; + +/** + * This is an identifier starting with namespace, such as namespace\Foo\Bar. + */ +declare const RELATIVE_NAME: string; + +/** + * The main program node + */ +declare class Namespace extends Block { + name: string; + withBrackets: boolean; +} + +/** + * Creates a new instance of the specified class + */ +declare class New extends Expression { + what: Identifier | Variable | Class; + arguments: Variable[]; +} + +/** + * A generic AST node + */ +declare class Node { + /** + * Attach comments to current node + */ + setTrailingComments(docs: any): void; + /** + * Destroying an unused node + */ + destroy(): void; + /** + * Includes current token position of the parser + */ + includeToken(parser: any): void; + /** + * Helper for extending the Node class + */ + static extends(type: string, constructor: (...params: any[]) => any): (...params: any[]) => any; + loc: Location | null; + leadingComments: Comment[] | null; + trailingComments: Comment[] | null; + kind: string; +} + +/** + * Ignore this node, it implies a no operation block, for example : + * [$foo, $bar, /* here a noop node * /] + */ +declare class Noop extends Node { +} + +/** + * Defines a nowdoc string + */ +declare class NowDoc extends Literal { + label: string; + raw: string; +} + +/** + * Represents the null keyword + */ +declare class NullKeyword extends Node { +} + +/** + * Defines a numeric value + */ +declare class Number extends Literal { +} + +/** + * Lookup on an offset in an array + */ +declare class OffsetLookup extends Lookup { +} + +/** + * Defines binary operations + */ +declare class Operation extends Expression { +} + +/** + * Defines a function parameter + */ +declare class Parameter extends Declaration { + type: Identifier | null; + value: Node | null; + byref: boolean; + variadic: boolean; + nullable: boolean; +} + +/** + * Defines a class reference node + */ +declare class ParentReference extends Reference { +} + +/** + * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): + */ +declare class Position { + line: number; + column: number; + offset: number; +} + +/** + * Defines a post operation `$i++` or `$i--` + */ +declare class Post extends Operation { + type: string; + what: Variable; +} + +/** + * Defines a pre operation `++$i` or `--$i` + */ +declare class Pre extends Operation { + type: string; + what: Variable; +} + +/** + * Outputs + */ +declare class Print extends Expression { +} + +/** + * The main program node + */ +declare class Program extends Block { + errors: Error[]; + comments: Comment[] | null; + tokens: String[] | null; +} + +/** + * Defines a class property + */ +declare class Property extends Statement { + name: string; + value: Node | null; + nullable: boolean; + type: Identifier | Identifier[] | null; +} + +/** + * Lookup to an object property + */ +declare class PropertyLookup extends Lookup { +} + +/** + * Declares a properties into the current scope + */ +declare class PropertyStatement extends Statement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; + properties: Property[]; +} + +/** + * Defines a reference node + */ +declare class Reference extends Node { +} + +/** + * Defines a short if statement that returns a value + */ +declare class RetIf extends Expression { + test: Expression; + trueExpr: Expression; + falseExpr: Expression; +} + +/** + * A continue statement + */ +declare class Return extends Statement { + expr: Expression | null; +} + +/** + * Defines a class reference node + */ +declare class SelfReference extends Reference { +} + +/** + * Avoids to show/log warnings & notices from the inner expression + */ +declare class Silent extends Expression { + expr: Expression; +} + +/** + * Any statement. + */ +declare class Statement extends Node { +} + +/** + * Declares a static variable into the current scope + */ +declare class Static extends Statement { + variables: StaticVariable[]; +} + +/** + * Lookup to a static property + */ +declare class StaticLookup extends Lookup { +} + +/** + * Defines a class reference node + */ +declare class StaticReference extends Reference { +} + +/** + * Defines a constant + */ +declare class StaticVariable extends Node { + variable: Variable; + defaultValue: Node | string | number | boolean | null; +} + +/** + * Defines a string (simple or double quoted) - chars are already escaped + */ +declare class String extends Literal { + unicode: boolean; + isDoubleQuote: boolean; +} + +/** + * Defines a switch statement + */ +declare class Switch extends Statement { + test: Expression; + body: Block; + shortForm: boolean; +} + +/** + * Defines a throw statement + */ +declare class Throw extends Statement { + what: Expression; +} + +/** + * A trait definition + */ +declare class Trait extends Declaration { + body: Declaration[]; +} + +/** + * Defines a trait alias + */ +declare class TraitAlias extends Node { + trait: Identifier | null; + method: Identifier; + as: Identifier | null; + visibility: string | null; +} + +/** + * Defines a trait alias + */ +declare class TraitPrecedence extends Node { + trait: Identifier | null; + method: Identifier; + instead: Identifier[]; +} + +/** + * Defines a trait usage + */ +declare class TraitUse extends Node { + traits: Identifier[]; + adaptations: Node[] | null; +} + +/** + * Defines a try statement + */ +declare class Try extends Statement { + body: Block; + catches: Catch[]; + allways: Block; +} + +/** + * Defines a class reference node + */ +declare class TypeReference extends Reference { + name: string; +} + +/** + * Unary operations + */ +declare class Unary extends Operation { + type: string; + what: Expression; +} + +/** + * Deletes references to a list of variables + */ +declare class Unset extends Statement { +} + +/** + * Defines a use statement (with a list of use items) + * @property type - Possible value : function, const + */ +declare class UseGroup extends Statement { + name: string | null; + /** + * Possible value : function, const + */ + type: string | null; + item: UseItem[]; +} + +/** + * Defines a use statement (from namespace) + * @property type - Possible value : function, const + */ +declare class UseItem extends Statement { + name: string; + /** + * Possible value : function, const + */ + type: string | null; + alias: Identifier | null; +} + +/** + * Importing a constant + */ +declare const TYPE_CONST: string; + +/** + * Importing a function + */ +declare const TYPE_FUNC: string; + +/** + * Any expression node. Since the left-hand side of an assignment may + * be any expression in general, an expression can also be a pattern. + * @example + * // PHP code : + * $foo + * // AST output + * { + * "kind": "variable", + * "name": "foo", + * "curly": false + * } + * @property name - The variable name (can be a complex expression when the name is resolved dynamically) + * @property curly - Indicate if the name is defined between curlies, ex `${foo}` + */ +declare class Variable extends Expression { + /** + * The variable name (can be a complex expression when the name is resolved dynamically) + */ + name: string | Node; + /** + * Indicate if the name is defined between curlies, ex `${foo}` + */ + curly: boolean; +} + +/** + * Introduce a list of items into the arguments of the call + */ +declare class variadic extends Expression { + what: any[] | Expression; +} + +/** + * Defines a while statement + */ +declare class While extends Statement { + test: Expression; + body: Statement; + shortForm: boolean; +} + +/** + * Defines a yield generator statement + */ +declare class Yield extends Expression { + value: Expression | null; + key: Expression | null; +} + +/** + * Defines a yield from generator statement + */ +declare class YieldFrom extends Expression { + value: Expression; +} + +/** + * The AST builder class + * @property withPositions - Should locate any node (by default false) + * @property withSource - Should extract the node original code (by default false) + */ +declare class AST { + /** + * Change parent node informations after swapping childs + */ + swapLocations(): void; + /** + * Includes locations from first & last into the target + */ + resolveLocations(): void; + /** + * Check and fix precence, by default using right + */ + resolvePrecedence(): void; + /** + * Prepares an AST node + * @param kind - Defines the node type + * (if null, the kind must be passed at the function call) + * @param parser - The parser instance (use for extracting locations) + */ + prepare(kind: string | null, parser: Parser): (...params: any[]) => any; + /** + * Should locate any node (by default false) + */ + withPositions: boolean; + /** + * Should extract the node original code (by default false) + */ + withSource: boolean; +} + +/** + * Initialise a new parser instance with the specified options + * @example + * var parser = require('php-parser'); + * var instance = new parser({ + * parser: { + * extractDoc: true, + * suppressErrors: true, + * version: 704 // or '7.4' + * }, + * ast: { + * withPositions: true + * }, + * lexer: { + * short_tags: true, + * asp_tags: true + * } + * }); + * + * var evalAST = instance.parseEval('some php code'); + * var codeAST = instance.parseCode(' Note that the output tokens are *STRICLY* similar to PHP function `token_get_all` + * @returns - Each item can be a string or an array with following informations [token_name, text, line_number] + */ + tokenGetAll(buffer: string): String[]; + lexer: Lexer; + parser: Parser; + ast: AST; + tokens: any; +} + +/** + * Check if the inpyt is a buffer or a string + * @param buffer - Input value that can be either a buffer or a string + * @returns Returns the string from input + */ +declare function getStringBuffer(buffer: Buffer | string): string; + +/** + * This is the php lexer. It will tokenize the string for helping the + * parser to build the AST from its grammar. + * @property all_tokens - defines if all tokens must be retrieved (used by token_get_all only) + * @property comment_tokens - extracts comments tokens + * @property mode_eval - enables the evald mode (ignore opening tags) + * @property asp_tags - disables by default asp tags mode + * @property short_tags - enables by default short tags mode + * @property keywords - List of php keyword + * @property castKeywords - List of php keywords for type casting + */ +declare class Lexer { + /** + * Initialize the lexer with the specified input + */ + setInput(): void; + /** + * consumes and returns one char from the input + */ + input(): void; + /** + * revert eating specified size + */ + unput(): void; + /** + * check if the text matches + */ + tryMatch(text: string): boolean; + /** + * check if the text matches + */ + tryMatchCaseless(text: string): boolean; + /** + * look ahead + */ + ahead(size: number): string; + /** + * consume the specified size + */ + consume(size: number): Lexer; + /** + * Gets the current state + */ + getState(): void; + /** + * Sets the current lexer state + */ + setState(): void; + /** + * prepend next token + */ + appendToken(value: any, ahead: any): Lexer; + /** + * return next match that has a token + */ + lex(): number | string; + /** + * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) + */ + begin(condition: any): Lexer; + /** + * pop the previously active lexer condition state off the condition stack + */ + popState(): T | string | any; + /** + * return next match in input + */ + next(): number | any; + EOF: number; + /** + * defines if all tokens must be retrieved (used by token_get_all only) + */ + all_tokens: boolean; + /** + * extracts comments tokens + */ + comment_tokens: boolean; + /** + * enables the evald mode (ignore opening tags) + */ + mode_eval: boolean; + /** + * disables by default asp tags mode + */ + asp_tags: boolean; + /** + * enables by default short tags mode + */ + short_tags: boolean; + /** + * List of php keyword + */ + keywords: any; + /** + * List of php keywords for type casting + */ + castKeywords: any; +} + +/** + * reset to true after a new line + */ +declare var inCoutingState: boolean; + +/** + * The PHP Parser class that build the AST tree from the lexer + * @property lexer - current lexer instance + * @property ast - the AST factory instance + * @property token - current token + * @property extractDoc - should extract documentation as AST node + * @property extractTokens - should extract each token + * @property suppressErrors - should ignore parsing errors and continue + * @property debug - should output debug informations + */ +declare class Parser { + /** + * helper : gets a token name + */ + getTokenName(): void; + /** + * main entry point : converts a source code to AST + */ + parse(): void; + /** + * Raise an error + */ + raiseError(): void; + /** + * handling errors + */ + error(): void; + /** + * Creates a new AST node + */ + node(): void; + /** + * expects an end of statement or end of file + */ + expectEndOfStatement(): boolean; + /** + * Force the parser to check the current token. + * + * If the current token does not match to expected token, + * the an error will be raised. + * + * If the suppressError mode is activated, then the error will + * be added to the program error stack and this function will return `false`. + */ + expect(token: string | number): boolean; + /** + * Returns the current token contents + */ + text(): string; + /** + * consume the next token + */ + next(): void; + /** + * Eating a token + */ + lex(): void; + /** + * Check if token is of specified type + */ + is(): void; + /** + * current lexer instance + */ + lexer: Lexer; + /** + * the AST factory instance + */ + ast: AST; + /** + * current token + */ + token: number | string; + /** + * should extract documentation as AST node + */ + extractDoc: boolean; + /** + * should extract each token + */ + extractTokens: boolean; + /** + * should ignore parsing errors and continue + */ + suppressErrors: boolean; + /** + * should output debug informations + */ + debug: boolean; +} + +/** + * outputs some debug information on current token + */ +declare const ignoreStack: any; + diff --git a/package-lock.json b/package-lock.json index 83ac595fb..786a9d6c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13401,6 +13401,15 @@ "punycode": "^2.1.1" } }, + "tsd-jsdoc": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/tsd-jsdoc/-/tsd-jsdoc-2.5.0.tgz", + "integrity": "sha512-80fcJLAiUeerg4xPftp+iEEKWUjJjHk9AvcHwJqA8Zw0R4oASdu3kT/plE/Zj19QUTz8KupyOX25zStlNJjS9g==", + "dev": true, + "requires": { + "typescript": "^3.2.1" + } + }, "tslib": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", @@ -13473,6 +13482,12 @@ "is-typedarray": "^1.0.0" } }, + "typescript": { + "version": "3.9.7", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", + "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", + "dev": true + }, "uc.micro": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", diff --git a/package.json b/package.json index 85b04591e..1fd6bb14f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "build": "webpack --config webpack.config.js", "-postbuild": "npm run build-docs", "build-docs": "jsdoc -c .jsdoc.json", + "build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src", "publish-docs": "git subtree push --prefix docs origin gh-pages" }, "husky": { @@ -70,6 +71,7 @@ "jsdoc": "^3.5.5", "jsdoc-template": "^1.2.0", "prettier": "^2.0.2", + "tsd-jsdoc": "^2.5.0", "uglifyjs-webpack-plugin": "^2.1.2", "webpack": "^4.29.6", "webpack-cli": "^3.2.3" diff --git a/src/ast.js b/src/ast.js index bff1c2959..f5a6de287 100644 --- a/src/ast.js +++ b/src/ast.js @@ -131,7 +131,7 @@ const AST = function (withPositions, withSource) { /** * Create a position node from specified parser * including it's lexer current state - * @param {Parser} + * @param {Parser} parser * @return {Position} * @private */ @@ -426,6 +426,7 @@ AST.prototype.prepare = function (kind, docs, parser) { /** * Release a node without using it on the AST + * @param {*} target */ result.destroy = function (target) { if (docs) { diff --git a/src/ast/assignref.js b/src/ast/assignref.js index 185250fce..a725d244a 100644 --- a/src/ast/assignref.js +++ b/src/ast/assignref.js @@ -10,7 +10,7 @@ const KIND = "assignref"; /** * Assigns a value to the specified target - * @constructor Assign + * @constructor AssignRef * @extends {Expression} * @property {Expression} left * @property {Expression} right diff --git a/src/ast/byref.js b/src/ast/byref.js index 312997733..39ea207fd 100644 --- a/src/ast/byref.js +++ b/src/ast/byref.js @@ -12,7 +12,7 @@ const KIND = "byref"; * Passing by Reference - so the function can modify the variable * @constructor ByRef * @extends {Expression} - * @property {expr} what + * @property {ExpressionStatement} what */ module.exports = Expression.extends(KIND, function ByRef(what, docs, location) { Expression.apply(this, [KIND, docs, location]); diff --git a/src/ast/call.js b/src/ast/call.js index 5d41f14c0..153e0f011 100644 --- a/src/ast/call.js +++ b/src/ast/call.js @@ -12,8 +12,8 @@ const KIND = "call"; * Executes a call statement * @constructor Call * @extends {Expression} - * @property {Identifier|Variable|??} what - * @property {Arguments[]} arguments + * @property {Identifier|Variable} what + * @property {Variable[]} arguments */ module.exports = Expression.extends(KIND, function Call( what, diff --git a/src/ast/classconstant.js b/src/ast/classconstant.js index 2cba246fa..8bbfca754 100644 --- a/src/ast/classconstant.js +++ b/src/ast/classconstant.js @@ -32,7 +32,7 @@ const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant( /** * Generic flags parser - * @param {Integer[]} flags + * @param {Array} flags * @return {void} */ ClassConstant.prototype.parseFlags = function (flags) { diff --git a/src/ast/closure.js b/src/ast/closure.js index a1f975df5..10aada5ef 100644 --- a/src/ast/closure.js +++ b/src/ast/closure.js @@ -15,7 +15,7 @@ const KIND = "closure"; * @property {Parameter[]} arguments * @property {Variable[]} uses * @property {Identifier} type - * @property {boolean} byref + * @property {Boolean} byref * @property {boolean} nullable * @property {Block|null} body * @property {boolean} isStatic diff --git a/src/ast/continue.js b/src/ast/continue.js index 5794282a6..94e21e73a 100644 --- a/src/ast/continue.js +++ b/src/ast/continue.js @@ -12,7 +12,7 @@ const KIND = "continue"; * A continue statement * @constructor Continue * @extends {Statement} - * @property {Number|Null} level + * @property {number|null} level */ module.exports = Statement.extends(KIND, function Continue( level, diff --git a/src/ast/declaration.js b/src/ast/declaration.js index 6f1bc1f98..583f46b91 100644 --- a/src/ast/declaration.js +++ b/src/ast/declaration.js @@ -17,7 +17,7 @@ const IS_PRIVATE = "private"; * A declaration statement (function, class, interface...) * @constructor Declaration * @extends {Statement} - * @property {Identifier|string} name + * @property {number|string} name */ const Declaration = Statement.extends(KIND, function Declaration( kind, @@ -31,7 +31,7 @@ const Declaration = Statement.extends(KIND, function Declaration( /** * Generic flags parser - * @param {Integer[]} flags + * @param {Array} flags * @return {void} */ Declaration.prototype.parseFlags = function (flags) { diff --git a/src/ast/exit.js b/src/ast/exit.js index 19b31c37c..25b02f628 100644 --- a/src/ast/exit.js +++ b/src/ast/exit.js @@ -13,7 +13,7 @@ const KIND = "exit"; * @constructor Exit * @extends {Expression} * @property {Node|null} expression - * @property {Boolean} useDie + * @property {boolean} useDie */ module.exports = Expression.extends(KIND, function Exit( expression, diff --git a/src/ast/goto.js b/src/ast/goto.js index e80f70174..fda91e49f 100644 --- a/src/ast/goto.js +++ b/src/ast/goto.js @@ -12,7 +12,7 @@ const KIND = "goto"; * Defines goto statement * @constructor Goto * @extends {Statement} - * @property {String} label + * @property {string} label * @see {Label} */ module.exports = Statement.extends(KIND, function Goto(label, docs, location) { diff --git a/src/ast/location.js b/src/ast/location.js index 4015c23fe..c557c3ad5 100644 --- a/src/ast/location.js +++ b/src/ast/location.js @@ -8,7 +8,7 @@ /** * Defines the location of the node (with it's source contents as string) * @constructor Location - * @property {String|null} source + * @property {string|null} source * @property {Position} start * @property {Position} end */ diff --git a/src/ast/method.js b/src/ast/method.js index e5127ce8a..a8a5567c8 100644 --- a/src/ast/method.js +++ b/src/ast/method.js @@ -5,19 +5,19 @@ */ "use strict"; -const _Function = require("./function"); +const Function_ = require("./function"); const KIND = "method"; /** * Defines a class/interface/trait method * @constructor Method - * @extends {_Function} + * @extends {Function} * @property {boolean} isAbstract * @property {boolean} isFinal * @property {boolean} isStatic * @property {string} visibility */ -module.exports = _Function.extends(KIND, function Method() { - _Function.apply(this, arguments); +module.exports = Function_.extends(KIND, function Method() { + Function_.apply(this, arguments); this.kind = KIND; }); diff --git a/src/ast/namespace.js b/src/ast/namespace.js index ea9f52e58..46a5d3269 100644 --- a/src/ast/namespace.js +++ b/src/ast/namespace.js @@ -12,8 +12,8 @@ const KIND = "namespace"; * The main program node * @constructor Namespace * @extends {Block} - * @property {String} name - * @property {Boolean} withBrackets + * @property {string} name + * @property {boolean} withBrackets */ module.exports = Block.extends(KIND, function Namespace( name, diff --git a/src/ast/new.js b/src/ast/new.js index 79b23fee6..67cb43983 100644 --- a/src/ast/new.js +++ b/src/ast/new.js @@ -13,7 +13,7 @@ const KIND = "new"; * @constructor New * @extends {Expression} * @property {Identifier|Variable|Class} what - * @property {Arguments[]} arguments + * @property {Variable[]} arguments */ module.exports = Expression.extends(KIND, function New( what, diff --git a/src/ast/node.js b/src/ast/node.js index a77fe61e7..7ae3bf558 100644 --- a/src/ast/node.js +++ b/src/ast/node.js @@ -9,9 +9,9 @@ * A generic AST node * @constructor Node * @property {Location|null} loc - * @property {Comment[]} leadingComments - * @property {Comment[]?} trailingComments - * @property {String} kind + * @property {Comment[]|null} leadingComments + * @property {Comment[]|null} trailingComments + * @property {string} kind */ const Node = function Node(kind, docs, location) { this.kind = kind; @@ -86,7 +86,7 @@ Node.prototype.includeToken = function (parser) { /** * Helper for extending the Node class - * @param {String} type + * @param {string} type * @param {Function} constructor * @return {Function} */ diff --git a/src/ast/nowdoc.js b/src/ast/nowdoc.js index 47951b00a..a90da1b49 100644 --- a/src/ast/nowdoc.js +++ b/src/ast/nowdoc.js @@ -12,8 +12,8 @@ const KIND = "nowdoc"; * Defines a nowdoc string * @constructor NowDoc * @extends {Literal} - * @property {String} label - * @property {String} raw + * @property {string} label + * @property {string} raw */ module.exports = Literal.extends(KIND, function Nowdoc( value, diff --git a/src/ast/position.js b/src/ast/position.js index b802df37e..4e5c43ea0 100644 --- a/src/ast/position.js +++ b/src/ast/position.js @@ -8,9 +8,9 @@ /** * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): * @constructor Position - * @property {Number} line - * @property {Number} column - * @property {Number} offset + * @property {number} line + * @property {number} column + * @property {number} offset */ const Position = function (line, column, offset) { this.line = line; diff --git a/src/ast/program.js b/src/ast/program.js index 3d921a162..7e9bc6c79 100644 --- a/src/ast/program.js +++ b/src/ast/program.js @@ -13,8 +13,8 @@ const KIND = "program"; * @constructor Program * @extends {Block} * @property {Error[]} errors - * @property {Doc[]?} comments - * @property {String[]?} tokens + * @property {Comment[]|null} comments + * @property {String[]|null} tokens */ module.exports = Block.extends(KIND, function Program( children, diff --git a/src/ast/propertystatement.js b/src/ast/propertystatement.js index c7fc0cb70..9b6dbe5a5 100644 --- a/src/ast/propertystatement.js +++ b/src/ast/propertystatement.js @@ -33,7 +33,7 @@ const PropertyStatement = Statement.extends(KIND, function PropertyStatement( /** * Generic flags parser - * @param {Integer[]} flags + * @param {Array} flags * @return {void} */ PropertyStatement.prototype.parseFlags = function (flags) { diff --git a/src/ast/string.js b/src/ast/string.js index dc739041a..e20efa3b8 100644 --- a/src/ast/string.js +++ b/src/ast/string.js @@ -9,7 +9,7 @@ const Literal = require("./literal"); const KIND = "string"; /** - * Defines a string (simple ou double quoted) - chars are already escaped + * Defines a string (simple or double quoted) - chars are already escaped * @constructor String * @extends {Literal} * @property {boolean} unicode diff --git a/src/ast/unary.js b/src/ast/unary.js index cfcbd2aee..8c2ce1763 100644 --- a/src/ast/unary.js +++ b/src/ast/unary.js @@ -12,7 +12,7 @@ const KIND = "unary"; * Unary operations * @constructor Unary * @extends {Operation} - * @property {String} type + * @property {string} type * @property {Expression} what */ module.exports = Operation.extends(KIND, function Unary( diff --git a/src/ast/usegroup.js b/src/ast/usegroup.js index 0eeafd3d9..eeceb6ccc 100644 --- a/src/ast/usegroup.js +++ b/src/ast/usegroup.js @@ -12,8 +12,8 @@ const KIND = "usegroup"; * Defines a use statement (with a list of use items) * @constructor UseGroup * @extends {Statement} - * @property {String|null} name - * @property {String|null} type - Possible value : function, const + * @property {string|null} name + * @property {string|null} type - Possible value : function, const * @property {UseItem[]} item * @see {Namespace} * @see http://php.net/manual/en/language.namespaces.importing.php diff --git a/src/ast/useitem.js b/src/ast/useitem.js index b1067fb37..adaa039d6 100644 --- a/src/ast/useitem.js +++ b/src/ast/useitem.js @@ -12,8 +12,8 @@ const KIND = "useitem"; * Defines a use statement (from namespace) * @constructor UseItem * @extends {Statement} - * @property {String} name - * @property {String|null} type - Possible value : function, const + * @property {string} name + * @property {string|null} type - Possible value : function, const * @property {Identifier|null} alias * @see {Namespace} * @see http://php.net/manual/en/language.namespaces.importing.php diff --git a/src/ast/variable.js b/src/ast/variable.js index bead291b2..5b40eeea1 100644 --- a/src/ast/variable.js +++ b/src/ast/variable.js @@ -22,7 +22,7 @@ const KIND = "variable"; * "name": "foo", * "curly": false * } - * @property {String|Node} name The variable name (can be a complex expression when the name is resolved dynamically) + * @property {string|Node} name The variable name (can be a complex expression when the name is resolved dynamically) * @property {boolean} curly Indicate if the name is defined between curlies, ex `${foo}` */ module.exports = Expression.extends(KIND, function Variable( diff --git a/src/ast/yield.js b/src/ast/yield.js index 5214d0a6f..5ea28034e 100644 --- a/src/ast/yield.js +++ b/src/ast/yield.js @@ -12,8 +12,8 @@ const KIND = "yield"; * Defines a yield generator statement * @constructor Yield * @extends {Expression} - * @property {Expression|Null} value - * @property {Expression|Null} key + * @property {Expression|null} value + * @property {Expression|null} key * @see http://php.net/manual/en/language.generators.syntax.php */ module.exports = Expression.extends(KIND, function Yield( diff --git a/src/index.js b/src/index.js index 24bd5c874..40ac0f033 100644 --- a/src/index.js +++ b/src/index.js @@ -66,7 +66,7 @@ function combine(src, to) { * @property {AST} ast * @property {Object} tokens */ -const engine = function (options) { +const Engine = function (options) { if (typeof this === "function") { return new this(options); } @@ -119,16 +119,16 @@ const getStringBuffer = function (buffer) { * @return {Engine} * @private */ -engine.create = function (options) { - return new engine(options); +Engine.create = function (options) { + return new Engine(options); }; /** * Evaluate the buffer * @private */ -engine.parseEval = function (buffer, options) { - const self = new engine(options); +Engine.parseEval = function (buffer, options) { + const self = new Engine(options); return self.parseEval(buffer); }; @@ -137,7 +137,7 @@ engine.parseEval = function (buffer, options) { * @param {String} buffer * @return {Program} */ -engine.prototype.parseEval = function (buffer) { +Engine.prototype.parseEval = function (buffer) { this.lexer.mode_eval = true; this.lexer.all_tokens = false; buffer = getStringBuffer(buffer); @@ -148,13 +148,13 @@ engine.prototype.parseEval = function (buffer) { * Static function that parse a php code with open/close tags * @private */ -engine.parseCode = function (buffer, filename, options) { +Engine.parseCode = function (buffer, filename, options) { if (typeof filename === "object" && !options) { // retro-compatibility options = filename; filename = "unknown"; } - const self = new engine(options); + const self = new Engine(options); return self.parseCode(buffer, filename); }; @@ -178,7 +178,7 @@ engine.parseCode = function (buffer, filename, options) { * @param {String} filename - Filename * @return {Program} */ -engine.prototype.parseCode = function (buffer, filename) { +Engine.prototype.parseCode = function (buffer, filename) { this.lexer.mode_eval = false; this.lexer.all_tokens = false; buffer = getStringBuffer(buffer); @@ -189,8 +189,8 @@ engine.prototype.parseCode = function (buffer, filename) { * Split the buffer into tokens * @private */ -engine.tokenGetAll = function (buffer, options) { - const self = new engine(options); +Engine.tokenGetAll = function (buffer, options) { + const self = new Engine(options); return self.tokenGetAll(buffer); }; @@ -200,7 +200,7 @@ engine.tokenGetAll = function (buffer, options) { * @param {String} buffer * @return {String[]} - Each item can be a string or an array with following informations [token_name, text, line_number] */ -engine.prototype.tokenGetAll = function (buffer) { +Engine.prototype.tokenGetAll = function (buffer) { this.lexer.mode_eval = false; this.lexer.all_tokens = true; buffer = getStringBuffer(buffer); @@ -221,7 +221,7 @@ engine.prototype.tokenGetAll = function (buffer) { }; // exports the function -module.exports = engine; +module.exports = Engine; // makes libraries public module.exports.tokens = tokens; @@ -231,4 +231,4 @@ module.exports.parser = parser; module.exports.combine = combine; // allow the default export in index.d.ts -module.exports.default = engine; +module.exports.default = Engine; diff --git a/src/lexer.js b/src/lexer.js index 008c0dc1e..abe592dd8 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -9,17 +9,17 @@ * This is the php lexer. It will tokenize the string for helping the * parser to build the AST from its grammar. * - * @class - * @property {Integer} EOF - * @property {Boolean} all_tokens defines if all tokens must be retrieved (used by token_get_all only) - * @property {Boolean} comment_tokens extracts comments tokens - * @property {Boolean} mode_eval enables the evald mode (ignore opening tags) - * @property {Boolean} asp_tags disables by default asp tags mode - * @property {Boolean} short_tags enables by default short tags mode - * @property {Object} keywords List of php keyword - * @property {Object} castKeywords List of php keywords for type casting + * @constructor Lexer + * @property {number} EOF + * @property {boolean} all_tokens defines if all tokens must be retrieved (used by token_get_all only) + * @property {boolean} comment_tokens extracts comments tokens + * @property {boolean} mode_eval enables the evald mode (ignore opening tags) + * @property {boolean} asp_tags disables by default asp tags mode + * @property {boolean} short_tags enables by default short tags mode + * @property {object} keywords List of php keyword + * @property {object} castKeywords List of php keywords for type casting */ -const lexer = function (engine) { +const Lexer = function (engine) { this.engine = engine; this.tok = this.engine.tokens.names; this.EOF = 1; @@ -126,7 +126,7 @@ const lexer = function (engine) { /** * Initialize the lexer with the specified input */ -lexer.prototype.setInput = function (input) { +Lexer.prototype.setInput = function (input) { this._input = input; this.size = input.length; this.yylineno = 1; @@ -183,7 +183,7 @@ lexer.prototype.setInput = function (input) { /** * consumes and returns one char from the input */ -lexer.prototype.input = function () { +Lexer.prototype.input = function () { const ch = this._input[this.offset]; if (!ch) return ""; this.yytext += ch; @@ -205,7 +205,7 @@ lexer.prototype.input = function () { /** * revert eating specified size */ -lexer.prototype.unput = function (size) { +Lexer.prototype.unput = function (size) { if (size === 1) { // 1 char unput (most cases) this.offset--; @@ -268,18 +268,30 @@ lexer.prototype.unput = function (size) { return this; }; -// check if the text matches -lexer.prototype.tryMatch = function (text) { +/** + * check if the text matches + * @param {string} text + * @returns {boolean} + */ +Lexer.prototype.tryMatch = function (text) { return text === this.ahead(text.length); }; -// check if the text matches -lexer.prototype.tryMatchCaseless = function (text) { +/** + * check if the text matches + * @param {string} text + * @returns {boolean} + */ +Lexer.prototype.tryMatchCaseless = function (text) { return text === this.ahead(text.length).toLowerCase(); }; -// look ahead -lexer.prototype.ahead = function (size) { +/** + * look ahead + * @param {number} size + * @returns {string} + */ +Lexer.prototype.ahead = function (size) { let text = this._input.substring(this.offset, this.offset + size); if ( text[text.length - 1] === "\r" && @@ -290,8 +302,12 @@ lexer.prototype.ahead = function (size) { return text; }; -// consume the specified size -lexer.prototype.consume = function (size) { +/** + * consume the specified size + * @param {number} size + * @returns {Lexer} + */ +Lexer.prototype.consume = function (size) { for (let i = 0; i < size; i++) { const ch = this._input[this.offset]; if (!ch) break; @@ -316,7 +332,7 @@ lexer.prototype.consume = function (size) { /** * Gets the current state */ -lexer.prototype.getState = function () { +Lexer.prototype.getState = function () { return { yytext: this.yytext, offset: this.offset, @@ -336,7 +352,7 @@ lexer.prototype.getState = function () { /** * Sets the current lexer state */ -lexer.prototype.setState = function (state) { +Lexer.prototype.setState = function (state) { this.yytext = state.yytext; this.offset = state.offset; this.yylineno = state.yylineno; @@ -348,14 +364,22 @@ lexer.prototype.setState = function (state) { return this; }; -// prepend next token -lexer.prototype.appendToken = function (value, ahead) { +/** + * prepend next token + * @param value + * @param ahead + * @returns {Lexer} + */ +Lexer.prototype.appendToken = function (value, ahead) { this.tokens.push([value, ahead]); return this; }; -// return next match that has a token -lexer.prototype.lex = function () { +/** + * return next match that has a token + * @returns {number|string} + */ +Lexer.prototype.lex = function () { this.yylloc.prev_offset = this.offset; this.yylloc.prev_line = this.yylloc.last_line; this.yylloc.prev_column = this.yylloc.last_column; @@ -391,8 +415,12 @@ lexer.prototype.lex = function () { return token; }; -// activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) -lexer.prototype.begin = function (condition) { +/** + * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) + * @param condition + * @returns {Lexer} + */ +Lexer.prototype.begin = function (condition) { this.conditionStack.push(condition); this.curCondition = condition; this.stateCb = this["match" + condition]; @@ -402,8 +430,11 @@ lexer.prototype.begin = function (condition) { return this; }; -// pop the previously active lexer condition state off the condition stack -lexer.prototype.popState = function () { +/** + * pop the previously active lexer condition state off the condition stack + * @returns {T|string|*} + */ +Lexer.prototype.popState = function () { const n = this.conditionStack.length - 1; const condition = n > 0 ? this.conditionStack.pop() : this.conditionStack[0]; this.curCondition = this.conditionStack[this.conditionStack.length - 1]; @@ -414,8 +445,11 @@ lexer.prototype.popState = function () { return condition; }; -// return next match in input -lexer.prototype.next = function () { +/** + * return next match in input + * @returns {number|*} + */ +Lexer.prototype.next = function () { let token; if (!this._input) { this.done = true; @@ -483,8 +517,8 @@ lexer.prototype.next = function () { require("./lexer/utils.js"), ].forEach(function (ext) { for (const k in ext) { - lexer.prototype[k] = ext[k]; + Lexer.prototype[k] = ext[k]; } }); -module.exports = lexer; +module.exports = Lexer; diff --git a/src/parser.js b/src/parser.js index 3d4cab0b4..2a47607a3 100644 --- a/src/parser.js +++ b/src/parser.js @@ -15,17 +15,17 @@ function isNumber(n) { /** * The PHP Parser class that build the AST tree from the lexer * - * @class + * @constructor Parser * @tutorial Parser * @property {Lexer} lexer - current lexer instance * @property {AST} ast - the AST factory instance - * @property {Integer|String} token - current token - * @property {Boolean} extractDoc - should extract documentation as AST node - * @property {Boolean} extractTokens - should extract each token - * @property {Boolean} suppressErrors - should ignore parsing errors and continue - * @property {Boolean} debug - should output debug informations + * @property {number|string} token - current token + * @property {boolean} extractDoc - should extract documentation as AST node + * @property {boolean} extractTokens - should extract each token + * @property {boolean} suppressErrors - should ignore parsing errors and continue + * @property {boolean} debug - should output debug informations */ -const parser = function (lexer, ast) { +const Parser = function (lexer, ast) { this.lexer = lexer; this.ast = ast; this.tok = lexer.tok; @@ -244,7 +244,7 @@ const parser = function (lexer, ast) { /** * helper : gets a token name */ -parser.prototype.getTokenName = function (token) { +Parser.prototype.getTokenName = function (token) { if (!isNumber(token)) { return "'" + token + "'"; } else { @@ -256,7 +256,7 @@ parser.prototype.getTokenName = function (token) { /** * main entry point : converts a source code to AST */ -parser.prototype.parse = function (code, filename) { +Parser.prototype.parse = function (code, filename) { this._errors = []; this.filename = filename || "eval"; this.currentNamespace = [""]; @@ -324,7 +324,7 @@ parser.prototype.parse = function (code, filename) { /** * Raise an error */ -parser.prototype.raiseError = function (message, msgExpect, expect, token) { +Parser.prototype.raiseError = function (message, msgExpect, expect, token) { message += " on line " + this.lexer.yylloc.first_line; if (!this.suppressErrors) { const err = new SyntaxError( @@ -351,7 +351,7 @@ parser.prototype.raiseError = function (message, msgExpect, expect, token) { /** * handling errors */ -parser.prototype.error = function (expect) { +Parser.prototype.error = function (expect) { let msg = "Parse Error : syntax error"; let token = this.getTokenName(this.token); let msgExpect = ""; @@ -378,7 +378,7 @@ parser.prototype.error = function (expect) { /** * Creates a new AST node */ -parser.prototype.node = function (name) { +Parser.prototype.node = function (name) { if (this.extractDoc) { let docs = null; if (this._docIndex < this._docs.length) { @@ -455,7 +455,7 @@ parser.prototype.node = function (name) { * expects an end of statement or end of file * @return {boolean} */ -parser.prototype.expectEndOfStatement = function (node) { +Parser.prototype.expectEndOfStatement = function (node) { if (this.token === ";") { // include only real ';' statements // https://github.com/glayzzle/php-parser/issues/164 @@ -472,7 +472,7 @@ parser.prototype.expectEndOfStatement = function (node) { /** outputs some debug information on current token **/ const ignoreStack = ["parser.next", "parser.node", "parser.showlog"]; -parser.prototype.showlog = function () { +Parser.prototype.showlog = function () { const stack = new Error().stack.split("\n"); let line; for (let offset = 2; offset < stack.length; offset++) { @@ -516,7 +516,7 @@ parser.prototype.showlog = function () { * @return {boolean} * @throws Error */ -parser.prototype.expect = function (token) { +Parser.prototype.expect = function (token) { if (Array.isArray(token)) { if (token.indexOf(this.token) === -1) { this.error(token); @@ -533,12 +533,12 @@ parser.prototype.expect = function (token) { * Returns the current token contents * @return {String} */ -parser.prototype.text = function () { +Parser.prototype.text = function () { return this.lexer.yytext; }; /** consume the next token **/ -parser.prototype.next = function () { +Parser.prototype.next = function () { // prepare the back command if (this.token !== ";" || this.lexer.yytext === ";") { // ignore '?>' from automated resolution @@ -579,7 +579,7 @@ parser.prototype.next = function () { /** * Eating a token */ -parser.prototype.lex = function () { +Parser.prototype.lex = function () { // append on token stack if (this.extractTokens) { do { @@ -630,7 +630,7 @@ parser.prototype.lex = function () { /** * Check if token is of specified type */ -parser.prototype.is = function (type) { +Parser.prototype.is = function (type) { if (Array.isArray(type)) { return type.indexOf(this.token) !== -1; } @@ -656,12 +656,12 @@ parser.prototype.is = function (type) { require("./parser/variable.js"), ].forEach(function (ext) { for (const k in ext) { - if (parser.prototype.hasOwnProperty(k)) { + if (Parser.prototype.hasOwnProperty(k)) { // @see https://github.com/glayzzle/php-parser/issues/234 throw new Error("Function " + k + " is already defined - collision"); } - parser.prototype[k] = ext[k]; + Parser.prototype[k] = ext[k]; } }); -module.exports = parser; +module.exports = Parser; From 1e1a4469bd93e04a4bd2cebcb164f43b8ac7fe06 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Wed, 5 Aug 2020 00:19:20 -0700 Subject: [PATCH 02/12] Fix Array item type + Comment body types --- out/types.d.ts | 6 +++--- src/ast/array.js | 2 +- src/ast/node.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/out/types.d.ts b/out/types.d.ts index 41f991214..5503cc0a9 100644 --- a/out/types.d.ts +++ b/out/types.d.ts @@ -25,7 +25,7 @@ declare class Array extends Expression { /** * List of array items */ - items: Entry | Expr | Variable; + items: Entry | Expression | Variable; /** * Indicate if the short array syntax is used, ex `[]` instead `array()` */ @@ -659,8 +659,8 @@ declare class Node { */ static extends(type: string, constructor: (...params: any[]) => any): (...params: any[]) => any; loc: Location | null; - leadingComments: Comment[] | null; - trailingComments: Comment[] | null; + leadingComments: CommentBlock[] | Comment[] | null; + trailingComments: CommentBlock[] | Comment[] | null; kind: string; } diff --git a/src/ast/array.js b/src/ast/array.js index f458d3b73..80f836acc 100644 --- a/src/ast/array.js +++ b/src/ast/array.js @@ -30,7 +30,7 @@ const KIND = "array"; * ] * } * @extends {Expression} - * @property {Entry|Expr|Variable} items List of array items + * @property {Entry|Expression|Variable} items List of array items * @property {boolean} shortForm Indicate if the short array syntax is used, ex `[]` instead `array()` */ module.exports = Expr.extends(KIND, function Array( diff --git a/src/ast/node.js b/src/ast/node.js index 7ae3bf558..41a975e19 100644 --- a/src/ast/node.js +++ b/src/ast/node.js @@ -9,8 +9,8 @@ * A generic AST node * @constructor Node * @property {Location|null} loc - * @property {Comment[]|null} leadingComments - * @property {Comment[]|null} trailingComments + * @property {CommentBlock[]|Comment[]|null} leadingComments + * @property {CommentBlock[]|Comment[]|null} trailingComments * @property {string} kind */ const Node = function Node(kind, docs, location) { From 635dcd2a9209ddbacbd54ddd2f36727861db28a7 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 8 Aug 2020 20:51:30 -0700 Subject: [PATCH 03/12] Fix typing of `name` for Declaration object --- src/ast/declaration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/declaration.js b/src/ast/declaration.js index 583f46b91..7774356a9 100644 --- a/src/ast/declaration.js +++ b/src/ast/declaration.js @@ -17,7 +17,7 @@ const IS_PRIVATE = "private"; * A declaration statement (function, class, interface...) * @constructor Declaration * @extends {Statement} - * @property {number|string} name + * @property {Identifier|string} name */ const Declaration = Statement.extends(KIND, function Declaration( kind, From ee6ff077d2086a198243b024446a69408c68ff4d Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 8 Aug 2020 20:54:04 -0700 Subject: [PATCH 04/12] Move types.d.ts to root of project --- package.json | 3 ++- out/types.d.ts => types.d.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) rename out/types.d.ts => types.d.ts (99%) diff --git a/package.json b/package.json index 1fd6bb14f..4273ed559 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "index.d.ts", "LICENSE" ], + "types": "types.d.ts", "scripts": { "fix": "eslint . --fix", "test": "jest", @@ -18,7 +19,7 @@ "build": "webpack --config webpack.config.js", "-postbuild": "npm run build-docs", "build-docs": "jsdoc -c .jsdoc.json", - "build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src", + "build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src -d .", "publish-docs": "git subtree push --prefix docs origin gh-pages" }, "husky": { diff --git a/out/types.d.ts b/types.d.ts similarity index 99% rename from out/types.d.ts rename to types.d.ts index 5503cc0a9..4381b3399 100644 --- a/out/types.d.ts +++ b/types.d.ts @@ -228,7 +228,7 @@ declare class Declaration extends Statement { * Generic flags parser */ parseFlags(flags: (number | null)[]): void; - name: number | string; + name: Identifier | string; } /** From 09ec6076ac450261f3e900c3a92464ad3e56143b Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sun, 9 Aug 2020 16:43:11 -0700 Subject: [PATCH 05/12] Move all type definitions to php-parser module --- src/ast.js | 1 + src/ast/array.js | 1 + src/ast/arrowfunc.js | 1 + src/ast/assign.js | 1 + src/ast/assignref.js | 1 + src/ast/bin.js | 1 + src/ast/block.js | 1 + src/ast/boolean.js | 1 + src/ast/break.js | 1 + src/ast/byref.js | 1 + src/ast/call.js | 1 + src/ast/case.js | 1 + src/ast/cast.js | 1 + src/ast/catch.js | 1 + src/ast/class.js | 1 + src/ast/classconstant.js | 3 + src/ast/clone.js | 1 + src/ast/closure.js | 1 + src/ast/comment.js | 1 + src/ast/commentblock.js | 1 + src/ast/commentline.js | 1 + src/ast/constant.js | 1 + src/ast/constantstatement.js | 1 + src/ast/continue.js | 1 + src/ast/declaration.js | 1 + src/ast/declare.js | 4 + src/ast/declaredirective.js | 1 + src/ast/do.js | 1 + src/ast/echo.js | 1 + src/ast/empty.js | 1 + src/ast/encapsed.js | 5 + src/ast/encapsedpart.js | 1 + src/ast/entry.js | 1 + src/ast/error.js | 1 + src/ast/eval.js | 1 + src/ast/exit.js | 1 + src/ast/expression.js | 1 + src/ast/expressionstatement.js | 1 + src/ast/for.js | 1 + src/ast/foreach.js | 1 + src/ast/function.js | 1 + src/ast/global.js | 1 + src/ast/goto.js | 1 + src/ast/halt.js | 1 + src/ast/identifier.js | 1 + src/ast/if.js | 1 + src/ast/include.js | 1 + src/ast/inline.js | 1 + src/ast/interface.js | 1 + src/ast/isset.js | 1 + src/ast/label.js | 1 + src/ast/list.js | 1 + src/ast/literal.js | 1 + src/ast/location.js | 1 + src/ast/lookup.js | 1 + src/ast/magic.js | 1 + src/ast/method.js | 1 + src/ast/name.js | 5 + src/ast/namespace.js | 1 + src/ast/new.js | 1 + src/ast/node.js | 1 + src/ast/noop.js | 1 + src/ast/nowdoc.js | 1 + src/ast/nullkeyword.js | 1 + src/ast/number.js | 1 + src/ast/offsetlookup.js | 1 + src/ast/operation.js | 1 + src/ast/parameter.js | 1 + src/ast/parentreference.js | 1 + src/ast/position.js | 1 + src/ast/post.js | 1 + src/ast/pre.js | 1 + src/ast/print.js | 1 + src/ast/program.js | 1 + src/ast/property.js | 1 + src/ast/propertylookup.js | 1 + src/ast/propertystatement.js | 1 + src/ast/reference.js | 1 + src/ast/retif.js | 1 + src/ast/return.js | 1 + src/ast/selfreference.js | 1 + src/ast/silent.js | 1 + src/ast/statement.js | 1 + src/ast/static.js | 1 + src/ast/staticlookup.js | 1 + src/ast/staticreference.js | 1 + src/ast/staticvariable.js | 1 + src/ast/string.js | 1 + src/ast/switch.js | 1 + src/ast/throw.js | 1 + src/ast/trait.js | 1 + src/ast/traitalias.js | 1 + src/ast/traitprecedence.js | 1 + src/ast/traituse.js | 1 + src/ast/try.js | 1 + src/ast/typereference.js | 1 + src/ast/unary.js | 1 + src/ast/unset.js | 1 + src/ast/usegroup.js | 1 + src/ast/useitem.js | 7 +- src/ast/variable.js | 1 + src/ast/variadic.js | 1 + src/ast/while.js | 1 + src/ast/yield.js | 1 + src/ast/yieldfrom.js | 1 + src/index.js | 4 + src/lexer.js | 1 + src/parser.js | 6 +- src/parser/scalar.js | 1 + types.d.ts | 2122 +++++++++++++------------------- 110 files changed, 1003 insertions(+), 1255 deletions(-) diff --git a/src/ast.js b/src/ast.js index f5a6de287..33fb4d226 100644 --- a/src/ast.js +++ b/src/ast.js @@ -119,6 +119,7 @@ const Position = require("./ast/position"); /** * The AST builder class * @constructor AST + * @memberOf module:php-parser * @tutorial AST * @property {Boolean} withPositions - Should locate any node (by default false) * @property {Boolean} withSource - Should extract the node original code (by default false) diff --git a/src/ast/array.js b/src/ast/array.js index 80f836acc..5619f599b 100644 --- a/src/ast/array.js +++ b/src/ast/array.js @@ -11,6 +11,7 @@ const KIND = "array"; /** * Defines an array structure * @constructor Array + * @memberOf module:php-parser * @example * // PHP code : * [1, 'foo' => 'bar', 3] diff --git a/src/ast/arrowfunc.js b/src/ast/arrowfunc.js index edaa78f24..e2a5c173f 100644 --- a/src/ast/arrowfunc.js +++ b/src/ast/arrowfunc.js @@ -11,6 +11,7 @@ const KIND = "arrowfunc"; /** * Defines an arrow function (it's like a closure) * @constructor ArrowFunc + * @memberOf module:php-parser * @extends {Expression} * @property {Parameter[]} arguments * @property {Identifier} type diff --git a/src/ast/assign.js b/src/ast/assign.js index 0d8050398..2b9f10afa 100644 --- a/src/ast/assign.js +++ b/src/ast/assign.js @@ -11,6 +11,7 @@ const KIND = "assign"; /** * Assigns a value to the specified target * @constructor Assign + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} left * @property {Expression} right diff --git a/src/ast/assignref.js b/src/ast/assignref.js index a725d244a..80c9b6856 100644 --- a/src/ast/assignref.js +++ b/src/ast/assignref.js @@ -11,6 +11,7 @@ const KIND = "assignref"; /** * Assigns a value to the specified target * @constructor AssignRef + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} left * @property {Expression} right diff --git a/src/ast/bin.js b/src/ast/bin.js index 9c3724ef3..412afd66c 100644 --- a/src/ast/bin.js +++ b/src/ast/bin.js @@ -10,6 +10,7 @@ const KIND = "bin"; /** * Binary operations * @constructor Bin + * @memberOf module:php-parser * @extends {Operation} * @property {String} type * @property {Expression} left diff --git a/src/ast/block.js b/src/ast/block.js index 4df2baff6..1a91bc17f 100644 --- a/src/ast/block.js +++ b/src/ast/block.js @@ -11,6 +11,7 @@ const KIND = "block"; /** * A block statement, i.e., a sequence of statements surrounded by braces. * @constructor Block + * @memberOf module:php-parser * @extends {Statement} * @property {Node[]} children */ diff --git a/src/ast/boolean.js b/src/ast/boolean.js index 7fd6e359f..e5c5b7d23 100644 --- a/src/ast/boolean.js +++ b/src/ast/boolean.js @@ -11,6 +11,7 @@ const KIND = "boolean"; /** * Defines a boolean value (true/false) * @constructor Boolean + * @memberOf module:php-parser * @extends {Literal} */ module.exports = Literal.extends(KIND, function Boolean( diff --git a/src/ast/break.js b/src/ast/break.js index 79e073153..2e98e2515 100644 --- a/src/ast/break.js +++ b/src/ast/break.js @@ -11,6 +11,7 @@ const KIND = "break"; /** * A break statement * @constructor Break + * @memberOf module:php-parser * @extends {Statement} * @property {Number|Null} level */ diff --git a/src/ast/byref.js b/src/ast/byref.js index 39ea207fd..89cfb98c2 100644 --- a/src/ast/byref.js +++ b/src/ast/byref.js @@ -11,6 +11,7 @@ const KIND = "byref"; /** * Passing by Reference - so the function can modify the variable * @constructor ByRef + * @memberOf module:php-parser * @extends {Expression} * @property {ExpressionStatement} what */ diff --git a/src/ast/call.js b/src/ast/call.js index 153e0f011..0d605ed2f 100644 --- a/src/ast/call.js +++ b/src/ast/call.js @@ -11,6 +11,7 @@ const KIND = "call"; /** * Executes a call statement * @constructor Call + * @memberOf module:php-parser * @extends {Expression} * @property {Identifier|Variable} what * @property {Variable[]} arguments diff --git a/src/ast/case.js b/src/ast/case.js index 47cb18a1e..a4027b1f3 100644 --- a/src/ast/case.js +++ b/src/ast/case.js @@ -11,6 +11,7 @@ const KIND = "case"; /** * A switch case statement * @constructor Case + * @memberOf module:php-parser * @extends {Statement} * @property {Expression|null} test - if null, means that the default case * @property {Block|null} body diff --git a/src/ast/cast.js b/src/ast/cast.js index c34701e5f..3305a14ef 100644 --- a/src/ast/cast.js +++ b/src/ast/cast.js @@ -11,6 +11,7 @@ const KIND = "cast"; /** * Binary operations * @constructor Cast + * @memberOf module:php-parser * @extends {Operation} * @property {String} type * @property {String} raw diff --git a/src/ast/catch.js b/src/ast/catch.js index 7640a84a5..60081ac0a 100644 --- a/src/ast/catch.js +++ b/src/ast/catch.js @@ -11,6 +11,7 @@ const KIND = "catch"; /** * Defines a catch statement * @constructor Catch + * @memberOf module:php-parser * @extends {Statement} * @property {Identifier[]} what * @property {Variable} variable diff --git a/src/ast/class.js b/src/ast/class.js index ecc94d9d5..4d9fe887b 100644 --- a/src/ast/class.js +++ b/src/ast/class.js @@ -11,6 +11,7 @@ const KIND = "class"; /** * A class definition * @constructor Class + * @memberOf module:php-parser * @extends {Declaration} * @property {Identifier|null} extends * @property {Identifier[]} implements diff --git a/src/ast/classconstant.js b/src/ast/classconstant.js index 8bbfca754..ac6a1b92b 100644 --- a/src/ast/classconstant.js +++ b/src/ast/classconstant.js @@ -16,6 +16,7 @@ const IS_PRIVATE = "private"; /** * Defines a class/interface/trait constant * @constructor ClassConstant + * @memberOf module:php-parser * @extends {ConstantStatement} * @property {string} visibility */ @@ -32,6 +33,8 @@ const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant( /** * Generic flags parser + * @function + * @name ClassConstant#parseFlags * @param {Array} flags * @return {void} */ diff --git a/src/ast/clone.js b/src/ast/clone.js index f2ee4f9b7..54ff3e37b 100644 --- a/src/ast/clone.js +++ b/src/ast/clone.js @@ -11,6 +11,7 @@ const KIND = "clone"; /** * Defines a clone call * @constructor Clone + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} what */ diff --git a/src/ast/closure.js b/src/ast/closure.js index 10aada5ef..f3af008c5 100644 --- a/src/ast/closure.js +++ b/src/ast/closure.js @@ -11,6 +11,7 @@ const KIND = "closure"; /** * Defines a closure * @constructor Closure + * @memberOf module:php-parser * @extends {Expression} * @property {Parameter[]} arguments * @property {Variable[]} uses diff --git a/src/ast/comment.js b/src/ast/comment.js index e725a4d8c..ead9dc86d 100644 --- a/src/ast/comment.js +++ b/src/ast/comment.js @@ -10,6 +10,7 @@ const Node = require("./node"); /** * Abstract documentation node (ComentLine or CommentBlock) * @constructor Comment + * @memberOf module:php-parser * @extends {Node} * @property {String} value */ diff --git a/src/ast/commentblock.js b/src/ast/commentblock.js index 1f58ecac7..92d1aef51 100644 --- a/src/ast/commentblock.js +++ b/src/ast/commentblock.js @@ -11,6 +11,7 @@ const KIND = "commentblock"; /** * A comment block (multiline) * @constructor CommentBlock + * @memberOf module:php-parser * @extends {Comment} */ module.exports = Comment.extends(KIND, function CommentBlock( diff --git a/src/ast/commentline.js b/src/ast/commentline.js index 65b0357f4..df2e33850 100644 --- a/src/ast/commentline.js +++ b/src/ast/commentline.js @@ -11,6 +11,7 @@ const KIND = "commentline"; /** * A single line comment * @constructor CommentLine + * @memberOf module:php-parser * @extends {Comment} */ module.exports = Comment.extends(KIND, function CommentLine( diff --git a/src/ast/constant.js b/src/ast/constant.js index 750f434d9..4f18f0d30 100644 --- a/src/ast/constant.js +++ b/src/ast/constant.js @@ -11,6 +11,7 @@ const KIND = "constant"; /** * Defines a constant * @constructor Constant + * @memberOf module:php-parser * @extends {Node} * @property {string} name * @property {Node|string|number|boolean|null} value diff --git a/src/ast/constantstatement.js b/src/ast/constantstatement.js index 5c79e23d0..6556abeb6 100644 --- a/src/ast/constantstatement.js +++ b/src/ast/constantstatement.js @@ -11,6 +11,7 @@ const KIND = "constantstatement"; /** * Declares a constants into the current scope * @constructor ConstantStatement + * @memberOf module:php-parser * @extends {Statement} * @property {Constant[]} constants */ diff --git a/src/ast/continue.js b/src/ast/continue.js index 94e21e73a..355567c19 100644 --- a/src/ast/continue.js +++ b/src/ast/continue.js @@ -11,6 +11,7 @@ const KIND = "continue"; /** * A continue statement * @constructor Continue + * @memberOf module:php-parser * @extends {Statement} * @property {number|null} level */ diff --git a/src/ast/declaration.js b/src/ast/declaration.js index 7774356a9..5450b3815 100644 --- a/src/ast/declaration.js +++ b/src/ast/declaration.js @@ -16,6 +16,7 @@ const IS_PRIVATE = "private"; /** * A declaration statement (function, class, interface...) * @constructor Declaration + * @memberOf module:php-parser * @extends {Statement} * @property {Identifier|string} name */ diff --git a/src/ast/declare.js b/src/ast/declare.js index 2dbaea8ca..2b416b715 100644 --- a/src/ast/declare.js +++ b/src/ast/declare.js @@ -11,6 +11,7 @@ const KIND = "declare"; /** * The declare construct is used to set execution directives for a block of code * @constructor Declare + * @memberOf module:php-parser * @extends {Block} * @property {Array[]} directives * @property {String} mode @@ -37,6 +38,7 @@ const Declare = Block.extends(KIND, function Declare( * enddeclare; * ``` * @constant {String} MODE_SHORT + * @memberOf Declare */ Declare.MODE_SHORT = "short"; @@ -49,6 +51,7 @@ Declare.MODE_SHORT = "short"; * } * ``` * @constant {String} MODE_BLOCK + * @memberOf Declare */ Declare.MODE_BLOCK = "block"; @@ -64,6 +67,7 @@ Declare.MODE_BLOCK = "block"; * // some statements * ``` * @constant {String} MODE_NONE + * @memberOf Declare */ Declare.MODE_NONE = "none"; diff --git a/src/ast/declaredirective.js b/src/ast/declaredirective.js index 719fbf50c..82a84363c 100644 --- a/src/ast/declaredirective.js +++ b/src/ast/declaredirective.js @@ -11,6 +11,7 @@ const KIND = "declaredirective"; /** * Defines a constant * @constructor DeclareDirective + * @memberOf module:php-parser * @extends {Node} * @property {Identifier} name * @property {Node|string|number|boolean|null} value diff --git a/src/ast/do.js b/src/ast/do.js index 80a1cff00..fa2e2adb8 100644 --- a/src/ast/do.js +++ b/src/ast/do.js @@ -11,6 +11,7 @@ const KIND = "do"; /** * Defines a do/while statement * @constructor Do + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} test * @property {Statement} body diff --git a/src/ast/echo.js b/src/ast/echo.js index 65af28ab2..18a03c839 100644 --- a/src/ast/echo.js +++ b/src/ast/echo.js @@ -11,6 +11,7 @@ const KIND = "echo"; /** * Defines system based call * @constructor Echo + * @memberOf module:php-parser * @property {boolean} shortForm * @extends {Statement} */ diff --git a/src/ast/empty.js b/src/ast/empty.js index 142c0ed07..511c9324b 100644 --- a/src/ast/empty.js +++ b/src/ast/empty.js @@ -11,6 +11,7 @@ const KIND = "empty"; /** * Defines an empty check call * @constructor Empty + * @memberOf module:php-parser * @extends {Expression} */ module.exports = Expression.extends(KIND, function Empty( diff --git a/src/ast/encapsed.js b/src/ast/encapsed.js index d78c0afa2..c3e25bc9b 100644 --- a/src/ast/encapsed.js +++ b/src/ast/encapsed.js @@ -11,6 +11,7 @@ const KIND = "encapsed"; /** * Defines an encapsed string (contains expressions) * @constructor Encapsed + * @memberOf module:php-parser * @extends {Literal} * @property {String} type - Defines the type of encapsed string (shell, heredoc, string) * @property {String|Null} label - The heredoc label, defined only when the type is heredoc @@ -33,6 +34,7 @@ const Encapsed = Literal.extends(KIND, function Encapsed( * echo "hello $world"; * ``` * @constant {String} TYPE_STRING - `string` + * @memberOf Encapsed */ Encapsed.TYPE_STRING = "string"; @@ -43,6 +45,7 @@ Encapsed.TYPE_STRING = "string"; * echo `ls -larth $path`; * ``` * @constant {String} TYPE_SHELL - `shell` + * @memberOf Encapsed */ Encapsed.TYPE_SHELL = "shell"; @@ -56,6 +59,7 @@ Encapsed.TYPE_SHELL = "shell"; * ; * ``` * @constant {String} TYPE_HEREDOC - `heredoc` + * @memberOf Encapsed */ Encapsed.TYPE_HEREDOC = "heredoc"; @@ -66,6 +70,7 @@ Encapsed.TYPE_HEREDOC = "heredoc"; * echo $foo->bar_$baz; * ``` * @constant {String} TYPE_OFFSET - `offset` + * @memberOf Encapsed */ Encapsed.TYPE_OFFSET = "offset"; diff --git a/src/ast/encapsedpart.js b/src/ast/encapsedpart.js index 3aedbae4a..878878276 100644 --- a/src/ast/encapsedpart.js +++ b/src/ast/encapsedpart.js @@ -11,6 +11,7 @@ const KIND = "encapsedpart"; /** * Part of `Encapsed` node * @constructor EncapsedPart + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} expression * @property {String} syntax diff --git a/src/ast/entry.js b/src/ast/entry.js index d3f48c138..8217c6a3e 100644 --- a/src/ast/entry.js +++ b/src/ast/entry.js @@ -11,6 +11,7 @@ const KIND = "entry"; /** * An array entry - see [Array](#array) * @constructor Entry + * @memberOf module:php-parser * @extends {Expression} * @property {Node|null} key The entry key/offset * @property {Node} value The entry value diff --git a/src/ast/error.js b/src/ast/error.js index a8f6b7e52..43c1bf263 100644 --- a/src/ast/error.js +++ b/src/ast/error.js @@ -11,6 +11,7 @@ const KIND = "error"; /** * Defines an error node (used only on silentMode) * @constructor Error + * @memberOf module:php-parser * @extends {Node} * @property {string} message * @property {number} line diff --git a/src/ast/eval.js b/src/ast/eval.js index 2d81dc6c2..888b247d4 100644 --- a/src/ast/eval.js +++ b/src/ast/eval.js @@ -11,6 +11,7 @@ const KIND = "eval"; /** * Defines an eval statement * @constructor Eval + * @memberOf module:php-parser * @extends {Expression} * @property {Node} source */ diff --git a/src/ast/exit.js b/src/ast/exit.js index 25b02f628..b39a6ef17 100644 --- a/src/ast/exit.js +++ b/src/ast/exit.js @@ -11,6 +11,7 @@ const KIND = "exit"; /** * Defines an exit / die call * @constructor Exit + * @memberOf module:php-parser * @extends {Expression} * @property {Node|null} expression * @property {boolean} useDie diff --git a/src/ast/expression.js b/src/ast/expression.js index 3b4d39d8e..c64f16a6c 100644 --- a/src/ast/expression.js +++ b/src/ast/expression.js @@ -12,6 +12,7 @@ const KIND = "expression"; * Any expression node. Since the left-hand side of an assignment may * be any expression in general, an expression can also be a pattern. * @constructor Expression + * @memberOf module:php-parser * @extends {Node} */ module.exports = Node.extends(KIND, function Expression(kind, docs, location) { diff --git a/src/ast/expressionstatement.js b/src/ast/expressionstatement.js index f8d2fca40..b58944915 100644 --- a/src/ast/expressionstatement.js +++ b/src/ast/expressionstatement.js @@ -11,6 +11,7 @@ const KIND = "expressionstatement"; /** * Defines an expression based statement * @constructor ExpressionStatement + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} expression */ diff --git a/src/ast/for.js b/src/ast/for.js index 773743b86..db58d8e7d 100644 --- a/src/ast/for.js +++ b/src/ast/for.js @@ -11,6 +11,7 @@ const KIND = "for"; /** * Defines a for iterator * @constructor For + * @memberOf module:php-parser * @extends {Statement} * @property {Expression[]} init * @property {Expression[]} test diff --git a/src/ast/foreach.js b/src/ast/foreach.js index e2143fb4c..29d311a67 100644 --- a/src/ast/foreach.js +++ b/src/ast/foreach.js @@ -11,6 +11,7 @@ const KIND = "foreach"; /** * Defines a foreach iterator * @constructor Foreach + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} source * @property {Expression|null} key diff --git a/src/ast/function.js b/src/ast/function.js index abbbdd4e9..c6ebe0019 100644 --- a/src/ast/function.js +++ b/src/ast/function.js @@ -11,6 +11,7 @@ const KIND = "function"; /** * Defines a classic function * @constructor Function + * @memberOf module:php-parser * @extends {Declaration} * @property {Parameter[]} arguments * @property {Identifier} type diff --git a/src/ast/global.js b/src/ast/global.js index f9cc9c064..589015ff0 100644 --- a/src/ast/global.js +++ b/src/ast/global.js @@ -11,6 +11,7 @@ const KIND = "global"; /** * Imports a variable from the global scope * @constructor Global + * @memberOf module:php-parser * @extends {Statement} * @property {Variable[]} items */ diff --git a/src/ast/goto.js b/src/ast/goto.js index fda91e49f..4e00653eb 100644 --- a/src/ast/goto.js +++ b/src/ast/goto.js @@ -11,6 +11,7 @@ const KIND = "goto"; /** * Defines goto statement * @constructor Goto + * @memberOf module:php-parser * @extends {Statement} * @property {string} label * @see {Label} diff --git a/src/ast/halt.js b/src/ast/halt.js index eaa0dd2a9..acad687dc 100644 --- a/src/ast/halt.js +++ b/src/ast/halt.js @@ -11,6 +11,7 @@ const KIND = "halt"; /** * Halts the compiler execution * @constructor Halt + * @memberOf module:php-parser * @extends {Statement} * @property {String} after - String after the halt statement * @see http://php.net/manual/en/function.halt-compiler.php diff --git a/src/ast/identifier.js b/src/ast/identifier.js index 9b7c25419..fc2f94d6a 100644 --- a/src/ast/identifier.js +++ b/src/ast/identifier.js @@ -11,6 +11,7 @@ const KIND = "identifier"; /** * Defines an identifier node * @constructor Identifier + * @memberOf module:php-parser * @extends {Node} * @property {string} name */ diff --git a/src/ast/if.js b/src/ast/if.js index 780e41554..7d8e63bbb 100644 --- a/src/ast/if.js +++ b/src/ast/if.js @@ -11,6 +11,7 @@ const KIND = "if"; /** * Defines a if statement * @constructor If + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} test * @property {Block} body diff --git a/src/ast/include.js b/src/ast/include.js index 10e19ee00..9ce45831c 100644 --- a/src/ast/include.js +++ b/src/ast/include.js @@ -11,6 +11,7 @@ const KIND = "include"; /** * Defines system include call * @constructor Include + * @memberOf module:php-parser * @extends {Expression} * @property {Node} target * @property {boolean} once diff --git a/src/ast/inline.js b/src/ast/inline.js index 2aa857520..ce829ecb0 100644 --- a/src/ast/inline.js +++ b/src/ast/inline.js @@ -11,6 +11,7 @@ const KIND = "inline"; /** * Defines inline html output (treated as echo output) * @constructor Inline + * @memberOf module:php-parser * @extends {Literal} */ module.exports = Literal.extends(KIND, function Inline( diff --git a/src/ast/interface.js b/src/ast/interface.js index b86eddea7..78acbfcba 100644 --- a/src/ast/interface.js +++ b/src/ast/interface.js @@ -11,6 +11,7 @@ const KIND = "interface"; /** * An interface definition * @constructor Interface + * @memberOf module:php-parser * @extends {Declaration} * @property {Identifier[]} extends * @property {Declaration[]} body diff --git a/src/ast/isset.js b/src/ast/isset.js index d66d62d13..460c79db1 100644 --- a/src/ast/isset.js +++ b/src/ast/isset.js @@ -11,6 +11,7 @@ const KIND = "isset"; /** * Defines an isset call * @constructor Isset + * @memberOf module:php-parser * @extends {Expression} */ module.exports = Expression.extends(KIND, function Isset( diff --git a/src/ast/label.js b/src/ast/label.js index 6acc239c0..c1e630413 100644 --- a/src/ast/label.js +++ b/src/ast/label.js @@ -11,6 +11,7 @@ const KIND = "label"; /** * A label statement (referenced by goto) * @constructor Label + * @memberOf module:php-parser * @extends {Statement} * @property {String} name */ diff --git a/src/ast/list.js b/src/ast/list.js index 20d93fb1f..77fe5a9a5 100644 --- a/src/ast/list.js +++ b/src/ast/list.js @@ -11,6 +11,7 @@ const KIND = "list"; /** * Defines list assignment * @constructor List + * @memberOf module:php-parser * @extends {Expression} * @property {boolean} shortForm */ diff --git a/src/ast/literal.js b/src/ast/literal.js index 67156f546..5ccfe09a0 100644 --- a/src/ast/literal.js +++ b/src/ast/literal.js @@ -11,6 +11,7 @@ const KIND = "literal"; /** * Defines an array structure * @constructor Literal + * @memberOf module:php-parser * @extends {Expression} * @property {string} raw * @property {Node|string|number|boolean|null} value diff --git a/src/ast/location.js b/src/ast/location.js index c557c3ad5..65a975e89 100644 --- a/src/ast/location.js +++ b/src/ast/location.js @@ -8,6 +8,7 @@ /** * Defines the location of the node (with it's source contents as string) * @constructor Location + * @memberOf module:php-parser * @property {string|null} source * @property {Position} start * @property {Position} end diff --git a/src/ast/lookup.js b/src/ast/lookup.js index d91e036b6..94e2d1d10 100644 --- a/src/ast/lookup.js +++ b/src/ast/lookup.js @@ -11,6 +11,7 @@ const KIND = "lookup"; /** * Lookup on an offset in the specified object * @constructor Lookup + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} what * @property {Expression} offset diff --git a/src/ast/magic.js b/src/ast/magic.js index 74259e942..871378893 100644 --- a/src/ast/magic.js +++ b/src/ast/magic.js @@ -11,6 +11,7 @@ const KIND = "magic"; /** * Defines magic constant * @constructor Magic + * @memberOf module:php-parser * @extends {Literal} */ module.exports = Literal.extends(KIND, function Magic( diff --git a/src/ast/method.js b/src/ast/method.js index a8a5567c8..727db188a 100644 --- a/src/ast/method.js +++ b/src/ast/method.js @@ -11,6 +11,7 @@ const KIND = "method"; /** * Defines a class/interface/trait method * @constructor Method + * @memberOf module:php-parser * @extends {Function} * @property {boolean} isAbstract * @property {boolean} isFinal diff --git a/src/ast/name.js b/src/ast/name.js index 874b4dd87..171d15588 100644 --- a/src/ast/name.js +++ b/src/ast/name.js @@ -11,6 +11,7 @@ const KIND = "name"; /** * Defines a class reference node * @constructor Name + * @memberOf module:php-parser * @extends {Reference} * @property {string} name * @property {string} resolution @@ -37,11 +38,13 @@ const Name = Reference.extends(KIND, function Name( /** * This is an identifier without a namespace separator, such as Foo * @constant {String} UNQUALIFIED_NAME + * @memberOf Name */ Name.UNQUALIFIED_NAME = "uqn"; /** * This is an identifier with a namespace separator, such as Foo\Bar * @constant {String} QUALIFIED_NAME + * @memberOf Name */ Name.QUALIFIED_NAME = "qn"; /** @@ -49,11 +52,13 @@ Name.QUALIFIED_NAME = "qn"; * a namespace separator, such as \Foo\Bar. The namespace \Foo is also * a fully qualified name. * @constant {String} FULL_QUALIFIED_NAME + * @memberOf Name */ Name.FULL_QUALIFIED_NAME = "fqn"; /** * This is an identifier starting with namespace, such as namespace\Foo\Bar. * @constant {String} RELATIVE_NAME + * @memberOf Name */ Name.RELATIVE_NAME = "rn"; diff --git a/src/ast/namespace.js b/src/ast/namespace.js index 46a5d3269..e3b18a261 100644 --- a/src/ast/namespace.js +++ b/src/ast/namespace.js @@ -11,6 +11,7 @@ const KIND = "namespace"; /** * The main program node * @constructor Namespace + * @memberOf module:php-parser * @extends {Block} * @property {string} name * @property {boolean} withBrackets diff --git a/src/ast/new.js b/src/ast/new.js index 67cb43983..49b21830e 100644 --- a/src/ast/new.js +++ b/src/ast/new.js @@ -11,6 +11,7 @@ const KIND = "new"; /** * Creates a new instance of the specified class * @constructor New + * @memberOf module:php-parser * @extends {Expression} * @property {Identifier|Variable|Class} what * @property {Variable[]} arguments diff --git a/src/ast/node.js b/src/ast/node.js index 41a975e19..7c92287ea 100644 --- a/src/ast/node.js +++ b/src/ast/node.js @@ -8,6 +8,7 @@ /** * A generic AST node * @constructor Node + * @memberOf module:php-parser * @property {Location|null} loc * @property {CommentBlock[]|Comment[]|null} leadingComments * @property {CommentBlock[]|Comment[]|null} trailingComments diff --git a/src/ast/noop.js b/src/ast/noop.js index 0b512b936..f9d9e1ef0 100644 --- a/src/ast/noop.js +++ b/src/ast/noop.js @@ -12,6 +12,7 @@ const KIND = "noop"; * Ignore this node, it implies a no operation block, for example : * [$foo, $bar, /* here a noop node * /] * @constructor Noop + * @memberOf module:php-parser * @extends {Node} */ module.exports = Node.extends(KIND, function Noop(docs, location) { diff --git a/src/ast/nowdoc.js b/src/ast/nowdoc.js index a90da1b49..2477903c6 100644 --- a/src/ast/nowdoc.js +++ b/src/ast/nowdoc.js @@ -11,6 +11,7 @@ const KIND = "nowdoc"; /** * Defines a nowdoc string * @constructor NowDoc + * @memberOf module:php-parser * @extends {Literal} * @property {string} label * @property {string} raw diff --git a/src/ast/nullkeyword.js b/src/ast/nullkeyword.js index 5640a179d..cc6bf5f4d 100644 --- a/src/ast/nullkeyword.js +++ b/src/ast/nullkeyword.js @@ -11,6 +11,7 @@ const KIND = "nullkeyword"; /** * Represents the null keyword * @constructor NullKeyword + * @memberOf module:php-parser * @extends {Node} */ module.exports = Node.extends(KIND, function NullKeyword(raw, docs, location) { diff --git a/src/ast/number.js b/src/ast/number.js index ffd18211d..ae2accf28 100644 --- a/src/ast/number.js +++ b/src/ast/number.js @@ -11,6 +11,7 @@ const KIND = "number"; /** * Defines a numeric value * @constructor Number + * @memberOf module:php-parser * @extends {Literal} */ module.exports = Literal.extends(KIND, function Number( diff --git a/src/ast/offsetlookup.js b/src/ast/offsetlookup.js index 6a842e066..009a51408 100644 --- a/src/ast/offsetlookup.js +++ b/src/ast/offsetlookup.js @@ -11,6 +11,7 @@ const KIND = "offsetlookup"; /** * Lookup on an offset in an array * @constructor OffsetLookup + * @memberOf module:php-parser * @extends {Lookup} */ module.exports = Lookup.extends(KIND, function OffsetLookup( diff --git a/src/ast/operation.js b/src/ast/operation.js index 852fbd848..6e3eda9ea 100644 --- a/src/ast/operation.js +++ b/src/ast/operation.js @@ -11,6 +11,7 @@ const KIND = "operation"; /** * Defines binary operations * @constructor Operation + * @memberOf module:php-parser * @extends {Expression} */ module.exports = Expr.extends(KIND, function Operation(kind, docs, location) { diff --git a/src/ast/parameter.js b/src/ast/parameter.js index 6a1fe9270..d017962c9 100644 --- a/src/ast/parameter.js +++ b/src/ast/parameter.js @@ -11,6 +11,7 @@ const KIND = "parameter"; /** * Defines a function parameter * @constructor Parameter + * @memberOf module:php-parser * @extends {Declaration} * @property {Identifier|null} type * @property {Node|null} value diff --git a/src/ast/parentreference.js b/src/ast/parentreference.js index 357b4fefa..dc64fff13 100644 --- a/src/ast/parentreference.js +++ b/src/ast/parentreference.js @@ -11,6 +11,7 @@ const KIND = "parentreference"; /** * Defines a class reference node * @constructor ParentReference + * @memberOf module:php-parser * @extends {Reference} */ const ParentReference = Reference.extends(KIND, function ParentReference( diff --git a/src/ast/position.js b/src/ast/position.js index 4e5c43ea0..6e4f123c4 100644 --- a/src/ast/position.js +++ b/src/ast/position.js @@ -8,6 +8,7 @@ /** * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): * @constructor Position + * @memberOf module:php-parser * @property {number} line * @property {number} column * @property {number} offset diff --git a/src/ast/post.js b/src/ast/post.js index 70aa72f3b..20d60418c 100644 --- a/src/ast/post.js +++ b/src/ast/post.js @@ -11,6 +11,7 @@ const KIND = "post"; /** * Defines a post operation `$i++` or `$i--` * @constructor Post + * @memberOf module:php-parser * @extends {Operation} * @property {String} type * @property {Variable} what diff --git a/src/ast/pre.js b/src/ast/pre.js index 7ef0efe5f..90e3fd396 100644 --- a/src/ast/pre.js +++ b/src/ast/pre.js @@ -11,6 +11,7 @@ const KIND = "pre"; /** * Defines a pre operation `++$i` or `--$i` * @constructor Pre + * @memberOf module:php-parser * @extends {Operation} * @property {String} type * @property {Variable} what diff --git a/src/ast/print.js b/src/ast/print.js index e329cefee..d5fd0854b 100644 --- a/src/ast/print.js +++ b/src/ast/print.js @@ -11,6 +11,7 @@ const KIND = "print"; /** * Outputs * @constructor Print + * @memberOf module:php-parser * @extends {Expression} */ module.exports = Expression.extends(KIND, function Print( diff --git a/src/ast/program.js b/src/ast/program.js index 7e9bc6c79..d9e801fc4 100644 --- a/src/ast/program.js +++ b/src/ast/program.js @@ -11,6 +11,7 @@ const KIND = "program"; /** * The main program node * @constructor Program + * @memberOf module:php-parser * @extends {Block} * @property {Error[]} errors * @property {Comment[]|null} comments diff --git a/src/ast/property.js b/src/ast/property.js index 073ff4369..bd2a20bca 100644 --- a/src/ast/property.js +++ b/src/ast/property.js @@ -11,6 +11,7 @@ const KIND = "property"; /** * Defines a class property * @constructor Property + * @memberOf module:php-parser * @extends {Statement} * @property {string} name * @property {Node|null} value diff --git a/src/ast/propertylookup.js b/src/ast/propertylookup.js index 21a966dec..991a2989f 100644 --- a/src/ast/propertylookup.js +++ b/src/ast/propertylookup.js @@ -11,6 +11,7 @@ const KIND = "propertylookup"; /** * Lookup to an object property * @constructor PropertyLookup + * @memberOf module:php-parser * @extends {Lookup} */ module.exports = Lookup.extends(KIND, function PropertyLookup( diff --git a/src/ast/propertystatement.js b/src/ast/propertystatement.js index 9b6dbe5a5..5e681d43e 100644 --- a/src/ast/propertystatement.js +++ b/src/ast/propertystatement.js @@ -16,6 +16,7 @@ const IS_PRIVATE = "private"; /** * Declares a properties into the current scope * @constructor PropertyStatement + * @memberOf module:php-parser * @extends {Statement} * @property {Property[]} properties */ diff --git a/src/ast/reference.js b/src/ast/reference.js index bd86b3837..e000937ad 100644 --- a/src/ast/reference.js +++ b/src/ast/reference.js @@ -11,6 +11,7 @@ const KIND = "reference"; /** * Defines a reference node * @constructor Reference + * @memberOf module:php-parser * @extends {Node} */ const Reference = Node.extends(KIND, function Reference(kind, docs, location) { diff --git a/src/ast/retif.js b/src/ast/retif.js index bc874d30d..976238cc2 100644 --- a/src/ast/retif.js +++ b/src/ast/retif.js @@ -11,6 +11,7 @@ const KIND = "retif"; /** * Defines a short if statement that returns a value * @constructor RetIf + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} test * @property {Expression} trueExpr diff --git a/src/ast/return.js b/src/ast/return.js index b320eaf49..c7c049baa 100644 --- a/src/ast/return.js +++ b/src/ast/return.js @@ -11,6 +11,7 @@ const KIND = "return"; /** * A continue statement * @constructor Return + * @memberOf module:php-parser * @extends {Statement} * @property {Expression|null} expr */ diff --git a/src/ast/selfreference.js b/src/ast/selfreference.js index 51ff0a3ea..10333a0ac 100644 --- a/src/ast/selfreference.js +++ b/src/ast/selfreference.js @@ -11,6 +11,7 @@ const KIND = "selfreference"; /** * Defines a class reference node * @constructor SelfReference + * @memberOf module:php-parser * @extends {Reference} */ const SelfReference = Reference.extends(KIND, function SelfReference( diff --git a/src/ast/silent.js b/src/ast/silent.js index 91b15ef7e..534d90dd7 100644 --- a/src/ast/silent.js +++ b/src/ast/silent.js @@ -11,6 +11,7 @@ const KIND = "silent"; /** * Avoids to show/log warnings & notices from the inner expression * @constructor Silent + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} expr */ diff --git a/src/ast/statement.js b/src/ast/statement.js index c4c578fc7..6f25bd8ee 100644 --- a/src/ast/statement.js +++ b/src/ast/statement.js @@ -11,6 +11,7 @@ const KIND = "statement"; /** * Any statement. * @constructor Statement + * @memberOf module:php-parser * @extends {Node} */ module.exports = Node.extends(KIND, function Statement(kind, docs, location) { diff --git a/src/ast/static.js b/src/ast/static.js index 7c456b2a0..1e8a00613 100644 --- a/src/ast/static.js +++ b/src/ast/static.js @@ -11,6 +11,7 @@ const KIND = "static"; /** * Declares a static variable into the current scope * @constructor Static + * @memberOf module:php-parser * @extends {Statement} * @property {StaticVariable[]} variables */ diff --git a/src/ast/staticlookup.js b/src/ast/staticlookup.js index cf9407d9f..42a33aa48 100644 --- a/src/ast/staticlookup.js +++ b/src/ast/staticlookup.js @@ -11,6 +11,7 @@ const KIND = "staticlookup"; /** * Lookup to a static property * @constructor StaticLookup + * @memberOf module:php-parser * @extends {Lookup} */ module.exports = Lookup.extends(KIND, function StaticLookup( diff --git a/src/ast/staticreference.js b/src/ast/staticreference.js index 310c4d838..aa89752c1 100644 --- a/src/ast/staticreference.js +++ b/src/ast/staticreference.js @@ -11,6 +11,7 @@ const KIND = "staticreference"; /** * Defines a class reference node * @constructor StaticReference + * @memberOf module:php-parser * @extends {Reference} */ const StaticReference = Reference.extends(KIND, function StaticReference( diff --git a/src/ast/staticvariable.js b/src/ast/staticvariable.js index 6fc4b9ace..dba8e9104 100644 --- a/src/ast/staticvariable.js +++ b/src/ast/staticvariable.js @@ -11,6 +11,7 @@ const KIND = "staticvariable"; /** * Defines a constant * @constructor StaticVariable + * @memberOf module:php-parser * @extends {Node} * @property {Variable} variable * @property {Node|string|number|boolean|null} defaultValue diff --git a/src/ast/string.js b/src/ast/string.js index e20efa3b8..65e26370b 100644 --- a/src/ast/string.js +++ b/src/ast/string.js @@ -11,6 +11,7 @@ const KIND = "string"; /** * Defines a string (simple or double quoted) - chars are already escaped * @constructor String + * @memberOf module:php-parser * @extends {Literal} * @property {boolean} unicode * @property {boolean} isDoubleQuote diff --git a/src/ast/switch.js b/src/ast/switch.js index dc39a5376..ded53ef01 100644 --- a/src/ast/switch.js +++ b/src/ast/switch.js @@ -11,6 +11,7 @@ const KIND = "switch"; /** * Defines a switch statement * @constructor Switch + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} test * @property {Block} body diff --git a/src/ast/throw.js b/src/ast/throw.js index cb617df0d..36c51f88e 100644 --- a/src/ast/throw.js +++ b/src/ast/throw.js @@ -11,6 +11,7 @@ const KIND = "throw"; /** * Defines a throw statement * @constructor Throw + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} what */ diff --git a/src/ast/trait.js b/src/ast/trait.js index 3d0763b52..ecd5030e5 100644 --- a/src/ast/trait.js +++ b/src/ast/trait.js @@ -11,6 +11,7 @@ const KIND = "trait"; /** * A trait definition * @constructor Trait + * @memberOf module:php-parser * @extends {Declaration} * @property {Declaration[]} body */ diff --git a/src/ast/traitalias.js b/src/ast/traitalias.js index 32214375e..a342ce06e 100644 --- a/src/ast/traitalias.js +++ b/src/ast/traitalias.js @@ -16,6 +16,7 @@ const IS_PRIVATE = "private"; /** * Defines a trait alias * @constructor TraitAlias + * @memberOf module:php-parser * @extends {Node} * @property {Identifier|null} trait * @property {Identifier} method diff --git a/src/ast/traitprecedence.js b/src/ast/traitprecedence.js index 945511a31..67ed3eebf 100644 --- a/src/ast/traitprecedence.js +++ b/src/ast/traitprecedence.js @@ -11,6 +11,7 @@ const KIND = "traitprecedence"; /** * Defines a trait alias * @constructor TraitPrecedence + * @memberOf module:php-parser * @extends {Node} * @property {Identifier|null} trait * @property {Identifier} method diff --git a/src/ast/traituse.js b/src/ast/traituse.js index 14fe02978..bf754740f 100644 --- a/src/ast/traituse.js +++ b/src/ast/traituse.js @@ -11,6 +11,7 @@ const KIND = "traituse"; /** * Defines a trait usage * @constructor TraitUse + * @memberOf module:php-parser * @extends {Node} * @property {Identifier[]} traits * @property {Node[]|null} adaptations diff --git a/src/ast/try.js b/src/ast/try.js index c3de1f311..c6f23be77 100644 --- a/src/ast/try.js +++ b/src/ast/try.js @@ -11,6 +11,7 @@ const KIND = "try"; /** * Defines a try statement * @constructor Try + * @memberOf module:php-parser * @extends {Statement} * @property {Block} body * @property {Catch[]} catches diff --git a/src/ast/typereference.js b/src/ast/typereference.js index 350c5c5a5..9066b3251 100644 --- a/src/ast/typereference.js +++ b/src/ast/typereference.js @@ -11,6 +11,7 @@ const KIND = "typereference"; /** * Defines a class reference node * @constructor TypeReference + * @memberOf module:php-parser * @extends {Reference} * @property {string} name */ diff --git a/src/ast/unary.js b/src/ast/unary.js index 8c2ce1763..eb55ccb9a 100644 --- a/src/ast/unary.js +++ b/src/ast/unary.js @@ -11,6 +11,7 @@ const KIND = "unary"; /** * Unary operations * @constructor Unary + * @memberOf module:php-parser * @extends {Operation} * @property {string} type * @property {Expression} what diff --git a/src/ast/unset.js b/src/ast/unset.js index db5b84840..115ce3b4f 100644 --- a/src/ast/unset.js +++ b/src/ast/unset.js @@ -11,6 +11,7 @@ const KIND = "unset"; /** * Deletes references to a list of variables * @constructor Unset + * @memberOf module:php-parser * @extends {Statement} */ module.exports = Statement.extends(KIND, function Unset( diff --git a/src/ast/usegroup.js b/src/ast/usegroup.js index eeceb6ccc..4d6d44182 100644 --- a/src/ast/usegroup.js +++ b/src/ast/usegroup.js @@ -11,6 +11,7 @@ const KIND = "usegroup"; /** * Defines a use statement (with a list of use items) * @constructor UseGroup + * @memberOf module:php-parser * @extends {Statement} * @property {string|null} name * @property {string|null} type - Possible value : function, const diff --git a/src/ast/useitem.js b/src/ast/useitem.js index adaa039d6..32bdfefa7 100644 --- a/src/ast/useitem.js +++ b/src/ast/useitem.js @@ -11,6 +11,7 @@ const KIND = "useitem"; /** * Defines a use statement (from namespace) * @constructor UseItem + * @memberOf module:php-parser * @extends {Statement} * @property {string} name * @property {string|null} type - Possible value : function, const @@ -33,12 +34,14 @@ const UseItem = Statement.extends(KIND, function UseItem( /** * Importing a constant - * @constant {String} TYPE_CONST + * @constant {string} TYPE_CONST + * @memberOf UseItem */ UseItem.TYPE_CONST = "const"; /** * Importing a function - * @constant {String} TYPE_FUNC + * @constant {string} TYPE_FUNC + * @memberOf UseItem */ UseItem.TYPE_FUNCTION = "function"; diff --git a/src/ast/variable.js b/src/ast/variable.js index 5b40eeea1..6edc6bb31 100644 --- a/src/ast/variable.js +++ b/src/ast/variable.js @@ -12,6 +12,7 @@ const KIND = "variable"; * Any expression node. Since the left-hand side of an assignment may * be any expression in general, an expression can also be a pattern. * @constructor Variable + * @memberOf module:php-parser * @extends {Expression} * @example * // PHP code : diff --git a/src/ast/variadic.js b/src/ast/variadic.js index 369ecbc26..b591e4691 100644 --- a/src/ast/variadic.js +++ b/src/ast/variadic.js @@ -11,6 +11,7 @@ const KIND = "variadic"; /** * Introduce a list of items into the arguments of the call * @constructor variadic + * @memberOf module:php-parser * @extends {Expression} * @property {Array|Expression} what * @see https://wiki.php.net/rfc/argument_unpacking diff --git a/src/ast/while.js b/src/ast/while.js index 5887ec950..0a77c4f86 100644 --- a/src/ast/while.js +++ b/src/ast/while.js @@ -11,6 +11,7 @@ const KIND = "while"; /** * Defines a while statement * @constructor While + * @memberOf module:php-parser * @extends {Statement} * @property {Expression} test * @property {Statement} body diff --git a/src/ast/yield.js b/src/ast/yield.js index 5ea28034e..8f76777eb 100644 --- a/src/ast/yield.js +++ b/src/ast/yield.js @@ -11,6 +11,7 @@ const KIND = "yield"; /** * Defines a yield generator statement * @constructor Yield + * @memberOf module:php-parser * @extends {Expression} * @property {Expression|null} value * @property {Expression|null} key diff --git a/src/ast/yieldfrom.js b/src/ast/yieldfrom.js index b7dd53cc0..6f0d32914 100644 --- a/src/ast/yieldfrom.js +++ b/src/ast/yieldfrom.js @@ -11,6 +11,7 @@ const KIND = "yieldfrom"; /** * Defines a yield from generator statement * @constructor YieldFrom + * @memberOf module:php-parser * @extends {Expression} * @property {Expression} value * @see http://php.net/manual/en/language.generators.syntax.php diff --git a/src/index.js b/src/index.js index 40ac0f033..6848c92b3 100644 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,7 @@ function combine(src, to) { * Initialise a new parser instance with the specified options * * @class + * @memberOf module:php-parser * @tutorial Engine * @example * var parser = require('php-parser'); @@ -106,6 +107,7 @@ const Engine = function (options) { /** * Check if the inpyt is a buffer or a string + * @private * @param {Buffer|String} buffer Input value that can be either a buffer or a string * @return {String} Returns the string from input */ @@ -220,6 +222,8 @@ Engine.prototype.tokenGetAll = function (buffer) { return result; }; +/** @module php-parser */ + // exports the function module.exports = Engine; diff --git a/src/lexer.js b/src/lexer.js index abe592dd8..90b3951d6 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -10,6 +10,7 @@ * parser to build the AST from its grammar. * * @constructor Lexer + * @memberOf module:php-parser * @property {number} EOF * @property {boolean} all_tokens defines if all tokens must be retrieved (used by token_get_all only) * @property {boolean} comment_tokens extracts comments tokens diff --git a/src/parser.js b/src/parser.js index 2a47607a3..4f8b532a2 100644 --- a/src/parser.js +++ b/src/parser.js @@ -16,6 +16,7 @@ function isNumber(n) { * The PHP Parser class that build the AST tree from the lexer * * @constructor Parser + * @memberOf module:php-parser * @tutorial Parser * @property {Lexer} lexer - current lexer instance * @property {AST} ast - the AST factory instance @@ -470,7 +471,10 @@ Parser.prototype.expectEndOfStatement = function (node) { return true; }; -/** outputs some debug information on current token **/ +/** + * outputs some debug information on current token + * @private + */ const ignoreStack = ["parser.next", "parser.node", "parser.showlog"]; Parser.prototype.showlog = function () { const stack = new Error().stack.split("\n"); diff --git a/src/parser/scalar.js b/src/parser/scalar.js index 88ceace12..df1ed6153 100644 --- a/src/parser/scalar.js +++ b/src/parser/scalar.js @@ -104,6 +104,7 @@ module.exports = { let leadingWhitespaceCharCount = 0; /** * @var inCoutingState {boolean} reset to true after a new line + * @private */ let inCoutingState = true; const chToCheck = indentation_uses_spaces ? " " : "\t"; diff --git a/types.d.ts b/types.d.ts index 4381b3399..2a410a153 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,1342 +1,960 @@ -/** - * Defines an array structure - * @example - * // PHP code : - * [1, 'foo' => 'bar', 3] - * - * // AST structure : - * { - * "kind": "array", - * "shortForm": true - * "items": [ - * {"kind": "number", "value": "1"}, - * { - * "kind": "entry", - * "key": {"kind": "string", "value": "foo", "isDoubleQuote": false}, - * "value": {"kind": "string", "value": "bar", "isDoubleQuote": false} - * }, - * {"kind": "number", "value": "3"} - * ] - * } - * @property items - List of array items - * @property shortForm - Indicate if the short array syntax is used, ex `[]` instead `array()` - */ -declare class Array extends Expression { - /** - * List of array items - */ - items: Entry | Expression | Variable; - /** - * Indicate if the short array syntax is used, ex `[]` instead `array()` - */ - shortForm: boolean; -} - -/** - * Defines an arrow function (it's like a closure) - */ -declare class ArrowFunc extends Expression { - arguments: Parameter[]; - type: Identifier; - body: Expression; - byref: boolean; - nullable: boolean; - isStatic: boolean; -} - -/** - * Assigns a value to the specified target - */ -declare class Assign extends Expression { - left: Expression; - right: Expression; - operator: string; -} - -/** - * Assigns a value to the specified target - */ -declare class AssignRef extends Expression { - left: Expression; - right: Expression; - operator: string; -} - -/** - * Binary operations - */ -declare class Bin extends Operation { - type: string; - left: Expression; - right: Expression; -} - -/** - * A block statement, i.e., a sequence of statements surrounded by braces. - */ -declare class Block extends Statement { - children: Node[]; -} - -/** - * Defines a boolean value (true/false) - */ -declare class Boolean extends Literal { -} - -/** - * A break statement - */ -declare class Break extends Statement { - level: number | null; -} - -/** - * Passing by Reference - so the function can modify the variable - */ -declare class ByRef extends Expression { - what: ExpressionStatement; -} - -/** - * Executes a call statement - */ -declare class Call extends Expression { - what: Identifier | Variable; - arguments: Variable[]; -} - -/** - * A switch case statement - * @property test - if null, means that the default case - */ -declare class Case extends Statement { - /** - * if null, means that the default case - */ - test: Expression | null; - body: Block | null; -} - -/** - * Binary operations - */ -declare class Cast extends Operation { - type: string; - raw: string; - expr: Expression; -} - -/** - * Defines a catch statement - */ -declare class Catch extends Statement { - what: Identifier[]; - variable: Variable; - body: Statement; -} - -/** - * A class definition - */ -declare class Class extends Declaration { - extends: Identifier | null; - implements: Identifier[]; - body: Declaration[]; - isAnonymous: boolean; - isAbstract: boolean; - isFinal: boolean; -} - -/** - * Defines a class/interface/trait constant - */ -declare class ClassConstant extends ConstantStatement { +declare module "php-parser" { /** - * Generic flags parser + * Defines an array structure + * @example + * // PHP code : + * [1, 'foo' => 'bar', 3] + * + * // AST structure : + * { + * "kind": "array", + * "shortForm": true + * "items": [ + * {"kind": "number", "value": "1"}, + * { + * "kind": "entry", + * "key": {"kind": "string", "value": "foo", "isDoubleQuote": false}, + * "value": {"kind": "string", "value": "bar", "isDoubleQuote": false} + * }, + * {"kind": "number", "value": "3"} + * ] + * } + * @property items - List of array items + * @property shortForm - Indicate if the short array syntax is used, ex `[]` instead `array()` + */ + class Array extends Expression { + /** + * List of array items + */ + items: Entry | Expression | Variable; + /** + * Indicate if the short array syntax is used, ex `[]` instead `array()` + */ + shortForm: boolean; + } + /** + * Defines an arrow function (it's like a closure) + */ + class ArrowFunc extends Expression { + arguments: Parameter[]; + type: Identifier; + body: Expression; + byref: boolean; + nullable: boolean; + isStatic: boolean; + } + /** + * Assigns a value to the specified target + */ + class Assign extends Expression { + left: Expression; + right: Expression; + operator: string; + } + /** + * Assigns a value to the specified target + */ + class AssignRef extends Expression { + left: Expression; + right: Expression; + operator: string; + } + /** + * Binary operations + */ + class Bin extends Operation { + type: string; + left: Expression; + right: Expression; + } + /** + * A block statement, i.e., a sequence of statements surrounded by braces. + */ + class Block extends Statement { + children: Node[]; + } + /** + * Defines a boolean value (true/false) + */ + class Boolean extends Literal { + } + /** + * A break statement + */ + class Break extends Statement { + level: number | null; + } + /** + * Passing by Reference - so the function can modify the variable + */ + class ByRef extends Expression { + what: ExpressionStatement; + } + /** + * Executes a call statement + */ + class Call extends Expression { + what: Identifier | Variable; + arguments: Variable[]; + } + /** + * A switch case statement + * @property test - if null, means that the default case + */ + class Case extends Statement { + /** + * if null, means that the default case + */ + test: Expression | null; + body: Block | null; + } + /** + * Binary operations */ - parseFlags(flags: (number | null)[]): void; - visibility: string; -} - -/** - * Defines a clone call - */ -declare class Clone extends Expression { - what: Expression; -} - -/** - * Defines a closure - */ -declare class Closure extends Expression { - arguments: Parameter[]; - uses: Variable[]; - type: Identifier; - byref: boolean; - nullable: boolean; - body: Block | null; - isStatic: boolean; -} - -/** - * Abstract documentation node (ComentLine or CommentBlock) - */ -declare class Comment extends Node { - value: string; -} - -/** - * A comment block (multiline) - */ -declare class CommentBlock extends Comment { -} - -/** - * A single line comment - */ -declare class CommentLine extends Comment { -} - -/** - * Defines a constant - */ -declare class Constant extends Node { - name: string; - value: Node | string | number | boolean | null; -} - -/** - * Declares a constants into the current scope - */ -declare class ConstantStatement extends Statement { - constants: Constant[]; -} - -/** - * A continue statement - */ -declare class Continue extends Statement { - level: number | null; -} - -/** - * A declaration statement (function, class, interface...) - */ -declare class Declaration extends Statement { + class Cast extends Operation { + type: string; + raw: string; + expr: Expression; + } /** - * Generic flags parser + * Defines a catch statement */ - parseFlags(flags: (number | null)[]): void; - name: Identifier | string; -} - -/** - * The declare construct is used to set execution directives for a block of code - */ -declare class Declare extends Block { - directives: any[][]; - mode: string; -} - -/** - * The node is declared as a short tag syntax : - * ```php - * bar_$baz; - * ``` - */ -declare const TYPE_OFFSET: string; - -/** - * Part of `Encapsed` node - */ -declare class EncapsedPart extends Expression { - expression: Expression; - syntax: string; - curly: boolean; -} - -/** - * An array entry - see [Array](#array) - * @property key - The entry key/offset - * @property value - The entry value - * @property byRef - By reference - * @property unpack - Argument unpacking - */ -declare class Entry extends Expression { - /** - * The entry key/offset - */ - key: Node | null; - /** - * The entry value - */ - value: Node; - /** - * By reference - */ - byRef: boolean; - /** - * Argument unpacking - */ - unpack: boolean; -} - -/** - * Defines an error node (used only on silentMode) - */ -declare class Error extends Node { - message: string; - line: number; - token: number | string; - expected: string | any[]; -} - -/** - * Defines an eval statement - */ -declare class Eval extends Expression { - source: Node; -} - -/** - * Defines an exit / die call - */ -declare class Exit extends Expression { - expression: Node | null; - useDie: boolean; -} - -/** - * Any expression node. Since the left-hand side of an assignment may - * be any expression in general, an expression can also be a pattern. - */ -declare class Expression extends Node { -} - -/** - * Defines an expression based statement - */ -declare class ExpressionStatement extends Statement { - expression: Expression; -} - -/** - * Defines a for iterator - */ -declare class For extends Statement { - init: Expression[]; - test: Expression[]; - increment: Expression[]; - body: Statement; - shortForm: boolean; -} - -/** - * Defines a foreach iterator - */ -declare class Foreach extends Statement { - source: Expression; - key: Expression | null; - value: Expression; - body: Statement; - shortForm: boolean; -} - -/** - * Defines a classic function - */ -declare class Function extends Declaration { - arguments: Parameter[]; - type: Identifier; - byref: boolean; - nullable: boolean; - body: Block | null; -} - -/** - * Imports a variable from the global scope - */ -declare class Global extends Statement { - items: Variable[]; -} - -/** - * Defines goto statement - */ -declare class Goto extends Statement { - label: string; -} - -/** - * Halts the compiler execution - * @property after - String after the halt statement - */ -declare class Halt extends Statement { - /** - * String after the halt statement - */ - after: string; -} - -/** - * Defines an identifier node - */ -declare class Identifier extends Node { - name: string; -} - -/** - * Defines a if statement - */ -declare class If extends Statement { - test: Expression; - body: Block; - alternate: Block | If | null; - shortForm: boolean; -} - -/** - * Defines system include call - */ -declare class Include extends Expression { - target: Node; - once: boolean; - require: boolean; -} - -/** - * Defines inline html output (treated as echo output) - */ -declare class Inline extends Literal { -} - -/** - * An interface definition - */ -declare class Interface extends Declaration { - extends: Identifier[]; - body: Declaration[]; -} - -/** - * Defines an isset call - */ -declare class Isset extends Expression { -} - -/** - * A label statement (referenced by goto) - */ -declare class Label extends Statement { - name: string; -} - -/** - * Defines list assignment - */ -declare class List extends Expression { - shortForm: boolean; -} - -/** - * Defines an array structure - */ -declare class Literal extends Expression { - raw: string; - value: Node | string | number | boolean | null; -} - -/** - * Defines the location of the node (with it's source contents as string) - */ -declare class Location { - source: string | null; - start: Position; - end: Position; -} - -/** - * Lookup on an offset in the specified object - */ -declare class Lookup extends Expression { - what: Expression; - offset: Expression; -} - -/** - * Defines magic constant - */ -declare class Magic extends Literal { -} - -/** - * Defines a class/interface/trait method - */ -declare class Method extends Function { - isAbstract: boolean; - isFinal: boolean; - isStatic: boolean; - visibility: string; -} - -/** - * Defines a class reference node - */ -declare class Name extends Reference { - name: string; - resolution: string; -} - -/** - * This is an identifier without a namespace separator, such as Foo - */ -declare const UNQUALIFIED_NAME: string; - -/** - * This is an identifier with a namespace separator, such as Foo\Bar - */ -declare const QUALIFIED_NAME: string; - -/** - * This is an identifier with a namespace separator that begins with - * a namespace separator, such as \Foo\Bar. The namespace \Foo is also - * a fully qualified name. - */ -declare const FULL_QUALIFIED_NAME: string; - -/** - * This is an identifier starting with namespace, such as namespace\Foo\Bar. - */ -declare const RELATIVE_NAME: string; - -/** - * The main program node - */ -declare class Namespace extends Block { - name: string; - withBrackets: boolean; -} - -/** - * Creates a new instance of the specified class - */ -declare class New extends Expression { - what: Identifier | Variable | Class; - arguments: Variable[]; -} - -/** - * A generic AST node - */ -declare class Node { + class Catch extends Statement { + what: Identifier[]; + variable: Variable; + body: Statement; + } /** - * Attach comments to current node + * A class definition */ - setTrailingComments(docs: any): void; + class Class extends Declaration { + extends: Identifier | null; + implements: Identifier[]; + body: Declaration[]; + isAnonymous: boolean; + isAbstract: boolean; + isFinal: boolean; + } /** - * Destroying an unused node + * Defines a class/interface/trait constant */ - destroy(): void; + class ClassConstant extends ConstantStatement { + visibility: string; + } /** - * Includes current token position of the parser + * Defines a clone call */ - includeToken(parser: any): void; + class Clone extends Expression { + what: Expression; + } /** - * Helper for extending the Node class + * Defines a closure */ - static extends(type: string, constructor: (...params: any[]) => any): (...params: any[]) => any; - loc: Location | null; - leadingComments: CommentBlock[] | Comment[] | null; - trailingComments: CommentBlock[] | Comment[] | null; - kind: string; -} - -/** - * Ignore this node, it implies a no operation block, for example : - * [$foo, $bar, /* here a noop node * /] - */ -declare class Noop extends Node { -} - -/** - * Defines a nowdoc string - */ -declare class NowDoc extends Literal { - label: string; - raw: string; -} - -/** - * Represents the null keyword - */ -declare class NullKeyword extends Node { -} - -/** - * Defines a numeric value - */ -declare class Number extends Literal { -} - -/** - * Lookup on an offset in an array - */ -declare class OffsetLookup extends Lookup { -} - -/** - * Defines binary operations - */ -declare class Operation extends Expression { -} - -/** - * Defines a function parameter - */ -declare class Parameter extends Declaration { - type: Identifier | null; - value: Node | null; - byref: boolean; - variadic: boolean; - nullable: boolean; -} - -/** - * Defines a class reference node - */ -declare class ParentReference extends Reference { -} - -/** - * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): - */ -declare class Position { - line: number; - column: number; - offset: number; -} - -/** - * Defines a post operation `$i++` or `$i--` - */ -declare class Post extends Operation { - type: string; - what: Variable; -} - -/** - * Defines a pre operation `++$i` or `--$i` - */ -declare class Pre extends Operation { - type: string; - what: Variable; -} - -/** - * Outputs - */ -declare class Print extends Expression { -} - -/** - * The main program node - */ -declare class Program extends Block { - errors: Error[]; - comments: Comment[] | null; - tokens: String[] | null; -} - -/** - * Defines a class property - */ -declare class Property extends Statement { - name: string; - value: Node | null; - nullable: boolean; - type: Identifier | Identifier[] | null; -} - -/** - * Lookup to an object property - */ -declare class PropertyLookup extends Lookup { -} - -/** - * Declares a properties into the current scope - */ -declare class PropertyStatement extends Statement { + class Closure extends Expression { + arguments: Parameter[]; + uses: Variable[]; + type: Identifier; + byref: boolean; + nullable: boolean; + body: Block | null; + isStatic: boolean; + } /** - * Generic flags parser + * Abstract documentation node (ComentLine or CommentBlock) */ - parseFlags(flags: (number | null)[]): void; - properties: Property[]; -} - -/** - * Defines a reference node - */ -declare class Reference extends Node { -} - -/** - * Defines a short if statement that returns a value - */ -declare class RetIf extends Expression { - test: Expression; - trueExpr: Expression; - falseExpr: Expression; -} - -/** - * A continue statement - */ -declare class Return extends Statement { - expr: Expression | null; -} - -/** - * Defines a class reference node - */ -declare class SelfReference extends Reference { -} - -/** - * Avoids to show/log warnings & notices from the inner expression - */ -declare class Silent extends Expression { - expr: Expression; -} - -/** - * Any statement. - */ -declare class Statement extends Node { -} - -/** - * Declares a static variable into the current scope - */ -declare class Static extends Statement { - variables: StaticVariable[]; -} - -/** - * Lookup to a static property - */ -declare class StaticLookup extends Lookup { -} - -/** - * Defines a class reference node - */ -declare class StaticReference extends Reference { -} - -/** - * Defines a constant - */ -declare class StaticVariable extends Node { - variable: Variable; - defaultValue: Node | string | number | boolean | null; -} - -/** - * Defines a string (simple or double quoted) - chars are already escaped - */ -declare class String extends Literal { - unicode: boolean; - isDoubleQuote: boolean; -} - -/** - * Defines a switch statement - */ -declare class Switch extends Statement { - test: Expression; - body: Block; - shortForm: boolean; -} - -/** - * Defines a throw statement - */ -declare class Throw extends Statement { - what: Expression; -} - -/** - * A trait definition - */ -declare class Trait extends Declaration { - body: Declaration[]; -} - -/** - * Defines a trait alias - */ -declare class TraitAlias extends Node { - trait: Identifier | null; - method: Identifier; - as: Identifier | null; - visibility: string | null; -} - -/** - * Defines a trait alias - */ -declare class TraitPrecedence extends Node { - trait: Identifier | null; - method: Identifier; - instead: Identifier[]; -} - -/** - * Defines a trait usage - */ -declare class TraitUse extends Node { - traits: Identifier[]; - adaptations: Node[] | null; -} - -/** - * Defines a try statement - */ -declare class Try extends Statement { - body: Block; - catches: Catch[]; - allways: Block; -} - -/** - * Defines a class reference node - */ -declare class TypeReference extends Reference { - name: string; -} - -/** - * Unary operations - */ -declare class Unary extends Operation { - type: string; - what: Expression; -} - -/** - * Deletes references to a list of variables - */ -declare class Unset extends Statement { -} - -/** - * Defines a use statement (with a list of use items) - * @property type - Possible value : function, const - */ -declare class UseGroup extends Statement { - name: string | null; - /** - * Possible value : function, const - */ - type: string | null; - item: UseItem[]; -} - -/** - * Defines a use statement (from namespace) - * @property type - Possible value : function, const - */ -declare class UseItem extends Statement { - name: string; - /** - * Possible value : function, const - */ - type: string | null; - alias: Identifier | null; -} - -/** - * Importing a constant - */ -declare const TYPE_CONST: string; - -/** - * Importing a function - */ -declare const TYPE_FUNC: string; - -/** - * Any expression node. Since the left-hand side of an assignment may - * be any expression in general, an expression can also be a pattern. - * @example - * // PHP code : - * $foo - * // AST output - * { - * "kind": "variable", - * "name": "foo", - * "curly": false - * } - * @property name - The variable name (can be a complex expression when the name is resolved dynamically) - * @property curly - Indicate if the name is defined between curlies, ex `${foo}` - */ -declare class Variable extends Expression { - /** - * The variable name (can be a complex expression when the name is resolved dynamically) - */ - name: string | Node; - /** - * Indicate if the name is defined between curlies, ex `${foo}` - */ - curly: boolean; -} - -/** - * Introduce a list of items into the arguments of the call - */ -declare class variadic extends Expression { - what: any[] | Expression; -} - -/** - * Defines a while statement - */ -declare class While extends Statement { - test: Expression; - body: Statement; - shortForm: boolean; -} - -/** - * Defines a yield generator statement - */ -declare class Yield extends Expression { - value: Expression | null; - key: Expression | null; -} - -/** - * Defines a yield from generator statement - */ -declare class YieldFrom extends Expression { - value: Expression; -} - -/** - * The AST builder class - * @property withPositions - Should locate any node (by default false) - * @property withSource - Should extract the node original code (by default false) - */ -declare class AST { + class Comment extends Node { + value: string; + } /** - * Change parent node informations after swapping childs + * A comment block (multiline) */ - swapLocations(): void; + class CommentBlock extends Comment { + } /** - * Includes locations from first & last into the target + * A single line comment */ - resolveLocations(): void; + class CommentLine extends Comment { + } /** - * Check and fix precence, by default using right + * Defines a constant */ - resolvePrecedence(): void; + class Constant extends Node { + name: string; + value: Node | string | number | boolean | null; + } /** - * Prepares an AST node - * @param kind - Defines the node type - * (if null, the kind must be passed at the function call) - * @param parser - The parser instance (use for extracting locations) + * Declares a constants into the current scope */ - prepare(kind: string | null, parser: Parser): (...params: any[]) => any; + class ConstantStatement extends Statement { + constants: Constant[]; + } + /** + * A continue statement + */ + class Continue extends Statement { + level: number | null; + } + /** + * A declaration statement (function, class, interface...) + */ + class Declaration extends Statement { + name: Identifier | string; + } + /** + * The declare construct is used to set execution directives for a block of code + */ + class Declare extends Block { + directives: any[][]; + mode: string; + } /** - * Should locate any node (by default false) - */ - withPositions: boolean; + * Defines a constant + */ + class DeclareDirective extends Node { + name: Identifier; + value: Node | string | number | boolean | null; + } /** - * Should extract the node original code (by default false) - */ - withSource: boolean; -} - -/** - * Initialise a new parser instance with the specified options - * @example - * var parser = require('php-parser'); - * var instance = new parser({ - * parser: { - * extractDoc: true, - * suppressErrors: true, - * version: 704 // or '7.4' - * }, - * ast: { - * withPositions: true - * }, - * lexer: { - * short_tags: true, - * asp_tags: true - * } - * }); - * - * var evalAST = instance.parseEval('some php code'); - * var codeAST = instance.parseCode(' Note that the output tokens are *STRICLY* similar to PHP function `token_get_all` - * @returns - Each item can be a string or an array with following informations [token_name, text, line_number] - */ - tokenGetAll(buffer: string): String[]; - lexer: Lexer; - parser: Parser; - ast: AST; - tokens: any; -} - -/** - * Check if the inpyt is a buffer or a string - * @param buffer - Input value that can be either a buffer or a string - * @returns Returns the string from input - */ -declare function getStringBuffer(buffer: Buffer | string): string; - -/** - * This is the php lexer. It will tokenize the string for helping the - * parser to build the AST from its grammar. - * @property all_tokens - defines if all tokens must be retrieved (used by token_get_all only) - * @property comment_tokens - extracts comments tokens - * @property mode_eval - enables the evald mode (ignore opening tags) - * @property asp_tags - disables by default asp tags mode - * @property short_tags - enables by default short tags mode - * @property keywords - List of php keyword - * @property castKeywords - List of php keywords for type casting - */ -declare class Lexer { + * Defines a do/while statement + */ + class Do extends Statement { + test: Expression; + body: Statement; + } /** - * Initialize the lexer with the specified input + * Defines system based call */ - setInput(): void; + class Echo extends Statement { + shortForm: boolean; + } /** - * consumes and returns one char from the input + * Defines an empty check call */ - input(): void; + class Empty extends Expression { + } /** - * revert eating specified size + * Defines an encapsed string (contains expressions) + * @property type - Defines the type of encapsed string (shell, heredoc, string) + * @property label - The heredoc label, defined only when the type is heredoc */ - unput(): void; + class Encapsed extends Literal { + /** + * Defines the type of encapsed string (shell, heredoc, string) + */ + type: string; + /** + * The heredoc label, defined only when the type is heredoc + */ + label: string | null; + } /** - * check if the text matches + * Part of `Encapsed` node + */ + class EncapsedPart extends Expression { + expression: Expression; + syntax: string; + curly: boolean; + } + /** + * An array entry - see [Array](#array) + * @property key - The entry key/offset + * @property value - The entry value + * @property byRef - By reference + * @property unpack - Argument unpacking */ - tryMatch(text: string): boolean; + class Entry extends Expression { + /** + * The entry key/offset + */ + key: Node | null; + /** + * The entry value + */ + value: Node; + /** + * By reference + */ + byRef: boolean; + /** + * Argument unpacking + */ + unpack: boolean; + } + /** + * Defines an error node (used only on silentMode) + */ + class Error extends Node { + message: string; + line: number; + token: number | string; + expected: string | any[]; + } + /** + * Defines an eval statement + */ + class Eval extends Expression { + source: Node; + } + /** + * Defines an exit / die call + */ + class Exit extends Expression { + expression: Node | null; + useDie: boolean; + } + /** + * Any expression node. Since the left-hand side of an assignment may + * be any expression in general, an expression can also be a pattern. + */ + class Expression extends Node { + } + /** + * Defines an expression based statement + */ + class ExpressionStatement extends Statement { + expression: Expression; + } + /** + * Defines a for iterator + */ + class For extends Statement { + init: Expression[]; + test: Expression[]; + increment: Expression[]; + body: Statement; + shortForm: boolean; + } + /** + * Defines a foreach iterator + */ + class Foreach extends Statement { + source: Expression; + key: Expression | null; + value: Expression; + body: Statement; + shortForm: boolean; + } + /** + * Defines a classic function + */ + class Function extends Declaration { + arguments: Parameter[]; + type: Identifier; + byref: boolean; + nullable: boolean; + body: Block | null; + } /** - * check if the text matches + * Imports a variable from the global scope */ - tryMatchCaseless(text: string): boolean; + class Global extends Statement { + items: Variable[]; + } /** - * look ahead + * Defines goto statement */ - ahead(size: number): string; + class Goto extends Statement { + label: string; + } /** - * consume the specified size + * Halts the compiler execution + * @property after - String after the halt statement */ - consume(size: number): Lexer; + class Halt extends Statement { + /** + * String after the halt statement + */ + after: string; + } /** - * Gets the current state + * Defines an identifier node */ - getState(): void; + class Identifier extends Node { + name: string; + } /** - * Sets the current lexer state + * Defines a if statement */ - setState(): void; + class If extends Statement { + test: Expression; + body: Block; + alternate: Block | If | null; + shortForm: boolean; + } /** - * prepend next token + * Defines system include call */ - appendToken(value: any, ahead: any): Lexer; + class Include extends Expression { + target: Node; + once: boolean; + require: boolean; + } /** - * return next match that has a token + * Defines inline html output (treated as echo output) */ - lex(): number | string; + class Inline extends Literal { + } /** - * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) + * An interface definition */ - begin(condition: any): Lexer; + class Interface extends Declaration { + extends: Identifier[]; + body: Declaration[]; + } /** - * pop the previously active lexer condition state off the condition stack + * Defines an isset call */ - popState(): T | string | any; + class Isset extends Expression { + } /** - * return next match in input + * A label statement (referenced by goto) */ - next(): number | any; - EOF: number; + class Label extends Statement { + name: string; + } /** - * defines if all tokens must be retrieved (used by token_get_all only) - */ - all_tokens: boolean; + * Defines list assignment + */ + class List extends Expression { + shortForm: boolean; + } /** - * extracts comments tokens - */ - comment_tokens: boolean; + * Defines an array structure + */ + class Literal extends Expression { + raw: string; + value: Node | string | number | boolean | null; + } /** - * enables the evald mode (ignore opening tags) - */ - mode_eval: boolean; + * Defines the location of the node (with it's source contents as string) + */ + class Location { + source: string | null; + start: Position; + end: Position; + } /** - * disables by default asp tags mode - */ - asp_tags: boolean; + * Lookup on an offset in the specified object + */ + class Lookup extends Expression { + what: Expression; + offset: Expression; + } /** - * enables by default short tags mode - */ - short_tags: boolean; + * Defines magic constant + */ + class Magic extends Literal { + } /** - * List of php keyword - */ - keywords: any; + * Defines a class/interface/trait method + */ + class Method extends Function { + isAbstract: boolean; + isFinal: boolean; + isStatic: boolean; + visibility: string; + } /** - * List of php keywords for type casting - */ - castKeywords: any; -} - -/** - * reset to true after a new line - */ -declare var inCoutingState: boolean; - -/** - * The PHP Parser class that build the AST tree from the lexer - * @property lexer - current lexer instance - * @property ast - the AST factory instance - * @property token - current token - * @property extractDoc - should extract documentation as AST node - * @property extractTokens - should extract each token - * @property suppressErrors - should ignore parsing errors and continue - * @property debug - should output debug informations - */ -declare class Parser { + * Defines a class reference node + */ + class Name extends Reference { + name: string; + resolution: string; + } /** - * helper : gets a token name + * The main program node */ - getTokenName(): void; + class Namespace extends Block { + name: string; + withBrackets: boolean; + } /** - * main entry point : converts a source code to AST + * Creates a new instance of the specified class */ - parse(): void; + class New extends Expression { + what: Identifier | Variable | Class; + arguments: Variable[]; + } /** - * Raise an error + * A generic AST node */ - raiseError(): void; + class Node { + loc: Location | null; + leadingComments: CommentBlock[] | Comment[] | null; + trailingComments: CommentBlock[] | Comment[] | null; + kind: string; + } /** - * handling errors + * Ignore this node, it implies a no operation block, for example : + * [$foo, $bar, /* here a noop node * /] */ - error(): void; + class Noop extends Node { + } /** - * Creates a new AST node + * Defines a nowdoc string */ - node(): void; + class NowDoc extends Literal { + label: string; + raw: string; + } /** - * expects an end of statement or end of file + * Represents the null keyword */ - expectEndOfStatement(): boolean; + class NullKeyword extends Node { + } /** - * Force the parser to check the current token. - * - * If the current token does not match to expected token, - * the an error will be raised. - * - * If the suppressError mode is activated, then the error will - * be added to the program error stack and this function will return `false`. + * Defines a numeric value + */ + class Number extends Literal { + } + /** + * Lookup on an offset in an array */ - expect(token: string | number): boolean; + class OffsetLookup extends Lookup { + } /** - * Returns the current token contents + * Defines binary operations */ - text(): string; + class Operation extends Expression { + } /** - * consume the next token + * Defines a function parameter */ - next(): void; + class Parameter extends Declaration { + type: Identifier | null; + value: Node | null; + byref: boolean; + variadic: boolean; + nullable: boolean; + } /** - * Eating a token + * Defines a class reference node */ - lex(): void; + class ParentReference extends Reference { + } /** - * Check if token is of specified type + * Each Position object consists of a line number (1-indexed) and a column number (0-indexed): */ - is(): void; + class Position { + line: number; + column: number; + offset: number; + } /** - * current lexer instance - */ - lexer: Lexer; + * Defines a post operation `$i++` or `$i--` + */ + class Post extends Operation { + type: string; + what: Variable; + } /** - * the AST factory instance - */ - ast: AST; + * Defines a pre operation `++$i` or `--$i` + */ + class Pre extends Operation { + type: string; + what: Variable; + } /** - * current token - */ - token: number | string; + * Outputs + */ + class Print extends Expression { + } /** - * should extract documentation as AST node - */ - extractDoc: boolean; + * The main program node + */ + class Program extends Block { + errors: Error[]; + comments: Comment[] | null; + tokens: String[] | null; + } /** - * should extract each token - */ - extractTokens: boolean; + * Defines a class property + */ + class Property extends Statement { + name: string; + value: Node | null; + nullable: boolean; + type: Identifier | Identifier[] | null; + } /** - * should ignore parsing errors and continue - */ - suppressErrors: boolean; + * Lookup to an object property + */ + class PropertyLookup extends Lookup { + } + /** + * Declares a properties into the current scope + */ + class PropertyStatement extends Statement { + properties: Property[]; + } + /** + * Defines a reference node + */ + class Reference extends Node { + } + /** + * Defines a short if statement that returns a value + */ + class RetIf extends Expression { + test: Expression; + trueExpr: Expression; + falseExpr: Expression; + } + /** + * A continue statement + */ + class Return extends Statement { + expr: Expression | null; + } /** - * should output debug informations - */ - debug: boolean; + * Defines a class reference node + */ + class SelfReference extends Reference { + } + /** + * Avoids to show/log warnings & notices from the inner expression + */ + class Silent extends Expression { + expr: Expression; + } + /** + * Any statement. + */ + class Statement extends Node { + } + /** + * Declares a static variable into the current scope + */ + class Static extends Statement { + variables: StaticVariable[]; + } + /** + * Lookup to a static property + */ + class StaticLookup extends Lookup { + } + /** + * Defines a class reference node + */ + class StaticReference extends Reference { + } + /** + * Defines a constant + */ + class StaticVariable extends Node { + variable: Variable; + defaultValue: Node | string | number | boolean | null; + } + /** + * Defines a string (simple or double quoted) - chars are already escaped + */ + class String extends Literal { + unicode: boolean; + isDoubleQuote: boolean; + } + /** + * Defines a switch statement + */ + class Switch extends Statement { + test: Expression; + body: Block; + shortForm: boolean; + } + /** + * Defines a throw statement + */ + class Throw extends Statement { + what: Expression; + } + /** + * A trait definition + */ + class Trait extends Declaration { + body: Declaration[]; + } + /** + * Defines a trait alias + */ + class TraitAlias extends Node { + trait: Identifier | null; + method: Identifier; + as: Identifier | null; + visibility: string | null; + } + /** + * Defines a trait alias + */ + class TraitPrecedence extends Node { + trait: Identifier | null; + method: Identifier; + instead: Identifier[]; + } + /** + * Defines a trait usage + */ + class TraitUse extends Node { + traits: Identifier[]; + adaptations: Node[] | null; + } + /** + * Defines a try statement + */ + class Try extends Statement { + body: Block; + catches: Catch[]; + allways: Block; + } + /** + * Defines a class reference node + */ + class TypeReference extends Reference { + name: string; + } + /** + * Unary operations + */ + class Unary extends Operation { + type: string; + what: Expression; + } + /** + * Deletes references to a list of variables + */ + class Unset extends Statement { + } + /** + * Defines a use statement (with a list of use items) + * @property type - Possible value : function, const + */ + class UseGroup extends Statement { + name: string | null; + /** + * Possible value : function, const + */ + type: string | null; + item: UseItem[]; + } + /** + * Defines a use statement (from namespace) + * @property type - Possible value : function, const + */ + class UseItem extends Statement { + name: string; + /** + * Possible value : function, const + */ + type: string | null; + alias: Identifier | null; + } + /** + * Any expression node. Since the left-hand side of an assignment may + * be any expression in general, an expression can also be a pattern. + * @example + * // PHP code : + * $foo + * // AST output + * { + * "kind": "variable", + * "name": "foo", + * "curly": false + * } + * @property name - The variable name (can be a complex expression when the name is resolved dynamically) + * @property curly - Indicate if the name is defined between curlies, ex `${foo}` + */ + class Variable extends Expression { + /** + * The variable name (can be a complex expression when the name is resolved dynamically) + */ + name: string | Node; + /** + * Indicate if the name is defined between curlies, ex `${foo}` + */ + curly: boolean; + } + /** + * Introduce a list of items into the arguments of the call + */ + class variadic extends Expression { + what: any[] | Expression; + } + /** + * Defines a while statement + */ + class While extends Statement { + test: Expression; + body: Statement; + shortForm: boolean; + } + /** + * Defines a yield generator statement + */ + class Yield extends Expression { + value: Expression | null; + key: Expression | null; + } + /** + * Defines a yield from generator statement + */ + class YieldFrom extends Expression { + value: Expression; + } + /** + * The AST builder class + * @property withPositions - Should locate any node (by default false) + * @property withSource - Should extract the node original code (by default false) + */ + class AST { + /** + * Should locate any node (by default false) + */ + withPositions: boolean; + /** + * Should extract the node original code (by default false) + */ + withSource: boolean; + } + /** + * Initialise a new parser instance with the specified options + * @example + * var parser = require('php-parser'); + * var instance = new parser({ + * parser: { + * extractDoc: true, + * suppressErrors: true, + * version: 704 // or '7.4' + * }, + * ast: { + * withPositions: true + * }, + * lexer: { + * short_tags: true, + * asp_tags: true + * } + * }); + * + * var evalAST = instance.parseEval('some php code'); + * var codeAST = instance.parseCode(' Note that the output tokens are *STRICLY* similar to PHP function `token_get_all` + * @returns - Each item can be a string or an array with following informations [token_name, text, line_number] + */ + tokenGetAll(buffer: string): String[]; + lexer: Lexer; + parser: Parser; + ast: AST; + tokens: any; + } + /** + * This is the php lexer. It will tokenize the string for helping the + * parser to build the AST from its grammar. + * @property all_tokens - defines if all tokens must be retrieved (used by token_get_all only) + * @property comment_tokens - extracts comments tokens + * @property mode_eval - enables the evald mode (ignore opening tags) + * @property asp_tags - disables by default asp tags mode + * @property short_tags - enables by default short tags mode + * @property keywords - List of php keyword + * @property castKeywords - List of php keywords for type casting + */ + class Lexer { + EOF: number; + /** + * defines if all tokens must be retrieved (used by token_get_all only) + */ + all_tokens: boolean; + /** + * extracts comments tokens + */ + comment_tokens: boolean; + /** + * enables the evald mode (ignore opening tags) + */ + mode_eval: boolean; + /** + * disables by default asp tags mode + */ + asp_tags: boolean; + /** + * enables by default short tags mode + */ + short_tags: boolean; + /** + * List of php keyword + */ + keywords: any; + /** + * List of php keywords for type casting + */ + castKeywords: any; + } + /** + * The PHP Parser class that build the AST tree from the lexer + * @property lexer - current lexer instance + * @property ast - the AST factory instance + * @property token - current token + * @property extractDoc - should extract documentation as AST node + * @property extractTokens - should extract each token + * @property suppressErrors - should ignore parsing errors and continue + * @property debug - should output debug informations + */ + class Parser { + /** + * current lexer instance + */ + lexer: Lexer; + /** + * the AST factory instance + */ + ast: AST; + /** + * current token + */ + token: number | string; + /** + * should extract documentation as AST node + */ + extractDoc: boolean; + /** + * should extract each token + */ + extractTokens: boolean; + /** + * should ignore parsing errors and continue + */ + suppressErrors: boolean; + /** + * should output debug informations + */ + debug: boolean; + } } -/** - * outputs some debug information on current token - */ -declare const ignoreStack: any; - From ab88673ce4049b25b2082a0d4833762b3b741bf1 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sun, 9 Aug 2020 16:48:51 -0700 Subject: [PATCH 06/12] Include correct type definition --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4273ed559..a7f37e48e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "files": [ "src", "dist", - "index.d.ts", + "types.d.ts", "LICENSE" ], "types": "types.d.ts", From c6df9e9fb5143e87cff9cd34548a4c2c216b4ba8 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 10 Aug 2020 00:37:46 -0700 Subject: [PATCH 07/12] Fix Variadic class name --- src/ast/variadic.js | 2 +- types.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast/variadic.js b/src/ast/variadic.js index b591e4691..06646b1e0 100644 --- a/src/ast/variadic.js +++ b/src/ast/variadic.js @@ -10,7 +10,7 @@ const KIND = "variadic"; /** * Introduce a list of items into the arguments of the call - * @constructor variadic + * @constructor Variadic * @memberOf module:php-parser * @extends {Expression} * @property {Array|Expression} what diff --git a/types.d.ts b/types.d.ts index 2a410a153..ab621a07e 100644 --- a/types.d.ts +++ b/types.d.ts @@ -774,7 +774,7 @@ declare module "php-parser" { /** * Introduce a list of items into the arguments of the call */ - class variadic extends Expression { + class Variadic extends Expression { what: any[] | Expression; } /** From b8675398bf6df204d16d7b21270c48af87f63d7f Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 18 Dec 2020 15:32:33 -0800 Subject: [PATCH 08/12] Fix a lot of the remaining type errors --- src/ast.js | 27 ++++- src/ast/classconstant.js | 1 + src/ast/declaration.js | 3 + src/ast/declare.js | 12 +- src/ast/encapsed.js | 16 +-- src/ast/name.js | 16 +-- src/ast/node.js | 8 ++ src/ast/propertystatement.js | 2 + src/ast/useitem.js | 8 +- src/lexer.js | 32 ++++- src/lexer/strings.js | 8 +- src/parser.js | 32 ++++- types.d.ts | 228 +++++++++++++++++++++++++++++++++++ 13 files changed, 356 insertions(+), 37 deletions(-) diff --git a/src/ast.js b/src/ast.js index 33fb4d226..93e8b704b 100644 --- a/src/ast.js +++ b/src/ast.js @@ -132,9 +132,11 @@ const AST = function (withPositions, withSource) { /** * Create a position node from specified parser * including it's lexer current state + * @private + * @function AST#position + * @memberOf module:php-parser * @param {Parser} parser * @return {Position} - * @private */ AST.prototype.position = function (parser) { return new Position( @@ -175,12 +177,22 @@ AST.precedence = {}; }); }); +/** + * @private + * @function AST#isRightAssociative + * @memberOf module:php-parser + * @param operator + * @return {boolean} + */ AST.prototype.isRightAssociative = function (operator) { return operator === "**" || operator === "??"; }; /** * Change parent node informations after swapping childs + * @private + * @function AST#swapLocations + * @memberOf module:php-parser */ AST.prototype.swapLocations = function (target, first, last, parser) { if (this.withPositions) { @@ -197,6 +209,9 @@ AST.prototype.swapLocations = function (target, first, last, parser) { /** * Includes locations from first & last into the target + * @private + * @function AST#resolveLocations + * @memberOf module:php-parser */ AST.prototype.resolveLocations = function (target, first, last, parser) { if (this.withPositions) { @@ -217,6 +232,9 @@ AST.prototype.resolveLocations = function (target, first, last, parser) { /** * Check and fix precence, by default using right + * @private + * @function AST#resolvePrecedence + * @memberOf module:php-parser */ AST.prototype.resolvePrecedence = function (result, parser) { let buffer, lLevel, rLevel; @@ -346,8 +364,11 @@ AST.prototype.resolvePrecedence = function (result, parser) { /** * Prepares an AST node + * @private + * @function AST#prepare + * @memberOf module:php-parser * @param {String|null} kind - Defines the node type - * (if null, the kind must be passed at the function call) + * @param {*} docs - (if null, the kind must be passed at the function call) * @param {Parser} parser - The parser instance (use for extracting locations) * @return {Function} */ @@ -414,6 +435,7 @@ AST.prototype.prepare = function (kind, docs, parser) { /** * Sets a list of trailing comments + * @private * @param {*} docs */ result.setTrailingComments = function (docs) { @@ -427,6 +449,7 @@ AST.prototype.prepare = function (kind, docs, parser) { /** * Release a node without using it on the AST + * @private * @param {*} target */ result.destroy = function (target) { diff --git a/src/ast/classconstant.js b/src/ast/classconstant.js index ac6a1b92b..a2bce35d0 100644 --- a/src/ast/classconstant.js +++ b/src/ast/classconstant.js @@ -35,6 +35,7 @@ const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant( * Generic flags parser * @function * @name ClassConstant#parseFlags + * @memberOf module:php-parser * @param {Array} flags * @return {void} */ diff --git a/src/ast/declaration.js b/src/ast/declaration.js index 5450b3815..31ec08df3 100644 --- a/src/ast/declaration.js +++ b/src/ast/declaration.js @@ -32,6 +32,9 @@ const Declaration = Statement.extends(KIND, function Declaration( /** * Generic flags parser + * @function + * @name Declaration#parseFlags + * @memberOf module:php-parser * @param {Array} flags * @return {void} */ diff --git a/src/ast/declare.js b/src/ast/declare.js index 2b416b715..cddfea310 100644 --- a/src/ast/declare.js +++ b/src/ast/declare.js @@ -37,8 +37,8 @@ const Declare = Block.extends(KIND, function Declare( * // some statements * enddeclare; * ``` - * @constant {String} MODE_SHORT - * @memberOf Declare + * @constant {String} Declare#MODE_SHORT + * @memberOf module:php-parser */ Declare.MODE_SHORT = "short"; @@ -50,8 +50,8 @@ Declare.MODE_SHORT = "short"; * // some statements * } * ``` - * @constant {String} MODE_BLOCK - * @memberOf Declare + * @constant {String} Declare#MODE_BLOCK + * @memberOf module:php-parser */ Declare.MODE_BLOCK = "block"; @@ -66,8 +66,8 @@ Declare.MODE_BLOCK = "block"; * declare(ticks=2); * // some statements * ``` - * @constant {String} MODE_NONE - * @memberOf Declare + * @constant {String} Declare#MODE_NONE + * @memberOf module:php-parser */ Declare.MODE_NONE = "none"; diff --git a/src/ast/encapsed.js b/src/ast/encapsed.js index c3e25bc9b..8198b1168 100644 --- a/src/ast/encapsed.js +++ b/src/ast/encapsed.js @@ -33,8 +33,8 @@ const Encapsed = Literal.extends(KIND, function Encapsed( * bar_$baz; * ``` - * @constant {String} TYPE_OFFSET - `offset` - * @memberOf Encapsed + * @constant {String} Encapsed#TYPE_OFFSET - `offset` + * @memberOf module:php-parser */ Encapsed.TYPE_OFFSET = "offset"; diff --git a/src/ast/name.js b/src/ast/name.js index 171d15588..146013b47 100644 --- a/src/ast/name.js +++ b/src/ast/name.js @@ -37,28 +37,28 @@ const Name = Reference.extends(KIND, function Name( /** * This is an identifier without a namespace separator, such as Foo - * @constant {String} UNQUALIFIED_NAME - * @memberOf Name + * @constant {String} Name#UNQUALIFIED_NAME + * @memberOf module:php-parser */ Name.UNQUALIFIED_NAME = "uqn"; /** * This is an identifier with a namespace separator, such as Foo\Bar - * @constant {String} QUALIFIED_NAME - * @memberOf Name + * @constant {String} Name#QUALIFIED_NAME + * @memberOf module:php-parser */ Name.QUALIFIED_NAME = "qn"; /** * This is an identifier with a namespace separator that begins with * a namespace separator, such as \Foo\Bar. The namespace \Foo is also * a fully qualified name. - * @constant {String} FULL_QUALIFIED_NAME - * @memberOf Name + * @constant {String} Name#FULL_QUALIFIED_NAME + * @memberOf module:php-parser */ Name.FULL_QUALIFIED_NAME = "fqn"; /** * This is an identifier starting with namespace, such as namespace\Foo\Bar. - * @constant {String} RELATIVE_NAME - * @memberOf Name + * @constant {String} Name#RELATIVE_NAME + * @memberOf module:php-parser */ Name.RELATIVE_NAME = "rn"; diff --git a/src/ast/node.js b/src/ast/node.js index 7c92287ea..98a69ad22 100644 --- a/src/ast/node.js +++ b/src/ast/node.js @@ -26,6 +26,8 @@ const Node = function Node(kind, docs, location) { /** * Attach comments to current node + * @function Node#setTrailingComments + * @memberOf module:php-parser * @param {*} docs */ Node.prototype.setTrailingComments = function (docs) { @@ -34,6 +36,8 @@ Node.prototype.setTrailingComments = function (docs) { /** * Destroying an unused node + * @function Node#destroy + * @memberOf module:php-parser */ Node.prototype.destroy = function (node) { if (!node) { @@ -66,6 +70,8 @@ Node.prototype.destroy = function (node) { /** * Includes current token position of the parser + * @function Node#includeToken + * @memberOf module:php-parser * @param {*} parser */ Node.prototype.includeToken = function (parser) { @@ -87,6 +93,8 @@ Node.prototype.includeToken = function (parser) { /** * Helper for extending the Node class + * @function Node.extends + * @memberOf module:php-parser * @param {string} type * @param {Function} constructor * @return {Function} diff --git a/src/ast/propertystatement.js b/src/ast/propertystatement.js index 5e681d43e..479836ec5 100644 --- a/src/ast/propertystatement.js +++ b/src/ast/propertystatement.js @@ -34,6 +34,8 @@ const PropertyStatement = Statement.extends(KIND, function PropertyStatement( /** * Generic flags parser + * @function PropertyStatement#parseFlags + * @memberOf module:php-parser * @param {Array} flags * @return {void} */ diff --git a/src/ast/useitem.js b/src/ast/useitem.js index 32bdfefa7..5c8c1a254 100644 --- a/src/ast/useitem.js +++ b/src/ast/useitem.js @@ -34,14 +34,14 @@ const UseItem = Statement.extends(KIND, function UseItem( /** * Importing a constant - * @constant {string} TYPE_CONST - * @memberOf UseItem + * @constant {string} UseItem#TYPE_CONST + * @memberOf module:php-parser */ UseItem.TYPE_CONST = "const"; /** * Importing a function - * @constant {string} TYPE_FUNC - * @memberOf UseItem + * @constant {string} UseItem#TYPE_FUNC + * @memberOf module:php-parser */ UseItem.TYPE_FUNCTION = "function"; diff --git a/src/lexer.js b/src/lexer.js index 90b3951d6..72f3141ae 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -126,6 +126,8 @@ const Lexer = function (engine) { /** * Initialize the lexer with the specified input + * @function Lexer#setInput + * @memberOf module:php-parser */ Lexer.prototype.setInput = function (input) { this._input = input; @@ -166,7 +168,7 @@ Lexer.prototype.setInput = function (input) { indentation: 0, indentation_uses_spaces: false, finished: false, - /** + /* * this used for parser to detemine the if current node segment is first encaps node. * if ture, the indentation will remove from the begining. and if false, the prev node * might be a variable '}' ,and the leading spaces should not be removed util meet the @@ -183,6 +185,8 @@ Lexer.prototype.setInput = function (input) { /** * consumes and returns one char from the input + * @function Lexer#input + * @memberOf module:php-parser */ Lexer.prototype.input = function () { const ch = this._input[this.offset]; @@ -205,6 +209,8 @@ Lexer.prototype.input = function () { /** * revert eating specified size + * @function Lexer#unput + * @memberOf module:php-parser */ Lexer.prototype.unput = function (size) { if (size === 1) { @@ -271,6 +277,8 @@ Lexer.prototype.unput = function (size) { /** * check if the text matches + * @function Lexer#tryMatch + * @memberOf module:php-parser * @param {string} text * @returns {boolean} */ @@ -280,6 +288,8 @@ Lexer.prototype.tryMatch = function (text) { /** * check if the text matches + * @function Lexer#tryMatchCaseless + * @memberOf module:php-parser * @param {string} text * @returns {boolean} */ @@ -289,6 +299,8 @@ Lexer.prototype.tryMatchCaseless = function (text) { /** * look ahead + * @function Lexer#ahead + * @memberOf module:php-parser * @param {number} size * @returns {string} */ @@ -305,6 +317,8 @@ Lexer.prototype.ahead = function (size) { /** * consume the specified size + * @function Lexer#consume + * @memberOf module:php-parser * @param {number} size * @returns {Lexer} */ @@ -332,6 +346,8 @@ Lexer.prototype.consume = function (size) { /** * Gets the current state + * @function Lexer#getState + * @memberOf module:php-parser */ Lexer.prototype.getState = function () { return { @@ -352,6 +368,8 @@ Lexer.prototype.getState = function () { /** * Sets the current lexer state + * @function Lexer#setState + * @memberOf module:php-parser */ Lexer.prototype.setState = function (state) { this.yytext = state.yytext; @@ -367,6 +385,8 @@ Lexer.prototype.setState = function (state) { /** * prepend next token + * @function Lexer#appendToken + * @memberOf module:php-parser * @param value * @param ahead * @returns {Lexer} @@ -378,6 +398,8 @@ Lexer.prototype.appendToken = function (value, ahead) { /** * return next match that has a token + * @function Lexer#lex + * @memberOf module:php-parser * @returns {number|string} */ Lexer.prototype.lex = function () { @@ -418,6 +440,8 @@ Lexer.prototype.lex = function () { /** * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) + * @function Lexer#begin + * @memberOf module:php-parser * @param condition * @returns {Lexer} */ @@ -433,7 +457,9 @@ Lexer.prototype.begin = function (condition) { /** * pop the previously active lexer condition state off the condition stack - * @returns {T|string|*} + * @function Lexer#popState + * @memberOf module:php-parser + * @returns {string|*} */ Lexer.prototype.popState = function () { const n = this.conditionStack.length - 1; @@ -448,6 +474,8 @@ Lexer.prototype.popState = function () { /** * return next match in input + * @function Lexer#next + * @memberOf module:php-parser * @returns {number|*} */ Lexer.prototype.next = function () { diff --git a/src/lexer/strings.js b/src/lexer/strings.js index ef575b41c..abb1937a7 100644 --- a/src/lexer/strings.js +++ b/src/lexer/strings.js @@ -249,14 +249,14 @@ module.exports = { }, matchST_NOWDOC: function () { - /** edge case : empty now doc **/ + // edge case : empty now doc if (this.isDOC_MATCH(this.offset, true)) { // @fixme : never reached (may be caused by quotes) this.consume(this.heredoc_label.length); this.popState(); return this.tok.T_END_HEREDOC; } - /** SCANNING CONTENTS **/ + // SCANNING CONTENTS let ch = this._input[this.offset - 1]; while (this.offset < this.size) { if (newline.includes(ch)) { @@ -275,14 +275,14 @@ module.exports = { }, matchST_HEREDOC: function () { - /** edge case : empty here doc **/ + // edge case : empty here doc let ch = this.input(); if (this.isDOC_MATCH(this.offset, true)) { this.consume(this.heredoc_label.length - 1); this.popState(); return this.tok.T_END_HEREDOC; } - /** SCANNING CONTENTS **/ + // SCANNING CONTENTS while (this.offset < this.size) { if (ch === "\\") { ch = this.input(); // ignore next diff --git a/src/parser.js b/src/parser.js index 4f8b532a2..946939d4e 100644 --- a/src/parser.js +++ b/src/parser.js @@ -244,6 +244,8 @@ const Parser = function (lexer, ast) { /** * helper : gets a token name + * @function Parser#getTokenName + * @memberOf module:php-parser */ Parser.prototype.getTokenName = function (token) { if (!isNumber(token)) { @@ -256,6 +258,8 @@ Parser.prototype.getTokenName = function (token) { /** * main entry point : converts a source code to AST + * @function Parser#parse + * @memberOf module:php-parser */ Parser.prototype.parse = function (code, filename) { this._errors = []; @@ -324,6 +328,8 @@ Parser.prototype.parse = function (code, filename) { /** * Raise an error + * @function Parser#raiseError + * @memberOf module:php-parser */ Parser.prototype.raiseError = function (message, msgExpect, expect, token) { message += " on line " + this.lexer.yylloc.first_line; @@ -351,6 +357,8 @@ Parser.prototype.raiseError = function (message, msgExpect, expect, token) { /** * handling errors + * @function Parser#error + * @memberOf module:php-parser */ Parser.prototype.error = function (expect) { let msg = "Parse Error : syntax error"; @@ -378,6 +386,8 @@ Parser.prototype.error = function (expect) { /** * Creates a new AST node + * @function Parser#node + * @memberOf module:php-parser */ Parser.prototype.node = function (name) { if (this.extractDoc) { @@ -393,7 +403,7 @@ Parser.prototype.node = function (name) { } } const node = this.ast.prepare(name, docs, this); - /** + /* * TOKENS : * node1 commentA token commmentB node2 commentC token commentD node3 commentE token * @@ -454,6 +464,8 @@ Parser.prototype.node = function (name) { /** * expects an end of statement or end of file + * @function Parser#expectEndOfStatement + * @memberOf module:php-parser * @return {boolean} */ Parser.prototype.expectEndOfStatement = function (node) { @@ -471,11 +483,13 @@ Parser.prototype.expectEndOfStatement = function (node) { return true; }; +const ignoreStack = ["parser.next", "parser.node", "parser.showlog"]; /** * outputs some debug information on current token * @private + * @function Parser#showlog + * @memberOf module:php-parser */ -const ignoreStack = ["parser.next", "parser.node", "parser.showlog"]; Parser.prototype.showlog = function () { const stack = new Error().stack.split("\n"); let line; @@ -516,6 +530,8 @@ Parser.prototype.showlog = function () { * If the suppressError mode is activated, then the error will * be added to the program error stack and this function will return `false`. * + * @function Parser#expect + * @memberOf module:php-parser * @param {String|Number} token * @return {boolean} * @throws Error @@ -535,13 +551,19 @@ Parser.prototype.expect = function (token) { /** * Returns the current token contents + * @function Parser#text + * @memberOf module:php-parser * @return {String} */ Parser.prototype.text = function () { return this.lexer.yytext; }; -/** consume the next token **/ +/** + * consume the next token + * @function Parser#next + * @memberOf module:php-parser + */ Parser.prototype.next = function () { // prepare the back command if (this.token !== ";" || this.lexer.yytext === ";") { @@ -582,6 +604,8 @@ Parser.prototype.next = function () { /** * Eating a token + * @function Parser#lex + * @memberOf module:php-parser */ Parser.prototype.lex = function () { // append on token stack @@ -633,6 +657,8 @@ Parser.prototype.lex = function () { /** * Check if token is of specified type + * @function Parser#is + * @memberOf module:php-parser */ Parser.prototype.is = function (type) { if (Array.isArray(type)) { diff --git a/types.d.ts b/types.d.ts index ab621a07e..1b29b6fcc 100644 --- a/types.d.ts +++ b/types.d.ts @@ -139,6 +139,10 @@ declare module "php-parser" { * Defines a class/interface/trait constant */ class ClassConstant extends ConstantStatement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; visibility: string; } /** @@ -198,12 +202,49 @@ declare module "php-parser" { * A declaration statement (function, class, interface...) */ class Declaration extends Statement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; name: Identifier | string; } /** * The declare construct is used to set execution directives for a block of code */ class Declare extends Block { + /** + * The node is declared as a short tag syntax : + * ```php + * bar_$baz; + * ``` + */ + readonly TYPE_OFFSET: string; /** * Defines the type of encapsed string (shell, heredoc, string) */ @@ -458,6 +534,24 @@ declare module "php-parser" { * Defines a class reference node */ class Name extends Reference { + /** + * This is an identifier without a namespace separator, such as Foo + */ + readonly UNQUALIFIED_NAME: string; + /** + * This is an identifier with a namespace separator, such as Foo\Bar + */ + readonly QUALIFIED_NAME: string; + /** + * This is an identifier with a namespace separator that begins with + * a namespace separator, such as \Foo\Bar. The namespace \Foo is also + * a fully qualified name. + */ + readonly FULL_QUALIFIED_NAME: string; + /** + * This is an identifier starting with namespace, such as namespace\Foo\Bar. + */ + readonly RELATIVE_NAME: string; name: string; resolution: string; } @@ -479,6 +573,22 @@ declare module "php-parser" { * A generic AST node */ class Node { + /** + * Attach comments to current node + */ + setTrailingComments(docs: any): void; + /** + * Destroying an unused node + */ + destroy(): void; + /** + * Includes current token position of the parser + */ + includeToken(parser: any): void; + /** + * Helper for extending the Node class + */ + static extends(type: string, constructor: (...params: any[]) => any): (...params: any[]) => any; loc: Location | null; leadingComments: CommentBlock[] | Comment[] | null; trailingComments: CommentBlock[] | Comment[] | null; @@ -585,6 +695,10 @@ declare module "php-parser" { * Declares a properties into the current scope */ class PropertyStatement extends Statement { + /** + * Generic flags parser + */ + parseFlags(flags: (number | null)[]): void; properties: Property[]; } /** @@ -739,6 +853,14 @@ declare module "php-parser" { * @property type - Possible value : function, const */ class UseItem extends Statement { + /** + * Importing a constant + */ + readonly TYPE_CONST: string; + /** + * Importing a function + */ + readonly TYPE_FUNC: string; name: string; /** * Possible value : function, const @@ -886,6 +1008,62 @@ declare module "php-parser" { * @property castKeywords - List of php keywords for type casting */ class Lexer { + /** + * Initialize the lexer with the specified input + */ + setInput(): void; + /** + * consumes and returns one char from the input + */ + input(): void; + /** + * revert eating specified size + */ + unput(): void; + /** + * check if the text matches + */ + tryMatch(text: string): boolean; + /** + * check if the text matches + */ + tryMatchCaseless(text: string): boolean; + /** + * look ahead + */ + ahead(size: number): string; + /** + * consume the specified size + */ + consume(size: number): Lexer; + /** + * Gets the current state + */ + getState(): void; + /** + * Sets the current lexer state + */ + setState(): void; + /** + * prepend next token + */ + appendToken(value: any, ahead: any): Lexer; + /** + * return next match that has a token + */ + lex(): number | string; + /** + * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) + */ + begin(condition: any): Lexer; + /** + * pop the previously active lexer condition state off the condition stack + */ + popState(): string | any; + /** + * return next match in input + */ + next(): number | any; EOF: number; /** * defines if all tokens must be retrieved (used by token_get_all only) @@ -927,6 +1105,56 @@ declare module "php-parser" { * @property debug - should output debug informations */ class Parser { + /** + * helper : gets a token name + */ + getTokenName(): void; + /** + * main entry point : converts a source code to AST + */ + parse(): void; + /** + * Raise an error + */ + raiseError(): void; + /** + * handling errors + */ + error(): void; + /** + * Creates a new AST node + */ + node(): void; + /** + * expects an end of statement or end of file + */ + expectEndOfStatement(): boolean; + /** + * Force the parser to check the current token. + * + * If the current token does not match to expected token, + * the an error will be raised. + * + * If the suppressError mode is activated, then the error will + * be added to the program error stack and this function will return `false`. + */ + expect(token: string | number): boolean; + /** + * Returns the current token contents + */ + text(): string; + /** + * consume the next token + */ + next(): void; + /** + * Eating a token + */ + lex(): void; + /** + * Check if token is of specified type + */ + is(): void; /** * current lexer instance */ From aa9e47599a4959242e1a77d65be5bd30efba089c Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 21 Dec 2020 10:24:25 -0800 Subject: [PATCH 09/12] Try to fix typing of AST tokens --- src/tokens.js | 292 ++++++++++++++++++++++++++------------------------ types.d.ts | 146 +++++++++++++++++++++++++ 2 files changed, 299 insertions(+), 139 deletions(-) diff --git a/src/tokens.js b/src/tokens.js index 1e1af82c4..e840eb675 100644 --- a/src/tokens.js +++ b/src/tokens.js @@ -5,11 +5,159 @@ */ "use strict"; +/** + * @memberOf module:php-parser + * @readonly + * @enum + */ +const TokenNames = { + T_HALT_COMPILER: 101, + T_USE: 102, + T_ENCAPSED_AND_WHITESPACE: 103, + T_OBJECT_OPERATOR: 104, + T_STRING: 105, + T_DOLLAR_OPEN_CURLY_BRACES: 106, + T_STRING_VARNAME: 107, + T_CURLY_OPEN: 108, + T_NUM_STRING: 109, + T_ISSET: 110, + T_EMPTY: 111, + T_INCLUDE: 112, + T_INCLUDE_ONCE: 113, + T_EVAL: 114, + T_REQUIRE: 115, + T_REQUIRE_ONCE: 116, + T_NAMESPACE: 117, + T_NS_SEPARATOR: 118, + T_AS: 119, + T_IF: 120, + T_ENDIF: 121, + T_WHILE: 122, + T_DO: 123, + T_FOR: 124, + T_SWITCH: 125, + T_BREAK: 126, + T_CONTINUE: 127, + T_RETURN: 128, + T_GLOBAL: 129, + T_STATIC: 130, + T_ECHO: 131, + T_INLINE_HTML: 132, + T_UNSET: 133, + T_FOREACH: 134, + T_DECLARE: 135, + T_TRY: 136, + T_THROW: 137, + T_GOTO: 138, + T_FINALLY: 139, + T_CATCH: 140, + T_ENDDECLARE: 141, + T_LIST: 142, + T_CLONE: 143, + T_PLUS_EQUAL: 144, + T_MINUS_EQUAL: 145, + T_MUL_EQUAL: 146, + T_DIV_EQUAL: 147, + T_CONCAT_EQUAL: 148, + T_MOD_EQUAL: 149, + T_AND_EQUAL: 150, + T_OR_EQUAL: 151, + T_XOR_EQUAL: 152, + T_SL_EQUAL: 153, + T_SR_EQUAL: 154, + T_INC: 155, + T_DEC: 156, + T_BOOLEAN_OR: 157, + T_BOOLEAN_AND: 158, + T_LOGICAL_OR: 159, + T_LOGICAL_AND: 160, + T_LOGICAL_XOR: 161, + T_SL: 162, + T_SR: 163, + T_IS_IDENTICAL: 164, + T_IS_NOT_IDENTICAL: 165, + T_IS_EQUAL: 166, + T_IS_NOT_EQUAL: 167, + T_IS_SMALLER_OR_EQUAL: 168, + T_IS_GREATER_OR_EQUAL: 169, + T_INSTANCEOF: 170, + T_INT_CAST: 171, + T_DOUBLE_CAST: 172, + T_STRING_CAST: 173, + T_ARRAY_CAST: 174, + T_OBJECT_CAST: 175, + T_BOOL_CAST: 176, + T_UNSET_CAST: 177, + T_EXIT: 178, + T_PRINT: 179, + T_YIELD: 180, + T_YIELD_FROM: 181, + T_FUNCTION: 182, + T_DOUBLE_ARROW: 183, + T_DOUBLE_COLON: 184, + T_ARRAY: 185, + T_CALLABLE: 186, + T_CLASS: 187, + T_ABSTRACT: 188, + T_TRAIT: 189, + T_FINAL: 190, + T_EXTENDS: 191, + T_INTERFACE: 192, + T_IMPLEMENTS: 193, + T_VAR: 194, + T_PUBLIC: 195, + T_PROTECTED: 196, + T_PRIVATE: 197, + T_CONST: 198, + T_NEW: 199, + T_INSTEADOF: 200, + T_ELSEIF: 201, + T_ELSE: 202, + T_ENDSWITCH: 203, + T_CASE: 204, + T_DEFAULT: 205, + T_ENDFOR: 206, + T_ENDFOREACH: 207, + T_ENDWHILE: 208, + T_CONSTANT_ENCAPSED_STRING: 209, + T_LNUMBER: 210, + T_DNUMBER: 211, + T_LINE: 212, + T_FILE: 213, + T_DIR: 214, + T_TRAIT_C: 215, + T_METHOD_C: 216, + T_FUNC_C: 217, + T_NS_C: 218, + T_START_HEREDOC: 219, + T_END_HEREDOC: 220, + T_CLASS_C: 221, + T_VARIABLE: 222, + T_OPEN_TAG: 223, + T_OPEN_TAG_WITH_ECHO: 224, + T_CLOSE_TAG: 225, + T_WHITESPACE: 226, + T_COMMENT: 227, + T_DOC_COMMENT: 228, + T_ELLIPSIS: 229, + T_COALESCE: 230, + T_POW: 231, + T_POW_EQUAL: 232, + T_SPACESHIP: 233, + T_COALESCE_EQUAL: 234, + T_FN: 235, +}; + /** * PHP AST Tokens - * @type {Object} + * @readonly + * @memberOf module:php-parser + * + * @type {object} + * @property {Object.} values + * @property {TokenNames} names */ -module.exports = { +const tokens = { values: { 101: "T_HALT_COMPILER", 102: "T_USE", @@ -147,141 +295,7 @@ module.exports = { 234: "T_COALESCE_EQUAL", 235: "T_FN", }, - names: { - T_HALT_COMPILER: 101, - T_USE: 102, - T_ENCAPSED_AND_WHITESPACE: 103, - T_OBJECT_OPERATOR: 104, - T_STRING: 105, - T_DOLLAR_OPEN_CURLY_BRACES: 106, - T_STRING_VARNAME: 107, - T_CURLY_OPEN: 108, - T_NUM_STRING: 109, - T_ISSET: 110, - T_EMPTY: 111, - T_INCLUDE: 112, - T_INCLUDE_ONCE: 113, - T_EVAL: 114, - T_REQUIRE: 115, - T_REQUIRE_ONCE: 116, - T_NAMESPACE: 117, - T_NS_SEPARATOR: 118, - T_AS: 119, - T_IF: 120, - T_ENDIF: 121, - T_WHILE: 122, - T_DO: 123, - T_FOR: 124, - T_SWITCH: 125, - T_BREAK: 126, - T_CONTINUE: 127, - T_RETURN: 128, - T_GLOBAL: 129, - T_STATIC: 130, - T_ECHO: 131, - T_INLINE_HTML: 132, - T_UNSET: 133, - T_FOREACH: 134, - T_DECLARE: 135, - T_TRY: 136, - T_THROW: 137, - T_GOTO: 138, - T_FINALLY: 139, - T_CATCH: 140, - T_ENDDECLARE: 141, - T_LIST: 142, - T_CLONE: 143, - T_PLUS_EQUAL: 144, - T_MINUS_EQUAL: 145, - T_MUL_EQUAL: 146, - T_DIV_EQUAL: 147, - T_CONCAT_EQUAL: 148, - T_MOD_EQUAL: 149, - T_AND_EQUAL: 150, - T_OR_EQUAL: 151, - T_XOR_EQUAL: 152, - T_SL_EQUAL: 153, - T_SR_EQUAL: 154, - T_INC: 155, - T_DEC: 156, - T_BOOLEAN_OR: 157, - T_BOOLEAN_AND: 158, - T_LOGICAL_OR: 159, - T_LOGICAL_AND: 160, - T_LOGICAL_XOR: 161, - T_SL: 162, - T_SR: 163, - T_IS_IDENTICAL: 164, - T_IS_NOT_IDENTICAL: 165, - T_IS_EQUAL: 166, - T_IS_NOT_EQUAL: 167, - T_IS_SMALLER_OR_EQUAL: 168, - T_IS_GREATER_OR_EQUAL: 169, - T_INSTANCEOF: 170, - T_INT_CAST: 171, - T_DOUBLE_CAST: 172, - T_STRING_CAST: 173, - T_ARRAY_CAST: 174, - T_OBJECT_CAST: 175, - T_BOOL_CAST: 176, - T_UNSET_CAST: 177, - T_EXIT: 178, - T_PRINT: 179, - T_YIELD: 180, - T_YIELD_FROM: 181, - T_FUNCTION: 182, - T_DOUBLE_ARROW: 183, - T_DOUBLE_COLON: 184, - T_ARRAY: 185, - T_CALLABLE: 186, - T_CLASS: 187, - T_ABSTRACT: 188, - T_TRAIT: 189, - T_FINAL: 190, - T_EXTENDS: 191, - T_INTERFACE: 192, - T_IMPLEMENTS: 193, - T_VAR: 194, - T_PUBLIC: 195, - T_PROTECTED: 196, - T_PRIVATE: 197, - T_CONST: 198, - T_NEW: 199, - T_INSTEADOF: 200, - T_ELSEIF: 201, - T_ELSE: 202, - T_ENDSWITCH: 203, - T_CASE: 204, - T_DEFAULT: 205, - T_ENDFOR: 206, - T_ENDFOREACH: 207, - T_ENDWHILE: 208, - T_CONSTANT_ENCAPSED_STRING: 209, - T_LNUMBER: 210, - T_DNUMBER: 211, - T_LINE: 212, - T_FILE: 213, - T_DIR: 214, - T_TRAIT_C: 215, - T_METHOD_C: 216, - T_FUNC_C: 217, - T_NS_C: 218, - T_START_HEREDOC: 219, - T_END_HEREDOC: 220, - T_CLASS_C: 221, - T_VARIABLE: 222, - T_OPEN_TAG: 223, - T_OPEN_TAG_WITH_ECHO: 224, - T_CLOSE_TAG: 225, - T_WHITESPACE: 226, - T_COMMENT: 227, - T_DOC_COMMENT: 228, - T_ELLIPSIS: 229, - T_COALESCE: 230, - T_POW: 231, - T_POW_EQUAL: 232, - T_SPACESHIP: 233, - T_COALESCE_EQUAL: 234, - T_FN: 235, - }, + names: TokenNames, }; + +module.exports = Object.freeze(tokens); diff --git a/types.d.ts b/types.d.ts index 1b29b6fcc..d0b17f82a 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1184,5 +1184,151 @@ declare module "php-parser" { */ debug: boolean; } + const enum TokenNames { + T_HALT_COMPILER = 101, + T_USE = 102, + T_ENCAPSED_AND_WHITESPACE = 103, + T_OBJECT_OPERATOR = 104, + T_STRING = 105, + T_DOLLAR_OPEN_CURLY_BRACES = 106, + T_STRING_VARNAME = 107, + T_CURLY_OPEN = 108, + T_NUM_STRING = 109, + T_ISSET = 110, + T_EMPTY = 111, + T_INCLUDE = 112, + T_INCLUDE_ONCE = 113, + T_EVAL = 114, + T_REQUIRE = 115, + T_REQUIRE_ONCE = 116, + T_NAMESPACE = 117, + T_NS_SEPARATOR = 118, + T_AS = 119, + T_IF = 120, + T_ENDIF = 121, + T_WHILE = 122, + T_DO = 123, + T_FOR = 124, + T_SWITCH = 125, + T_BREAK = 126, + T_CONTINUE = 127, + T_RETURN = 128, + T_GLOBAL = 129, + T_STATIC = 130, + T_ECHO = 131, + T_INLINE_HTML = 132, + T_UNSET = 133, + T_FOREACH = 134, + T_DECLARE = 135, + T_TRY = 136, + T_THROW = 137, + T_GOTO = 138, + T_FINALLY = 139, + T_CATCH = 140, + T_ENDDECLARE = 141, + T_LIST = 142, + T_CLONE = 143, + T_PLUS_EQUAL = 144, + T_MINUS_EQUAL = 145, + T_MUL_EQUAL = 146, + T_DIV_EQUAL = 147, + T_CONCAT_EQUAL = 148, + T_MOD_EQUAL = 149, + T_AND_EQUAL = 150, + T_OR_EQUAL = 151, + T_XOR_EQUAL = 152, + T_SL_EQUAL = 153, + T_SR_EQUAL = 154, + T_INC = 155, + T_DEC = 156, + T_BOOLEAN_OR = 157, + T_BOOLEAN_AND = 158, + T_LOGICAL_OR = 159, + T_LOGICAL_AND = 160, + T_LOGICAL_XOR = 161, + T_SL = 162, + T_SR = 163, + T_IS_IDENTICAL = 164, + T_IS_NOT_IDENTICAL = 165, + T_IS_EQUAL = 166, + T_IS_NOT_EQUAL = 167, + T_IS_SMALLER_OR_EQUAL = 168, + T_IS_GREATER_OR_EQUAL = 169, + T_INSTANCEOF = 170, + T_INT_CAST = 171, + T_DOUBLE_CAST = 172, + T_STRING_CAST = 173, + T_ARRAY_CAST = 174, + T_OBJECT_CAST = 175, + T_BOOL_CAST = 176, + T_UNSET_CAST = 177, + T_EXIT = 178, + T_PRINT = 179, + T_YIELD = 180, + T_YIELD_FROM = 181, + T_FUNCTION = 182, + T_DOUBLE_ARROW = 183, + T_DOUBLE_COLON = 184, + T_ARRAY = 185, + T_CALLABLE = 186, + T_CLASS = 187, + T_ABSTRACT = 188, + T_TRAIT = 189, + T_FINAL = 190, + T_EXTENDS = 191, + T_INTERFACE = 192, + T_IMPLEMENTS = 193, + T_VAR = 194, + T_PUBLIC = 195, + T_PROTECTED = 196, + T_PRIVATE = 197, + T_CONST = 198, + T_NEW = 199, + T_INSTEADOF = 200, + T_ELSEIF = 201, + T_ELSE = 202, + T_ENDSWITCH = 203, + T_CASE = 204, + T_DEFAULT = 205, + T_ENDFOR = 206, + T_ENDFOREACH = 207, + T_ENDWHILE = 208, + T_CONSTANT_ENCAPSED_STRING = 209, + T_LNUMBER = 210, + T_DNUMBER = 211, + T_LINE = 212, + T_FILE = 213, + T_DIR = 214, + T_TRAIT_C = 215, + T_METHOD_C = 216, + T_FUNC_C = 217, + T_NS_C = 218, + T_START_HEREDOC = 219, + T_END_HEREDOC = 220, + T_CLASS_C = 221, + T_VARIABLE = 222, + T_OPEN_TAG = 223, + T_OPEN_TAG_WITH_ECHO = 224, + T_CLOSE_TAG = 225, + T_WHITESPACE = 226, + T_COMMENT = 227, + T_DOC_COMMENT = 228, + T_ELLIPSIS = 229, + T_COALESCE = 230, + T_POW = 231, + T_POW_EQUAL = 232, + T_SPACESHIP = 233, + T_COALESCE_EQUAL = 234, + T_FN = 235 + } + /** + * PHP AST Tokens + */ + const tokens: { + values: { + [key: number]: string; + }; + names: TokenNames; + }; } From 32d9f56f554580edec01e44be616bf061037d34d Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 21 Dec 2020 10:30:43 -0800 Subject: [PATCH 10/12] Replace some JSDoc comments with regular comments --- src/lexer/comments.js | 4 ++-- src/lexer/strings.js | 2 +- src/parser/array.js | 6 +++--- src/parser/class.js | 26 +++++++++++++------------- src/parser/comment.js | 4 ++-- src/parser/expr.js | 22 +++++++++++----------- src/parser/function.js | 22 +++++++++++----------- src/parser/if.js | 8 ++++---- src/parser/loops.js | 10 +++++----- src/parser/main.js | 2 +- src/parser/namespace.js | 16 ++++++++-------- src/parser/scalar.js | 20 ++++++++++---------- src/parser/statement.js | 16 ++++++++-------- src/parser/switch.js | 6 +++--- src/parser/try.js | 2 +- src/parser/utils.js | 12 ++++++------ src/parser/variable.js | 8 ++++---- 17 files changed, 93 insertions(+), 93 deletions(-) diff --git a/src/lexer/comments.js b/src/lexer/comments.js index ed8ba5e21..43501d701 100644 --- a/src/lexer/comments.js +++ b/src/lexer/comments.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a single line comment */ T_COMMENT: function () { @@ -32,7 +32,7 @@ module.exports = { } return this.tok.T_COMMENT; }, - /** + /* * Behaviour : https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1927 */ T_DOC_COMMENT: function () { diff --git a/src/lexer/strings.js b/src/lexer/strings.js index abb1937a7..d51310f2a 100644 --- a/src/lexer/strings.js +++ b/src/lexer/strings.js @@ -217,7 +217,7 @@ module.exports = { return false; }, - /** + /* * Prematch the end of HEREDOC/NOWDOC end tag to preset the * context of this.heredoc_label */ diff --git a/src/parser/array.js b/src/parser/array.js index 51dcc5c9f..19b3b40cd 100644 --- a/src/parser/array.js +++ b/src/parser/array.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Parse an array * ```ebnf * array ::= T_ARRAY '(' array_pair_list ')' | @@ -33,7 +33,7 @@ module.exports = { this.next(); return result(shortForm, items); }, - /** + /* * Reads an array of items * ```ebnf * array_pair_list ::= array_pair (',' array_pair?)* @@ -49,7 +49,7 @@ module.exports = { true ); }, - /** + /* * Reads an entry * array_pair: * expr T_DOUBLE_ARROW expr diff --git a/src/parser/class.js b/src/parser/class.js index 6019433d1..d72306e68 100644 --- a/src/parser/class.js +++ b/src/parser/class.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * reading a class * ```ebnf * class ::= class_scope? T_CLASS T_STRING (T_EXTENDS NAMESPACE_NAME)? (T_IMPLEMENTS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' CLASS_BODY '}' @@ -51,7 +51,7 @@ module.exports = { return result; }, - /** + /* * Reads a class body * ```ebnf * class_body ::= (member_flags? (T_VAR | T_STRING | T_FUNCTION))* @@ -131,7 +131,7 @@ module.exports = { this.next(); return result; }, - /** + /* * Reads variable list * ```ebnf * variable_list ::= (variable_declaration ',')* variable_declaration @@ -141,7 +141,7 @@ module.exports = { const result = this.node("propertystatement"); const properties = this.read_list( - /** + /* * Reads a variable declaration * * ```ebnf @@ -171,7 +171,7 @@ module.exports = { return result(null, properties, flags); }, - /** + /* * Reads constant list * ```ebnf * constant_list ::= T_CONST (constant_declaration ',')* constant_declaration @@ -183,7 +183,7 @@ module.exports = { } const result = this.node("classconstant"); const items = this.read_list( - /** + /* * Reads a constant declaration * * ```ebnf @@ -216,7 +216,7 @@ module.exports = { return result(null, items, flags); }, - /** + /* * Read member flags * @return array * 1st index : 0 => public, 1 => protected, 2 => private @@ -280,7 +280,7 @@ module.exports = { return result; }, - /** + /* * optional_type: * /- empty -/ { $$ = NULL; } * | type_expr { $$ = $1; } @@ -333,7 +333,7 @@ module.exports = { return [nullable, type]; }, - /** + /* * reading an interface * ```ebnf * interface ::= T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}' @@ -356,7 +356,7 @@ module.exports = { const body = this.next().read_interface_body(); return result(propName, propExtends, body); }, - /** + /* * Reads an interface body * ```ebnf * interface_body ::= (member_flags? (T_CONST | T_FUNCTION))* @@ -405,7 +405,7 @@ module.exports = { } return result; }, - /** + /* * reading a trait * ```ebnf * trait ::= T_TRAIT T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' FUNCTION* '}' @@ -428,7 +428,7 @@ module.exports = { const body = this.next().read_class_body(); return result(propName, body); }, - /** + /* * reading a use statement * ```ebnf * trait_use_statement ::= namespace_name (',' namespace_name)* ('{' trait_use_alias '}')? @@ -461,7 +461,7 @@ module.exports = { } return node(traits, adaptations); }, - /** + /* * Reading trait alias * ```ebnf * trait_use_alias ::= namespace_name ( T_DOUBLE_COLON T_STRING )? (T_INSTEADOF namespace_name) | (T_AS member_flags? T_STRING) diff --git a/src/parser/comment.js b/src/parser/comment.js index a8dd9e5c8..e2e905dfe 100644 --- a/src/parser/comment.js +++ b/src/parser/comment.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Comments with // or # or / * ... * / */ read_comment: function () { @@ -30,7 +30,7 @@ module.exports = { this.prev = prev; return result; }, - /** + /* * Comments with / ** ... * / */ read_doc_comment: function () { diff --git a/src/parser/expr.js b/src/parser/expr.js index ec8e46c14..dfd1f8b31 100644 --- a/src/parser/expr.js +++ b/src/parser/expr.js @@ -109,21 +109,21 @@ module.exports = { return expr; }, - /** + /* * Reads a cast expression */ read_expr_cast: function (type) { return this.node("cast")(type, this.text(), this.next().read_expr()); }, - /** + /* * Read a isset variable */ read_isset_variable: function () { return this.read_expr(); }, - /** + /* * Reads isset variables */ read_isset_variables: function () { @@ -192,7 +192,7 @@ module.exports = { return result; }, - /** + /* * Reads optional expression */ read_optional_expr: function (stopToken) { @@ -203,7 +203,7 @@ module.exports = { return null; }, - /** + /* * Reads exit expression */ read_exit_expr: function () { @@ -218,7 +218,7 @@ module.exports = { return expression; }, - /** + /* * ```ebnf * Reads an expression * expr ::= @todo @@ -508,7 +508,7 @@ module.exports = { return expr; }, - /** + /* * Recursively convert nested array to nested list. */ convertToList: function (array) { @@ -529,7 +529,7 @@ module.exports = { return node; }, - /** + /* * Reads assignment * @param {*} left */ @@ -548,7 +548,7 @@ module.exports = { return result("assignref", left, right); }, - /** + /* * * inline_function: * function returns_ref backup_doc_comment '(' parameter_list ')' lexical_vars return_type @@ -602,7 +602,7 @@ module.exports = { ); }, - /** + /* * ```ebnf * new_expr ::= T_NEW (namespace_name function_argument_list) | (T_CLASS ... class declaration) * ``` @@ -636,7 +636,7 @@ module.exports = { } return result(name, args); }, - /** + /* * Reads a class name * ```ebnf * read_new_class_name ::= namespace_name | variable diff --git a/src/parser/function.js b/src/parser/function.js index a9f79b75e..396acf343 100644 --- a/src/parser/function.js +++ b/src/parser/function.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * checks if current token is a reference keyword */ is_reference: function () { @@ -16,7 +16,7 @@ module.exports = { } return false; }, - /** + /* * checks if current token is a variadic keyword */ is_variadic: function () { @@ -26,7 +26,7 @@ module.exports = { } return false; }, - /** + /* * reading a function * ```ebnf * function ::= function_declaration code_block @@ -56,7 +56,7 @@ module.exports = { } return result; }, - /** + /* * reads a function declaration (without his body) * ```ebnf * function_declaration ::= T_FUNCTION '&'? T_STRING '(' parameter_list ')' @@ -154,7 +154,7 @@ module.exports = { return this.read_list(this.read_lexical_var, ","); }, - /** + /* * ```ebnf * lexical_var ::= '&'? T_VARIABLE * ``` @@ -169,7 +169,7 @@ module.exports = { this.next(); return result(name, false); }, - /** + /* * reads a list of parameters * ```ebnf * parameter_list ::= (parameter ',')* parameter? @@ -192,7 +192,7 @@ module.exports = { } return result; }, - /** + /* * ```ebnf * parameter ::= type? '&'? T_ELLIPSIS? T_VARIABLE ('=' expr)? * ``` @@ -227,7 +227,7 @@ module.exports = { } return node(parameterName, type, value, isRef, isVariadic, nullable); }, - /** + /* * Reads a list of arguments * ```ebnf * function_argument_list ::= '(' (argument_list (',' argument_list)*)? ')' @@ -242,7 +242,7 @@ module.exports = { this.expect(")") && this.next(); return result; }, - /** + /* * Reads non empty argument list */ read_non_empty_argument_list: function () { @@ -264,7 +264,7 @@ module.exports = { "," ); }, - /** + /* * ```ebnf * argument_list ::= T_ELLIPSIS? expr * ``` @@ -275,7 +275,7 @@ module.exports = { } return this.read_expr(); }, - /** + /* * read type hinting * ```ebnf * type ::= T_ARRAY | T_CALLABLE | namespace_name diff --git a/src/parser/if.js b/src/parser/if.js index b2a46ae5d..fda94ee19 100644 --- a/src/parser/if.js +++ b/src/parser/if.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads an IF statement * * ```ebnf @@ -48,7 +48,7 @@ module.exports = { } return result(test, body, alternate, shortForm); }, - /** + /* * reads an if expression : '(' expr ')' */ read_if_expr: function () { @@ -57,7 +57,7 @@ module.exports = { this.expect(")") && this.next(); return result; }, - /** + /* * reads an elseif (expr): statements */ read_elseif_short: function () { @@ -79,7 +79,7 @@ module.exports = { } return result(test, body(null, items), alternate, true); }, - /** + /* * */ read_else_short: function () { diff --git a/src/parser/loops.js b/src/parser/loops.js index e84e63d51..62847a6b8 100644 --- a/src/parser/loops.js +++ b/src/parser/loops.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a while statement * ```ebnf * while ::= T_WHILE (statement | ':' inner_statement_list T_ENDWHILE ';') @@ -31,7 +31,7 @@ module.exports = { } return result(test, body, shortForm); }, - /** + /* * Reads a do / while loop * ```ebnf * do ::= T_DO statement T_WHILE '(' expr ')' ';' @@ -53,7 +53,7 @@ module.exports = { } return result(test, body); }, - /** + /* * Read a for incremental loop * ```ebnf * for ::= T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement @@ -98,7 +98,7 @@ module.exports = { } return result(init, test, increment, body, shortForm); }, - /** + /* * Reads a foreach loop * ```ebnf * foreach ::= '(' expr T_AS foreach_variable (T_DOUBLE_ARROW foreach_variable)? ')' statement @@ -140,7 +140,7 @@ module.exports = { } return result(source, key, value, body, shortForm); }, - /** + /* * Reads a foreach variable statement * ```ebnf * foreach_variable = diff --git a/src/parser/main.js b/src/parser/main.js index ba5a5db1c..8b18dfff8 100644 --- a/src/parser/main.js +++ b/src/parser/main.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * ```ebnf * start ::= (namespace | top_statement)* * ``` diff --git a/src/parser/namespace.js b/src/parser/namespace.js index 2825fe337..57d1d3974 100644 --- a/src/parser/namespace.js +++ b/src/parser/namespace.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a namespace declaration block * ```ebnf * namespace ::= T_NAMESPACE namespace_name? '{' @@ -64,7 +64,7 @@ module.exports = { return result(name, body, false); } }, - /** + /* * Reads a namespace name * ```ebnf * namespace_name ::= T_NS_SEPARATOR? (T_STRING T_NS_SEPARATOR)* T_STRING @@ -97,7 +97,7 @@ module.exports = { } return result("name", names, relative); }, - /** + /* * Reads a use statement * ```ebnf * use_statement ::= T_USE @@ -127,7 +127,7 @@ module.exports = { this.expect(";") && this.next(); return result; }, - /** + /* * * @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1045 */ @@ -135,7 +135,7 @@ module.exports = { // resolved as the same return this.read_variable(true, false); }, - /** + /* * Reads a use declaration * ```ebnf * use_declaration ::= use_type? namespace_name use_alias @@ -151,7 +151,7 @@ module.exports = { const alias = this.read_use_alias(); return result(name.name, alias, type); }, - /** + /* * Reads a list of use declarations * ```ebnf * use_declarations ::= use_declaration (',' use_declaration)* @@ -181,7 +181,7 @@ module.exports = { } return result; }, - /** + /* * Reads a use statement * ```ebnf * use_alias ::= (T_AS T_STRING)? @@ -200,7 +200,7 @@ module.exports = { } return result; }, - /** + /* * Reads the namespace type declaration * ```ebnf * use_type ::= (T_FUNCTION | T_CONST)? diff --git a/src/parser/scalar.js b/src/parser/scalar.js index df1ed6153..4af5695c6 100644 --- a/src/parser/scalar.js +++ b/src/parser/scalar.js @@ -17,7 +17,7 @@ const specialChar = { }; module.exports = { - /** + /* * Unescape special chars */ resolve_special_chars: function (text, doubleQuote) { @@ -43,7 +43,7 @@ module.exports = { ); }, - /** + /* * Remove all leading spaces each line for heredoc text if there is a indentation * @param {string} text * @param {number} indentation @@ -86,7 +86,7 @@ module.exports = { return text.replace(removementRegExp, "\n"); }, - /** + /* * Check indentation level of heredoc in text, if mismatch, raiseError * @param {string} text * @param {number} indentation @@ -102,7 +102,7 @@ module.exports = { const textSize = text.length; let offset = 0; let leadingWhitespaceCharCount = 0; - /** + /* * @var inCoutingState {boolean} reset to true after a new line * @private */ @@ -150,7 +150,7 @@ module.exports = { } }, - /** + /* * Reads dereferencable scalar */ read_dereferencable_scalar: function () { @@ -197,7 +197,7 @@ module.exports = { return result; }, - /** + /* * ```ebnf * scalar ::= T_MAGIC_CONST * | T_LNUMBER | T_DNUMBER @@ -290,7 +290,7 @@ module.exports = { } } }, - /** + /* * Handles the dereferencing */ read_dereferencable: function (expr) { @@ -306,7 +306,7 @@ module.exports = { } return result; }, - /** + /* * Reads and extracts an encapsed item * ```ebnf * encapsed_string_item ::= T_ENCAPSED_AND_WHITESPACE @@ -422,7 +422,7 @@ module.exports = { this.lexer.heredoc_label.first_encaps_node = false; return encapsedPart(result, syntax, curly); }, - /** + /* * Reads an encapsed string */ read_encapsed_string: function (expect, isBinary = false) { @@ -478,7 +478,7 @@ module.exports = { } return node; }, - /** + /* * Constant token */ get_magic_constant: function () { diff --git a/src/parser/statement.js b/src/parser/statement.js index c3444a659..d75fd6504 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * reading a list of top statements (helper for top_statement*) * ```ebnf * top_statements ::= top_statement* @@ -26,7 +26,7 @@ module.exports = { } return result; }, - /** + /* * reading a top statement * ```ebnf * top_statement ::= @@ -71,7 +71,7 @@ module.exports = { return this.read_statement(); } }, - /** + /* * reads a list of simple inner statements (helper for inner_statement*) * ```ebnf * inner_statements ::= inner_statement* @@ -91,7 +91,7 @@ module.exports = { } return result; }, - /** + /* * Reads a list of constants declaration * ```ebnf * const_list ::= T_CONST T_STRING '=' expr (',' T_STRING '=' expr)* ';' @@ -117,7 +117,7 @@ module.exports = { false ); }, - /** + /* * Reads a list of constants declaration * ```ebnf * declare_list ::= IDENTIFIER '=' expr (',' IDENTIFIER '=' expr)* @@ -143,7 +143,7 @@ module.exports = { } return result; }, - /** + /* * reads a simple inner statement * ```ebnf * inner_statement ::= '{' inner_statements '}' | token @@ -178,7 +178,7 @@ module.exports = { return this.read_statement(); } }, - /** + /* * Reads statements */ read_statement: function () { @@ -404,7 +404,7 @@ module.exports = { } } }, - /** + /* * ```ebnf * code_block ::= '{' (inner_statements | top_statements) '}' * ``` diff --git a/src/parser/switch.js b/src/parser/switch.js index 1b927d602..07c1785e7 100644 --- a/src/parser/switch.js +++ b/src/parser/switch.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a switch statement * ```ebnf * switch ::= T_SWITCH '(' expr ')' switch_case_list @@ -24,7 +24,7 @@ module.exports = { const body = this.read_switch_case_list(); return result(test, body, shortForm); }, - /** + /* * ```ebnf * switch_case_list ::= '{' ';'? case_list* '}' | ':' ';'? case_list* T_ENDSWITCH ';' * ``` @@ -66,7 +66,7 @@ module.exports = { } return result(null, items); }, - /** + /* * ```ebnf * case_list ::= ((T_CASE expr) | T_DEFAULT) (':' | ';') inner_statement* * ``` diff --git a/src/parser/try.js b/src/parser/try.js index 404c2adde..bc2db96a5 100644 --- a/src/parser/try.js +++ b/src/parser/try.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * ```ebnf * try ::= T_TRY '{' inner_statement* '}' * ( diff --git a/src/parser/utils.js b/src/parser/utils.js index 6bbdd1a4f..e5f6e9193 100644 --- a/src/parser/utils.js +++ b/src/parser/utils.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a short form of tokens * @param {Number} token - The ending token * @return {Block} @@ -30,7 +30,7 @@ module.exports = { return body(null, items); }, - /** + /* * https://wiki.php.net/rfc/trailing-comma-function-calls * @param {*} item * @param {*} separator @@ -53,7 +53,7 @@ module.exports = { return result; }, - /** + /* * Helper : reads a list of tokens / sample : T_STRING ',' T_STRING ... * ```ebnf * list ::= separator? ( item separator )* item @@ -95,7 +95,7 @@ module.exports = { return result; }, - /** + /* * Reads a list of names separated by a comma * * ```ebnf @@ -114,7 +114,7 @@ module.exports = { return this.read_list(this.read_namespace_name, ",", false); }, - /** + /* * Reads the byref token and assign it to the specified node * @param {*} cb */ @@ -130,7 +130,7 @@ module.exports = { return result; }, - /** + /* * Reads a list of variables declarations * * ```ebnf diff --git a/src/parser/variable.js b/src/parser/variable.js index 1f3d17e23..f1ff5e540 100644 --- a/src/parser/variable.js +++ b/src/parser/variable.js @@ -6,7 +6,7 @@ "use strict"; module.exports = { - /** + /* * Reads a variable * * ```ebnf @@ -234,7 +234,7 @@ module.exports = { } return result; }, - /** + /* * https://github.com/php/php-src/blob/493524454d66adde84e00d249d607ecd540de99f/Zend/zend_language_parser.y#L1231 */ read_encaps_var_offset: function () { @@ -270,7 +270,7 @@ module.exports = { } return offset; }, - /** + /* * ```ebnf * reference_variable ::= simple_variable ('[' OFFSET ']')* | '{' EXPR '}' * ``` @@ -298,7 +298,7 @@ module.exports = { } return result; }, - /** + /* * ```ebnf * simple_variable ::= T_VARIABLE | '$' '{' expr '}' | '$' simple_variable * ``` From 930f07d016c6ff8d28e09fe97156ca4786b80ef9 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 21 Dec 2020 10:45:24 -0800 Subject: [PATCH 11/12] Fix "implicit any" warnings for lexer.js --- src/lexer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index 72f3141ae..f7449f601 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -387,8 +387,8 @@ Lexer.prototype.setState = function (state) { * prepend next token * @function Lexer#appendToken * @memberOf module:php-parser - * @param value - * @param ahead + * @param {*} value + * @param {*} ahead * @returns {Lexer} */ Lexer.prototype.appendToken = function (value, ahead) { @@ -442,7 +442,7 @@ Lexer.prototype.lex = function () { * activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) * @function Lexer#begin * @memberOf module:php-parser - * @param condition + * @param {*} condition * @returns {Lexer} */ Lexer.prototype.begin = function (condition) { From 05fcedda111f8d5b39b3d496ec58132467b394a0 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sun, 27 Dec 2020 17:58:52 -0800 Subject: [PATCH 12/12] Export Engine as non-default as well --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 6848c92b3..d3aaec4fa 100644 --- a/src/index.js +++ b/src/index.js @@ -233,6 +233,7 @@ module.exports.lexer = lexer; module.exports.AST = AST; module.exports.parser = parser; module.exports.combine = combine; +module.exports.Engine = Engine; // allow the default export in index.d.ts module.exports.default = Engine;