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 class
DocumentParser.ParagraphPreProcessorCache
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<BlockParser>
activeBlockParsers
private boolean
blank
private boolean
blankLinesInAst
private java.util.List<BlockParserFactory>
blockParserFactories
private java.util.List<java.util.List<BlockPreProcessorFactory>>
blockPreProcessorDependencies
private ClassifyingBlockTracker
blockTracker
private int
column
current column of input line (tab causes column to go to next 4-space tab stop) (0-based)private boolean
columnIsInTab
if the current column is within a tab character (partially consumed tab)private static java.util.HashMap<CustomBlockParserFactory,DataKey<java.lang.Boolean>>
CORE_FACTORIES_DATA_KEYS
private static java.util.HashMap<DataKey<java.lang.Boolean>,ParagraphPreProcessorFactory>
CORE_PARAGRAPH_PRE_PROCESSORS
private ParserPhase
currentPhase
private DocumentBlockParser
documentBlockParser
private int
indent
private int
index
current index (offset) in input line (0-based)static InlineParserFactory
INLINE_PARSER_FACTORY
private InlineParser
inlineParser
private boolean
isBlankLine
private java.util.Map<Node,java.lang.Boolean>
lastLineBlank
private BasedSequence
line
private int
lineEndIndex
current end of line offset in the input including EOLprivate int
lineEOLIndex
current lines EOL sequenceprivate int
lineNumber
current line number in the inputprivate java.util.List<BasedSequence>
lineSegments
private int
lineStart
current start of line offset in the inputprivate BasedSequence
lineWithEOL
private Parsing
myParsing
private int
nextNonSpace
private int
nextNonSpaceColumn
private DataHolder
options
private java.util.List<java.util.List<ParagraphPreProcessorFactory>>
paragraphPreProcessorDependencies
private boolean
trackDocumentLines
-
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 void
activateBlockParser(BlockParser blockParser)
private <T extends BlockParser>
TaddChild(T blockParser)
Add block parser of type T as a child of the currently active parsers.private void
addLine()
Add line content to the active block parser.private void
advance()
void
blockAdded(@NotNull Block node)
void
blockAddedWithChildren(@NotNull Block node)
void
blockAddedWithDescendants(@NotNull Block node)
void
blockParserAdded(BlockParser blockParser)
void
blockParserRemoved(BlockParser blockParser)
void
blockRemoved(@NotNull Block node)
void
blockRemovedWithChildren(@NotNull Block node)
void
blockRemovedWithDescendants(@NotNull Block node)
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.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 void
deactivateBlockParser()
boolean
endsWithBlankLine(Node block)
Test the block to see if it ends in a blank line.private void
finalize(BlockParser blockParser)
Finalize a block.private Document
finalizeAndProcess()
private boolean
finalizeBlocks(java.util.List<BlockParser> blockParsers)
Finalize blocks of previous line.private BlockStartImpl
findBlockStart(BlockParser blockParser)
private void
findNextNonSpace()
BlockParser
getActiveBlockParser()
BlockParser
getActiveBlockParser(Block node)
java.util.List<BlockParser>
getActiveBlockParsers()
int
getColumn()
The column is the position within the line after tab characters have been processed as 4-space tab stops.int
getIndent()
int
getIndex()
InlineParser
getInlineParser()
BasedSequence
getLine()
int
getLineEndIndex()
int
getLineEolLength()
int
getLineNumber()
java.util.List<BasedSequence>
getLineSegments()
Returns a list of document lines encountered this far in the parsing processint
getLineStart()
BasedSequence
getLineWithEOL()
int
getNextNonSpaceIndex()
ParserPhase
getParserPhase()
Get the current parser phaseParsing
getParsing()
MutableDataHolder
getProperties()
private void
incorporateLine(BasedSequence ln)
Analyze a line of text and update the document appropriately.boolean
isBlank()
boolean
isBlankLine()
boolean
isLastLineBlank(Node node)
Test a block to see if the last line of the block is blank.Document
parse(java.io.Reader input)
Document
parse(java.lang.CharSequence source)
The main parsing function.private void
preProcessBlocks()
private void
preProcessParagraph(Paragraph block, java.util.List<ParagraphPreProcessorFactory> stage, DocumentParser.ParagraphPreProcessorCache processorMap)
pre-process a paragraph blockprivate void
preProcessParagraphs()
private void
processInlines()
Walk through a block & children recursively, parsing string content into inline content where appropriate.private void
propagateLastLineBlank(BlockParser blockParser, BlockParser lastMatchedBlockParser)
private void
removeActiveBlockParser()
private void
setLastLineBlank(Node node, boolean value)
private void
setNewColumn(int newColumn)
private void
setNewIndex(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:ParserState
Returns a list of document lines encountered this far in the parsing process- Specified by:
getLineSegments
in interfaceParserState
- Returns:
- list of line sequences (including EOLs)
-
blockParserAdded
public void blockParserAdded(BlockParser blockParser)
- Specified by:
blockParserAdded
in interfaceBlockParserTracker
-
blockParserRemoved
public void blockParserRemoved(BlockParser blockParser)
- Specified by:
blockParserRemoved
in interfaceBlockParserTracker
-
blockAdded
public void blockAdded(@NotNull @NotNull Block node)
- Specified by:
blockAdded
in interfaceBlockTracker
-
blockAddedWithChildren
public void blockAddedWithChildren(@NotNull @NotNull Block node)
- Specified by:
blockAddedWithChildren
in interfaceBlockTracker
-
blockAddedWithDescendants
public void blockAddedWithDescendants(@NotNull @NotNull Block node)
- Specified by:
blockAddedWithDescendants
in interfaceBlockTracker
-
blockRemoved
public void blockRemoved(@NotNull @NotNull Block node)
- Specified by:
blockRemoved
in interfaceBlockTracker
-
blockRemovedWithChildren
public void blockRemovedWithChildren(@NotNull @NotNull Block node)
- Specified by:
blockRemovedWithChildren
in interfaceBlockTracker
-
blockRemovedWithDescendants
public void blockRemovedWithDescendants(@NotNull @NotNull Block node)
- Specified by:
blockRemovedWithDescendants
in interfaceBlockTracker
-
getParserPhase
public ParserPhase getParserPhase()
Description copied from interface:ParserState
Get the current parser phase- Specified by:
getParserPhase
in interfaceParserState
- Returns:
- the current parser phase
ParserPhase
-
getParsing
public Parsing getParsing()
- Specified by:
getParsing
in interfaceParserState
- Returns:
- strings and patterns class adjusted for options
Parsing
-
getProperties
public MutableDataHolder getProperties()
- Specified by:
getProperties
in 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:
getInlineParser
in 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:
getLineNumber
in interfaceParserState
- Returns:
- The 0 based current line number within the input
-
getLineStart
public int getLineStart()
- Specified by:
getLineStart
in interfaceParserState
- Returns:
- the start of line offset into the input stream corresponding to current index into the line
-
getLineEndIndex
public int getLineEndIndex()
- Specified by:
getLineEndIndex
in 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:
getLine
in interfaceParserState
- Returns:
- the current line
-
getLineWithEOL
public BasedSequence getLineWithEOL()
- Specified by:
getLineWithEOL
in interfaceParserState
- Returns:
- the current line with EOL
-
getLineEolLength
public int getLineEolLength()
- Specified by:
getLineEolLength
in interfaceParserState
- Returns:
- the EOL offset into the input stream corresponding to current index into the line
-
getIndex
public int getIndex()
- Specified by:
getIndex
in interfaceParserState
- Returns:
- the current index within the line (0-based)
-
getNextNonSpaceIndex
public int getNextNonSpaceIndex()
- Specified by:
getNextNonSpaceIndex
in 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:ParserState
The 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:
getColumn
in interfaceParserState
- Returns:
- the current column within the line (0-based)
-
getIndent
public int getIndent()
- Specified by:
getIndent
in interfaceParserState
- Returns:
- the indentation in columns (either by spaces or tab stop of 4), starting from
ParserState.getColumn()
-
isBlank
public boolean isBlank()
- Specified by:
isBlank
in interfaceParserState
- Returns:
- true if the current line is blank starting from the index
-
isBlankLine
public boolean isBlankLine()
- Specified by:
isBlankLine
in interfaceParserState
- Returns:
- true if the current line is blank starting from the index
-
getActiveBlockParser
public BlockParser getActiveBlockParser()
- Specified by:
getActiveBlockParser
in interfaceParserState
- Returns:
- the deepest open block parser
-
getActiveBlockParser
public BlockParser getActiveBlockParser(Block node)
- Specified by:
getActiveBlockParser
in 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:
getActiveBlockParsers
in 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:ParserState
Test 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:
endsWithBlankLine
in 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:ParserState
Test a block to see if the last line of the block is blank. Children not tested.- Specified by:
isLastLineBlank
in 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()
-
-