listparser

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package listparser parses AdGuard, EasyList, and hosts-style filter lists into canonical Rule objects that represent domain-focused blocking or allow rules.

Supported constructs:

  • Domain filters: "example.com", "||example.com^", "||sub.example.com^"
  • Exception rules: "@@||example.com^"
  • Wildcards: "||*.ads.example.com^", "ads*.example.com"
  • Hosts entries: "0.0.0.0 example.com", "127.0.0.1 example.com"

Unsupported constructs (logged and skipped):

  • Non-network rules (##, #@#, #?#, #$#, $$, #%, ...)
  • Advanced modifiers that change rule semantics ($script, $domain=, etc.)
  • Path-only rules without hostnames

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Warnf(format string, args ...interface{})
	Infof(format string, args ...interface{})
}

Logger is the minimal warning sink required by the parser.

Implementations receive best-effort warnings for unsupported lines or other non-fatal parse issues. The interface keeps the package decoupled from any concrete logging implementation used by callers.

type Rule

type Rule struct {
	// Pattern is the canonical domain pattern.
	// It uses a restricted subset: literal chars, '*' for wildcard sequences,
	// '.' for literal dot. Implicitly anchored as a full-match.
	Pattern string
	// Source is the origin of this rule, typically "filepath:line".
	Source string
	// IsAllow is true for exception/whitelist rules (@@).
	IsAllow bool
}

Rule represents one canonicalized filter entry ready for compilation.

func ParseFile

func ParseFile(path string, logger Logger) (rules []Rule, err error)

ParseFile loads one filter list from path and returns the parsed Rule slice.

The path parameter may point to AdGuard, EasyList, or hosts-style files. The logger parameter receives warnings for unsupported or malformed lines and may be nil when the caller wants silent best-effort parsing. ParseFile returns the successfully parsed rules together with any terminal read error; individual line failures are logged and skipped so callers can continue with partially valid upstream lists.

func ParseLine

func ParseLine(line string) (Rule, error)

ParseLine parses one raw filter line into a canonical Rule.

The line parameter may contain AdGuard, EasyList, or hosts-style syntax. The returned Rule contains the normalized domain pattern and allow/deny flag when parsing succeeds. ParseLine returns errSkip for comments and blank lines and returns a descriptive error for unsupported modifiers, path-based rules, or invalid pattern characters. Callers normally use ParseLine inside ParseFile or fuzz and unit tests that exercise individual rule forms.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL