1. token
Parses and returns the next PostScript token from a file or string.
1.1. Syntax
file token → any true (if token found)
→ false (if end-of-file)
string token → post any true (if token found)
→ false (if only whitespace)
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
token reads characters from file or string, interpreting them according to PostScript syntax rules until a complete object is scanned.
File case:
* Returns the scanned object and true if successful
* Closes file and returns false if end-of-file encountered before any non-whitespace characters
String case:
* Returns post (remaining substring), scanned object, and true if successful
* Returns false if only whitespace characters present
The scanned object may be:
* Simple: integer, real, or name
* Composite: string (…) or procedure { … }
1.5. Examples
(15(St1) { 1 2 add }) token
% Result: ((St1) { 1 2 add }) 15 true
((St1) { 1 2 add }) token
% Result: ( { 1 2 add }) (St1) true
( { 1 2 add }) token
% Result: ( ) { 1 2 add } true
( ) token
% Result: false
(123 456) token
% Result: ( 456) 123 true
1.7. Common Pitfalls
Consumes Whitespace - token consumes trailing whitespace after names/numbers.
|
(123 456) token % Consumes space after 123
% Result: ( 456) not (456)
| Evaluates Syntax - Returns actual objects, not string representations. |
(123) token % Returns integer 123, not string
({ 1 2 }) token % Returns procedure, not string
| Special Character Handling - Different characters consumed differently after tokens. |
Binary Token Support - token also handles binary tokens and binary object sequences.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
String/file has no-access attribute |
[ |
File I/O error |
[ |
Token too large for implementation |
[ |
Not enough room for results |
[ |
No operand on stack |
[ |
Invalid PostScript syntax |
[ |
Operand not file or string |
[ |
Number out of range |
[ |
Insufficient VM for composite object |
1.9. Token Consumption Rules
token consumes characters differently based on token type:
| Token Type | Characters Consumed |
|---|---|
Name/number + whitespace |
Token + first whitespace char |
String |
Including closing |
Procedure |
Including closing |
Array |
Including closing |
Name preceded by |
Not including the |
Binary token |
Exact bytes, no extra |
1.10. Implementation Notes
-
Same parsing logic as the PostScript interpreter
-
Returns literal objects (use
cvx) if execution needed -
File position updated past consumed characters
-
For strings, post points into original string (shared value)
1.11. Advanced Example
/tokenize { % string => array-of-tokens
[ exch
{
token not { exit } if
} loop
]
} def
(123 (abc) /name { 1 2 add }) tokenize
% Result: [123 (abc) /name { 1 2 add }]
1.12. See Also
-
search- Find substring -
anchorsearch- Test for prefix -
cvs- Convert to string -
cvn- Convert to name -
cvx- Make executable