Skip to content

Commit

Permalink
refactor(parser): unexport 'opts' field in Context type (#1029)
Browse files Browse the repository at this point in the history
finally addressing this long-time TODO comment ;)

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored May 21, 2022
1 parent 7bc20ec commit 2e7b5cb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
20 changes: 11 additions & 9 deletions pkg/parser/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@ import (

type ParseContext struct {
filename string
Opts []Option // TODO: unexport this field?
opts []Option
levelOffsets levelOffsets
attributes *contextAttributes
userMacros map[string]configuration.MacroTemplate
counters map[string]interface{}
}

func NewParseContext(config *configuration.Configuration, opts ...Option) *ParseContext {
func NewParseContext(config *configuration.Configuration, options ...Option) *ParseContext {
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("new parser context with attributes: %s", spew.Sdump(config.Attributes))
}
opts = append(opts, Entrypoint("DocumentFragment"))
opts = append(opts, GlobalStore(frontMatterKey, true))
opts = append(opts, GlobalStore(documentHeaderKey, true))
opts = append(opts, GlobalStore(enabledSubstitutions, []string{AttributeRefs}))
opts = append(opts, GlobalStore(usermacrosKey, config.Macros))
opts := []Option{
Entrypoint("DocumentFragment"),
GlobalStore(frontMatterKey, true),
GlobalStore(documentHeaderKey, true),
GlobalStore(enabledSubstitutions, []string{AttributeRefs}),
GlobalStore(usermacrosKey, config.Macros)}
opts = append(opts, options...)
return &ParseContext{
filename: config.Filename,
Opts: opts,
opts: opts,
levelOffsets: []*levelOffset{},
attributes: newContextAttributes(config.Attributes),
userMacros: config.Macros,
Expand All @@ -40,7 +42,7 @@ func NewParseContext(config *configuration.Configuration, opts ...Option) *Parse
func (c *ParseContext) Clone() *ParseContext {
return &ParseContext{
filename: c.filename,
Opts: options(c.Opts).clone(),
opts: options(c.opts).clone(),
levelOffsets: c.levelOffsets.clone(),
attributes: c.attributes.clone(),
userMacros: c.userMacros,
Expand Down
8 changes: 4 additions & 4 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
t := newBlockDelimiterTracker()
for scanner.Scan() {
line := scanner.Bytes()
element, err := Parse("", line, append(ctx.Opts, Entrypoint("DocumentRawLine"))...)
element, err := Parse("", line, append(ctx.opts, Entrypoint("DocumentRawLine"))...)
if err != nil {
// log.Error(err)
// content of line was not relevant in the context of preparsing (ie, it's a regular line), so let's keep it as-is
Expand All @@ -61,7 +61,7 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
b.WriteString(f)
case *types.BlockDelimiter:
t.push(types.BlockDelimiterKind(e.Kind), e.Length)
ctx.Opts = append(ctx.Opts, withinDelimitedBlock(t.withinDelimitedBlock()))
ctx.opts = append(ctx.opts, withinDelimitedBlock(t.withinDelimitedBlock()))
b.WriteString(e.RawText())
case types.ConditionalInclusion:
if content, ok := e.SingleLineContent(); ok {
Expand All @@ -86,7 +86,7 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
// fragment, making it potentially big, but at the same time we ensure that the context
// of the inclusion (for example, within a delimited block) is not lost.
func includeFile(ctx *ParseContext, incl *types.FileInclusion) (string, error) {
ctx.Opts = append(ctx.Opts, GlobalStore(documentHeaderKey, false))
ctx.opts = append(ctx.opts, GlobalStore(documentHeaderKey, false))
if l, ok := incl.GetLocation().Path.([]interface{}); ok {
l, _, err := replaceAttributeRefsInSlice(ctx, l, noneSubstitutions())
if err != nil {
Expand All @@ -101,7 +101,7 @@ func includeFile(ctx *ParseContext, incl *types.FileInclusion) (string, error) {
if !adoc {
return string(content), nil
}
ctx.Opts = append(ctx.Opts, sectionEnabled())
ctx.opts = append(ctx.opts, sectionEnabled())
return preprocess(ctx, bytes.NewReader(content))
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func applySubstitutionsOnFragment(ctx *ParseContext, f types.DocumentFragment) t
return f
}
for i := range f.Elements {
if err := applySubstitutionsOnElement(ctx, f.Elements[i], ctx.Opts...); err != nil {
if err := applySubstitutionsOnElement(ctx, f.Elements[i], ctx.opts...); err != nil {
return types.NewErrorFragment(f.Position, err)
}
}
Expand All @@ -53,13 +53,13 @@ func applySubstitutionsOnElement(ctx *ParseContext, element interface{}, opts ..
case *types.AttributeDeclaration:
ctx.attributes.set(b.Name, b.Value)
if b.Name == types.AttrExperimental {
ctx.Opts = append(ctx.Opts, enableExperimentalMacros(true))
ctx.opts = append(ctx.opts, enableExperimentalMacros(true))
}
return nil
case *types.AttributeReset:
ctx.attributes.unset(b.Name)
if b.Name == types.AttrExperimental {
ctx.Opts = append(ctx.Opts, enableExperimentalMacros(false))
ctx.opts = append(ctx.opts, enableExperimentalMacros(false))
}
return nil
case *types.DocumentHeader:
Expand Down
6 changes: 3 additions & 3 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ParseFragments(ctx *ParseContext, source io.Reader, done <-chan interface{}
resultStream <- types.NewErrorFragment(types.Position{}, err)
return
}
p := newParser(ctx.filename, b, ctx.Opts...) // we want to parse block attributes to detect AttributeReferences
p := newParser(ctx.filename, b, ctx.opts...) // we want to parse block attributes to detect AttributeReferences
if err := p.setup(g); err != nil {
resultStream <- types.NewErrorFragment(types.Position{}, err)
return
Expand Down Expand Up @@ -138,7 +138,7 @@ func reparseTableCell(ctx *ParseContext, c *types.TableCell) error {
log.Debugf("reparsing content of table cell")
switch c.Format {
case "a":
opts := append(ctx.Opts, Entrypoint("DelimitedBlockElements"))
opts := append(ctx.opts, Entrypoint("DelimitedBlockElements"))
elements, err := reparseElements(c.Elements, opts...)
if err != nil {
return err
Expand All @@ -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"), withinDelimitedBlock(true))
elements, err := reparseElements(b.Elements, opts...)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions testsupport/parse_document_fragment_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
// ParseDocumentFragments parses the actual source with the options
func ParseDocumentFragments(actual string, options ...parser.Option) ([]types.DocumentFragment, error) {
r := strings.NewReader(actual)
ctx := parser.NewParseContext(configuration.NewConfiguration())
ctx.Opts = append(ctx.Opts, options...)
ctx := parser.NewParseContext(configuration.NewConfiguration(), options...)
done := make(chan interface{})
defer close(done)
// ctx.Opts = append(ctx.Opts, parser.Debug(true))
Expand Down

0 comments on commit 2e7b5cb

Please sign in to comment.