Documentation ¶
Overview ¶
Package scanner implements a lexical scanner for PostScript.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 (*Scanner) Float64 ¶
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 ¶
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 ¶
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) 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.
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.