Skip to content

Commit

Permalink
refactor(parser): simplify DocumentFragment rule (bytesparadise#1038)
Browse files Browse the repository at this point in the history
also, refactor block delimiter tracking in preprocessing

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored Jun 8, 2022
1 parent c3e86f3 commit 9823021
Show file tree
Hide file tree
Showing 6 changed files with 16,338 additions and 16,312 deletions.
37 changes: 35 additions & 2 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
}
b.WriteString(f)
case *types.BlockDelimiter:
t.push(e.Kind, e.Length)
ctx.opts = append(ctx.opts, withinDelimitedBlock(t.withinDelimitedBlock()))
t.track(e.Kind, e.Length)
ctx.opts = append(ctx.opts, t.withinDelimitedBlock())
b.WriteString(e.RawText())
case types.ConditionalInclusion:
if content, ok := e.SingleLineContent(); ok {
Expand All @@ -81,6 +81,39 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
return b.String(), nil
}

type blockDelimiterTracker struct {
stack []blockDelimiter
}

type blockDelimiter struct {
kind string
length int
}

func newBlockDelimiterTracker() *blockDelimiterTracker {
return &blockDelimiterTracker{
stack: []blockDelimiter{},
}
}

func (t *blockDelimiterTracker) track(kind string, length int) {
switch {
case len(t.stack) > 0 && t.stack[len(t.stack)-1].kind == kind && t.stack[len(t.stack)-1].length == length:
// pop
t.stack = t.stack[:len(t.stack)-1]
default:
// push
t.stack = append(t.stack, blockDelimiter{
kind: kind,
length: length,
})
}
}

func (t *blockDelimiterTracker) withinDelimitedBlock() Option {
return GlobalStore(withinDelimitedBlockKey, len(t.stack) > 0)
}

// replace the content of this FileInclusion element the content of the target file
// note: there is a trade-off here: we include the whole content of the file in the current
// fragment, making it potentially big, but at the same time we ensure that the context
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func reparseDelimitedBlock(ctx *ParseContext, b *types.DelimitedBlock) error {
switch b.Kind {
case types.Example, types.Quote, types.Sidebar, types.Open:
log.Debugf("parsing elements of delimited block of kind '%s'", b.Kind)
opts := append(ctx.opts, Entrypoint("DelimitedBlockElements"), withinDelimitedBlock(true))
opts := append(ctx.opts, Entrypoint("DelimitedBlockElements"))
elements, err := reparseElements(b.Elements, opts...)
if err != nil {
return err
Expand Down Expand Up @@ -238,6 +238,6 @@ func (c *current) isWithinLiteralParagraph() bool {
log.Debugf("within literal paragraph: %t", attrs[types.AttrStyle] == types.Literal)
return attrs[types.AttrPositional1] == types.Literal || attrs[types.AttrStyle] == types.Literal
}
log.Debug("not within literal paragraph")
// log.Debug("not within literal paragraph")
return false
}
Loading

0 comments on commit 9823021

Please sign in to comment.