class Parser

Constants

RULE_AUTO_CLOSE

RULE_AUTO_REOPEN

RULE_BREAK_PARAGRAPH

RULE_CREATE_PARAGRAPHS

RULE_DISABLE_AUTO_BR

RULE_ENABLE_AUTO_BR

RULE_IGNORE_TAGS

RULE_IGNORE_TEXT

RULE_IGNORE_WHITESPACE

RULE_IS_TRANSPARENT

RULE_PREVENT_BR

RULE_SUSPEND_AUTO_BR

RULE_TRIM_FIRST_LINE

RULES_AUTO_LINEBREAKS

Bitwise disjunction of rules related to automatic line breaks

RULES_INHERITANCE

Bitwise disjunction of rules that are inherited by subcontexts

WHITESPACE

All the characters that are considered whitespace

Properties

protected array $cntOpen
protected array $cntTotal
protected array $context
protected int $currentFixingCost
protected Tag $currentTag
protected bool $isRich
protected Logger $logger
int $maxFixingCost
protected array $namespaces
protected array $openTags
protected string $output
protected int $pos
protected array $pluginParsers
protected array $pluginsConfig
array $registeredVars
protected array $rootContext
protected array $tagsConfig
protected array $tagStack
protected bool $tagStackIsSorted
protected string $text
protected int $textLen
protected int $uid
protected int $wsPos

Methods

__construct(array $config)

Constructor

array
__sleep()

Serializer

void
__wakeup()

Unserializer

void
reset(string $text)

Reset the parser for a new parsing

void
setTagOption(string $tagName, string $optionName, mixed $optionValue)

Set a tag's option

void
disableTag(string $tagName)

Disable a tag

void
enableTag(string $tagName)

Enable a tag

getLogger()

Get this parser's Logger instance

string
getText()

Return the last text parsed

string
parse(string $text)

Parse a text

void
setTagLimit(string $tagName, int $tagLimit)

Change a tag's tagLimit

void
setNestingLimit(string $tagName, int $nestingLimit)

Change a tag's nestingLimit

void
finalizeOutput()

Finalize the output by appending the rest of the unprocessed text and create the root node

void
outputTag(Tag $tag)

Append a tag to the output

void
outputText(int $catchupPos, int $maxLines, bool $closeParagraph)

Output the text between the cursor's position (included) and given position (not included)

void
outputBrTag(Tag $tag)

Output a linebreak tag

void
outputIgnoreTag(Tag $tag)

Output an ignore tag

void
outputParagraphStart(int $maxPos)

Start a paragraph between current position and given position, if applicable

void
outputParagraphEnd()

Close current paragraph at current position if applicable

void
outputVerbatim(Tag $tag)

Output the content of a verbatim tag

void
outputWhitespace(int $maxPos)

Skip as much whitespace after current position as possible

void
disablePlugin(string $pluginName)

Disable a plugin

void
enablePlugin(string $pluginName)

Enable a plugin

void
executePluginParser(string $pluginName)

Execute given plugin

void
executePluginParsers()

Execute all the plugins

array
getMatches(string $regexp, int $limit)

Execute given regexp and returns as many matches as given limit

callable
getPluginParser(string $pluginName)

Get the cached callback for given plugin's parser

void
registerParser(string $pluginName, callable $parser, string $regexp = null, int $limit = PHP_INT_MAX)

Register a parser

bool
closeAncestor(Tag $tag)

Apply closeAncestor rules associated with given tag

bool
closeParent(Tag $tag)

Apply closeParent rules associated with given tag

void
createChild(Tag $tag)

Apply the createChild rules associated with given tag

bool
fosterParent(Tag $tag)

Apply fosterParent rules associated with given tag

bool
requireAncestor(Tag $tag)

Apply requireAncestor rules associated with given tag

void
addFosterTag(Tag $tag, Tag $fosterTag)

Create and add a copy of a tag as a child of a given tag

Tag
addMagicEndTag(Tag $startTag, int $tagPos, int $prio = 0)

Create and add an end tag for given start tag at given position

int
getMagicEndPos(int $tagPos)

Compute the position of a magic end tag, adjusted for whitespace

int[]
getMagicStartCoords(int $tagPos)

Compute the position and priority of a magic start tag, adjusted for whitespace

bool
isFollowedByClosingTag(Tag $tag)

Test whether given start tag is immediately followed by a closing tag

void
processTags()

Process all tags in the stack

void
processCurrentTag()

Process current tag

