donatj/printf-parser

PHP printf语法兼容的printf字符串解析器。将printf字符串解析为词法流。

v0.4.0 2024-08-21 17:03 UTC

README

Latest Stable Version Total Downloads License ci.yml Scrutinizer Code Quality Code Coverage

PHP printf语法兼容的printf字符串解析器。

将printf字符串解析为词法流。

要求

  • php: >=7.2

安装

使用以下命令安装最新版本

composer require 'donatj/printf-parser'

示例

以下是一个简单的示例

<?php

require __DIR__ . '/../vendor/autoload.php';

$emitter = new \donatj\Printf\LexemeEmitter;
$parser  = new \donatj\Printf\Parser($emitter);

$parser->parseStr('percent of %s: %d%%');

$lexemes = $emitter->getLexemes();

foreach( $lexemes as $lexeme ) {
	echo $lexeme->getLexItemType() . ' -> ';
	echo var_export($lexeme->getVal(), true);

	if( $lexeme instanceof \donatj\Printf\ArgumentLexeme ) {
		echo ' arg type: ' . $lexeme->argType();
	}

	echo PHP_EOL;
}

输出

! -> 'percent of '
s -> 's' arg type: string
! -> ': '
d -> 'd' arg type: int
! -> '%'

文档

类: donatj\Printf\Parser

解析器实现了PHP Printf兼容的Printf字符串解析器。

方法: Parser->__construct

function __construct(\donatj\Printf\Emitter $emitter)

解析器构造函数。

参数
  • \donatj\Printf\Emitter $emitter - 要解析词法并作为解析结果输出的给定发射器

方法: Parser->parseStr

function parseStr(string $string) : void

解析printf字符串并将解析后的词法输出到配置的发射器

类: donatj\Printf\LexemeEmitter

方法: LexemeEmitter->getLexemes

function getLexemes() : \donatj\Printf\LexemeCollection

返回发射器接收到的不可变词法集合

类: donatj\Printf\LexemeCollection

LexemeCollection是一个具有ArrayAccess的不可变词法迭代集合

方法: LexemeCollection->getInvalid

function getInvalid() : ?\donatj\Printf\Lexeme

检索第一个无效词法或null(如果所有词法都有效)。

这在检查printf字符串是否解析无错时很有用。

方法: LexemeCollection->toArray

function toArray() : array

获取词法集合作为有序词法的数组

返回值
  • \donatj\Printf\Lexeme[]

方法: LexemeCollection->argTypes

function argTypes() : array
返回预期参数列表,以下是一个1索引映射
ArgumentLexeme::ARG_TYPE_MISSING  
ArgumentLexeme::ARG_TYPE_INT  
ArgumentLexeme::ARG_TYPE_DOUBLE  
ArgumentLexeme::ARG_TYPE_STRING  
返回值
  • string[]

类: donatj\Printf\Lexeme

Lexeme代表printf字符串的“基本”组件 - 字面字符串“!”或无效词法

<?php
namespace donatj\Printf;

class Lexeme {
	public const T_INVALID = '';
	public const T_LITERAL_STRING = '!';
}

方法: Lexeme->__construct

function __construct(string $lexItemType, string $val, int $pos)

LexItem构造函数。

方法: Lexeme->getLexItemType

function getLexItemType() : string

printf词法的类型

方法: Lexeme->getVal

function getVal() : string

词法的文本

方法: Lexeme->getPos

function getPos() : int

给定词法的字符串位置

类: donatj\Printf\ArgumentLexeme

<?php
namespace donatj\Printf;

