Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 150 lines (112 sloc) 3.915 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18
19 (**
20 Console Parser for interpreters.
21
22 @author Mathieu Barbin
23 *)
24
25 (**
26 This module is a simple tool for accumulating an input of the user,
27 until it parse a toplevel separator [";;"].
28
29 This is used in [qmltop], [qmlcompilers].
30
31 There is a support for compilers or interpreters directives,
32 this is about any separated line beginning with a sharp,
33 as in
34 {[
35 v = 5
36 ;;
37 #typer off;;
38 etc...
39 ]}
40
41 Comment:
42
43 + any thing in the same line than the semicolon separator is removed
44 {[
45 g = "toto" ;; this is a comment removed by the console parser
46 ]}
47 *)
48
49 (** {6 Known bug} *)
50
51 (**
52 This approch has some negative points :
53 + it behave very badly if a string or a comment contains a toplevel separator [";;"]
54
55 *)
56
57 (** {6 Console Parser} *)
58 (**
59 Some infos about the implementation :
60 + Use an intern imperative Buffer
61
62 + it performs a [String.rtrim] on the input.
63 *)
64
65 (**
66 The type of a console parser. Statefull
67 *)
68 type t
69
70 (**
71 Create a new console parser
72 *)
73 val create : unit -> t
74
75 (**
76 The type of a parsed entity.
77
78 In case a directive is parsed, the sharp char is keeped in the returned string.
79 *)
80 type input = Directive of string | Code of string
81
82 (** Reseting the state of the internal Buffer *)
83 val reset : t -> unit
84
85 (**
86 Accumulate a line, a return possibly something, or just accumulate the input.
87
88 The accumulate function will add a newline to the Buffer.
89 <!> The new line char should not be given to the string.
90
91 Typically, the correct use is :
92 {[
93 while true do
94 match ConsoleParser.accumulate (input_line stdin) with
95 | Some (ConsoleParser.Directive s) ->
96 | Some (ConsoleParser.Code s) ->
97 | None ->
98 done
99 ]}
100 *)
101 val accumulate : t -> string -> input option
102
103 (** Flush the last input. It is like [accumulate ";;"] *)
104 val flush : t -> input option
105
106
107 module Directive :
108 sig
109 (** {6 Support for directives} *)
110
111 (**
112 Directives in an interpreter are a way to interact with the it,
113 e.g. to modify a global property or an option once the loop is already started.
114
115 The syntax for directives is defined by the [ConsoleParser], which is used
116 in the interpreter for parsing the input.
117
118 {[
119 #directives [arguments] ;;
120 ]}
121
122 A directive can for example just modify the state of the interpreter, by modifying
123 some option related to its behavior (like [#typer on/off]) or enrich the environment
124 , as in [#load <file>].
125
126 The interface of directives is : [extra-arguments -> env -> directives arguments -> env],
127 so that we can handle all possible cases. (the env returned should be the same as the
128 input env in case of just a switch of a property.)
129 *)
130
131 type arguments = string list
132 type 'env action = 'env -> arguments -> 'env
133
134 (**
135 For parsing directive, we use a regexpr collection.
136 All regexp are successively used to match the input.
137 Then, we apply the first matching regexp action.
138 *)
139
140 type regexp = string
141 type argument_number = int
142
143 type 'env directive = regexp * argument_number * 'env action
144
145 type 'env handler
146 val empty : unit -> 'env handler
147 val add : 'env handler -> 'env directive -> 'env handler
148 val parse : 'env handler -> 'env -> string -> 'env option
149 end
Something went wrong with that request. Please try again.