package goabnf
import (
"fmt"
"strconv"
"strings"
)
type ElemItf interface {
fmt .Stringer
}
type Rule struct {
Name string
Alternation Alternation
}
func (rl Rule ) String () string {
return fmt .Sprintf ("%s = %s" , rl .Name , rl .Alternation )
}
type Alternation struct {
Concatenations []Concatenation
}
func (alt Alternation ) String () string {
str := ""
for _ , concat := range alt .Concatenations {
str += fmt .Sprintf ("%s / " , concat )
}
return strings .TrimSuffix (str , " / " )
}
type Concatenation struct {
Repetitions []Repetition
}
func (cnt Concatenation ) String () string {
str := ""
for _ , rep := range cnt .Repetitions {
str += fmt .Sprintf ("%s " , rep )
}
return strings .TrimSuffix (str , " " )
}
type Repetition struct {
Min, Max int
Element ElemItf
}
func (rep Repetition ) String () string {
if rep .Min == rep .Max {
if rep .Min == 1 {
return rep .Element .String ()
}
return strconv .Itoa (rep .Min ) + rep .Element .String ()
}
str := ""
if rep .Min != 0 {
str += strconv .Itoa (rep .Min )
}
str += "*"
if rep .Max != inf {
str += strconv .Itoa (rep .Max )
}
return str + rep .Element .String ()
}
type ElemRulename struct {
Name string
}
func (erln ElemRulename ) String () string {
return erln .Name
}
var _ ElemItf = (*ElemRulename )(nil )
type ElemGroup struct {
Alternation Alternation
}
func (egrp ElemGroup ) String () string {
return "(" + egrp .Alternation .String () + ")"
}
var _ ElemItf = (*ElemGroup )(nil )
type ElemOption struct {
Alternation Alternation
}
func (eopt ElemOption ) String () string {
return "[" + eopt .Alternation .String () + "]"
}
var _ ElemItf = (*ElemOption )(nil )
type ElemCharVal struct {
Sensitive bool
Values []rune
}
func (ecvl ElemCharVal ) String () string {
str := ""
for _ , val := range ecvl .Values {
str += string (val )
}
return `"` + str + `"`
}
var _ ElemItf = (*ElemCharVal )(nil )
type ElemNumVal struct {
Base string
Status Status
Elems []string
}
func (envl ElemNumVal ) String () string {
str := "%" + envl .Base
spl := "."
if envl .Status == StatRange {
spl = "-"
}
for _ , val := range envl .Elems {
str += val + spl
}
return strings .TrimSuffix (str , spl )
}
var _ ElemItf = (*ElemNumVal )(nil )
type ElemProseVal struct {
values []string
}
func (epvl ElemProseVal ) String () string {
str := ""
for _ , val := range epvl .values {
str += val
}
return "<" + str + ">"
}
var _ ElemItf = (*ElemProseVal )(nil )
type Status int
const (
StatSeries Status = iota
StatRange
)
var ABNF = &Grammar {
Rulemap : map [string ]*Rule {
abnfRulelist .Name : abnfRulelist ,
abnfRule .Name : abnfRule ,
abnfRulename .Name : abnfRulename ,
abnfDefinedAs .Name : abnfDefinedAs ,
abnfElements .Name : abnfElements ,
abnfCWsp .Name : abnfCWsp ,
abnfCNl .Name : abnfCNl ,
abnfComment .Name : abnfComment ,
abnfAlternation .Name : abnfAlternation ,
abnfConcatenation .Name : abnfConcatenation ,
abnfRepetition .Name : abnfRepetition ,
abnfRepeat .Name : abnfRepeat ,
abnfElement .Name : abnfElement ,
abnfGroup .Name : abnfGroup ,
abnfOption .Name : abnfOption ,
abnfCharVal .Name : abnfCharVal ,
abnfCaseInsensitiveString .Name : abnfCaseInsensitiveString ,
abnfCaseSensitiveString .Name : abnfCaseSensitiveString ,
abnfQuotedString .Name : abnfQuotedString ,
abnfNumVal .Name : abnfNumVal ,
abnfBinVal .Name : abnfBinVal ,
abnfDecVal .Name : abnfDecVal ,
abnfHexVal .Name : abnfHexVal ,
abnfProseVal .Name : abnfProseVal ,
},
}
The pages are generated with Golds v0.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 .