lexer

package
v0.0.0-...-6f11ea9 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

Package lexer

The lexer package produces a set of valid tokens from a valid cddl source.

Example

This example covers tokeizing a cddl source with literals, control operators, ranges and compositions. For more examples go to the examples folder.

package main

import (
	"fmt"

	"github.com/HannesKimara/cddlc/lexer"
	"github.com/HannesKimara/cddlc/token"
)

func main() {
	src := `
		min-age = 18
		max-age = 150

		byte = uint .size 1
		public-key = [24*24 byte]
		person = (name: tstr, public-key: public-key)

		adult = (person, age: min-age .. max-age) ; adults are composed from person
	`

	lex := lexer.NewLexer([]byte(src))

	for {
		tok, pos, lit := lex.Scan()
		fmt.Printf("%s: %s -> %s\n", pos, tok, lit)
		if tok == token.EOF {
			break
		}
	}
}

License

This project is licensed under the Apache-2.0 license. Please see the LICENSE file for more details.

Documentation

Index

Constants

View Source
const (
	EOF = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Pos token.Position
	Msg string
}

type ErrorFunction

type ErrorFunction func(pos token.Position, msg string)

type Lexer

type Lexer struct {
	ErrCount int
	Errors   []Error
	// contains filtered or unexported fields
}

func NewLexer

func NewLexer(src []byte) *Lexer

NewLexer returns a new Lexer given code sourcce in bytes

func (*Lexer) Scan

func (l *Lexer) Scan() (tok token.Token, pos token.Position, lit string)

Scan returns the next token.

type TokenSer

type TokenSer struct {
	Pos     token.Position `json:"pos"`
	Token   token.Token    `json:"token"`
	Literal string         `json:"literal"`
}

Jump to

Keyboard shortcuts

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