syntax.mli 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
(* The type [partial_grammar] describes the abstract syntax that is produced
   by the parsers (yacc-parser and fancy-parser).

   The type [grammar] describes the abstract syntax that is obtained after one
   or more partial grammars are joined (see [PartialGrammar]). It differs in
   that declarations are organized in a more useful way and a number of
   well-formedness checks have been performed. *)

(* ------------------------------------------------------------------------ *)

11 12 13 14 15 16 17 18
(* Terminals and nonterminal symbols are strings. Identifiers
   (which are used to refer to a symbol's semantic value) are
   strings. A file name is a string. *)

type terminal =
    string

type nonterminal =
POTTIER Francois's avatar
POTTIER Francois committed
19
    string
20 21

type symbol =
POTTIER Francois's avatar
POTTIER Francois committed
22
    string
23

POTTIER Francois's avatar
POTTIER Francois committed
24 25
type identifier =
    string
26

POTTIER Francois's avatar
POTTIER Francois committed
27
type filename =
28 29
    string

30 31
(* ------------------------------------------------------------------------ *)

32
(* A trailer is a source file fragment. *)
33 34

type trailer =
35
    Stretch.t
36

37 38
(* ------------------------------------------------------------------------ *)

39
(* OCaml semantic actions are represented as stretches. *)
40 41 42 43

type action =
    Action.t

44 45 46 47
(* ------------------------------------------------------------------------ *)

(* Information about tokens. *)

POTTIER Francois's avatar
POTTIER Francois committed
48 49
type token_associativity =
    LeftAssoc
50 51 52 53
  | RightAssoc
  | NonAssoc
  | UndefinedAssoc

POTTIER Francois's avatar
POTTIER Francois committed
54 55
type precedence_level =
    UndefinedPrecedence
56 57 58 59 60 61

  (* Items are incomparable when they originate in different files. A
     brand of type [Mark.t] is used to record an item's origin. The
     positions allow locating certain warnings. *)

  | PrecedenceLevel of Mark.t * int * Lexing.position * Lexing.position
POTTIER Francois's avatar
POTTIER Francois committed
62

63 64
type token_properties =
    {
65 66 67
               tk_filename      : filename;
               tk_ocamltype     : Stretch.ocamltype option;
               tk_position      : Positions.t;
68
      mutable  tk_associativity : token_associativity;
69
      mutable  tk_precedence    : precedence_level;
POTTIER Francois's avatar
POTTIER Francois committed
70
      mutable  tk_is_declared   : bool;
71 72
    }

73 74
(* ------------------------------------------------------------------------ *)

POTTIER Francois's avatar
POTTIER Francois committed
75
type parameter =
76 77 78
  | ParameterVar of symbol Positions.located
  | ParameterApp of symbol Positions.located * parameters

POTTIER Francois's avatar
POTTIER Francois committed
79
and parameters =
80 81 82 83
    parameter list

type declaration =

84
    (* Raw OCaml code. *)
85 86 87

  | DCode of Stretch.t

88
    (* Raw OCaml functor parameter. *)
89 90 91 92 93 94 95 96 97

  | DParameter of Stretch.ocamltype (* really a stretch *)

    (* Terminal symbol (token) declaration. *)

  | DToken of Stretch.ocamltype option * terminal

    (* Start symbol declaration. *)

POTTIER Francois's avatar
POTTIER Francois committed
98
  | DStart of nonterminal
99 100 101 102 103 104 105 106 107

    (* Priority and associativity declaration. *)

  | DTokenProperties of terminal * token_associativity * precedence_level

    (* Type declaration. *)

  | DType of Stretch.ocamltype * parameter

108 109 110 111
    (* On-error-reduce declaration. *)

  | DOnErrorReduce of parameter

112
(* A [%prec] annotation is optional. A production can carry at most one.
113
   If there is one, it is a symbol name. See [ParserAux]. *)
114 115

type branch_prec_annotation =
116 117
    symbol Positions.located option

118 119 120 121
(* A "production level" is used to solve reduce/reduce conflicts. It reflects
   which production appears first in the grammar. See [ParserAux]. *)

type branch_production_level =
122
  | ProductionLevel of Mark.t * int
123 124

type producer =
125
    identifier Positions.located * parameter
126 127

type parameterized_branch =
POTTIER Francois's avatar
POTTIER Francois committed
128
    {
129 130
      pr_branch_position           : Positions.t;
      pr_producers                 : producer list;
POTTIER Francois's avatar
POTTIER Francois committed
131
      pr_action                    : action;
132
      pr_branch_prec_annotation    : branch_prec_annotation;
133
      pr_branch_production_level   : branch_production_level
134 135 136 137
    }

type parameterized_rule =
    {
138 139 140 141 142 143
      pr_public_flag       : bool;
      pr_inline_flag       : bool;
      pr_nt                : nonterminal;
      pr_positions         : Positions.t list;
      pr_parameters        : symbol list;
      pr_branches          : parameterized_branch list;
144 145
    }

146
type partial_grammar =
147 148 149 150 151 152
    {
      pg_filename          : filename;
      pg_declarations      : declaration Positions.located list;
      pg_rules             : parameterized_rule list;
      pg_trailer           : trailer option;
    }
153 154 155 156 157 158 159 160 161 162 163 164

type grammar =
    {
      p_preludes           : Stretch.t list;
      p_postludes          : trailer list;
      p_parameters         : Stretch.t list;
      p_start_symbols      : Positions.t StringMap.t;
      p_types              : (parameter * Stretch.ocamltype Positions.located) list;
      p_tokens             : token_properties StringMap.t;
      p_rules              : parameterized_rule StringMap.t;
      p_on_error_reduce    : parameter list;
    }