Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 2017 Jason Carr. |
2 | * | |
3 | * MLton is released under a BSD-style license. | |
4 | * See the file MLton-LICENSE for details. | |
5 | *) | |
6 | ||
7 | signature PARSE = | |
8 | sig | |
9 | type 'a t | |
10 | structure State: | |
11 | sig | |
12 | type t | |
13 | end | |
14 | structure Location: | |
15 | sig | |
16 | type t = {line: int, column: int} | |
17 | end | |
18 | ||
19 | val parseFile: 'a t * File.t -> 'a Result.t | |
20 | val parseStream: 'a t * char Stream.t -> 'a Result.t | |
21 | val parseString: 'a t * string -> 'a Result.t | |
22 | ||
23 | (* | |
24 | * infix 1 <|> >>= | |
25 | * infix 3 <*> <* *> | |
26 | * infixr 4 <$> <$$> <$$$> <$ <$?> | |
27 | *) | |
28 | val >>= : 'a t * ('a -> 'b t) -> 'b t | |
29 | val <*> : ('a -> 'b) t * 'a t -> 'b t | |
30 | val <$> : ('a -> 'b) * 'a t -> 'b t | |
31 | (* replace the result of a parser witih a constant *) | |
32 | val <$ : 'b * 'a t -> 'b t | |
33 | (* map over pairs of parsers, joining their results together *) | |
34 | val <$$> : ('a * 'b -> 'c) * ('a t * 'b t) -> 'c t | |
35 | val <$$$> : ('a * 'b * 'c -> 'd) * ('a t * 'b t * 'c t) -> 'd t | |
36 | val <$?> : ('a -> 'b option) * 'a t -> 'b t | |
37 | (* match both parsers, and discard the right or left result respectively *) | |
38 | val <* : 'a t * 'b t -> 'a t | |
39 | val *> : 'a t * 'b t -> 'b t | |
40 | (* try both parsers, take the result of the first success *) | |
41 | val <|> : 'a t * 'a t -> 'a t | |
42 | (* try both parsers, fail if either fails, or take the last success *) | |
43 | structure Ops : sig | |
44 | val >>= : 'a t * ('a -> 'b t) -> 'b t | |
45 | val <*> : ('a -> 'b) t * 'a t -> 'b t | |
46 | val <$> : ('a -> 'b) * 'a t -> 'b t | |
47 | val <$ : 'b * 'a t -> 'b t | |
48 | val <$$> : ('a * 'b -> 'c) * ('a t * 'b t) -> 'c t | |
49 | val <$$$> : ('a * 'b * 'c -> 'd) * ('a t * 'b t * 'c t) -> 'd t | |
50 | val <$?> : ('a -> 'b option) * 'a t -> 'b t | |
51 | val <* : 'a t * 'b t -> 'a t | |
52 | val *> : 'a t * 'b t -> 'b t | |
53 | val <|> : 'a t * 'a t -> 'a t | |
54 | end | |
55 | ||
56 | val pure: 'a -> 'a t | |
57 | (* succeeds if any of the parsers succeed *) | |
58 | val any: 'a t list -> 'a t | |
59 | (* matches the given character *) | |
60 | val char: char -> char t | |
61 | (* composes two parsers in turn, the characters used for the second come | |
62 | * from the first *) | |
63 | val compose : char list t * 'a t -> 'a t | |
64 | (* if the parser fails, it will fail as a cut *) | |
65 | val cut: 'a t -> 'a t | |
66 | (* delays a stream lazily, for recursive combinations *) | |
67 | val delay: (unit -> 'a t) -> 'a t | |
68 | (* succeeds if all of the parsers succeed and combines their results *) | |
69 | val each: 'a t list -> 'a list t | |
70 | (* fail with a specified error message *) | |
71 | val fail: string -> 'a t | |
72 | (* as fail, but also cuts at the next choice point *) | |
73 | val failCut: string -> 'a t | |
74 | (* succeeds if and only if its argument fails to parse *) | |
75 | val failing: 'a t -> unit t | |
76 | (* return the parser representation of a given reader *) | |
77 | val fromReader: (State.t -> ('a * State.t) option)-> 'a t | |
78 | (* returns the current source location of the parser *) | |
79 | val location: Location.t t | |
80 | (* succeeds for each time the parser succeeds in succession *) | |
81 | val many: 'a t -> 'a list t | |
82 | (* as many, but one or more, rather than zero or more times *) | |
83 | val many1: 'a t -> 'a list t | |
84 | (* as many, but with failures of the second parser before each parse from the first *) | |
85 | val manyFailing: ('a t * 'b t) -> 'a list t | |
86 | (* manyFailing, specialized to the case that p is "next" *) | |
87 | val manyCharsFailing: 'a t -> char list t | |
88 | (* gets the next char of input *) | |
89 | val next: char t | |
90 | (* as sat, specialized to "next" *) | |
91 | val nextSat: (char -> bool) -> char t | |
92 | (* succeeds if the first parser succeeds and the second fails | |
93 | * the second parser will never consume any input *) | |
94 | val notFollowedBy: 'a t * 'b t -> 'a t | |
95 | (* succeeds with SOME if the parser succeeded and NONE otherwise *) | |
96 | val optional: 'a t -> 'a option t | |
97 | (* parse an integer, as Integer.scan StringCVT.DEC *) | |
98 | val int: int t | |
99 | val intInf: IntInf.t t | |
100 | (* run a parser but consume no input *) | |
101 | val peek: 'a t -> 'a t | |
102 | (* succeeds if the parser succeeded with an input that passed the predicate *) | |
103 | val sat: 'a t * ('a -> bool) -> 'a t | |
104 | (* as many, but an instance of the second parser separates each instance *) | |
105 | val sepBy: 'a t * 'b t -> 'a list t | |
106 | (* as many1, but an instance of the second parser separates each instance *) | |
107 | val sepBy1: 'a t * 'b t -> 'a list t | |
108 | val space: char t | |
109 | val spaces: char list t | |
110 | val str: string -> string t | |
111 | (* returns a reader representation of the parser *) | |
112 | val toReader: 'a t -> State.t -> ('a * State.t) option | |
113 | val tuple: 'a t -> 'a vector t | |
114 | (* parse a decimal word *) | |
115 | val uint: int t | |
116 | val uintInf: IntInf.t t | |
117 | val vector: 'a t -> 'a vector t | |
118 | end |