Giter VIP home page Giter VIP logo

scanner's Introduction

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

scanner's People

Contributors

ofabricio avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.