forked from standardml/cmlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.sml
127 lines (107 loc) · 3.43 KB
/
arguments.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
structure Arguments :> ARGUMENTS =
struct
structure Parsing =
ParsingFun (type token = string
structure Streamable =
MonomorphizeStreamable (type elem = string
structure Streamable = ListStreamable))
open Parsing
exception Usage
exception Exit
fun is str = test_ (fn str' => str = str')
val int = wrap (fn str => (case FromString.toInt str of SOME x => x | NONE => raise Usage)) accept
val string = accept
fun eol s =
(case s of
[] => ((), [])
| _ :: _ => raise Usage)
fun exec f = wrap f (return ())
fun call f = wrap f accept
fun assign r p = wrap (fn x => r := x) p
fun set r = exec (fn () => r := true)
fun clear r = exec (fn () => r := false)
fun full (str : string) p s =
(case s of
[] =>
raise SyntaxError
| str' :: s' =>
if str = str' then
p s'
else
raise SyntaxError)
fun prefix str p s =
(case s of
[] =>
raise SyntaxError
| str' :: s' =>
if String.isPrefix str str' then
p (String.extract (str', String.size str, NONE) :: s')
else
raise SyntaxError)
fun prefix' str p s =
(case s of
[] =>
raise SyntaxError
| str' :: s' =>
if String.isPrefix str str' then
p s
else
raise SyntaxError)
fun flex str p s =
(case s of
[] =>
raise SyntaxError
| str' :: s' =>
if String.isPrefix str str' then
let
val str'' = String.extract (str', String.size str, NONE)
in
if str'' = "" then
p s'
else
p (str'' :: s')
end
else
raise SyntaxError)
fun scan pl s =
let
fun loop pl' s acc =
(case pl' of
[] =>
(case s of
[] =>
((), rev acc)
| str :: s' =>
loop pl s' (str :: acc))
| p :: rest =>
(let
val (x, s') = p s
in
loop pl s' acc
end
handle SyntaxError => loop rest s acc))
in
loop pl s []
end
fun scanStrict pl s =
let
fun loop pl' s =
(case pl' of
[] =>
((), s)
| p :: rest =>
(let
val (x, s') = p s
in
loop pl s'
end
handle SyntaxError => loop rest s))
in
loop pl s
end
fun parse p usage s =
(p s; ())
handle Exit => ()
| Usage => print usage
| SyntaxError => print usage
end