Class DocumentParser
- java.lang.Object
-
- com.vladsch.flexmark.parser.internal.DocumentParser
-
- All Implemented Interfaces:
BlockParserTracker,ParserState,BlockTracker
public class DocumentParser extends java.lang.Object implements ParserState
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classDocumentParser.ParagraphPreProcessorCache
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<BlockParser>activeBlockParsersprivate booleanblankprivate booleanblankLinesInAstprivate java.util.List<BlockParserFactory>blockParserFactoriesprivate java.util.List<java.util.List<BlockPreProcessorFactory>>blockPreProcessorDependenciesprivate ClassifyingBlockTrackerblockTrackerprivate intcolumncurrent column of input line (tab causes column to go to next 4-space tab stop) (0-based)private booleancolumnIsInTabif the current column is within a tab character (partially consumed tab)private static java.util.HashMap<CustomBlockParserFactory,DataKey<java.lang.Boolean>>CORE_FACTORIES_DATA_KEYSprivate static java.util.HashMap<DataKey<java.lang.Boolean>,ParagraphPreProcessorFactory>CORE_PARAGRAPH_PRE_PROCESSORSprivate ParserPhasecurrentPhaseprivate DocumentBlockParserdocumentBlockParserprivate intindentprivate intindexcurrent index (offset) in input line (0-based)static InlineParserFactoryINLINE_PARSER_FACTORYprivate InlineParserinlineParserprivate booleanisBlankLineprivate java.util.Map<Node,java.lang.Boolean>lastLineBlankprivate BasedSequencelineprivate intlineEndIndexcurrent end of line offset in the input including EOLprivate intlineEOLIndexcurrent lines EOL sequenceprivate intlineNumbercurrent line number in the inputprivate java.util.List<BasedSequence>lineSegmentsprivate intlineStartcurrent start of line offset in the inputprivate BasedSequencelineWithEOLprivate ParsingmyParsingprivate intnextNonSpaceprivate intnextNonSpaceColumnprivate DataHolderoptionsprivate java.util.List<java.util.List<ParagraphPreProcessorFactory>>paragraphPreProcessorDependenciesprivate booleantrackDocumentLines
-
Constructor Summary
Constructors Constructor Description DocumentParser(DataHolder options, java.util.List<CustomBlockParserFactory> customBlockParserFactories, java.util.List<java.util.List<ParagraphPreProcessorFactory>> paragraphPreProcessorDependencies, java.util.List<java.util.List<BlockPreProcessorFactory>> blockPreProcessorDependencies, InlineParser inlineParser)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidactivateBlockParser(BlockParser blockParser)private <T extends BlockParser>
TaddChild(T blockParser)Add block parser of type T as a child of the currently active parsers.private voidaddLine()Add line content to the active block parser.private voidadvance()voidblockAdded(@NotNull Block node)voidblockAddedWithChildren(@NotNull Block node)voidblockAddedWithDescendants(@NotNull Block node)voidblockParserAdded(BlockParser blockParser)voidblockParserRemoved(BlockParser blockParser)voidblockRemoved(@NotNull Block node)voidblockRemovedWithChildren(@NotNull Block node)voidblockRemovedWithDescendants(@NotNull Block node)private voidbreakOutOfLists(java.util.List<BlockParser> blockParsers)Break out of all containing lists, resetting the tip of the document to the parent of the highest list, and finalizing all the lists.static java.util.List<CustomBlockParserFactory>calculateBlockParserFactories(DataHolder options, java.util.List<CustomBlockParserFactory> customBlockParserFactories)static java.util.List<java.util.List<BlockPreProcessorFactory>>calculateBlockPreProcessors(DataHolder options, java.util.List<BlockPreProcessorFactory> blockPreProcessors)static java.util.List<java.util.List<ParagraphPreProcessorFactory>>calculateParagraphPreProcessors(DataHolder options, java.util.List<ParagraphPreProcessorFactory> blockPreProcessors, InlineParserFactory inlineParserFactory)private voiddeactivateBlockParser()booleanendsWithBlankLine(Node block)Test the block to see if it ends in a blank line.private voidfinalize(BlockParser blockParser)Finalize a block.private DocumentfinalizeAndProcess()private booleanfinalizeBlocks(java.util.List<BlockParser> blockParsers)Finalize blocks of previous line.private BlockStartImplfindBlockStart(BlockParser blockParser)private voidfindNextNonSpace()BlockParsergetActiveBlockParser()BlockParsergetActiveBlockParser(Block node)java.util.List<BlockParser>getActiveBlockParsers()intgetColumn()The column is the position within the line after tab characters have been processed as 4-space tab stops.intgetIndent()intgetIndex()InlineParsergetInlineParser()BasedSequencegetLine()intgetLineEndIndex()intgetLineEolLength()intgetLineNumber()java.util.List<BasedSequence>getLineSegments()Returns a list of document lines encountered this far in the parsing processintgetLineStart()BasedSequencegetLineWithEOL()intgetNextNonSpaceIndex()ParserPhasegetParserPhase()Get the current parser phaseParsinggetParsing()MutableDataHoldergetProperties()private voidincorporateLine(BasedSequence ln)Analyze a line of text and update the document appropriately.booleanisBlank()booleanisBlankLine()booleanisLastLineBlank(Node node)Test a block to see if the last line of the block is blank.Documentparse(java.io.Reader input)Documentparse(java.lang.CharSequence source)The main parsing function.private voidpreProcessBlocks()private voidpreProcessParagraph(Paragraph block, java.util.List<ParagraphPreProcessorFactory> stage, DocumentParser.ParagraphPreProcessorCache processorMap)pre-process a paragraph blockprivate voidpreProcessParagraphs()private voidprocessInlines()Walk through a block & children recursively, parsing string content into inline content where appropriate.private voidpropagateLastLineBlank(BlockParser blockParser, BlockParser lastMatchedBlockParser)private voidremoveActiveBlockParser()private voidsetLastLineBlank(Node node, boolean value)private voidsetNewColumn(int newColumn)private voidsetNewIndex(int newIndex)
-
-
-
Field Detail
-
INLINE_PARSER_FACTORY
public static final InlineParserFactory INLINE_PARSER_FACTORY
-
CORE_FACTORIES_DATA_KEYS
private static final java.util.HashMap<CustomBlockParserFactory,DataKey<java.lang.Boolean>> CORE_FACTORIES_DATA_KEYS
-
CORE_PARAGRAPH_PRE_PROCESSORS
private static final java.util.HashMap<DataKey<java.lang.Boolean>,ParagraphPreProcessorFactory> CORE_PARAGRAPH_PRE_PROCESSORS
-
line
private BasedSequence line
-
lineWithEOL
private BasedSequence lineWithEOL
-
lineNumber
private int lineNumber
current line number in the input
-
lineStart
private int lineStart
current start of line offset in the input
-
lineEOLIndex
private int lineEOLIndex
current lines EOL sequence
-
lineEndIndex
private int lineEndIndex
current end of line offset in the input including EOL
-
index
private int index
current index (offset) in input line (0-based)
-
column
private int column
current column of input line (tab causes column to go to next 4-space tab stop) (0-based)
-
columnIsInTab
private boolean columnIsInTab
if the current column is within a tab character (partially consumed tab)
-
nextNonSpace
private int nextNonSpace
-
nextNonSpaceColumn
private int nextNonSpaceColumn
-
indent
private int indent
-
blank
private boolean blank
-
isBlankLine
private boolean isBlankLine
-
blockParserFactories
private final java.util.List<BlockParserFactory> blockParserFactories
-
paragraphPreProcessorDependencies
private final java.util.List<java.util.List<ParagraphPreProcessorFactory>> paragraphPreProcessorDependencies
-
blockPreProcessorDependencies
private final java.util.List<java.util.List<BlockPreProcessorFactory>> blockPreProcessorDependencies
-
inlineParser
private final InlineParser inlineParser
-
documentBlockParser
private final DocumentBlockParser documentBlockParser
-
blankLinesInAst
private final boolean blankLinesInAst
-
trackDocumentLines
private final boolean trackDocumentLines
-
lineSegments
private final java.util.List<BasedSequence> lineSegments
-
activeBlockParsers
private final java.util.List<BlockParser> activeBlockParsers
-
blockTracker
private final ClassifyingBlockTracker blockTracker
-
lastLineBlank
private final java.util.Map<Node,java.lang.Boolean> lastLineBlank
-
options
private final DataHolder options
-
currentPhase
private ParserPhase currentPhase
-
myParsing
private final Parsing myParsing
-
-
Constructor Detail
-
DocumentParser
public DocumentParser(DataHolder options, java.util.List<CustomBlockParserFactory> customBlockParserFactories, java.util.List<java.util.List<ParagraphPreProcessorFactory>> paragraphPreProcessorDependencies, java.util.List<java.util.List<BlockPreProcessorFactory>> blockPreProcessorDependencies, InlineParser inlineParser)
-
-
Method Detail
-
getLineSegments
public java.util.List<BasedSequence> getLineSegments()
Description copied from interface:ParserStateReturns a list of document lines encountered this far in the parsing process- Specified by:
getLineSegmentsin interfaceParserState- Returns:
- list of line sequences (including EOLs)
-
blockParserAdded
public void blockParserAdded(BlockParser blockParser)
- Specified by:
blockParserAddedin interfaceBlockParserTracker
-
blockParserRemoved
public void blockParserRemoved(BlockParser blockParser)
- Specified by:
blockParserRemovedin interfaceBlockParserTracker
-
blockAdded
public void blockAdded(@NotNull @NotNull Block node)- Specified by:
blockAddedin interfaceBlockTracker
-
blockAddedWithChildren
public void blockAddedWithChildren(@NotNull @NotNull Block node)- Specified by:
blockAddedWithChildrenin interfaceBlockTracker
-
blockAddedWithDescendants
public void blockAddedWithDescendants(@NotNull @NotNull Block node)- Specified by:
blockAddedWithDescendantsin interfaceBlockTracker
-
blockRemoved
public void blockRemoved(@NotNull @NotNull Block node)- Specified by:
blockRemovedin interfaceBlockTracker
-
blockRemovedWithChildren
public void blockRemovedWithChildren(@NotNull @NotNull Block node)- Specified by:
blockRemovedWithChildrenin interfaceBlockTracker
-
blockRemovedWithDescendants
public void blockRemovedWithDescendants(@NotNull @NotNull Block node)- Specified by:
blockRemovedWithDescendantsin interfaceBlockTracker
-
getParserPhase
public ParserPhase getParserPhase()
Description copied from interface:ParserStateGet the current parser phase- Specified by:
getParserPhasein interfaceParserState- Returns:
- the current parser phase
ParserPhase
-
getParsing
public Parsing getParsing()
- Specified by:
getParsingin interfaceParserState- Returns:
- strings and patterns class adjusted for options
Parsing
-
getProperties
public MutableDataHolder getProperties()
- Specified by:
getPropertiesin interfaceParserState- Returns:
- document properties of the document being parsed
-
calculateBlockParserFactories
public static java.util.List<CustomBlockParserFactory> calculateBlockParserFactories(DataHolder options, java.util.List<CustomBlockParserFactory> customBlockParserFactories)
-
calculateParagraphPreProcessors
public static java.util.List<java.util.List<ParagraphPreProcessorFactory>> calculateParagraphPreProcessors(DataHolder options, java.util.List<ParagraphPreProcessorFactory> blockPreProcessors, InlineParserFactory inlineParserFactory)
-
calculateBlockPreProcessors
public static java.util.List<java.util.List<BlockPreProcessorFactory>> calculateBlockPreProcessors(DataHolder options, java.util.List<BlockPreProcessorFactory> blockPreProcessors)
-
getInlineParser
public InlineParser getInlineParser()
- Specified by:
getInlineParserin interfaceParserState- Returns:
- inline parser instance for the parser state
-
parse
public Document parse(java.lang.CharSequence source)
The main parsing function. Returns a parsed document AST.- Parameters:
source- source sequence to parse- Returns:
- Document node of the resulting AST
-
parse
public Document parse(java.io.Reader input) throws java.io.IOException
- Throws:
java.io.IOException
-
getLineNumber
public int getLineNumber()
- Specified by:
getLineNumberin interfaceParserState- Returns:
- The 0 based current line number within the input
-
getLineStart
public int getLineStart()
- Specified by:
getLineStartin interfaceParserState- Returns:
- the start of line offset into the input stream corresponding to current index into the line
-
getLineEndIndex
public int getLineEndIndex()
- Specified by:
getLineEndIndexin interfaceParserState- Returns:
- the end of line offset into the input stream corresponding to current index into the line, including the EOL
-
getLine
public BasedSequence getLine()
- Specified by:
getLinein interfaceParserState- Returns:
- the current line
-
getLineWithEOL
public BasedSequence getLineWithEOL()
- Specified by:
getLineWithEOLin interfaceParserState- Returns:
- the current line with EOL
-
getLineEolLength
public int getLineEolLength()
- Specified by:
getLineEolLengthin interfaceParserState- Returns:
- the EOL offset into the input stream corresponding to current index into the line
-
getIndex
public int getIndex()
- Specified by:
getIndexin interfaceParserState- Returns:
- the current index within the line (0-based)
-
getNextNonSpaceIndex
public int getNextNonSpaceIndex()
- Specified by:
getNextNonSpaceIndexin interfaceParserState- Returns:
- the index of the next non-space character starting from
ParserState.getIndex()(may be the same) (0-based)
-
getColumn
public int getColumn()
Description copied from interface:ParserStateThe column is the position within the line after tab characters have been processed as 4-space tab stops. If the line doesn't contain any tabs, it's the same as theParserState.getIndex(). If the line starts with a tab, followed by text, then the column for the first character of the text is 4 (the index is 1).- Specified by:
getColumnin interfaceParserState- Returns:
- the current column within the line (0-based)
-
getIndent
public int getIndent()
- Specified by:
getIndentin interfaceParserState- Returns:
- the indentation in columns (either by spaces or tab stop of 4), starting from
ParserState.getColumn()
-
isBlank
public boolean isBlank()
- Specified by:
isBlankin interfaceParserState- Returns:
- true if the current line is blank starting from the index
-
isBlankLine
public boolean isBlankLine()
- Specified by:
isBlankLinein interfaceParserState- Returns:
- true if the current line is blank starting from the index
-
getActiveBlockParser
public BlockParser getActiveBlockParser()
- Specified by:
getActiveBlockParserin interfaceParserState- Returns:
- the deepest open block parser
-
getActiveBlockParser
public BlockParser getActiveBlockParser(Block node)
- Specified by:
getActiveBlockParserin interfaceParserState- Parameters:
node- block node for which to get the active block parser- Returns:
- an active block parser for the node or null if not found or the block is already closed.
-
getActiveBlockParsers
public java.util.List<BlockParser> getActiveBlockParsers()
- Specified by:
getActiveBlockParsersin interfaceParserState- Returns:
- the current list of active block parsers, deepest is last
-
incorporateLine
private void incorporateLine(BasedSequence ln)
Analyze a line of text and update the document appropriately. We parse markdown text by calling this on each line of input, then finalizing the document.- Parameters:
ln- sequence of the current line
-
findNextNonSpace
private void findNextNonSpace()
-
setNewIndex
private void setNewIndex(int newIndex)
-
setNewColumn
private void setNewColumn(int newColumn)
-
advance
private void advance()
-
addLine
private void addLine()
Add line content to the active block parser. We assume it can accept lines -- that check should be done before calling this.
-
findBlockStart
private BlockStartImpl findBlockStart(BlockParser blockParser)
-
finalize
private void finalize(BlockParser blockParser)
Finalize a block. Close it and do any necessary postprocessing, e.g. creating string_content from strings, setting the 'tight' or 'loose' status of a list, and parsing the beginnings of paragraphs for reference definitions.- Parameters:
blockParser- block parser instance to finalize
-
processInlines
private void processInlines()
Walk through a block & children recursively, parsing string content into inline content where appropriate.
-
endsWithBlankLine
public boolean endsWithBlankLine(Node block)
Description copied from interface:ParserStateTest the block to see if it ends in a blank line. The blank line can be in the block or its last child.- Specified by:
endsWithBlankLinein interfaceParserState- Parameters:
block- block to be tested- Returns:
- true if the block ends in a blank line
-
breakOutOfLists
private void breakOutOfLists(java.util.List<BlockParser> blockParsers)
Break out of all containing lists, resetting the tip of the document to the parent of the highest list, and finalizing all the lists. (This is used to implement the "two blank lines break of of all lists" feature.)- Parameters:
blockParsers- list of block parsers to break out on double blank line
-
addChild
private <T extends BlockParser> T addChild(T blockParser)
Add block parser of type T as a child of the currently active parsers. If the tip can't accept children, close and finalize it and try its parent, and so on til we find a block that can accept children.- Type Parameters:
T- block parser type- Parameters:
blockParser- new block parser to add as a child- Returns:
- block parser instance added as a child.
-
activateBlockParser
private void activateBlockParser(BlockParser blockParser)
-
deactivateBlockParser
private void deactivateBlockParser()
-
removeActiveBlockParser
private void removeActiveBlockParser()
-
propagateLastLineBlank
private void propagateLastLineBlank(BlockParser blockParser, BlockParser lastMatchedBlockParser)
-
setLastLineBlank
private void setLastLineBlank(Node node, boolean value)
-
isLastLineBlank
public boolean isLastLineBlank(Node node)
Description copied from interface:ParserStateTest a block to see if the last line of the block is blank. Children not tested.- Specified by:
isLastLineBlankin interfaceParserState- Parameters:
node- block instance to test- Returns:
- true if the block's last line is blank
-
finalizeBlocks
private boolean finalizeBlocks(java.util.List<BlockParser> blockParsers)
Finalize blocks of previous line.- Returns:
- true.
-
preProcessParagraph
private void preProcessParagraph(Paragraph block, java.util.List<ParagraphPreProcessorFactory> stage, DocumentParser.ParagraphPreProcessorCache processorMap)
pre-process a paragraph block- Parameters:
block- paragraph block to pre-processstage- paragraph pre-processor dependency stageprocessorMap- paragraph pre-processor cache
-
preProcessParagraphs
private void preProcessParagraphs()
-
preProcessBlocks
private void preProcessBlocks()
-
finalizeAndProcess
private Document finalizeAndProcess()
-
-