void
processStartTag(Tag $tag)

Process given start tag (including self-closing tags) at current position

void
processEndTag(Tag $tag)

Process given end tag at current position

void
popContext()

Update counters and replace current context with its parent context

void
pushContext(Tag $tag)

Update counters and replace current context with a new context based on given tag

bool
tagIsAllowed(string $tagName)

Return whether given tag is allowed in current context

Tag
addStartTag(string $name, int $pos, int $len, int $prio = 0)

Add a start tag

Tag
addEndTag(string $name, int $pos, int $len, int $prio = 0)

Add an end tag

Tag
addSelfClosingTag(string $name, int $pos, int $len, int $prio = 0)

Add a self-closing tag

Tag
addBrTag(int $pos, int $prio = 0)

Add a 0-width "br" tag to force a line break at given position

Tag
addIgnoreTag(int $pos, int $len, int $prio = 0)

Add an "ignore" tag

Tag
addParagraphBreak(int $pos, int $prio = 0)

Add a paragraph break at given position

Tag
addCopyTag(Tag $tag, int $pos, int $len, int $prio = null)

Add a copy of given tag at given position and length

Tag
addTag(int $type, string $name, int $pos, int $len, int $prio)

Add a tag

bool
isInvalidTextSpan(int $pos, int $len)

Test whether given text span is outside text boundaries or an invalid UTF sequence

void
insertTag(Tag $tag)

Insert given tag in the tag stack

Tag
addTagPair(string $name, int $startPos, int $startLen, int $endPos, int $endLen, int $prio = 0)

Add a pair of tags

Tag
addVerbatim(int $pos, int $len, int $prio = 0)

Add a tag that represents a verbatim copy of the original text

void
sortTags()

Sort tags by position and precedence

string
getSortKey(Tag $tag, int $tagIndex = 0)

Generate a key for given tag that can be used to compare its position using lexical comparisons

Details

at line 170
__construct(array $config)

Constructor

Parameters

array $config

at line 190
array __sleep()

Serializer

Returns the properties that need to persist through serialization.

NOTE: using __sleep() is preferable to implementing Serializable because it leaves the choice of the serializer to the user (e.g. igbinary)

Return Value

array

at line 200
void __wakeup()

Unserializer

Return Value

void

at line 211
protected void reset(string $text)

Reset the parser for a new parsing

Parameters

string $text

Text to be parsed

Return Value

void

at line 261
protected void setTagOption(string $tagName, string $optionName, mixed $optionValue)

Set a tag's option

This method ensures that the tag's config is a value and not a reference, to prevent potential side-effects. References contained inside the tag's config are left untouched

Parameters

string $tagName

Tag's name

string $optionName

Option's name

mixed $optionValue

Option's value

Return Value

void

at line 285
void disableTag(string $tagName)

Disable a tag

Parameters

string $tagName

Name of the tag

Return Value

void

at line 296
void enableTag(string $tagName)

Enable a tag

Parameters

string $tagName

Name of the tag

Return Value

void

at line 309
Logger getLogger()

Get this parser's Logger instance

Return Value

Logger

at line 326
string getText()

Return the last text parsed

This method returns the normalized text, which may be slightly different from the original text in that EOLs are normalized to LF and other control codes are stripped. This method is meant to be used in support of processing log entries, which contain offsets based on the normalized text

Return Value

string

See also

Parser::reset

at line 337
string parse(string $text)

Parse a text

Parameters

string $text

Text to parse

Return Value

string

XML representation

at line 374
void setTagLimit(string $tagName, int $tagLimit)

Change a tag's tagLimit

NOTE: the default tagLimit should generally be set during configuration instead

Parameters

string $tagName

The tag's name, in UPPERCASE

int $tagLimit

Return Value

void

at line 388
void setNestingLimit(string $tagName, int $nestingLimit)

Change a tag's nestingLimit

NOTE: the default nestingLimit should generally be set during configuration instead

Parameters

string $tagName

The tag's name, in UPPERCASE

int $nestingLimit

Return Value

void

at line 402
protected void finalizeOutput()

Finalize the output by appending the rest of the unprocessed text and create the root node

Return Value

void

at line 445
protected void outputTag(Tag $tag)

Append a tag to the output

Parameters

Tag $tag

Tag to append

Return Value

void

at line 559
protected void outputText(int $catchupPos, int $maxLines, bool $closeParagraph)

Output the text between the cursor's position (included) and given position (not included)

Parameters

