package goabnfimport ()var (// ErrNoSolutionFound is an error returned when parsing an ABNF // grammar and no solution has been found. // // On the ABNF parsing path it is wrapped by a [*ParseError] carrying the // position at which parsing got stuck; errors.Is(err, ErrNoSolutionFound) // keeps matching.ErrNoSolutionFound = errors.New("no solution found, input ABNF grammar may be invalid")// ErrHandlingProseVal is an error returned when an operation tried // to produce something on a prose-val, but it can't be handled properly.ErrHandlingProseVal = errors.New("can't handle prose-val descriptions")// ErrProseValInTransitionGraph is returned when building a transition graph // from a grammar that contains a prose-val, which has no defined expansion.ErrProseValInTransitionGraph = errors.New("prose value is not supported in transition graphs")// ErrRepetitionThreshold is returned when expanding a repetition whose bound // exceeds the threshold set with WithRepetitionThreshold.ErrRepetitionThreshold = errors.New("repetition threshold reached")// ErrRepetitionMinNotOne is returned when a mandatory (max == 1) repetition // has a minimum other than 1.ErrRepetitionMinNotOne = errors.New("repetition minimum must be 1 when maximum is 1")// ErrRepetitionMinGtMax is returned when a repetition has a minimum greater // than its maximum.ErrRepetitionMinGtMax = errors.New("repetition minimum is greater than maximum"))// ParseError reports where parsing input against a grammar got stuck. It is// returned (wrapped around [ErrNoSolutionFound]) when an input does not parse,// to help locate the offending construct -- in particular when writing or// debugging an ABNF grammar.typeParseErrorstruct {// Offset is the byte offset into the input of the furthest position the // parser was able to reach before failing. Offset int// Line and Col are the 1-based line and column of Offset. Line, Col int// Expected lists the terminals the parser could have consumed at Offset, if // that information was available. It may be empty. Expected []string// Found describes the input byte at Offset (e.g. `%x0A (LF)`), or "EOF" when // Offset is at the end of input. It may be empty. Found string// Hint, when set, gives a plain-language diagnosis of a common mistake (e.g. // bare LF line endings where ABNF requires CR LF). Hint string}var _ error = (*ParseError)(nil)func ( *ParseError) () string {varstrings.Builderfmt.Fprintf(&, "parse error at line %d, column %d (offset %d)", .Line, .Col, .Offset)switch {caselen(.Expected) > 0:fmt.Fprintf(&, ": expected %s", strings.Join(.Expected, ", "))if .Found != "" {fmt.Fprintf(&, ", found %s", .Found) }case .Found != "":fmt.Fprintf(&, ": unexpected %s", .Found)default:fmt.Fprintf(&, ": %s", ErrNoSolutionFound) }if .Hint != "" {fmt.Fprintf(&, " (hint: %s)", .Hint) }return .String()}// Unwrap reports ErrNoSolutionFound so that callers matching the historical// sentinel with errors.Is keep working.func ( *ParseError) () error { returnErrNoSolutionFound }// describeByte renders an input byte the way ABNF terminals are written, naming// the control characters that matter most when debugging a grammar.func describeByte( byte) string {switch {case'\r':return"%x0D (CR)"case'\n':return"%x0A (LF)"case'\t':return"%x09 (HTAB)"case' ':return"%x20 (SP)" }if >= 0x20 && < 0x7f {returnfmt.Sprintf("%q", string()) }returnfmt.Sprintf("%%x%02X", )}// newParseError builds a ParseError from a byte offset into input, computing the// 1-based line and column, the byte found there, and a hint for the bare-LF// line-ending mistake (the most common ABNF authoring error).func newParseError( []byte, int, []string) *ParseError {if < 0 { = 0 }if > len() { = len() } , := 1, 1for := 0; < ; ++ {if [] == '\n' { ++ = 1 } else { ++ } } := append([]string(nil), ...)sort.Strings() := &ParseError{Offset: , Line: , Col: , Expected: }if < len() { .Found = describeByte([])// Bare LF where the previous byte is not CR: ABNF lines end with CR LF, // so a lone LF is almost always the cause.if [] == '\n' && ( == 0 || [-1] != '\r') { .Hint = `input uses bare LF line endings, but ABNF requires CR LF ("\r\n")` } } else { .Found = "EOF" }return}// ErrMultipleSolutionsFound is an error returned when a parser found// multiple solutions when none or one were expected.typeErrMultipleSolutionsFoundstruct{}var _ error = (*ErrMultipleSolutionsFound)(nil)func ( ErrMultipleSolutionsFound) () string {return"multiple solutions found, this should not happen. Please open an issue. This could eventually need an Erratum from IETF tracking"}// ErrRuleNotFound is an error returned when the rule was not found// as part of the grammar.typeErrRuleNotFoundstruct { Rulename string}var _ error = (*ErrRuleNotFound)(nil)func ( ErrRuleNotFound) () string {returnfmt.Sprintf("rule %s was not found in grammar", .Rulename)}// ErrCoreRuleModify is an error returned when an incremental alternative// for a core rule.typeErrCoreRuleModifystruct { CoreRulename string}var _ error = (*ErrCoreRuleModify)(nil)func ( ErrCoreRuleModify) () string {returnfmt.Sprintf("core rule %s can't be modified", .CoreRulename)}// ErrDependencyNotFound is an error returned during ABNF grammar// semantic vaildation, if a rule depends on an unexisting rule.typeErrDependencyNotFoundstruct { Rulename string}var _ error = (*ErrDependencyNotFound)(nil)func ( ErrDependencyNotFound) () string {returnfmt.Sprintf("unsatisfied dependency (rule) %s", .Rulename)}// ErrSemanticRepetition is an error returned during ABNF grammar// semantic validation, if a repetition has min < max.typeErrSemanticRepetitionstruct { Repetition Repetition}var _ error = (*ErrSemanticRepetition)(nil)func ( ErrSemanticRepetition) () string {returnfmt.Sprintf("invalid semantic of input ABNF grammar for repetition %s", .Repetition)}// ErrTooLargeNumeral is an error returned when the numeral value// provided to parse cannot be handled as a 7-bit US-ASCII valid value.typeErrTooLargeNumeralstruct { Base, Value string}var _ error = (*ErrTooLargeNumeral)(nil)func ( ErrTooLargeNumeral) () string {returnfmt.Sprintf("too large numeral value %s for base %s", .Value, .Base)}// ErrDuplicatedRule is an error returned when the rule already// exist as part of the grammar.typeErrDuplicatedRulestruct { Rulename string}var _ error = (*ErrDuplicatedRule)(nil)func ( ErrDuplicatedRule) () string {returnfmt.Sprintf("rule %s was already defined in grammar", .Rulename)}// ErrCyclicRule is an error returned when can't work due to an// unavoidable cyclic rule.typeErrCyclicRulestruct { Rulename string}var _ error = (*ErrCyclicRule)(nil)func ( ErrCyclicRule) () string {returnfmt.Sprintf("can't generate a content as the rule %s involves an unavoidable cycle", .Rulename)}// ErrMaxNodesExceeded is returned by TransitionGraph when construction would// allocate more nodes than the budget set with WithMaxNodes.typeErrMaxNodesExceededstruct { Max int}var _ error = (*ErrMaxNodesExceeded)(nil)func ( ErrMaxNodesExceeded) () string {returnfmt.Sprintf("transition graph node budget of %d exceeded", .Max)}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.