src/HOL/Import/mono_scan.ML
author haftmann
Fri, 12 Oct 2007 08:25:48 +0200
changeset 24996 ebd5f4cc7118
parent 19093 6d584f9d2021
child 32960 69916a850301
permissions -rw-r--r--
moved class power to theory Power
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19093
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     1
(*  Title:      HOL/Import/mono_scan.ML
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     2
    ID:         $Id$
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     3
    Author:     Steven Obua, TU Muenchen
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     4
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     5
    Monomorphic scanner combinators for monomorphic sequences.
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     6
*)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     7
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     8
signature MONO_SCANNER =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
     9
sig
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    10
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    11
    include MONO_SCANNER_SEQ
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    12
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    13
    exception SyntaxError
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    14
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    15
    type 'a scanner = seq -> 'a * seq
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    16
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    17
    val :--      : 'a scanner * ('a -> 'b scanner)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    18
		   -> ('a*'b) scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    19
    val --       : 'a scanner * 'b scanner -> ('a * 'b) scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    20
    val >>       : 'a scanner * ('a -> 'b) -> 'b scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    21
    val --|      : 'a scanner * 'b scanner -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    22
    val |--      : 'a scanner * 'b scanner -> 'b scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    23
    val ^^       : string scanner * string scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    24
		   -> string scanner 
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    25
    val ||       : 'a scanner * 'a scanner -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    26
    val one      : (item -> bool) -> item scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    27
    val anyone   : item scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    28
    val succeed  : 'a -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    29
    val any      : (item -> bool) -> item list scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    30
    val any1     : (item -> bool) -> item list scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    31
    val optional : 'a scanner -> 'a -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    32
    val option   : 'a scanner -> 'a option scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    33
    val repeat   : 'a scanner -> 'a list scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    34
    val repeat1  : 'a scanner -> 'a list scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    35
    val repeat_fixed : int -> 'a scanner -> 'a list scanner  
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    36
    val ahead    : 'a scanner -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    37
    val unless   : 'a scanner -> 'b scanner -> 'b scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    38
    val !!       : (seq -> string) -> 'a scanner -> 'a scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    39
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    40
end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    41
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    42
signature STRING_SCANNER =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    43
sig
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    44
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    45
    include MONO_SCANNER  where type item = string
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    46
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    47
    val $$       : item -> item scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    48
    
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    49
    val scan_id : string scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    50
    val scan_nat : int scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    51
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    52
    val this : item list -> item list scanner
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    53
    val this_string : string -> string scanner					    
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    54
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    55
end    
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    56
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    57
functor MonoScanner (structure Seq : MONO_SCANNER_SEQ) : MONO_SCANNER =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    58
struct
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    59
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    60
infix 7 |-- --|
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    61
infix 5 :-- -- ^^
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    62
infix 3 >>
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    63
infix 0 ||
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    64
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    65
exception SyntaxError
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    66
exception Fail of string
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    67
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    68
type seq = Seq.seq
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    69
type item = Seq.item
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    70
type 'a scanner = seq -> 'a * seq
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    71
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    72
val pull = Seq.pull
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    73
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    74
fun (sc1 :-- sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    75
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    76
	val (x,toks2) = sc1 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    77
	val (y,toks3) = sc2 x toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    78
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    79
	((x,y),toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    80
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    81
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    82
fun (sc1 -- sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    83
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    84
	val (x,toks2) = sc1 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    85
	val (y,toks3) = sc2 toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    86
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    87
	((x,y),toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    88
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    89
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    90
fun (sc >> f) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    91
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    92
	val (x,toks2) = sc toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    93
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    94
	(f x,toks2)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    95
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    96
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    97
fun (sc1 --| sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    98
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
    99
	val (x,toks2) = sc1 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   100
	val (_,toks3) = sc2 toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   101
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   102
	(x,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   103
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   104
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   105
fun (sc1 |-- sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   106
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   107
	val (_,toks2) = sc1 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   108
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   109
	sc2 toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   110
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   111
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   112
fun (sc1 ^^ sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   113
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   114
	val (x,toks2) = sc1 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   115
	val (y,toks3) = sc2 toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   116
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   117
	(x^y,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   118
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   119
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   120
fun (sc1 || sc2) toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   121
    (sc1 toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   122
    handle SyntaxError => sc2 toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   123
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   124
fun anyone toks = case pull toks of NONE => raise SyntaxError | SOME x => x
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   125
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   126
fun one p toks = case anyone toks of x as (t, toks) => if p t then x else raise SyntaxError
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   127
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   128
fun succeed e toks = (e,toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   129
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   130
fun any p toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   131
    case pull toks of
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   132
	NONE =>  ([],toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   133
      | SOME(x,toks2) => if p x
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   134
			 then
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   135
			     let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   136
				 val (xs,toks3) = any p toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   137
			     in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   138
				 (x::xs,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   139
			     end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   140
			 else ([],toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   141
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   142
fun any1 p toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   143
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   144
	val (x,toks2) = one p toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   145
	val (xs,toks3) = any p toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   146
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   147
	(x::xs,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   148
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   149
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   150
fun optional sc def =  sc || succeed def
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   151
fun option sc = (sc >> SOME) || succeed NONE
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   152
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   153
(*
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   154
fun repeat sc =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   155
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   156
	fun R toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   157
	    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   158
		val (x,toks2) = sc toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   159
		val (xs,toks3) = R toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   160
	    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   161
		(x::xs,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   162
	    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   163
	    handle SyntaxError => ([],toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   164
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   165
	R
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   166
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   167
*)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   168
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   169
(* A tail-recursive version of repeat.  It is (ever so) slightly slower
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   170
 * than the above, non-tail-recursive version (due to the garbage generation
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   171
 * associated with the reversal of the list).  However,  this version will be
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   172
 * able to process input where the former version must give up (due to stack
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   173
 * overflow).  The slowdown seems to be around the one percent mark.
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   174
 *)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   175
fun repeat sc =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   176
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   177
	fun R xs toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   178
	    case SOME (sc toks) handle SyntaxError => NONE of
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   179
		SOME (x,toks2) => R (x::xs) toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   180
	      | NONE => (List.rev xs,toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   181
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   182
	R []
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   183
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   184
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   185
fun repeat1 sc toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   186
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   187
	val (x,toks2) = sc toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   188
	val (xs,toks3) = repeat sc toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   189
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   190
	(x::xs,toks3)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   191
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   192
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   193
fun repeat_fixed n sc =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   194
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   195
	fun R n xs toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   196
	    if (n <= 0) then (List.rev xs, toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   197
	    else case (sc toks) of (x, toks2) => R (n-1) (x::xs) toks2
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   198
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   199
	R n []
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   200
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   201
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   202
fun ahead (sc:'a->'b*'a) toks = (#1 (sc toks),toks)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   203
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   204
fun unless test sc toks =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   205
    let
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   206
	val test_failed = (test toks;false) handle SyntaxError => true
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   207
    in
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   208
	if test_failed
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   209
	then sc toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   210
	else raise SyntaxError
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   211
    end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   212
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   213
fun !! f sc toks = (sc toks
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   214
		    handle SyntaxError => raise Fail (f toks))
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   215
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   216
end
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   217
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   218
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   219
structure StringScanner : STRING_SCANNER =
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   220
struct
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   221
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   222
structure Scan = MonoScanner(structure Seq = StringScannerSeq)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   223
open Scan
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   224
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   225
fun $$ arg = one (fn x => x = arg)
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   226
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   227
val scan_id = one Symbol.is_letter ^^ (any Symbol.is_letdig >> implode);
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   228
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   229
val nat_of_list = the o Int.fromString o implode 
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   230
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   231
val scan_nat = repeat1 (one Symbol.is_digit) >> nat_of_list 
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   232
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   233
fun this [] = (fn toks => ([], toks))
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   234
  | this (xs' as (x::xs)) = one (fn y => x=y) -- this xs >> K xs'
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   235
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   236
fun this_string s = this (explode s) >> K s
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   237
6d584f9d2021 use monomorphic sequences / scanners
obua
parents:
diff changeset
   238
end