Documentation
¶
Index ¶
- Constants
- Variables
- func All(s *Scanner) string
- func Escape(ch rune) string
- func FormatTokenTable(ts []Token) string
- func Line(s *Scanner) string
- func Quote(s string) string
- func QuoteRune(r rune) string
- func Repeat(fn func(), n int)
- func RunTests(t *testing.T, rules RuleSet, tests []Test)
- func Space(s *Scanner)
- func UnexpectedRune() func(*Scanner)
- func UnexpectedUntil(c Class) func(*Scanner)
- func Until(s *Scanner, c Class, fn func())
- func UntilRule(s *Scanner, r Rule, fn func())
- func While(s *Scanner, c Class, fn func())
- func Word(s *Scanner) string
- type CharEnc
- type CharEncRule
- type Class
- type ClassRule
- type CommentRule
- type Error
- type Errors
- type HexEncRule
- type IdentRule
- type LiteralRule
- type NumRule
- func (r NumRule) Eval(s *Scanner) bool
- func (r NumRule) WithDecSep(c Class) NumRule
- func (r NumRule) WithDigitSep(c Class) NumRule
- func (r NumRule) WithEmptyPartsAllowed(b bool) NumRule
- func (r NumRule) WithExp(c Class) NumRule
- func (r NumRule) WithExpSign(c Class) NumRule
- func (r NumRule) WithIntType(t string) NumRule
- func (r NumRule) WithLeadingDigitSepAllowed(b bool) NumRule
- func (r NumRule) WithLeadingZeroAllowed(b bool) NumRule
- func (r NumRule) WithPrefix(rule Rule) NumRule
- func (r NumRule) WithRealType(t string) NumRule
- func (r NumRule) WithSign(c Class) NumRule
- func (r NumRule) WithSuffix(rules ...Rule) NumRule
- type OctEncRule
- type Pos
- type Rule
- type RuleSet
- type Runner
- type Scanner
- func (s *Scanner) Discard()
- func (s *Scanner) Emit() Token
- func (s *Scanner) Eval(r Rule) (Token, bool)
- func (s *Scanner) HasMore() bool
- func (s *Scanner) Illegal(format string, args ...any)
- func (s *Scanner) Init(name string, src io.Reader)
- func (s *Scanner) InitFromBytes(name string, src []byte)
- func (s *Scanner) InitFromString(name string, src string)
- func (s *Scanner) Keep()
- func (s *Scanner) Peek(i int) rune
- func (s *Scanner) Skip()
- func (s *Scanner) Undo()
- type StrRule
- func (r StrRule) Eval(s *Scanner) bool
- func (r StrRule) WithEscape(rn rune) StrRule
- func (r StrRule) WithEscapeRules(rules ...Rule) StrRule
- func (r StrRule) WithMaxLen(l uint) StrRule
- func (r StrRule) WithMultiline(b bool) StrRule
- func (r StrRule) WithNesting(t bool) StrRule
- func (r StrRule) WithOptionalTerminator(t bool) StrRule
- func (r StrRule) WithType(t string) StrRule
- type Test
- type Token
- type WhileRule
Examples ¶
Constants ¶
View Source
const ( BinType = "bin" CommentType = "comment" EndOfTextType = "end-of-text" ErrorType = "error" HexType = "hex" IdentType = "ident" IllegalType = "illegal" IntType = "int" OctType = "oct" RealType = "real" SpaceType = "space" StrType = "str" WordType = "word" )
View Source
const (
EndOfText = rune(-1)
)
Variables ¶
View Source
var ( Hex2EncRule = NewHexEncRule('x', 2) Hex4EncRule = NewHexEncRule('u', 4) Hex8EncRule = NewHexEncRule('U', 8) )
View Source
var ( StrDoubleQuoteRule = NewStrRule('"', '"').WithEscape('\\') StrSingleQuoteRule = NewStrRule('\'', '\'').WithEscape('\\') )
View Source
var ( SkipSpaceRule = NewWhileRule(IsSpace, SpaceType).WithKeep(false) KeepSpaceRule = NewWhileRule(IsSpace, SpaceType) WordRule = NewWhileRule(Not(IsSpace), WordType) )
View Source
var OctEnc = NewOctEncRule()
View Source
var TrueRule = trueRule{}
Functions ¶
func FormatTokenTable ¶
func UnexpectedRune ¶
func UnexpectedRune() func(*Scanner)
func UnexpectedUntil ¶
Types ¶
type CharEnc ¶
var ( AlertEnc CharEnc = NewCharEnc('a', '\a') BackspaceEnc CharEnc = NewCharEnc('b', '\b') BellEnc CharEnc = AlertEnc CarriageReturnEnc CharEnc = NewCharEnc('r', '\r') FormFeedEnc CharEnc = NewCharEnc('f', '\f') HorizontalTabEnc CharEnc = NewCharEnc('t', '\t') LineFeedEnc CharEnc = NewCharEnc('n', '\n') NewlineEnc CharEnc = LineFeedEnc TabEnc CharEnc = HorizontalTabEnc VerticalTabEnc CharEnc = NewCharEnc('v', '\v') )
func NewCharEnc ¶
type CharEncRule ¶
type CharEncRule struct {
// contains filtered or unexported fields
}
func NewCharEncRule ¶
func NewCharEncRule(maps ...CharEnc) CharEncRule
func (CharEncRule) Eval ¶
func (r CharEncRule) Eval(s *Scanner) bool
type Class ¶
Class returns true if the given rune is a member.
var ( // IsAny returns true as long as there is a rune available in the input // stream. IsAny Class = func(r rune) bool { return r != EndOfText } // IsCurrency returns true when given a rune that is a currency symbol as // defined by Unicode. IsCurrency Class = func(r rune) bool { return unicode.Is(unicode.Sc, r) } // IsDigit returns true when given a digit as defined by Unicode. IsDigit Class = unicode.IsDigit // IsDigit01 returns true when given a valid binary digit. IsDigit01 Class = Rune('0', '1') // IsDigit07 returns true when given a valid octal digit. IsDigit07 Class = Range('0', '7') // IsDigit09 returns true when given a valid decimal digit. IsDigit09 Class = Range('0', '9') // IsDigit0F returns true when given a valid hexadecimal digit. IsDigit0F Class = Or( IsDigit09, Range('a', 'f'), Range('A', 'F'), ) // IsLetter returns true when given rune is a letter as defined by Unicode. IsLetter Class = unicode.IsLetter // IsLetterAZ returns true when given letters from the Latin alphabet. IsLetterAZ Class = Or( Range('a', 'z'), Range('A', 'Z'), ) // IsLetterUnder returns true when given letters as defined by Unicode // or an underscore. IsLetterUnder = Or( IsLetter, Rune('_'), ) // IsLetterDigitUnder returns true when given letters as digits as defined // by Unicode or an underscore. IsLetterDigitUnder = Or( IsLetterUnder, IsDigit, ) // IsNone always returns false. IsNone Class = func(r rune) bool { return false } // IsPrintable returns true when given a rune that is printable as defined // by Unicode. IsPrintable Class = unicode.IsPrint // IsRune8 returns true when given a rune that can be represented by // an 8-bit number. IsRune8 Class = Range(0, 0xff) // IsRune16 returns true when given a rune that can be represented by // a 16-bit number. IsRune16 Class = Range(0, 0xffff) // IsSign returns true when the rune is a positive (+) or negative (-) // numeric symbol. IsSign Class = Rune('+', '-') // IsSpace returns true when the given rune is whitespace as // defined by Unicode. IsSpace Class = unicode.IsSpace )
func Not ¶
Not returns a Class function that returns true when given a rune that does not match class c.
Example ¶
isA := Rune('a') isNotA := Not(isA) fmt.Printf("%c: %v\n", 'a', isNotA('a')) fmt.Printf("%c: %v\n", 'b', isNotA('b'))
Output: a: false b: true
func Or ¶
Or returns a Class that returns true when given a rune that matches any class found in cs.
Example ¶
isLowerAZ := Range('a', 'z') isUpperAZ := Range('A', 'Z') isLetterAZ := Or(isLowerAZ, isUpperAZ) fmt.Printf("%c: %v\n", 'f', isLetterAZ('f')) fmt.Printf("%c: %v\n", 'F', isLetterAZ('F')) fmt.Printf("%c: %v\n", '4', isLetterAZ('4'))
Output: f: true F: true 4: false
func Range ¶
Range returns a Class function that returns true when given a rune that is between from and to inclusive.
Example ¶
isDigit09 := Range('0', '9') fmt.Printf("%c: %v\n", '3', isDigit09('3')) fmt.Printf("%c: %v\n", '6', isDigit09('6')) fmt.Printf("%c: %v\n", 'a', isDigit09('a'))
Output: 3: true 6: true a: false
type ClassRule ¶ added in v0.1.0
type ClassRule struct {
// contains filtered or unexported fields
}
func NewClassRule ¶ added in v0.1.0
type CommentRule ¶
type CommentRule struct {
// contains filtered or unexported fields
}
func NewCommentRule ¶
func NewCommentRule(begin LiteralRule, end LiteralRule) CommentRule
func (CommentRule) Eval ¶
func (r CommentRule) Eval(s *Scanner) bool
func (CommentRule) WithKeep ¶
func (r CommentRule) WithKeep(keep *bool) CommentRule
type HexEncRule ¶
type HexEncRule struct {
// contains filtered or unexported fields
}
func NewHexEncRule ¶
func NewHexEncRule(flag rune, digits int) HexEncRule
func (HexEncRule) AsByte ¶ added in v0.0.1
func (r HexEncRule) AsByte(b bool) HexEncRule
func (HexEncRule) Eval ¶
func (r HexEncRule) Eval(s *Scanner) bool
type IdentRule ¶
type IdentRule struct {
// contains filtered or unexported fields
}
var (
StandardIdentRule IdentRule = NewIdentRule(IsLetterUnder, IsLetterDigitUnder)
)
func NewIdentRule ¶
func (IdentRule) WithKeywords ¶
type LiteralRule ¶
type LiteralRule struct {
// contains filtered or unexported fields
}
func Literal ¶
func Literal(lits ...string) LiteralRule
func (LiteralRule) Eval ¶
func (r LiteralRule) Eval(s *Scanner) bool
func (LiteralRule) WithSkip ¶
func (r LiteralRule) WithSkip(b bool) LiteralRule
type NumRule ¶
type NumRule struct {
// contains filtered or unexported fields
}
var ( BinRule NumRule = NewNumRule(IsDigit01).WithIntType(BinType) Bin0bRule NumRule = BinRule.WithPrefix(Literal("0b", "0B")) HexRule NumRule = NewNumRule(IsDigit0F).WithIntType(HexType) Hex0xRule NumRule = HexRule.WithPrefix(Literal("0x", "0X")) OctRule NumRule = NewNumRule(IsDigit07).WithIntType(OctType) Oct0oRule NumRule = OctRule.WithPrefix(Literal("0o", "0O")) IntRule NumRule = NewNumRule(IsDigit09) RealRule NumRule = IntRule.WithDecSep(Rune('.')) RealExpRule NumRule = RealRule.WithExp(Rune('e', 'E')).WithExpSign(IsSign) SignedIntRule NumRule = IntRule.WithSign(IsSign) SignedRealRule NumRule = RealRule.WithSign(IsSign) SignedRealExpRule NumRule = RealExpRule.WithSign(IsSign) )
func NewNumRule ¶
func (NumRule) WithDecSep ¶
func (NumRule) WithDigitSep ¶
func (NumRule) WithEmptyPartsAllowed ¶
func (NumRule) WithExpSign ¶
func (NumRule) WithIntType ¶
func (NumRule) WithLeadingDigitSepAllowed ¶
func (NumRule) WithLeadingZeroAllowed ¶
func (NumRule) WithPrefix ¶
func (NumRule) WithRealType ¶
func (NumRule) WithSuffix ¶
type OctEncRule ¶
type OctEncRule struct { }
func NewOctEncRule ¶
func NewOctEncRule() OctEncRule
func (OctEncRule) Eval ¶
func (r OctEncRule) Eval(s *Scanner) bool
type Pos ¶
Pos represents a position within an input stream.
type RuleSet ¶
type RuleSet struct {
// contains filtered or unexported fields
}
func NewRuleSet ¶
func (RuleSet) WithNoMatchFunc ¶
func (RuleSet) WithPostTokenFunc ¶
func (RuleSet) WithPreTokenFunc ¶
type Runner ¶
type Scanner ¶
type Scanner struct { This rune Next rune Val strings.Builder Lit strings.Builder Pos Pos Errs Errors Type string // contains filtered or unexported fields }
func NewScannerFromBytes ¶
func NewScannerFromString ¶
func (*Scanner) Discard ¶
func (s *Scanner) Discard()
Discard advances the string to the next rune without adding the current rune to the token.
func (*Scanner) Emit ¶
Emit returns the token that has been built and resets the builder for the next token.
func (*Scanner) InitFromBytes ¶
func (*Scanner) InitFromString ¶
func (*Scanner) Keep ¶
func (s *Scanner) Keep()
Keep advances the stream to the next rune and adds the current rune to the token value and literal.
type StrRule ¶
type StrRule struct {
// contains filtered or unexported fields
}
func NewStrRule ¶
func (StrRule) WithEscape ¶
func (StrRule) WithEscapeRules ¶
func (StrRule) WithMaxLen ¶
func (StrRule) WithMultiline ¶
func (StrRule) WithNesting ¶ added in v0.3.0
func (StrRule) WithOptionalTerminator ¶ added in v0.3.0
type Token ¶
type Token struct { Val string `json:"val"` Lit string `json:"lit,omitempty"` Type string `json:"type"` Pos Pos `json:"pos"` Errs Errors `json:"errs,omitempty"` }
func (Token) IsEndOfText ¶ added in v0.1.0
type WhileRule ¶
type WhileRule struct {
// contains filtered or unexported fields
}
func NewWhileRule ¶ added in v0.2.0
Click to show internal directories.
Click to hide internal directories.