IncrementalEngine.ml 3.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
(* This signature describes the incremental LR engine. *)

(* In this mode, the user controls the lexer, and the parser suspends
   itself when it needs to read a new token. *)

module type INCREMENTAL_ENGINE = sig

  type token

  (* The type ['a result] represents an intermediate or final result of the
11
12
13
14
15
16
17
18
19
20
21
     parser. An intermediate result is a suspension: it records the parser's
     current state, and allows parsing to be resumed. The parameter ['a] is
     the type of the semantic value that will eventually be produced if the
     parser succeeds. *)

  (* [Accepted] and [Rejected] are final results. [Accepted] carries a
     semantic value. *)

  (* [InputNeeded] is an intermediate result. It means that the parser wishes
     to read one token before continuing. *)

22
23
24
25
26
27
28
29
30
31
  (* [AboutToReduce] is an intermediate result. It means that the parser is
     about to perform a reduction step. It does not need more input at this
     point. The parser suspends itself at this point only in order to give the
     user an opportunity to observe this reduction step. *)

  (* [HandlingError] is an intermediate result. It means that the parser has
     detected an error and is currently handling it, in several steps. It does
     not need more input at this point. The parser suspends itself at this
     point only in order to give the user an opportunity to handle this error
     in a different manner, if desired. *)
32

33
  type env
34

35
36
  type production

37
38
39
40
  type 'a result = private
    | InputNeeded of env
    | AboutToReduce of env * production
    | HandlingError of env
41
42
43
44
    | Accepted of 'a
    | Rejected

  (* [offer] allows the user to resume the parser after it has suspended
45
46
47
     itself with a result of the form [InputNeeded env]. [offer] expects the
     old result as well as a new token and produces a new result. It does not
     raise any exception. *)
48
49

  val offer:
50
    'a result ->
51
52
53
    token * Lexing.position * Lexing.position ->
    'a result

54
55
56
57
  (* [resume] allows the user to resume the parser after it has suspended
     itself with a result of the form [AboutToReduce (env, prod)] or
     [HandlingError env]. [resume] expects the old result and produces a new
     result. It does not raise any exception. *)
58

59
60
  val resume:
    'a result ->
61
62
    'a result

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
  (* The abstract type ['a lr1state] describes the non-initial states of the
     LR(1) automaton. The index ['a] represents the type of the semantic value
     associated with this state's incoming symbol. *)

  type 'a lr1state

  (* An element is a pair of a non-initial state [s] and a semantic value [v]
     associated with the incoming symbol of this state. The idea is, the value
     [v] was pushed onto the stack just before the state [s] was entered. Thus,
     for some type ['a], the type [s] has type ['a lr1state] and the value [v]
     has type ['a]. In other words, the type [element] is an existential type. *)

  type element =
    | Element: 'a lr1state * 'a * Lexing.position * Lexing.position -> element

  (* A stream is a list whose elements are produced on demand. *)

  type 'a stream =
      'a head Lazy.t

  and 'a head =
    | Nil
    | Cons of 'a * 'a stream

  (* We offer a read-only view of the parser's state as a stream of elements. *)

89
  val view: env -> element stream
90

91
end
92
93
94
95
96

(* TEMPORARY comment/document *)

module type INSPECTION = sig

97
  type 'a lr1state
98
99
100

  type production

101
102
103
104
105
106
  type 'a symbol

  type xsymbol

  val symbol: 'a lr1state -> 'a symbol

107
108
109
110
111
112
  val lhs: production -> xsymbol

  val rhs: production -> xsymbol list

end