int $catchupPos

Position we're catching up to

int $maxLines

Maximum number of lines to ignore at the end of the text

bool $closeParagraph

Whether to close the paragraph at the end, if applicable

Return Value

void

at line 717
protected void outputBrTag(Tag $tag)

Output a linebreak tag

Parameters

Tag $tag

Return Value

void

at line 729
protected void outputIgnoreTag(Tag $tag)

Output an ignore tag

Parameters

Tag $tag

Return Value

void

at line 752
protected void outputParagraphStart(int $maxPos)

Start a paragraph between current position and given position, if applicable

Parameters

int $maxPos

Rightmost position at which the paragraph can be opened

Return Value

void

at line 777
protected void outputParagraphEnd()

Close current paragraph at current position if applicable

Return Value

void

at line 795
protected void outputVerbatim(Tag $tag)

Output the content of a verbatim tag

Parameters

Tag $tag

Return Value

void

at line 809
protected void outputWhitespace(int $maxPos)

Skip as much whitespace after current position as possible

Parameters

int $maxPos

Rightmost character to be skipped

Return Value

void

at line 833
void disablePlugin(string $pluginName)

Disable a plugin

Parameters

string $pluginName

Name of the plugin

Return Value

void

at line 853
void enablePlugin(string $pluginName)

Enable a plugin

Parameters

string $pluginName

Name of the plugin

Return Value

void

at line 867
protected void executePluginParser(string $pluginName)

Execute given plugin

Parameters

string $pluginName

Plugin's name

Return Value

void

at line 894
protected void executePluginParsers()

Execute all the plugins

Return Value

void

at line 912
protected array getMatches(string $regexp, int $limit)

Execute given regexp and returns as many matches as given limit

Parameters

string $regexp
int $limit

Return Value

array

at line 929
protected callable getPluginParser(string $pluginName)

Get the cached callback for given plugin's parser

Parameters

string $pluginName

Plugin's name

Return Value

callable

at line 958
void registerParser(string $pluginName, callable $parser, string $regexp = null, int $limit = PHP_INT_MAX)

Register a parser

Can be used to add a new parser with no plugin config, or pre-generate a parser for an existing plugin

Parameters

string $pluginName
callable $parser
string $regexp
int $limit

Return Value

void

at line 987
protected bool closeAncestor(Tag $tag)

Apply closeAncestor rules associated with given tag

Parameters

Tag $tag Tag

Return Value

bool

Whether a new tag has been added

at line 1028
protected bool closeParent(Tag $tag)

Apply closeParent rules associated with given tag

Parameters

Tag $tag Tag

Return Value

bool

Whether a new tag has been added

at line 1064
protected void createChild(Tag $tag)

Apply the createChild rules associated with given tag

Parameters

Tag $tag Tag

Return Value

void

at line 1091
protected bool fosterParent(Tag $tag)

Apply fosterParent rules associated with given tag

NOTE: this rule has the potential for creating an unbounded loop, either if a tag tries to foster itself or two or more tags try to foster each other in a loop. We mitigate the risk by preventing a tag from creating a child of itself (the parent still gets closed) and by checking and increasing the currentFixingCost so that a loop of multiple tags do not run indefinitely. The default tagLimit and nestingLimit also serve to prevent the loop from running indefinitely

Parameters

Tag $tag Tag

Return Value

bool

Whether a new tag has been added

at line 1134
protected bool requireAncestor(Tag $tag)

Apply requireAncestor rules associated with given tag

Parameters

Tag $tag Tag

Return Value

bool

Whether this tag has an unfulfilled requireAncestor requirement

at line 1171
protected void addFosterTag(Tag $tag, Tag $fosterTag)

Create and add a copy of a tag as a child of a given tag

Parameters

Tag $tag

Current tag

Tag $fosterTag

Tag to foster

Return Value

void

at line 1188
protected Tag addMagicEndTag(Tag $startTag, int $tagPos, int $prio = 0)

Create and add an end tag for given start tag at given position

Parameters

Tag $startTag

Start tag

int $tagPos

End tag's position (will be adjusted for whitespace if applicable)

int $prio

End tag's priority

Return Value

Tag

at line 1211
protected int getMagicEndPos(int $tagPos)

Compute the position of a magic end tag, adjusted for whitespace

Parameters

int $tagPos

Rightmost possible position for the tag

Return Value

int

at line 1229
protected int[] getMagicStartCoords(int $tagPos)

