Commit b6bd2193 authored by POTTIER Francois's avatar POTTIER Francois

Cleanup in the construction of the [terminal] and [nonterminal] GADTs.

parent 755db5d9
......@@ -170,11 +170,7 @@ module Terminal = struct
declared using %token) are filtered out. *)
let (n : int), (name : string array), (map : int StringMap.t) =
let tokens =
StringMap.fold (fun token properties tokens ->
if properties.tk_is_declared then token :: tokens else tokens
) Front.grammar.tokens []
in
let tokens = tokens Front.grammar in
match tokens with
| [] ->
Error.error [] "no tokens have been declared."
......
......@@ -26,8 +26,14 @@ let nonterminalgadtdef grammar =
let comment, datadefs =
try
(* The ordering of this list matters. We want the data constructors
to respect the internal ordering (as determined by [nonterminals]
in [UnparameterizedSyntax]) of the nonterminal symbols. This may
be exploited in the table back-end to allow an unsafe conversion
of a data constructor to an integer code. *)
"The indexed type of nonterminal symbols.",
List.fold_left (fun defs nt ->
List.map (fun nt ->
let index =
match ocamltype_of_symbol grammar nt with
| Some t ->
......@@ -39,8 +45,8 @@ let nonterminalgadtdef grammar =
dataname = tnonterminalgadtdata nt;
datavalparams = [];
datatypeparams = Some [ index ]
} :: defs
) [] (nonterminals grammar)
}
) (nonterminals grammar)
with MissingOCamlType nt ->
(* If the type of some nonterminal symbol is unknown, give up
......
......@@ -2,7 +2,6 @@
the [token] type. In particular, if [--only-tokens] was specified,
it emits the type definition and exits. *)
open Syntax
open UnparameterizedSyntax
open IL
open CodeBits
......@@ -31,27 +30,11 @@ let ttokengadtdata token =
let tokentypedef grammar =
let datadefs =
StringMap.fold (fun token properties defs ->
(* Pseudo-tokens (used in %prec declarations, but never
declared using %token) are filtered out. *)
if properties.tk_is_declared then
let params =
match properties.tk_ocamltype with
| None ->
[]
| Some t ->
[ TypTextual t ]
in
{
dataname = token;
datavalparams = params;
datatypeparams = None
} :: defs
else
defs
) grammar.tokens []
List.map (fun (token, typo) -> {
dataname = token;
datavalparams = (match typo with None -> [] | Some t -> [ TypTextual t ]);
datatypeparams = None
}) (typed_tokens grammar)
in
[
IIComment "The type of tokens.";
......@@ -84,23 +67,12 @@ let tokengadtdef grammar =
(* the [error] token has a semantic value of type [unit] *)
} in
let datadefs =
StringMap.fold (fun token properties defs ->
if properties.tk_is_declared then
let index =
match properties.tk_ocamltype with
| None ->
tunit
| Some t ->
TypTextual t
in
{
dataname = ttokengadtdata token;
datavalparams = [];
datatypeparams = Some [ index ]
} :: defs
else
defs
) grammar.tokens [errordata]
errordata ::
List.map (fun (token, typo) -> {
dataname = ttokengadtdata token;
datavalparams = [];
datatypeparams = Some [ match typo with None -> tunit | Some t -> TypTextual t ]
}) (typed_tokens grammar)
in
[
IIComment "The indexed type of terminal symbols.";
......
......@@ -44,6 +44,24 @@ type grammar =
rules : rule StringMap.t;
}
(* [tokens grammar] is a list of all (real) tokens in the grammar
[grammar]. The special tokens "#" and "error" are not included.
Pseudo-tokens (used in %prec declarations, but never declared
using %token) are filtered out. *)
let tokens grammar =
StringMap.fold (fun token properties tokens ->
if properties.tk_is_declared then token :: tokens else tokens
) grammar.tokens []
(* [typed_tokens grammar] is analogous, but includes the OCaml type
of each token. *)
let typed_tokens grammar =
StringMap.fold (fun token properties tokens ->
if properties.tk_is_declared then (token, properties.tk_ocamltype) :: tokens else tokens
) grammar.tokens []
(* [nonterminals grammar] is a list of all nonterminal symbols in the
grammar [grammar]. *)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment