vdf

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2022 License: MIT Imports: 5 Imported by: 7

README

vdf: A Lexer and Parser for Valves Data Format (known as vdf)

GoDoc Go Report Card

A Lexer and Parser for Valves Data Format (known as vdf) written in Go.

Installation

It is go gettable

$ go get github.com/andygrunwald/vdf

Usage

Given a file named gamestate_integration_consolesample.cfg with content:

"Console Sample v.1"
{
	"uri" 		"http://127.0.0.1:3000"
	"timeout" 	"5.0"
	"buffer"  	"0.1"
	"throttle" 	"0.5"
	"heartbeat"	"60.0"
	[...]
}

Can be parsed with this Go code:

package main

import (
	"fmt"
	"os"

	"github.com/andygrunwald/vdf"
)

func main() {
	f, err := os.Open("gamestate_integration_consolesample.cfg")
	if err != nil {
		panic(err)
	}

	p := vdf.NewParser(f)
	m, err := p.Parse()
	if err != nil {
		panic(err)
	}

	fmt.Println(m)
}

And it will output:

map[
	Console Sample v.1:map[
		uri:http://127.0.0.1:3000
		timeout:5.0
		buffer:0.1
		throttle:0.5
		heartbeat:60.0
		[...]
	]
]

API-Documentation

The official Go package documentation can be found at https://pkg.go.dev/github.com/andygrunwald/vdf.

Development

Unit testing

To run the local unit tests, execute

$ make test

To run the local unit tests and view the unit test code coverage in your local web browser, execute

$ make test-coverage-html
Fuzzing tests

This library implements Go fuzzing. The generated fuzzing corpus is stored in andygrunwald/vdf-fuzzing-corpus, to avoid blowing up the size of this repository.

To run fuzzing locally, execute

$ make init-fuzzing   # Clone the corpus into testdata/fuzz
$ make clean-fuzzing  # Clean the local fuzzing cache
$ make test-fuzzing   # Execute the fuzzing

VDF parser in other languages

Inspiration

The code is inspired by @benbjohnson's article Handwritten Parsers & Lexers in Go and his example sql-parser. Thank you Ben!

License

This project is released under the terms of the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotValidFormat = errors.New("not valid vdf format")
)

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() (map[string]interface{}, error)

Parse is the main entry point of the vdf parser. If parsed the complete VDF content and returns a map as a key / value pair. The value is a string (normal value) or a map[string]interface{} again if there is a nested structure.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner represents a lexical scanner.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan(respectWhitespace bool) (tok Token, lit string)

Scan returns the next token and literal value.

type Token

type Token int

Token is our own type that represents a single token to work with during parsing

const (

	// Illegal represents a token that we
	// don`t know in contect of the VDF format
	Illegal Token

	// EOF represents the End of File token.
	// This is used if the file is end
	EOF

	// WS represents a whitespace.
	// This can be a space or a tab.
	WS

	// EOL represents a line ending.
	// This can be a \n or a \r.
	EOL

	// Ident represents a key or a value.
	// Typically this is a simple string
	Ident

	// CurlyBraceOpen represents a open curly brace "{"
	CurlyBraceOpen

	// CurlyBraceClose represents a close curly brace "}"
	CurlyBraceClose

	// QuotationMark represents a quote mark '"'
	QuotationMark

	// EscapeSequence represents an escape character "\"
	EscapeSequence

	// CommentDoubleSlash represents a comment prefix with a double slash "//"
	CommentDoubleSlash
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL