News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


Using the tab function for parsing in Icon

Recall that Icon's ``e1 ? e2'' operator not only sets the subject ``&subject'' to e1 during the evaluation of e2, but it also sets up an implied position in a variable called ``&pos''.

The implied subject for string scanning is strictly defined as the portion of the current subject past the current position. So, if ``&pos'' is 3, then the current subject is all but the first two characters of ``&subject''.

You can use the tab() function to work your way through a complex subject. Here is what a call to ``tab ( n )'' does:

  1. The current position (&pos) is saved.
  2. The position is set to n.
  3. The function returns the portion of the subject between the old (saved) position and the new position, as a string.

For example, ``tab ( many ( &digits ) )'' finds the position of the next character in the subject that is not a digit, sets the new position there, and returns the string of digits between the old and new positions.


Next: An example of Icon parsing
See also: Using the Icon programming language
Previous: Implicit matching in Icon

John W. Shipman, john@nmt.edu