scanner

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package scanner provides a scanner for WDTE tokens.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MacroMap added in v0.6.0

type MacroMap map[string]func(string) ([]Token, error)

MacroMap specifies mappings of macro names to their definitions. For example, an entry with the key "example" would be available in scanned code as

@example[some input or another]

In this example, the string "some input or another" would be passed to the definition. The tokens returned by the macro are inserted raw into the token stream that is yielded by the scanner. Returned tokens of type Macro are reprocessed via the same map.

type Scanner

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

A Scanner tokenizes runes from an io.Reader.

Example
package main

import (
	"io"

	"github.com/DeedleFake/wdte/scanner"
)

var r io.Reader

func main() {
	s := scanner.New(r, nil)
	for s.Scan() {
		/* Do something with s.Tok(). */
	}
	if err := s.Err(); err != nil {
		panic(err)
	}
}
Output:

func New

func New(r io.Reader, macros MacroMap) *Scanner

New returns a new Scanner that reads from r. macros, which may be nil, specifies mappings of macro names to their definitions.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the error that stopped the scanner, if any.

func (*Scanner) Pos

func (s *Scanner) Pos() (line, col int)

Pos returns the line and column of the input that the scanner is currently on.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan reads the next token from the underlying io.Reader. If a token was successfully read, it returns true. It is designed to be used in a loop, similarly to bufio.Scanner's API.

func (*Scanner) Tok

func (s *Scanner) Tok() Token

Tok returns the latest token scanned. If there was an error or a token hasn't been scanned yet, its return value is undefined.

type Token

type Token struct {
	Line, Col int
	Type      TokenType
	Val       interface{}
}

A Token is a usable element parsed from a string.

type TokenType

type TokenType uint

TokenType is the type of a token.

const (
	Invalid TokenType = iota
	Number
	String
	ID
	Keyword
	Macro
	EOF
)

func (TokenType) String

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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