Top Description Fields Constructors Methods
uk.ac.ed.ph.snuggletex.internal

public final Class TokenFixer

extends Object
Class Inheritance
Imports
uk.ac.ed.ph.snuggletex.InputError, .SnuggleLogicException, .SnuggleSession, uk.ac.ed.ph.snuggletex.definitions.BuiltinCommand, .BuiltinEnvironment, .Command, .ComputedStyle, .CoreErrorCode, .CorePackageDefinitions, .LaTeXMode, .TextFlowContext, uk.ac.ed.ph.snuggletex.semantics.InterpretationType, .MathBracketInterpretation, .MathBracketInterpretation.BracketType, .MathNumberInterpretation, uk.ac.ed.ph.snuggletex.tokens.ArgumentContainerToken, .BraceContainerToken, .CommandToken, .EnvironmentToken, .ErrorToken, .FlowToken, .RootToken, .SimpleToken, .Token, .TokenType, java.util.ArrayList, .List, .Stack

This takes the parse tree after StyleEvaluator has run and performs grouping operations on the FlowTokens to convert them to a more tree-like structure that is easier to handle.

Once this has run, all BraceContainerToken tokens present in PARAGRAPH mode will have been unwrapped or flattened. They are however kept in MATH and LR mode as they can be used for implicit brackets. (Unwinding of redundantly nested containers is done in all cases, though.)

Many other types of tokens, such as the "new paragraph" or "new list item" token will have been removed, with the surrounding content grouped accordingly.

Author
David McKain
Version
$Revision$
See Also
LaTeXTokeniser, StyleEvaluator, StyleRebuilder, SnuggleSession#parseInput(uk.ac.ed.ph.snuggletex.SnuggleInput)

Field Summary

Modifier and TypeField and Description
private final SessionContext

Constructor Summary

AccessConstructor and Description
public
TokenFixer(final SessionContext sessionContext)

Method Summary

Modifier and TypeMethod and Description
private CommandToken

Returns:

null if the builder is empty, otherwise grouped CommandToken
buildGroupedCommandToken
(final Token parentToken, final BuiltinCommand command, final List<? extends FlowToken> itemBuilder, final ComputedStyle computedStyle)

Useful helper.

private ErrorToken
createError(final FlowToken token, final CoreErrorCode errorCode, final Object... arguments)

private void
fencePairedParentheses(Token parentToken, List<FlowToken> tokens)

This looks for an outermost pairs of \left and \right and converts them to a more tree-like fence token.

private void
fixLeadingNegativeNumber(List<FlowToken> tokens)

Converts leading occurrences of '-' followed by a MathNumberInterpretation into a single token representing the negation of the given number.

private void
fixListEnvironmentContent(EnvironmentToken environmentToken)

Helper to fix the representation of a list environment.

private void
fixOverInstances(Token parentToken, List<FlowToken> tokens)

This handles the old-fashioned "...

private void
fixPrimes(List<FlowToken> tokens)

Hunts through tokens for occurrences of primes used as f', which are converted to a superscript by binding to the preceding token

private void
fixSubscriptAndSuperscripts(Token parentToken, List<FlowToken> tokens)

We'll look for any occurrences of T1_T2[^T3] or T1^T2[_T3], where [..] denotes "optional".

private void
fixTabularEnvironmentContent(EnvironmentToken environmentToken)

Helper to fix the content of tabular environments to make it more clearly demarcated as rows and columns.

public void
private void
private void
inferParagraphs(List<FlowToken> tokens)

Infers explicit paragraphs by searching for the traditional LaTeX TokenType#NEW_PARAGRAPH and/or CorePackageDefinitions#CMD_PAR tokens, replacing with the more tree-like CorePackageDefinitions#CMD_PARAGRAPH.

private void
inferParenthesisFences(Token parentToken, List<FlowToken> tokens)

This attempts to group (not necessarily matching) pairs of open and close brackets into a corresponding CorePackageDefinitions#ENV_BRACKETED environment that's easier to handle.

private void
replaceToken(List<FlowToken> tokens, final int index, FlowToken replacementToken)

private void
replaceTokens(List<FlowToken> tokens, final int startIndex, final int endIndex, FlowToken replacementToken)

Replaces elements in the tokens List with the given replacement Token, starting at the given startIndex (inclusive) up to endIndex (exclusive).

private void
stripBlocks(List<FlowToken> tokens)

LR Mode doesn't let "block" stuff do its normal thing.

private void
stripRedundantWhitespaceTokens(List<FlowToken> tokens)

Strips out redundant whitespace text tokens at the start and end of a List of siblings and between a pair of "block" tokens.

private void
private void
visitCommand(CommandToken commandToken)

private void
private void
private void
visitSiblings(Token parent, List<FlowToken> content)

private void
private void
visitSiblingsMathMode(Token parentToken, List<FlowToken> tokens)

private void
private void
visitToken(Token startToken)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionContextback to summary
private final SessionContext sessionContext

Constructor Detail

TokenFixerback to summary
public TokenFixer(final SessionContext sessionContext)

Method Detail

buildGroupedCommandTokenback to summary
private CommandToken buildGroupedCommandToken(final Token parentToken, final BuiltinCommand command, final List<? extends FlowToken> itemBuilder, final ComputedStyle computedStyle)

Useful helper. Takes a "builder" that has been accumulating tokens. Groups all tokens into a CommandToken containing the accumulated tokens as a single argument and clears the builder.

An empty group is legal and will result in a "fake" empty token assuming the same Slice and LaTeXMode as parentToken.

Returns:CommandToken

null if the builder is empty, otherwise grouped CommandToken