class ArgumentLexeme {
	/** @var string the argument is treated as an integer and presented as a binary number. */
	public const T_INT_AS_BINARY = 'b';
	/** @var string the argument is treated as an integer and presented as the character with that ASCII value. */
	public const T_INT_AS_CHARACTER = 'c';
	/** @var string the argument is treated as an integer and presented as a (signed) decimal number. */
	public const T_INT = 'd';
	/** @var string the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the
number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of
significant digits (one less). */
	public const T_DOUBLE_AS_SCI = 'e';
	/** @var string like %e but uses uppercase letter (e.g. 1.2E+2). */
	public const T_DOUBLE_AS_SCI_CAP = 'E';
	/** @var string the argument is treated as a float and presented as a floating-point number (locale aware). */
	public const T_FLOAT_LOCALE = 'f';
	/** @var string the argument is treated as a float and presented as a floating-point number (non-locale aware).
Available since PHP 5.0.3. */
	public const T_FLOAT_NO_LOCALE = 'F';
	/** @var string shorter of %e and %f. */
	public const T_FLOAT_AUTO_SCI = 'g';
	/** @var string shorter of %E and %F. */
	public const T_FLOAT_AUTO_SCI_CAP = 'G';
	/** @var string the argument is treated as an integer and presented as an octal number. */
	public const T_INT_AS_OCTAL = 'o';
	/** @var string the argument is treated as and presented as a string. */
	public const T_STRING = 's';
	/** @var string the argument is treated as an integer and presented as an unsigned decimal number. */
	public const T_INT_UNSIGNED = 'u';
	/** @var string the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). */
	public const T_INT_HEX = 'x';
	/** @var string the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). */
	public const T_INT_HEX_CAP = 'X';
	public const VALID_T_TYPES = [self::T_INT_AS_BINARY, self::T_INT_AS_CHARACTER, self::T_INT, self::T_DOUBLE_AS_SCI, self::T_DOUBLE_AS_SCI_CAP, self::T_FLOAT_LOCALE, self::T_FLOAT_NO_LOCALE, self::T_FLOAT_AUTO_SCI, self::T_FLOAT_AUTO_SCI_CAP, self::T_INT_AS_OCTAL, self::T_STRING, self::T_INT_UNSIGNED, self::T_INT_HEX, self::T_INT_HEX_CAP];
	public const ARG_TYPE_MISSING = '';
	public const ARG_TYPE_INT = 'int';
	public const ARG_TYPE_DOUBLE = 'float';
	public const ARG_TYPE_STRING = 'string';
	/** @var string[] string    s */
	public const STRING_TYPES = [self::T_STRING];
	/** @var string[] integer    d, u, c, o, x, X, b */
	public const INTEGER_TYPES = [self::T_INT, self::T_INT_UNSIGNED, self::T_INT_AS_CHARACTER, self::T_INT_AS_OCTAL, self::T_INT_HEX, self::T_INT_HEX_CAP, self::T_INT_AS_BINARY];
	/** @var string[] double    g, G, e, E, f, F */
	public const DOUBLE_TYPES = [self::T_FLOAT_AUTO_SCI, self::T_FLOAT_AUTO_SCI_CAP, self::T_DOUBLE_AS_SCI, self::T_DOUBLE_AS_SCI_CAP, self::T_FLOAT_LOCALE, self::T_FLOAT_NO_LOCALE];
	public const T_INVALID = '';
	public const T_LITERAL_STRING = '!';
}

方法: ArgumentLexeme->__construct

function __construct(string $lexItemType, string $val, int $pos, ?int $arg, bool $showPositive, ?string $padChar, ?int $padWidth, bool $leftJustified, ?int $precision)

ArgumentLexeme构造函数。

LexItem构造函数。

方法: ArgumentLexeme->getArg

function getArg() : ?int

位置指定符,如%3$s返回3,%s返回null

返回值
  • int | null - 未指定时返回null

方法: ArgumentLexeme->getShowPositive

function getShowPositive() : bool

是否启用“前缀正数以加号+”标志

方法: ArgumentLexeme->getPadChar

function getPadChar() : ?string

指定的填充字符标志

返回值
  • string | null - 未指定时返回null

方法: ArgumentLexeme->getPadWidth

function getPadWidth() : ?int

指定的填充宽度

返回值
  • int | null - 未指定时返回null

方法: ArgumentLexeme->getLeftJustified

function getLeftJustified() : bool

是否启用了左对齐标志?

方法: ArgumentLexeme->getPrecision

function getPrecision() : ?int

词法指示的精度。

返回值
  • int | null - 未指定时返回null

方法: ArgumentLexeme->argType

function argType() : string

根据参数类型返回以下之一

ArgumentLexeme::ARG_TYPE_MISSING
ArgumentLexeme::ARG_TYPE_INT
ArgumentLexeme::ARG_TYPE_DOUBLE
ArgumentLexeme::ARG_TYPE_STRING

方法: ArgumentLexeme->getLexItemType

function getLexItemType() : string

printf词法的类型

方法: ArgumentLexeme->getVal

function getVal() : string

词法的文本

方法: ArgumentLexeme->getPos

function getPos() : int

给定词法的字符串位置