Skip to content

ofabricio/scanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scanner

A text scanner.

Example

Here an example of a very simple JSON iterator using this scanner. It parses a JSON and prints its keys and values.

package main

import (
    "fmt"
    "unicode"
    . "github.com/ofabricio/scanner"
)

func main() {

    j := Json{`{ "one": "hello", "two": "world" }`}

    j.Iterate(func(k, v string) {
        fmt.Println(k, v)
    })

    // Output:
    // "one" "hello"
    // "two" "world"
}

type Json struct {
    Scanner
}

func (j *Json) Iterate(f func(k, v string)) {
    if j.Match("{") {
        for j.WS() && !j.Match("}") {
            k, _ := j.TokenFor(j.MatchString), j.Match(":")
            j.WS()
            v, _ := j.TokenFor(j.MatchString), j.Match(",")
            f(k, v)
        }
    }
}

func (j *Json) WS() bool {
    return j.MatchWhileRuneBy(unicode.IsSpace) || true
}

func (j *Json) MatchString() bool {
    return j.Scanner.UtilMatchString('"')
}

Operators

Equal

  • Equal(string) bool
  • EqualByte(byte) bool
  • EqualRune(rune) bool
  • EqualByteBy(func(byte) bool) bool
  • EqualRuneBy(func(rune) bool) bool
  • EqualByteRange(a, b byte) bool

Match

  • Match(string) bool
  • MatchByte(byte) bool
  • MatchRune(rune) bool
  • MatchByteBy(func(byte) bool) bool
  • MatchRuneBy(func(rune) bool) bool

Until

  • MatchUntil(string) bool
  • MatchUntilByte(byte) bool
  • MatchUntilRune(rune) bool
  • MatchUntilByteBy(func(byte) bool) bool
  • MatchUntilRuneBy(func(rune) bool) bool
  • MatchUntilAny(a, b string) bool
  • MatchUntilAnyByte(a, b byte) bool
  • MatchUntilAnyRune(a, b rune) bool
  • MatchUntilAnyByte3(a, b, c byte) bool
  • MatchUntilEsc(v, esc string) bool
  • MatchUntilEscByte(v, esc byte) bool
  • MatchUntilEscRune(v, esc rune) bool

While

  • MatchWhileByteBy(func(byte) bool) bool
  • MatchWhileRuneBy(func(rune) bool) bool

Token

  • Token(int) string
  • TokenByteBy(func(byte) bool) string
  • TokenRuneBy(func(rune) bool) string
  • TokenFor(func() bool) string
  • TokenWith(func(*Scanner) bool) string

Movement

  • Next()
  • NextRune()
  • Advance(int)
  • Mark() Scanner
  • Back(Scanner)
  • More() bool

Miscellaneous

  • Curr() byte
  • CurrRune() rune
  • String() string
  • Bytes() []byte

Utils

  • UtilMatchString(quote byte) bool
  • UtilMatchOpenCloseCount(o, c byte) bool
  • UtilMatchInteger() bool
  • UtilMatchFloat() bool
  • UtilMatchNumber() bool
  • UtilMatchHex() bool

Releases

No releases published

Languages