createErrorback to summary
private ErrorToken createError(final FlowToken token, final CoreErrorCode errorCode, final Object... arguments) throws SnuggleParseException
fencePairedParenthesesback to summary
private void fencePairedParentheses(Token parentToken, List<FlowToken> tokens) throws SnuggleParseException

This looks for an outermost pairs of \left and \right and converts them to a more tree-like fence token.

Mismatched pairs will cause an error, as they do in LaTeX.

See Also
inferParenthesisFences(Token, List)
fixLeadingNegativeNumberback to summary
private void fixLeadingNegativeNumber(List<FlowToken> tokens)

Converts leading occurrences of '-' followed by a MathNumberInterpretation into a single token representing the negation of the given number.

fixListEnvironmentContentback to summary
private void fixListEnvironmentContent(EnvironmentToken environmentToken) throws SnuggleParseException

Helper to fix the representation of a list environment. The original content should be of the form: \item ..... \item ..... \item ...... (and maybe include CorePackageDefinitions#CMD_LIST_ITEMs as well) We replace with a more tree-like version. If \item is used outside a suitable environment, then it will be left as-is. The DOMBuilder will cope with this in due course.

fixOverInstancesback to summary
private void fixOverInstances(Token parentToken, List<FlowToken> tokens) throws SnuggleParseException

This handles the old-fashioned "... \over ..." by refactoring the tokens into a \frac{...}{...}. As with LaTeX, we only allow one \over in a single level.

fixPrimesback to summary
private void fixPrimes(List<FlowToken> tokens)

Hunts through tokens for occurrences of primes used as f', which are converted to a superscript by binding to the preceding token

fixSubscriptAndSuperscriptsback to summary
private void fixSubscriptAndSuperscripts(Token parentToken, List<FlowToken> tokens) throws SnuggleParseException

We'll look for any occurrences of T1_T2[^T3] or T1^T2[_T3], where [..] denotes "optional". If found, we'll replace with the more tree-like equivalents: \msub{T1}{T2} \msup{T1}{T2} \msubsup{T1}{T2}{T3} which will be easier to handle later on.

fixTabularEnvironmentContentback to summary
private void fixTabularEnvironmentContent(EnvironmentToken environmentToken) throws SnuggleParseException

Helper to fix the content of tabular environments to make it more clearly demarcated as rows and columns. This kills off instances of CorePackageDefinitions#CMD_CHAR_BACKSLASH and TokenType#TAB_CHARACTER within tables/arrays and replaces then with zero or more CorePackageDefinitions#CMD_TABLE_ROW each containing zero or more CorePackageDefinitions#CMD_TABLE_COLUMN.

fixTokenTreeback to summary
public void fixTokenTree(RootToken rootToken) throws SnuggleParseException
flattenBraceContainersback to summary
private void flattenBraceContainers(List<FlowToken> tokens)
inferParagraphsback to summary
private void inferParagraphs(List<FlowToken> tokens)

Infers explicit paragraphs by searching for the traditional LaTeX TokenType#NEW_PARAGRAPH and/or CorePackageDefinitions#CMD_PAR tokens, replacing with the more tree-like CorePackageDefinitions#CMD_PARAGRAPH.

inferParenthesisFencesback to summary
private void inferParenthesisFences(Token parentToken, List<FlowToken> tokens)

This attempts to group (not necessarily matching) pairs of open and close brackets into a corresponding CorePackageDefinitions#ENV_BRACKETED environment that's easier to handle.

It also handles brackets which are not correctly nested, using fake empty opener/closer delimiters at the start/end of the group as required.

Note that this means that while $[1,2)$ will successfully be matched, the notation $[1,2[$ will *not* be considered matched, even though it is common in subjects like Mathematical Analysis. To get the correct semantics here, you must use $\left[1,2\right[$ and let fencePairedParentheses(Token, List) take care of this for you.

See Also
fencePairedParentheses(Token, List)
replaceTokenback to summary
private void replaceToken(List<FlowToken> tokens, final int index, FlowToken replacementToken)
replaceTokensback to summary
private void replaceTokens(List<FlowToken> tokens, final int startIndex, final int endIndex, FlowToken replacementToken)

Replaces elements in the tokens List with the given replacement Token, starting at the given startIndex (inclusive) up to endIndex (exclusive).

The computed style of the replacement token is set to be that of the first token it replaces.

stripBlocksback to summary
private void stripBlocks(List<FlowToken> tokens) throws SnuggleParseException

LR Mode doesn't let "block" stuff do its normal thing. We strip out any "new paragraph" markers and fail on any other kind of block tokens.

stripRedundantWhitespaceTokensback to summary
private void stripRedundantWhitespaceTokens(List<FlowToken> tokens)

Strips out redundant whitespace text tokens at the start and end of a List of siblings and between a pair of "block" tokens.

visitChildrenback to summary
private void visitChildren(List<FlowToken> tokens) throws SnuggleParseException
visitCommandback to summary
private void visitCommand(CommandToken commandToken) throws SnuggleParseException
visitContainerContentback to summary
private void visitContainerContent(ArgumentContainerToken parent) throws SnuggleParseException
visitEnvironmentback to summary
private void visitEnvironment(EnvironmentToken environmentToken) throws SnuggleParseException
visitSiblingsback to summary
private void visitSiblings(Token parent, List<FlowToken> content) throws SnuggleParseException
visitSiblingsLRModeback to summary
private void visitSiblingsLRMode(List<FlowToken> tokens) throws SnuggleParseException
visitSiblingsMathModeback to summary
private void visitSiblingsMathMode(Token parentToken, List<FlowToken> tokens) throws SnuggleParseException
visitSiblingsParagraphModeback to summary
private void visitSiblingsParagraphMode(List<FlowToken> tokens) throws SnuggleParseException
visitTokenback to summary
private void visitToken(Token startToken) throws SnuggleParseException