src/Pure/Syntax/parser.ML
author wenzelm
Wed, 25 Sep 2024 15:06:12 +0200
changeset 80958 0de153a15095
parent 80957 8aff3182ef82
child 80959 4749c9b0ba73
permissions -rw-r--r--
eliminate unused prod_count (see also 7afca3218b65);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Syntax/parser.ML
67545
wenzelm
parents: 67544
diff changeset
     2
    Author:     Carsten Clasohm, Sonia Mahjoub
wenzelm
parents: 67544
diff changeset
     3
    Author:     Makarius
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     4
67545
wenzelm
parents: 67544
diff changeset
     5
General context-free parser for the inner syntax of terms and types.
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     6
*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     7
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     8
signature PARSER =
15752
8cdc6249044b added make_gram;
wenzelm
parents: 15570
diff changeset
     9
sig
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    10
  type gram
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    11
  val empty_gram: gram
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
    12
  val make_gram: Syntax_Ext.xprod list -> Syntax_Ext.xprod list -> gram option -> gram
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    13
  val pretty_gram: gram -> Pretty.T list
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    14
  datatype parsetree =
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    15
    Node of string * parsetree list |
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    16
    Tip of Lexicon.token
80950
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
    17
  val pretty_parsetree: parsetree -> Pretty.T list
45641
20ef9135a9fb removed obsolete argument (cf. 954e9d6782ea);
wenzelm
parents: 45632
diff changeset
    18
  val parse: gram -> string -> Lexicon.token list -> parsetree list
41378
55286df6a423 configuration option "syntax_branching_level";
wenzelm
parents: 40959
diff changeset
    19
  val branching_level: int Config.T
15752
8cdc6249044b added make_gram;
wenzelm
parents: 15570
diff changeset
    20
end;
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1438
diff changeset
    21
15752
8cdc6249044b added make_gram;
wenzelm
parents: 15570
diff changeset
    22
structure Parser: PARSER =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    23
struct
15752
8cdc6249044b added make_gram;
wenzelm
parents: 15570
diff changeset
    24
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    25
(** datatype gram **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    26
67541
wenzelm
parents: 67540
diff changeset
    27
(* nonterminals *)
wenzelm
parents: 67540
diff changeset
    28
67545
wenzelm
parents: 67544
diff changeset
    29
(*production for the NTs are stored in a vector, indexed by the NT tag*)
67541
wenzelm
parents: 67540
diff changeset
    30
type nt = int;
wenzelm
parents: 67540
diff changeset
    31
67545
wenzelm
parents: 67544
diff changeset
    32
type tags = nt Symtab.table;
wenzelm
parents: 67544
diff changeset
    33
val tags_empty: tags = Symtab.empty;
wenzelm
parents: 67544
diff changeset
    34
fun tags_content (tags: tags) = sort_by #1 (Symtab.dest tags);
wenzelm
parents: 67544
diff changeset
    35
fun tags_lookup (tags: tags) = Symtab.lookup tags;
wenzelm
parents: 67544
diff changeset
    36
fun tags_insert tag (tags: tags) = Symtab.update_new tag tags;
78006
wenzelm
parents: 78005
diff changeset
    37
fun tags_name (tags: tags) =
wenzelm
parents: 78005
diff changeset
    38
  the o Inttab.lookup (Inttab.build (Symtab.fold (Inttab.update_new o swap) tags));
67545
wenzelm
parents: 67544
diff changeset
    39
79079
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    40
type nts = Bitset.T;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    41
val nts_empty: nts = Bitset.empty;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    42
val nts_merge: nts * nts -> nts = Bitset.merge;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    43
fun nts_insert nt : nts -> nts = Bitset.insert nt;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    44
fun nts_member (nts: nts) = Bitset.member nts;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    45
fun nts_fold f (nts: nts) = Bitset.fold f nts;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    46
fun nts_subset (nts1: nts, nts2: nts) = Bitset.forall (nts_member nts2) nts1;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    47
fun nts_is_empty (nts: nts) = Bitset.is_empty nts;
01f9128ec655 more compact representation;
wenzelm
parents: 78009
diff changeset
    48
fun nts_is_unique (nts: nts) = Bitset.is_unique nts;
67541
wenzelm
parents: 67540
diff changeset
    49
wenzelm
parents: 67540
diff changeset
    50
67539
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
    51
(* tokens *)
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
    52
77823
wenzelm
parents: 77818
diff changeset
    53
structure Tokens = Set(type key = Lexicon.token val ord = Lexicon.tokens_match_ord);
67539
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
    54
77823
wenzelm
parents: 77818
diff changeset
    55
fun tokens_find P tokens = Tokens.get_first (fn tok => if P tok then SOME tok else NONE) tokens;
wenzelm
parents: 77818
diff changeset
    56
fun tokens_add (nt: nt, tokens) = if Tokens.is_empty tokens then I else cons (nt, tokens);
67533
f253e5eaf995 clarified types and operations: potentially more efficient add_prods;
wenzelm
parents: 67532
diff changeset
    57
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
    58
67545
wenzelm
parents: 67544
diff changeset
    59
(* productions *)
wenzelm
parents: 67544
diff changeset
    60
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    61
datatype symb =
67545
wenzelm
parents: 67544
diff changeset
    62
  Terminal of Lexicon.token |
wenzelm
parents: 67544
diff changeset
    63
  Nonterminal of nt * int;  (*(tag, prio)*)
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    64
77823
wenzelm
parents: 77818
diff changeset
    65
structure Prods = Table(Tokens.Key);
wenzelm
parents: 77818
diff changeset
    66
type prods = (symb list * string * int) list Prods.table;  (*start_token ~> [(rhs, name, prio)]*)
67540
15297abe6472 tuned data structure and operations;
wenzelm
parents: 67539
diff changeset
    67
77823
wenzelm
parents: 77818
diff changeset
    68
val prods_empty: prods = Prods.empty;
wenzelm
parents: 77818
diff changeset
    69
fun prods_lookup (prods: prods) = Prods.lookup_list prods;
wenzelm
parents: 77818
diff changeset
    70
fun prods_update entry : prods -> prods = Prods.update entry;
wenzelm
parents: 77818
diff changeset
    71
fun prods_content (prods: prods) = distinct (op =) (maps #2 (Prods.dest prods));
67540
15297abe6472 tuned data structure and operations;
wenzelm
parents: 67539
diff changeset
    72
77823
wenzelm
parents: 77818
diff changeset
    73
type nt_gram = (nts * Tokens.T) * prods; (*dependent_nts, start_tokens, prods*)
67545
wenzelm
parents: 67544
diff changeset
    74
  (*depent_nts is a set of all NTs whose lookahead depends on this NT's lookahead*)
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
    75
77823
wenzelm
parents: 77818
diff changeset
    76
val nt_gram_empty: nt_gram = ((nts_empty, Tokens.empty), prods_empty);
67533
f253e5eaf995 clarified types and operations: potentially more efficient add_prods;
wenzelm
parents: 67532
diff changeset
    77
67531
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    78
type chains = unit Int_Graph.T;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    79
fun chains_preds (chains: chains) = Int_Graph.immediate_preds chains;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    80
fun chains_all_preds (chains: chains) = Int_Graph.all_preds chains;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    81
fun chains_all_succs (chains: chains) = Int_Graph.all_succs chains;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    82
val chains_empty: chains = Int_Graph.empty;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    83
fun chains_member (chains: chains) = Int_Graph.is_edge chains;
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    84
fun chains_declare nt : chains -> chains = Int_Graph.default_node (nt, ());
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    85
fun chains_insert (from, to) =
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    86
  chains_declare from #> chains_declare to #> Int_Graph.add_edge (from, to);
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    87
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
    88
datatype gram =
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    89
  Gram of
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    90
   {nt_count: int,
67531
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    91
    tags: tags,
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
    92
    chains: chains,
67533
f253e5eaf995 clarified types and operations: potentially more efficient add_prods;
wenzelm
parents: 67532
diff changeset
    93
    lambdas: nts,
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    94
    prods: nt_gram Vector.vector};
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    95
    (*"tags" is used to map NT names (i.e. strings) to tags;
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    96
     chain productions are not stored as normal productions
67517
add9a9f6a290 explicit graph for chains, which contains all nts as nodes;
wenzelm
parents: 67516
diff changeset
    97
     but instead as an entry in "chains": from -> to;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    98
     lambda productions are stored as normal productions
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
    99
     and also as an entry in "lambdas"*)
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   100
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   101
(*productions for which no starting token is
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   102
  known yet are associated with this token*)
38713
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   103
val unknown_start = Lexicon.eof;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   104
67539
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
   105
fun get_start tks =
77823
wenzelm
parents: 77818
diff changeset
   106
  (case Tokens.min tks of
77818
d1ad58e5fc95 performance tuning: replace Table() by Set();
wenzelm
parents: 77741
diff changeset
   107
    SOME tk => tk
67539
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
   108
  | NONE => unknown_start);
67516
wenzelm
parents: 67515
diff changeset
   109
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   110
fun add_production array_prods (lhs, new_prod as (rhs, _, pri)) (chains, lambdas) =
67543
wenzelm
parents: 67542
diff changeset
   111
  let
wenzelm
parents: 67542
diff changeset
   112
    (*store chain if it does not already exist*)
wenzelm
parents: 67542
diff changeset
   113
    val (chain, new_chain, chains') =
wenzelm
parents: 67542
diff changeset
   114
      (case (pri, rhs) of
wenzelm
parents: 67542
diff changeset
   115
        (~1, [Nonterminal (from, ~1)]) =>
wenzelm
parents: 67542
diff changeset
   116
          if chains_member chains (from, lhs)
wenzelm
parents: 67542
diff changeset
   117
          then (SOME from, false, chains)
wenzelm
parents: 67542
diff changeset
   118
          else (SOME from, true, chains_insert (from, lhs) chains)
71468
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   119
      | _ =>
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   120
        let
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   121
          val chains' = chains
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   122
            |> chains_declare lhs
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   123
            |> fold (fn Nonterminal (nt, _) => chains_declare nt | _ => I) rhs;
53fcbede7bf7 more robust (amending add9a9f6a290): proper syntax error instead of exception for grammar with unreachable nonterminals, e.g. nonterminal f1 syntax "_F" :: "f1 ⇒ 'b" ("F _" 10);
wenzelm
parents: 69575
diff changeset
   124
        in (NONE, false, chains') end);
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   125
67543
wenzelm
parents: 67542
diff changeset
   126
    (*propagate new chain in lookahead and lambdas;
wenzelm
parents: 67542
diff changeset
   127
      added_starts is used later to associate existing
wenzelm
parents: 67542
diff changeset
   128
      productions with new starting tokens*)
wenzelm
parents: 67542
diff changeset
   129
    val (added_starts, lambdas') =
wenzelm
parents: 67542
diff changeset
   130
      if not new_chain then ([], lambdas)
wenzelm
parents: 67542
diff changeset
   131
      else
wenzelm
parents: 67542
diff changeset
   132
        let (*lookahead of chain's source*)
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   133
          val ((_, from_tks), _) = Array.nth array_prods (the chain);
1175
1b15a4b1a4f7 added comments; fixed a bug; reduced memory usage slightly
clasohm
parents: 1147
diff changeset
   134
67543
wenzelm
parents: 67542
diff changeset
   135
          (*copy from's lookahead to chain's destinations*)
wenzelm
parents: 67542
diff changeset
   136
          fun copy_lookahead to =
wenzelm
parents: 67542
diff changeset
   137
            let
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   138
              val ((to_nts, to_tks), ps) = Array.nth array_prods to;
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   139
77823
wenzelm
parents: 77818
diff changeset
   140
              val new_tks = Tokens.subtract to_tks from_tks;  (*added lookahead tokens*)
wenzelm
parents: 77818
diff changeset
   141
              val to_tks' = Tokens.merge (to_tks, new_tks);
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   142
              val _ = Array.upd array_prods to ((to_nts, to_tks'), ps);
67543
wenzelm
parents: 67542
diff changeset
   143
            in tokens_add (to, new_tks) end;
1175
1b15a4b1a4f7 added comments; fixed a bug; reduced memory usage slightly
clasohm
parents: 1147
diff changeset
   144
67543
wenzelm
parents: 67542
diff changeset
   145
          val tos = chains_all_succs chains' [lhs];
wenzelm
parents: 67542
diff changeset
   146
        in
wenzelm
parents: 67542
diff changeset
   147
          (fold copy_lookahead tos [],
wenzelm
parents: 67542
diff changeset
   148
            lambdas |> nts_member lambdas lhs ? fold nts_insert tos)
wenzelm
parents: 67542
diff changeset
   149
        end;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   150
67543
wenzelm
parents: 67542
diff changeset
   151
    (*test if new production can produce lambda
wenzelm
parents: 67542
diff changeset
   152
      (rhs must either be empty or only consist of lambda NTs)*)
wenzelm
parents: 67542
diff changeset
   153
    val new_lambdas =
wenzelm
parents: 67542
diff changeset
   154
      if forall
wenzelm
parents: 67542
diff changeset
   155
        (fn Nonterminal (id, _) => nts_member lambdas' id
wenzelm
parents: 67542
diff changeset
   156
          | Terminal _ => false) rhs
wenzelm
parents: 67542
diff changeset
   157
      then SOME (filter_out (nts_member lambdas') (chains_all_succs chains' [lhs]))
wenzelm
parents: 67542
diff changeset
   158
      else NONE;
wenzelm
parents: 67542
diff changeset
   159
    val lambdas'' = fold nts_insert (these new_lambdas) lambdas';
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   160
67543
wenzelm
parents: 67542
diff changeset
   161
    (*list optional terminal and all nonterminals on which the lookahead
wenzelm
parents: 67542
diff changeset
   162
      of a production depends*)
wenzelm
parents: 67542
diff changeset
   163
    fun lookahead_dependency _ [] nts = (NONE, nts)
wenzelm
parents: 67542
diff changeset
   164
      | lookahead_dependency _ (Terminal tk :: _) nts = (SOME tk, nts)
wenzelm
parents: 67542
diff changeset
   165
      | lookahead_dependency lambdas (Nonterminal (nt, _) :: symbs) nts =
wenzelm
parents: 67542
diff changeset
   166
          if nts_member lambdas nt then
wenzelm
parents: 67542
diff changeset
   167
            lookahead_dependency lambdas symbs (nts_insert nt nts)
wenzelm
parents: 67542
diff changeset
   168
          else (NONE, nts_insert nt nts);
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   169
67543
wenzelm
parents: 67542
diff changeset
   170
    (*get all known starting tokens for a nonterminal*)
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   171
    fun starts_for_nt nt = snd (fst (Array.nth array_prods nt));
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   172
67543
wenzelm
parents: 67542
diff changeset
   173
    (*update prods, lookaheads, and lambdas according to new lambda NTs*)
wenzelm
parents: 67542
diff changeset
   174
    val (added_starts', lambdas') =
wenzelm
parents: 67542
diff changeset
   175
      let
wenzelm
parents: 67542
diff changeset
   176
        (*propagate added lambda NT*)
wenzelm
parents: 67542
diff changeset
   177
        fun propagate_lambda [] added_starts lambdas = (added_starts, lambdas)
wenzelm
parents: 67542
diff changeset
   178
          | propagate_lambda (l :: ls) added_starts lambdas =
wenzelm
parents: 67542
diff changeset
   179
              let
wenzelm
parents: 67542
diff changeset
   180
                (*get lookahead for lambda NT*)
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   181
                val ((dependent, l_starts), _) = Array.nth array_prods l;
1175
1b15a4b1a4f7 added comments; fixed a bug; reduced memory usage slightly
clasohm
parents: 1147
diff changeset
   182
67543
wenzelm
parents: 67542
diff changeset
   183
                (*check productions whose lookahead may depend on lambda NT*)
wenzelm
parents: 67542
diff changeset
   184
                fun examine_prods [] add_lambda nt_dependencies added_tks nt_prods =
wenzelm
parents: 67542
diff changeset
   185
                      (add_lambda, nt_dependencies, added_tks, nt_prods)
wenzelm
parents: 67542
diff changeset
   186
                  | examine_prods ((p as (rhs, _, _)) :: ps) add_lambda
wenzelm
parents: 67542
diff changeset
   187
                        nt_dependencies added_tks nt_prods =
wenzelm
parents: 67542
diff changeset
   188
                      let val (tk, nts) = lookahead_dependency lambdas rhs nts_empty in
wenzelm
parents: 67542
diff changeset
   189
                        if nts_member nts l then       (*update production's lookahead*)
wenzelm
parents: 67542
diff changeset
   190
                          let
wenzelm
parents: 67542
diff changeset
   191
                            val new_lambda =
wenzelm
parents: 67542
diff changeset
   192
                              is_none tk andalso nts_subset (nts, lambdas);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   193
67543
wenzelm
parents: 67542
diff changeset
   194
                            val new_tks =
77823
wenzelm
parents: 77818
diff changeset
   195
                              Tokens.empty
wenzelm
parents: 77818
diff changeset
   196
                              |> fold Tokens.insert (the_list tk)
wenzelm
parents: 77818
diff changeset
   197
                              |> nts_fold (curry Tokens.merge o starts_for_nt) nts
wenzelm
parents: 77818
diff changeset
   198
                              |> Tokens.subtract l_starts;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   199
77823
wenzelm
parents: 77818
diff changeset
   200
                            val added_tks' = Tokens.merge (added_tks, new_tks);
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   201
67543
wenzelm
parents: 67542
diff changeset
   202
                            val nt_dependencies' = nts_merge (nt_dependencies, nts);
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   203
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   204
                            (*associate production with new starting tokens*)
67539
1b8aad1909b7 tuned data structure and operations;
wenzelm
parents: 67538
diff changeset
   205
                            fun copy tk nt_prods =
67543
wenzelm
parents: 67542
diff changeset
   206
                              prods_update (tk, p :: prods_lookup nt_prods tk) nt_prods;
wenzelm
parents: 67542
diff changeset
   207
wenzelm
parents: 67542
diff changeset
   208
                            val nt_prods' = nt_prods
77823
wenzelm
parents: 77818
diff changeset
   209
                              |> Tokens.fold copy new_tks
67550
3b666615e3ce explicit dummy token;
wenzelm
parents: 67549
diff changeset
   210
                              |> new_lambda ? copy Lexicon.dummy;
67543
wenzelm
parents: 67542
diff changeset
   211
                          in
wenzelm
parents: 67542
diff changeset
   212
                            examine_prods ps (add_lambda orelse new_lambda)
wenzelm
parents: 67542
diff changeset
   213
                              nt_dependencies' added_tks' nt_prods'
wenzelm
parents: 67542
diff changeset
   214
                          end
wenzelm
parents: 67542
diff changeset
   215
                        else (*skip production*)
wenzelm
parents: 67542
diff changeset
   216
                          examine_prods ps add_lambda nt_dependencies added_tks nt_prods
wenzelm
parents: 67542
diff changeset
   217
                      end;
wenzelm
parents: 67542
diff changeset
   218
wenzelm
parents: 67542
diff changeset
   219
                (*check each NT whose lookahead depends on new lambda NT*)
wenzelm
parents: 67542
diff changeset
   220
                fun process_nts nt (added_lambdas, added_starts) =
wenzelm
parents: 67542
diff changeset
   221
                  let
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   222
                    val ((old_nts, old_tks), nt_prods) = Array.nth array_prods nt;
67543
wenzelm
parents: 67542
diff changeset
   223
wenzelm
parents: 67542
diff changeset
   224
                    (*existing productions whose lookahead may depend on l*)
wenzelm
parents: 67542
diff changeset
   225
                    val tk_prods = prods_lookup nt_prods (get_start l_starts);
wenzelm
parents: 67542
diff changeset
   226
wenzelm
parents: 67542
diff changeset
   227
                    (*add_lambda is true if an existing production of the nt
wenzelm
parents: 67542
diff changeset
   228
                      produces lambda due to the new lambda NT l*)
wenzelm
parents: 67542
diff changeset
   229
                    val (add_lambda, nt_dependencies, added_tks, nt_prods') =
77823
wenzelm
parents: 77818
diff changeset
   230
                      examine_prods tk_prods false nts_empty Tokens.empty nt_prods;
67543
wenzelm
parents: 67542
diff changeset
   231
wenzelm
parents: 67542
diff changeset
   232
                    val new_nts = nts_merge (old_nts, nt_dependencies);
77823
wenzelm
parents: 77818
diff changeset
   233
                    val new_tks = Tokens.merge (old_tks, added_tks);
67543
wenzelm
parents: 67542
diff changeset
   234
wenzelm
parents: 67542
diff changeset
   235
                    val added_lambdas' = added_lambdas |> add_lambda ? cons nt;
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   236
                    val _ = Array.upd array_prods nt ((new_nts, new_tks), nt_prods');
67543
wenzelm
parents: 67542
diff changeset
   237
                      (*N.B. that because the tks component
wenzelm
parents: 67542
diff changeset
   238
                        is used to access existing
wenzelm
parents: 67542
diff changeset
   239
                        productions we have to add new
wenzelm
parents: 67542
diff changeset
   240
                        tokens at the _end_ of the list*)
wenzelm
parents: 67542
diff changeset
   241
                    val added_starts' = tokens_add (nt, added_tks) added_starts;
wenzelm
parents: 67542
diff changeset
   242
                  in (added_lambdas', added_starts') end;
377
ab8917806779 lookaheads are now computed faster (during the grammar is built)
clasohm
parents: 373
diff changeset
   243
67543
wenzelm
parents: 67542
diff changeset
   244
                val (added_lambdas, added_starts') =
wenzelm
parents: 67542
diff changeset
   245
                  nts_fold process_nts dependent ([], added_starts);
wenzelm
parents: 67542
diff changeset
   246
                val added_lambdas' = filter_out (nts_member lambdas) added_lambdas;
wenzelm
parents: 67542
diff changeset
   247
              in
wenzelm
parents: 67542
diff changeset
   248
                propagate_lambda (ls @ added_lambdas') added_starts'
wenzelm
parents: 67542
diff changeset
   249
                  (fold nts_insert added_lambdas' lambdas)
wenzelm
parents: 67542
diff changeset
   250
              end;
wenzelm
parents: 67542
diff changeset
   251
      in
wenzelm
parents: 67542
diff changeset
   252
        propagate_lambda
wenzelm
parents: 67542
diff changeset
   253
          (nts_fold (fn l => not (nts_member lambdas l) ? cons l) lambdas'' [])
wenzelm
parents: 67542
diff changeset
   254
          added_starts lambdas''
wenzelm
parents: 67542
diff changeset
   255
      end;
wenzelm
parents: 67542
diff changeset
   256
wenzelm
parents: 67542
diff changeset
   257
    (*insert production into grammar*)
wenzelm
parents: 67542
diff changeset
   258
    val added_starts' =
wenzelm
parents: 67542
diff changeset
   259
      if is_some chain then added_starts'  (*don't store chain production*)
wenzelm
parents: 67542
diff changeset
   260
      else
wenzelm
parents: 67542
diff changeset
   261
        let
wenzelm
parents: 67542
diff changeset
   262
          (*lookahead tokens of new production and on which
wenzelm
parents: 67542
diff changeset
   263
            NTs lookahead depends*)
wenzelm
parents: 67542
diff changeset
   264
          val (start_tk, start_nts) = lookahead_dependency lambdas' rhs nts_empty;
wenzelm
parents: 67542
diff changeset
   265
wenzelm
parents: 67542
diff changeset
   266
          val start_tks =
77823
wenzelm
parents: 77818
diff changeset
   267
            Tokens.empty
wenzelm
parents: 77818
diff changeset
   268
            |> fold Tokens.insert (the_list start_tk)
wenzelm
parents: 77818
diff changeset
   269
            |> nts_fold (curry Tokens.merge o starts_for_nt) start_nts;
67543
wenzelm
parents: 67542
diff changeset
   270
wenzelm
parents: 67542
diff changeset
   271
          val start_tks' =
wenzelm
parents: 67542
diff changeset
   272
            start_tks
77823
wenzelm
parents: 77818
diff changeset
   273
            |> (if is_some new_lambdas then Tokens.insert Lexicon.dummy
wenzelm
parents: 77818
diff changeset
   274
                else if Tokens.is_empty start_tks then Tokens.insert unknown_start
67543
wenzelm
parents: 67542
diff changeset
   275
                else I);
wenzelm
parents: 67542
diff changeset
   276
wenzelm
parents: 67542
diff changeset
   277
          (*add lhs NT to list of dependent NTs in lookahead*)
wenzelm
parents: 67542
diff changeset
   278
          fun add_nts nt initial =
wenzelm
parents: 67542
diff changeset
   279
            (if initial then
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   280
              let val ((old_nts, old_tks), ps) = Array.nth array_prods nt in
67543
wenzelm
parents: 67542
diff changeset
   281
                if nts_member old_nts lhs then ()
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   282
                else Array.upd array_prods nt ((nts_insert lhs old_nts, old_tks), ps)
67543
wenzelm
parents: 67542
diff changeset
   283
              end
wenzelm
parents: 67542
diff changeset
   284
             else (); false);
wenzelm
parents: 67542
diff changeset
   285
wenzelm
parents: 67542
diff changeset
   286
          (*add new start tokens to chained NTs' lookahead list;
wenzelm
parents: 67542
diff changeset
   287
            also store new production for lhs NT*)
wenzelm
parents: 67542
diff changeset
   288
          fun add_tks [] added = added
wenzelm
parents: 67542
diff changeset
   289
            | add_tks (nt :: nts) added =
wenzelm
parents: 67542
diff changeset
   290
                let
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   291
                  val ((old_nts, old_tks), nt_prods) = Array.nth array_prods nt;
67543
wenzelm
parents: 67542
diff changeset
   292
77823
wenzelm
parents: 77818
diff changeset
   293
                  val new_tks = Tokens.subtract old_tks start_tks;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   294
67543
wenzelm
parents: 67542
diff changeset
   295
                  (*store new production*)
wenzelm
parents: 67542
diff changeset
   296
                  fun store tk (prods, _) =
wenzelm
parents: 67542
diff changeset
   297
                    let
wenzelm
parents: 67542
diff changeset
   298
                      val tk_prods = prods_lookup prods tk;
wenzelm
parents: 67542
diff changeset
   299
                      val tk_prods' = new_prod :: tk_prods;
wenzelm
parents: 67542
diff changeset
   300
                      val prods' = prods_update (tk, tk_prods') prods;
wenzelm
parents: 67542
diff changeset
   301
                    in (prods', true) end;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   302
67543
wenzelm
parents: 67542
diff changeset
   303
                  val (nt_prods', changed) = (nt_prods, false)
77823
wenzelm
parents: 77818
diff changeset
   304
                    |> nt = lhs ? Tokens.fold store start_tks';
67543
wenzelm
parents: 67542
diff changeset
   305
                  val _ =
77823
wenzelm
parents: 77818
diff changeset
   306
                    if not changed andalso Tokens.is_empty new_tks then ()
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   307
                    else Array.upd array_prods nt ((old_nts, Tokens.merge (old_tks, new_tks)), nt_prods');
67543
wenzelm
parents: 67542
diff changeset
   308
                in add_tks nts (tokens_add (nt, new_tks) added) end;
wenzelm
parents: 67542
diff changeset
   309
          val _ = nts_fold add_nts start_nts true;
wenzelm
parents: 67542
diff changeset
   310
        in add_tks (chains_all_succs chains' [lhs]) [] end;
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   311
67543
wenzelm
parents: 67542
diff changeset
   312
    (*associate productions with new lookaheads*)
wenzelm
parents: 67542
diff changeset
   313
    val _ =
wenzelm
parents: 67542
diff changeset
   314
      let
wenzelm
parents: 67542
diff changeset
   315
        (*propagate added start tokens*)
wenzelm
parents: 67542
diff changeset
   316
        fun add_starts [] = ()
wenzelm
parents: 67542
diff changeset
   317
          | add_starts ((changed_nt, new_tks) :: starts) =
wenzelm
parents: 67542
diff changeset
   318
              let
wenzelm
parents: 67542
diff changeset
   319
                (*token under which old productions which
wenzelm
parents: 67542
diff changeset
   320
                  depend on changed_nt could be stored*)
wenzelm
parents: 67542
diff changeset
   321
                val key =
77823
wenzelm
parents: 77818
diff changeset
   322
                  tokens_find (not o Tokens.member new_tks) (starts_for_nt changed_nt)
67543
wenzelm
parents: 67542
diff changeset
   323
                  |> the_default unknown_start;
wenzelm
parents: 67542
diff changeset
   324
wenzelm
parents: 67542
diff changeset
   325
                (*copy productions whose lookahead depends on changed_nt;
wenzelm
parents: 67542
diff changeset
   326
                  if key = SOME unknown_start then tk_prods is used to hold
wenzelm
parents: 67542
diff changeset
   327
                  the productions not copied*)
wenzelm
parents: 67542
diff changeset
   328
                fun update_prods [] result = result
wenzelm
parents: 67542
diff changeset
   329
                  | update_prods ((p as (rhs, _: string, _: nt)) :: ps)
wenzelm
parents: 67542
diff changeset
   330
                        (tk_prods, nt_prods) =
wenzelm
parents: 67542
diff changeset
   331
                      let
wenzelm
parents: 67542
diff changeset
   332
                        (*lookahead dependency for production*)
wenzelm
parents: 67542
diff changeset
   333
                        val (tk, depends) = lookahead_dependency lambdas' rhs nts_empty;
wenzelm
parents: 67542
diff changeset
   334
wenzelm
parents: 67542
diff changeset
   335
                        (*test if this production has to be copied*)
wenzelm
parents: 67542
diff changeset
   336
                        val update = nts_member depends changed_nt;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   337
67543
wenzelm
parents: 67542
diff changeset
   338
                        (*test if production could already be associated with
wenzelm
parents: 67542
diff changeset
   339
                          a member of new_tks*)
wenzelm
parents: 67542
diff changeset
   340
                        val lambda =
wenzelm
parents: 67542
diff changeset
   341
                          not (nts_is_unique depends) orelse
wenzelm
parents: 67542
diff changeset
   342
                          not (nts_is_empty depends) andalso is_some tk
77823
wenzelm
parents: 77818
diff changeset
   343
                          andalso Tokens.member new_tks (the tk);
67543
wenzelm
parents: 67542
diff changeset
   344
wenzelm
parents: 67542
diff changeset
   345
                        (*associate production with new starting tokens*)
wenzelm
parents: 67542
diff changeset
   346
                        fun copy tk nt_prods =
wenzelm
parents: 67542
diff changeset
   347
                          let
wenzelm
parents: 67542
diff changeset
   348
                            val tk_prods = prods_lookup nt_prods tk;
wenzelm
parents: 67542
diff changeset
   349
                            val tk_prods' =
wenzelm
parents: 67542
diff changeset
   350
                              if not lambda then p :: tk_prods
wenzelm
parents: 67542
diff changeset
   351
                              else insert (op =) p tk_prods;
wenzelm
parents: 67542
diff changeset
   352
                               (*if production depends on lambda NT we
wenzelm
parents: 67542
diff changeset
   353
                                 have to look for duplicates*)
wenzelm
parents: 67542
diff changeset
   354
                          in prods_update (tk, tk_prods') nt_prods end;
wenzelm
parents: 67542
diff changeset
   355
                        val result =
77823
wenzelm
parents: 77818
diff changeset
   356
                          if update then (tk_prods, Tokens.fold copy new_tks nt_prods)
67543
wenzelm
parents: 67542
diff changeset
   357
                          else if key = unknown_start then (p :: tk_prods, nt_prods)
wenzelm
parents: 67542
diff changeset
   358
                          else (tk_prods, nt_prods);
wenzelm
parents: 67542
diff changeset
   359
                      in update_prods ps result end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   360
67543
wenzelm
parents: 67542
diff changeset
   361
                (*copy existing productions for new starting tokens*)
wenzelm
parents: 67542
diff changeset
   362
                fun process_nts nt =
wenzelm
parents: 67542
diff changeset
   363
                  let
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   364
                    val ((nts, tks), nt_prods) = Array.nth array_prods nt;
67543
wenzelm
parents: 67542
diff changeset
   365
wenzelm
parents: 67542
diff changeset
   366
                    val tk_prods = prods_lookup nt_prods key;
wenzelm
parents: 67542
diff changeset
   367
wenzelm
parents: 67542
diff changeset
   368
                    (*associate productions with new lookahead tokens*)
wenzelm
parents: 67542
diff changeset
   369
                    val (tk_prods', nt_prods') = update_prods tk_prods ([], nt_prods);
wenzelm
parents: 67542
diff changeset
   370
wenzelm
parents: 67542
diff changeset
   371
                    val nt_prods'' =
wenzelm
parents: 67542
diff changeset
   372
                      if key = unknown_start then
wenzelm
parents: 67542
diff changeset
   373
                        prods_update (key, tk_prods') nt_prods'
wenzelm
parents: 67542
diff changeset
   374
                      else nt_prods';
wenzelm
parents: 67542
diff changeset
   375
77823
wenzelm
parents: 77818
diff changeset
   376
                    val added_tks = Tokens.subtract tks new_tks;
wenzelm
parents: 77818
diff changeset
   377
                    val tks' = Tokens.merge (tks, added_tks);
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   378
                    val _ = Array.upd array_prods nt ((nts, tks'), nt_prods'');
67543
wenzelm
parents: 67542
diff changeset
   379
                  in tokens_add (nt, added_tks) end;
wenzelm
parents: 67542
diff changeset
   380
78007
91fc15d9dbdf tuned: more readable ML;
wenzelm
parents: 78006
diff changeset
   381
                val ((dependent, _), _) = Array.nth array_prods changed_nt;
67543
wenzelm
parents: 67542
diff changeset
   382
              in add_starts (starts @ nts_fold process_nts dependent []) end;
wenzelm
parents: 67542
diff changeset
   383
      in add_starts added_starts' end;
wenzelm
parents: 67542
diff changeset
   384
  in (chains', lambdas') end;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   385
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   386
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   387
(* pretty_gram *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   388
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   389
fun pretty_gram (Gram {tags, prods, chains, ...}) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   390
  let
67531
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
   391
    val print_nt = tags_name tags;
67518
wenzelm
parents: 67517
diff changeset
   392
    fun print_pri p = if p < 0 then "" else Symbol.make_sup ("(" ^ string_of_int p ^ ")");
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   393
67552
679253fef277 clarified signature;
wenzelm
parents: 67551
diff changeset
   394
    fun pretty_symb (Terminal tok) =
679253fef277 clarified signature;
wenzelm
parents: 67551
diff changeset
   395
          if Lexicon.is_literal tok
679253fef277 clarified signature;
wenzelm
parents: 67551
diff changeset
   396
          then Pretty.quote (Pretty.keyword1 (Lexicon.str_of_token tok))
679253fef277 clarified signature;
wenzelm
parents: 67551
diff changeset
   397
          else Pretty.str (Lexicon.str_of_token tok)
67513
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   398
      | pretty_symb (Nonterminal (tag, p)) = Pretty.str (print_nt tag ^ print_pri p);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   399
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   400
    fun pretty_const "" = []
67513
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   401
      | pretty_const c = [Pretty.str ("\<^bold>\<Rightarrow> " ^ quote c)];
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   402
67513
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   403
    fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1);
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   404
67513
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   405
    fun pretty_prod (name, tag) =
77846
5ba68d3bd741 more operations, following Isabelle/ML conventions;
wenzelm
parents: 77823
diff changeset
   406
      (prods_content (#2 (Vector.nth prods tag)) @ map prod_of_chain (chains_preds chains tag))
67513
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   407
      |> map (fn (symbs, const, p) =>
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   408
          Pretty.block (Pretty.breaks
731b1ad6759a tuned output;
wenzelm
parents: 64556
diff changeset
   409
            (Pretty.str (name ^ print_pri p ^ " =") :: map pretty_symb symbs @ pretty_const const)));
67531
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
   410
  in maps pretty_prod (tags_content tags) end;
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   411
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   412
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   413
67545
wenzelm
parents: 67544
diff changeset
   414
(** operations on grammars **)
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   415
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   416
val empty_gram =
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   417
  Gram
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   418
   {nt_count = 0,
67531
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
   419
    tags = tags_empty,
d19686205f86 tuned: more explicit types;
wenzelm
parents: 67530
diff changeset
   420
    chains = chains_empty,
67533
f253e5eaf995 clarified types and operations: potentially more efficient add_prods;
wenzelm
parents: 67532
diff changeset
   421
    lambdas = nts_empty,
f253e5eaf995 clarified types and operations: potentially more efficient add_prods;
wenzelm
parents: 67532
diff changeset
   422
    prods = Vector.fromList [nt_gram_empty]};
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   423
80958
0de153a15095 eliminate unused prod_count (see also 7afca3218b65);
wenzelm
parents: 80957
diff changeset
   424
fun extend_gram xprods (Gram {nt_count, tags, chains, lambdas, prods}) =
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   425
  let
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   426
    (*Get tag for existing nonterminal or create a new one*)
80957
wenzelm
parents: 80950
diff changeset
   427
    fun get_tag (context as (nt_count, tags)) nt =
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   428
      (case tags_lookup tags nt of
80957
wenzelm
parents: 80950
diff changeset
   429
        SOME tag => (context, tag)
wenzelm
parents: 80950
diff changeset
   430
      | NONE => ((nt_count + 1, tags_insert (nt, nt_count) tags), nt_count));
1438
3cc22794ce71 added comments
clasohm
parents: 1177
diff changeset
   431
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   432
    (*Convert symbols to the form used by the parser;
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   433
      delimiters and predefined terms are stored as terminals,
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   434
      nonterminals are converted to integer tags*)
80957
wenzelm
parents: 80950
diff changeset
   435
    fun make_symbs [] context result = (context, rev result)
wenzelm
parents: 80950
diff changeset
   436
      | make_symbs (Syntax_Ext.Delim s :: ss) context result =
wenzelm
parents: 80950
diff changeset
   437
          make_symbs ss context (Terminal (Lexicon.literal s) :: result)
wenzelm
parents: 80950
diff changeset
   438
      | make_symbs (Syntax_Ext.Argument (s, p) :: ss) context result =
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   439
          let
80957
wenzelm
parents: 80950
diff changeset
   440
            val (context', new_symb) =
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   441
              (case Lexicon.get_terminal s of
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   442
                NONE =>
80957
wenzelm
parents: 80950
diff changeset
   443
                  let val (context', tag) = get_tag context s;
wenzelm
parents: 80950
diff changeset
   444
                  in (context', Nonterminal (tag, p)) end
wenzelm
parents: 80950
diff changeset
   445
              | SOME tk => (context, Terminal tk));
wenzelm
parents: 80950
diff changeset
   446
          in make_symbs ss context' (new_symb :: result) end
wenzelm
parents: 80950
diff changeset
   447
      | make_symbs (_ :: ss) context result = make_symbs ss context result;
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   448
80957
wenzelm
parents: 80950
diff changeset
   449
    fun make_prods [] context result = (context, result)
80958
0de153a15095 eliminate unused prod_count (see also 7afca3218b65);
wenzelm
parents: 80957
diff changeset
   450
      | make_prods (Syntax_Ext.XProd (lhs, xsymbs, const, pri) :: ps) (nt_count, tags) result =
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   451
          let
80957
wenzelm
parents: 80950
diff changeset
   452
            val (context', tag) = get_tag (nt_count, tags) lhs;
80958
0de153a15095 eliminate unused prod_count (see also 7afca3218b65);
wenzelm
parents: 80957
diff changeset
   453
            val (context'', symbs) = make_symbs xsymbs context' [];
80957
wenzelm
parents: 80950
diff changeset
   454
          in make_prods ps context'' ((tag, (symbs, const, pri)) :: result) end;
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   455
80958
0de153a15095 eliminate unused prod_count (see also 7afca3218b65);
wenzelm
parents: 80957
diff changeset
   456
    val ((nt_count', tags'), new_prods) = make_prods xprods (nt_count, tags) [];
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   457
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   458
    val array_prods' =
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   459
      Array.tabulate (nt_count', fn i =>
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   460
        if i < nt_count then Vector.nth prods i
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   461
        else nt_gram_empty);
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   462
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   463
    val (chains', lambdas') =
80957
wenzelm
parents: 80950
diff changeset
   464
      (chains, lambdas) |> fold (add_production array_prods') new_prods;
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   465
  in
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   466
    Gram
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   467
     {nt_count = nt_count',
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   468
      tags = tags',
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   469
      chains = chains',
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   470
      lambdas = lambdas',
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   471
      prods = Array.vector array_prods'}
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   472
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   473
78009
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   474
fun make_gram [] _ (SOME gram) = gram
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   475
  | make_gram new_xprods _ (SOME gram) = extend_gram new_xprods gram
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   476
  | make_gram [] [] NONE = empty_gram
f906f7f83dae performance tuning: cached non-persistent Parser.gram reduces heap size by approx. 1-4%;
wenzelm
parents: 78007
diff changeset
   477
  | make_gram new_xprods old_xprods NONE = extend_gram (new_xprods @ old_xprods) empty_gram;
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   478
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   479
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   480
42374
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   481
(** parser **)
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   482
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   483
(* parsetree *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   484
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   485
datatype parsetree =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   486
  Node of string * parsetree list |
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   487
  Tip of Lexicon.token;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   488
80950
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
   489
fun pretty_parsetree (Node (c, pts)) =
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
   490
      [Pretty.enclose "(" ")"
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
   491
        (Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty_parsetree pts))]
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
   492
  | pretty_parsetree (Tip tok) =
b4a6bee4621a clarified signature;
wenzelm
parents: 80947
diff changeset
   493
      if Lexicon.valued_token tok then [Pretty.str (Lexicon.str_of_token tok)] else [];
42374
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   494
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   495
b9a6b465da25 clarified pretty_parsetree: suppress literal tokens;
wenzelm
parents: 42288
diff changeset
   496
(* parser state *)
42205
22f5cc06c419 direct pretty printing of parsetrees -- prevent diagnostic output from crashing due to undeclared entities;
wenzelm
parents: 41378
diff changeset
   497
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   498
type state =
80930
a9e2f4e845a0 minor performance tuning: more concise tuples;
wenzelm
parents: 80929
diff changeset
   499
  (nt * int *       (*identification and production precedence*)
a9e2f4e845a0 minor performance tuning: more concise tuples;
wenzelm
parents: 80929
diff changeset
   500
   string *         (*name of production*)
a9e2f4e845a0 minor performance tuning: more concise tuples;
wenzelm
parents: 80929
diff changeset
   501
   int) *           (*index for previous state list*)
80943
wenzelm
parents: 80933
diff changeset
   502
  symb list *       (*input: rest of rhs*)
wenzelm
parents: 80933
diff changeset
   503
  parsetree list;   (*output (reversed): already parsed nonterminals on rhs*)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   504
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   505
38713
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   506
(*Get all rhss with precedence >= min_prec*)
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   507
fun get_RHS min_prec = filter (fn (_, _, prec: int) => prec >= min_prec);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   508
38713
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   509
(*Get all rhss with precedence >= min_prec and < max_prec*)
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   510
fun get_RHS' min_prec max_prec =
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   511
  filter (fn (_, _, prec: int) => prec >= min_prec andalso prec < max_prec);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   512
15752
8cdc6249044b added make_gram;
wenzelm
parents: 15570
diff changeset
   513
(*Add parse tree to list and eliminate duplicates
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   514
  saving the maximum precedence*)
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   515
fun conc (t: parsetree list, prec: int) [] = (NONE, [(t, prec)])
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   516
  | conc (t, prec) ((t', prec') :: ts) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   517
      if t = t' then
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   518
        (SOME prec',
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   519
          if prec' >= prec then (t', prec') :: ts
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   520
          else (t, prec) :: ts)
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   521
      else
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   522
        let val (n, ts') = conc (t, prec) ts
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   523
        in (n, (t', prec') :: ts') end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   524
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   525
(*Update entry in used*)
42226
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   526
fun update_trees (A, t) used =
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   527
  let
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   528
    val (i, ts) = the (Inttab.lookup used A);
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   529
    val (n, ts') = conc t ts;
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   530
  in (n, Inttab.update (A, (i, ts')) used) end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   531
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   532
(*Replace entry in used*)
42226
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   533
fun update_prec (A, prec) =
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   534
  Inttab.map_entry A (fn (_, ts) => (prec, ts));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   535
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   536
fun get_states_lambda A max opt_min Si : state list =
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   537
  let
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   538
    val prec =
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   539
      (case opt_min of
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   540
        NONE => (fn p => p <= max)
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   541
      | SOME min => (fn p => p <= max andalso p > min));
80943
wenzelm
parents: 80933
diff changeset
   542
  in filter (fn (_, Nonterminal (B, p) :: _, _) => A = B andalso prec p | _ => false) Si end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   543
80944
wenzelm
parents: 80943
diff changeset
   544
fun get_states A max_prec =
wenzelm
parents: 80943
diff changeset
   545
  filter (fn (_, Nonterminal (B, prec) :: _, _) => A = B andalso prec <= max_prec | _ => false);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   546
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   547
80943
wenzelm
parents: 80933
diff changeset
   548
fun movedot_nonterm tt (info, Nonterminal _ :: sa, ts) : state = (info, sa, tt @ ts);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   549
80944
wenzelm
parents: 80943
diff changeset
   550
fun movedot_lambda p ((info, sa, ts): state) =
wenzelm
parents: 80943
diff changeset
   551
  map_filter (fn (t, k) => if p <= k then SOME (info, sa, t @ ts) else NONE);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   552
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   553
41378
55286df6a423 configuration option "syntax_branching_level";
wenzelm
parents: 40959
diff changeset
   554
(*trigger value for warnings*)
69575
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 69002
diff changeset
   555
val branching_level = Config.declare_int ("syntax_branching_level", \<^here>) (K 600);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   556
1147
57b5f55bf879 removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
clasohm
parents: 890
diff changeset
   557
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   558
local
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   559
80947
wenzelm
parents: 80946
diff changeset
   560
fun process_states (Gram {prods = gram_prods, chains = gram_chains, ...}) stateset i c states =
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   561
  let
80947
wenzelm
parents: 80946
diff changeset
   562
    (*get all productions of a NT and NTs chained to it which can
wenzelm
parents: 80946
diff changeset
   563
      be started by specified token*)
wenzelm
parents: 80946
diff changeset
   564
    fun prods_for tok nt =
wenzelm
parents: 80946
diff changeset
   565
      let
wenzelm
parents: 80946
diff changeset
   566
        fun token_prods prods =
wenzelm
parents: 80946
diff changeset
   567
          fold cons (prods_lookup prods tok) #>
wenzelm
parents: 80946
diff changeset
   568
          fold cons (prods_lookup prods Lexicon.dummy);
wenzelm
parents: 80946
diff changeset
   569
        val nt_prods = #2 o Vector.nth gram_prods;
wenzelm
parents: 80946
diff changeset
   570
      in fold (token_prods o nt_prods) (chains_all_preds gram_chains [nt]) [] end;
wenzelm
parents: 80946
diff changeset
   571
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   572
    fun process _ [] (Si, Sii) = (Si, Sii)
80946
wenzelm
parents: 80945
diff changeset
   573
      | process used ((S as (info, symbs, ts)) :: States) (Si, Sii) =
wenzelm
parents: 80945
diff changeset
   574
          (case symbs of
wenzelm
parents: 80945
diff changeset
   575
            Nonterminal (nt, min_prec) :: sa =>
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   576
              let (*predictor operation*)
80945
wenzelm
parents: 80944
diff changeset
   577
                fun mk_state (rhs, id, prod_prec) = ((nt, prod_prec, id, i), rhs, []);
wenzelm
parents: 80944
diff changeset
   578
                fun movedot_lambda (t, k) = if min_prec <= k then SOME (info, sa, t @ ts) else NONE;
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   579
                val (used', new_states) =
42226
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   580
                  (case Inttab.lookup used nt of
38713
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   581
                    SOME (used_prec, l) => (*nonterminal has been processed*)
29d0298439be eliminated some old camel case stuff;
wenzelm
parents: 38712
diff changeset
   582
                      if used_prec <= min_prec then
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   583
                        (*wanted precedence has been processed*)
80945
wenzelm
parents: 80944
diff changeset
   584
                        (used, map_filter movedot_lambda l)
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   585
                      else (*wanted precedence hasn't been parsed yet*)
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   586
                        let
80947
wenzelm
parents: 80946
diff changeset
   587
                          val States2 = map mk_state (get_RHS' min_prec used_prec (prods_for c nt));
80945
wenzelm
parents: 80944
diff changeset
   588
                          val States1 = map_filter movedot_lambda l;
80944
wenzelm
parents: 80943
diff changeset
   589
                        in (update_prec (nt, min_prec) used, States1 @ States2) end
38712
f7688fd819a8 some attempts to recover Isabelle/ML style from the depths of time;
wenzelm
parents: 38711
diff changeset
   590
                  | NONE => (*nonterminal is parsed for the first time*)
80947
wenzelm
parents: 80946
diff changeset
   591
                      let val States' = map mk_state (get_RHS min_prec (prods_for c nt))
42226
cb650789f2f0 use standard tables with standard argument order;
wenzelm
parents: 42222
diff changeset
   592
                      in (Inttab.update (nt, (min_prec, [])) used, States') end);
80945
wenzelm
parents: 80944
diff changeset
   593
              in process used' (new_states @ States) (S :: Si, Sii) end
80946
wenzelm
parents: 80945
diff changeset
   594
          | Terminal a :: sa => (*scanner operation*)
80929
wenzelm
parents: 80928
diff changeset
   595
              let
80946
wenzelm
parents: 80945
diff changeset
   596
                val (_, _, id, _) = info;
80929
wenzelm
parents: 80928
diff changeset
   597
                val Sii' =
wenzelm
parents: 80928
diff changeset
   598
                  if Lexicon.tokens_match_ord (a, c) <> EQUAL then Sii
wenzelm
parents: 80928
diff changeset
   599
                  else (*move dot*)
wenzelm
parents: 80928
diff changeset
   600
                    let val ts' = if Lexicon.valued_token c orelse id <> "" then Tip c :: ts else ts
80943
wenzelm
parents: 80933
diff changeset
   601
                    in (info, sa, ts') :: Sii end;
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   602
              in process used States (S :: Si, Sii') end
80946
wenzelm
parents: 80945
diff changeset
   603
          | [] => (*completer operation*)
80928
wenzelm
parents: 80927
diff changeset
   604
              let
80946
wenzelm
parents: 80945
diff changeset
   605
                val (A, prec, id, j) = info;
80928
wenzelm
parents: 80927
diff changeset
   606
                val tt = if id = "" then ts else [Node (id, rev ts)];
wenzelm
parents: 80927
diff changeset
   607
                val (used', Slist) =
wenzelm
parents: 80927
diff changeset
   608
                  if j = i then (*lambda production?*)
wenzelm
parents: 80927
diff changeset
   609
                    let val (prec', used') = update_trees (A, (tt, prec)) used
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   610
                    in (used', get_states_lambda A prec prec' Si) end
80944
wenzelm
parents: 80943
diff changeset
   611
                  else (used, get_states A prec (Array.nth stateset j));
80928
wenzelm
parents: 80927
diff changeset
   612
                val States' = map (movedot_nonterm tt) Slist;
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   613
              in process used' (States' @ States) (S :: Si, Sii) end)
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   614
  in process Inttab.empty states ([], []) end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   615
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   616
fun produce gram stateset i input prev_token =
77888
wenzelm
parents: 77846
diff changeset
   617
  (case Array.nth stateset i of
25986
26f1e4c172c3 syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents: 24245
diff changeset
   618
    [] =>
26f1e4c172c3 syntax error: reduced spam -- print expected nonterminals instead of terminals;
wenzelm
parents: 24245
diff changeset
   619
      let
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   620
        val toks = if Lexicon.is_eof prev_token then input else prev_token :: input;
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 47060
diff changeset
   621
        val pos = Position.here (Lexicon.pos_of_token prev_token);
27801
0d0adaf0228d datatype token: maintain range, tuned representation;
wenzelm
parents: 26678
diff changeset
   622
      in
55624
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   623
        if null toks then
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   624
          error ("Inner syntax error: unexpected end of input" ^ pos)
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   625
        else
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   626
          error ("Inner syntax error" ^ pos ^
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   627
            Markup.markup Markup.no_report
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   628
              ("\n" ^ Pretty.string_of
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   629
                (Pretty.block [
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   630
                  Pretty.str "at", Pretty.brk 1,
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   631
                  Pretty.block
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   632
                   (Pretty.str "\"" ::
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   633
                    Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last toks))) @
d52409077135 tuned messages;
wenzelm
parents: 48992
diff changeset
   634
                    [Pretty.str "\""])])))
27801
0d0adaf0228d datatype token: maintain range, tuned representation;
wenzelm
parents: 26678
diff changeset
   635
      end
80931
wenzelm
parents: 80930
diff changeset
   636
  | states =>
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   637
      (case input of
80931
wenzelm
parents: 80930
diff changeset
   638
        [] => states
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   639
      | c :: rest =>
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   640
          let
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   641
            val (Si, Sii) = process_states gram stateset i c states;
80931
wenzelm
parents: 80930
diff changeset
   642
            val _ = Array.upd stateset i Si;
wenzelm
parents: 80930
diff changeset
   643
            val _ = Array.upd stateset (i + 1) Sii;
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   644
          in produce gram stateset (i + 1) rest c end));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   645
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   646
in
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   647
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   648
fun parse (gram as Gram {tags, ...}) start toks =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   649
  let
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   650
    val start_tag =
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   651
      (case tags_lookup tags start of
37683
ece640e48a6c do not open auxiliary ML structures;
wenzelm
parents: 37216
diff changeset
   652
        SOME tag => tag
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   653
      | NONE => error ("Inner syntax: bad grammar root symbol " ^ quote start));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   654
27801
0d0adaf0228d datatype token: maintain range, tuned representation;
wenzelm
parents: 26678
diff changeset
   655
    val end_pos =
0d0adaf0228d datatype token: maintain range, tuned representation;
wenzelm
parents: 26678
diff changeset
   656
      (case try List.last toks of
0d0adaf0228d datatype token: maintain range, tuned representation;
wenzelm
parents: 26678
diff changeset
   657
        NONE => Position.none
67551
4a087b9a29c5 clarified signature;
wenzelm
parents: 67550
diff changeset
   658
      | SOME tok => Lexicon.end_pos_of_token tok);
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   659
    val input = toks @ [Lexicon.mk_eof end_pos];
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   660
80943
wenzelm
parents: 80933
diff changeset
   661
    val S0: state = ((~1, 0, "", 0), [Nonterminal (start_tag, 0), Terminal Lexicon.eof], []);
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   662
    val stateset = Array.array (length input + 1, []);
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   663
    val _ = Array.upd stateset 0 [S0];
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   664
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   665
    val pts =
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   666
      produce gram stateset 0 input Lexicon.eof
80943
wenzelm
parents: 80933
diff changeset
   667
      |> map_filter (fn (_, _, [pt]) => SOME pt | _ => NONE);
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   668
  in if null pts then raise Fail "Inner syntax: no parse trees" else pts end;
26678
a3ae088dc20f educated guess for infix syntax
haftmann
parents: 26069
diff changeset
   669
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   670
end;
80933
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   671
56f1c0af602c misc tuning and clarification;
wenzelm
parents: 80931
diff changeset
   672
end;