                    Listing One


#!/bin/omnimark -sb
;
; parnest.xom
; omnimark: pattern functions used in recursive pattern matching
;
; Run this program with the command
;
;   omnimark -sb parnest.xom parnest.txt
;
; where parnest.txt is an input file with nested sets of parentheses
;
; This program extracts nested matching parentheses from a file
; and prints the outermost set with all of the intervening text
; as a single unit.
;
; In the following line, the underlined text would be printed out
;
;   ( skip this ( but ( show ) this ) and ( not this
;               ---------------------
;
; Try using this program as input to itself:
;
;   omnimark -sb parnest.xom parnest.xom


; forward definition because functions refer to each other
define switch function paren-block
elsewhere

define switch function paren-block-interior
as
    repeat scan #current-input
        match [any except "()"]     ;any except start or end of block
        match paren-block           ;any contained block
    again
    return true

define switch function paren-block
as
    return #current-input matches ( "(" paren-block-interior ")" )

find paren-block => text
    ; list each outer block
    output text || "%n"

find any        ; Grab everything rejected by the first 'find' rule
    ; discard all characters

process
    submit #main-input  ;Send all input through the 'find' rules above
