scanner

package
v0.0.0-...-4037ba1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package scanner implements a lexical scanner for PostScript.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidFormat = errors.New("invalid format")

ErrInvalidFormat is reported when decoding a token value that does not match the specified result format.

Functions

func NeedSpaceBetween

func NeedSpaceBetween(prev, next Type) bool

NeedSpaceBetween reports whether spaces are required between a token of type prev and a token of type next to preserve lexical structure.

Types

type Scanner

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

A Scanner consumes PostScript tokens from an input stream. Use the Next method to parse tokens sequentially from the input.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/creachadair/postscript/scanner"
)

func needsSpace(t scanner.Type) bool {
	switch t {
	case scanner.Decimal, scanner.Radix, scanner.Real, scanner.Name:
		return true
	}
	return false
}

func main() {
	s := scanner.New(strings.NewReader(`% Draw a line
/in { 72 mul } def
/mt { moveto } def
newpath
  .5 in .5 in mt   % use half inch margins
  8 in 10.5 in mt
  10 setlinewidth
  0 setgray        % black
stroke % and that's all!`))

	// Use the scanner to strip out comments and unnecessary whitespace, and
	// wrap the source to 80 columns.
	col := 0
	var prev scanner.Type
	for s.Next() == nil {
		t := s.Type()
		if t == scanner.Comment {
			continue
		}
		if col+len(s.Text()) > 80 {
			fmt.Print("\n")
			col = 0
		} else if needsSpace(prev) && needsSpace(t) {
			fmt.Print(" ")
			col++
		}

		prev = t
		n, _ := fmt.Print(s.Text())
		col += n
	}
}
Output:

/in{72 mul}def/mt{moveto}def newpath .5 in .5 in mt 8 in 10.5 in mt 10
setlinewidth 0 setgray stroke

func New

func New(r io.Reader) *Scanner

New constructs a *Scanner that reads from r.

func (*Scanner) End

func (s *Scanner) End() int

End returns the ending byte offset of the current token in the input.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the last error reported by Next.

func (*Scanner) Float64

func (s *Scanner) Float64() (float64, error)

Float64 returns the value of the current token as a float64. If the token cannot be converted to a floating-point value it returns 0, ErrInvalidFormat. Integer tokens value converted to float64 without error.

func (*Scanner) Int64

func (s *Scanner) Int64() (int64, error)

Int64 returns the value of the current token as an int64. If the token cannot be converted to an integer value it returns 0, ErrInvalidFormat. Real tokens are truncated to an integer without error.

func (*Scanner) Next

func (s *Scanner) Next() error

Next advances s to the next token in the stream and returns nil if a valid token is available. If no further tokens are available, it returns io.EOF; otherwise it reports what went wrong.

func (*Scanner) Pos

func (s *Scanner) Pos() int

Pos returns the starting byte offset of the current token in the input.

func (*Scanner) String

func (s *Scanner) String() string

String returns the decoded value of the current token as a string. This has different effects depending on the type:

Numeric tokens and punctuation are returned as-written.

Name tokens are returned with quotes removed (that is, /name becomes name).

String literals are stripped of their quotes and decoded.

Comment tokens are stripped of all leading "%" as well as any leading and trailing whitespace that remains after doing so.

func (*Scanner) Text

func (s *Scanner) Text() string

Text returns the literal text of the current token, or "".

func (*Scanner) Type

func (s *Scanner) Type() Type

Type reports the lexical type of the current token.

type Type

type Type int

Type denotes the lexical type of a token.

const (
	Invalid       Type = iota // an invalid token
	Comment                   // a comment: % foo
	LitString                 // a string literal: (foo)
	HexString                 // a hex literal: <666f6f>
	A85String                 // an ascii85 literal: <~AoDS~>
	Decimal                   // a decimal integer: 25
	Radix                     // a radix integer: 2#1101
	Real                      // a floating-point value: -6.3e2
	Name                      // a name: foo
	QuotedName                // a quoted name: /foo
	ImmediateName             // an immediate name: //foo
	Left                      // a left bracket: {
	Right                     // a right bracket: }

)

The legal values for a token type.

Jump to

Keyboard shortcuts

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