Compute the position and priority of a magic start tag, adjusted for whitespace

Parameters

int $tagPos

Leftmost possible position for the tag

Return Value

int[]

[Tag pos, priority]

at line 1262
protected bool isFollowedByClosingTag(Tag $tag)

Test whether given start tag is immediately followed by a closing tag

Parameters

Tag $tag

Start tag

Return Value

bool

at line 1272
protected void processTags()

Process all tags in the stack

Return Value

void

at line 1317
protected void processCurrentTag()

Process current tag

Return Value

void

at line 1410
protected void processStartTag(Tag $tag)

Process given start tag (including self-closing tags) at current position

Parameters

Tag $tag

Start tag (including self-closing)

Return Value

void

at line 1530
protected void processEndTag(Tag $tag)

Process given end tag at current position

Parameters

Tag $tag

end tag

Return Value

void

at line 1694
protected void popContext()

Update counters and replace current context with its parent context

Return Value

void

at line 1709
protected void pushContext(Tag $tag)

Update counters and replace current context with a new context based on given tag

If given tag is a self-closing tag, the context won't change

Parameters

Tag $tag

Start tag (including self-closing)

Return Value

void

at line 1761
protected bool tagIsAllowed(string $tagName)

Return whether given tag is allowed in current context

Parameters

string $tagName

Return Value

bool

at line 1781
Tag addStartTag(string $name, int $pos, int $len, int $prio = 0)

Add a start tag

Parameters

string $name

Name of the tag

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1795
Tag addEndTag(string $name, int $pos, int $len, int $prio = 0)

Add an end tag

Parameters

string $name

Name of the tag

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1809
Tag addSelfClosingTag(string $name, int $pos, int $len, int $prio = 0)

Add a self-closing tag

Parameters

string $name

Name of the tag

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1821
Tag addBrTag(int $pos, int $prio = 0)

Add a 0-width "br" tag to force a line break at given position

Parameters

int $pos

Position of the tag in the text

int $prio

Tag's priority

Return Value

Tag

at line 1834
Tag addIgnoreTag(int $pos, int $len, int $prio = 0)

Add an "ignore" tag

Parameters

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1848
Tag addParagraphBreak(int $pos, int $prio = 0)

Add a paragraph break at given position

Uses a zero-width tag that is actually never output in the result

Parameters

int $pos

Position of the tag in the text

int $prio

Tag's priority

Return Value

Tag

at line 1862
Tag addCopyTag(Tag $tag, int $pos, int $len, int $prio = null)

Add a copy of given tag at given position and length

Parameters

Tag $tag

Original tag

int $pos

Copy's position

int $len

Copy's length

int $prio

Copy's priority (same as original by default)

Return Value

Tag

Copy tag

at line 1884
protected Tag addTag(int $type, string $name, int $pos, int $len, int $prio)

Add a tag

Parameters

int $type

Tag's type

string $name

Name of the tag

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1928
protected bool isInvalidTextSpan(int $pos, int $len)

Test whether given text span is outside text boundaries or an invalid UTF sequence

Parameters

int $pos

Start of text

int $len

Length of text

Return Value

bool

at line 1939
protected void insertTag(Tag $tag)

Insert given tag in the tag stack

Parameters

Tag $tag

Return Value

void

at line 1970
Tag addTagPair(string $name, int $startPos, int $startLen, int $endPos, int $endLen, int $prio = 0)

Add a pair of tags

Parameters

string $name

Name of the tags

int $startPos

Position of the start tag

int $startLen

Length of the start tag

int $endPos

Position of the start tag

int $endLen

Length of the start tag

int $prio

Start tag's priority (the end tag will be set to minus that value)

Return Value

Tag

Start tag

at line 1988
Tag addVerbatim(int $pos, int $len, int $prio = 0)

Add a tag that represents a verbatim copy of the original text

Parameters

int $pos

Position of the tag in the text

int $len

Length of text consumed by the tag

int $prio

Tag's priority

Return Value

Tag

at line 1998
protected void sortTags()

Sort tags by position and precedence

Return Value

void

at line 2025
protected string getSortKey(Tag $tag, int $tagIndex = 0)

Generate a key for given tag that can be used to compare its position using lexical comparisons

Tags are sorted by position first, then by priority, then by whether they consume any text, then by length, and finally in order of their creation.

The stack's array is in reverse order. Therefore, tags that appear at the start of the text are at the end of the array.

Parameters

Tag $tag
int $tagIndex

Return Value

string