src/Pure/PIDE/command_span.ML
author wenzelm
Thu, 12 Mar 2015 22:00:51 +0100
changeset 59685 c043306d2598
parent 59683 d6824d8490be
child 59689 7968c57ea240
permissions -rw-r--r--
clarified markup for embedded files, early before execution;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/command_span.ML
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     3
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     4
Syntactic representation of command spans.
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     5
*)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     6
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     7
signature COMMAND_SPAN =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     8
sig
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     9
  datatype kind = Command_Span of string * Position.T | Ignored_Span | Malformed_Span
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    10
  datatype span = Span of kind * Token.T list
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    11
  val kind: span -> kind
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    12
  val content: span -> Token.T list
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 57905
diff changeset
    13
  val resolve_files: Keyword.keywords ->
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 57905
diff changeset
    14
    (string -> Path.T * Position.T -> Token.file Exn.result list) -> span -> span
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    15
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    16
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    17
structure Command_Span: COMMAND_SPAN =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    18
struct
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    19
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    20
datatype kind = Command_Span of string * Position.T | Ignored_Span | Malformed_Span;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    21
datatype span = Span of kind * Token.T list;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    22
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    23
fun kind (Span (k, _)) = k;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    24
fun content (Span (_, toks)) = toks;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    25
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    26
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    27
(* resolve inlined files *)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    28
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    29
local
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    30
59683
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    31
fun clean ((t1, i1) :: (t2, i2) :: rest) =
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    32
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean rest
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    33
      else (t1, i1) :: clean ((t2, i2) :: rest)
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    34
  | clean toks = toks;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    35
59683
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    36
fun clean_tokens tokens =
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    37
  clean (filter (fn (t, _) => Token.is_proper t) (tokens ~~ (0 upto length tokens - 1)));
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    38
59683
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    39
fun find_file ((tok, _) :: toks) =
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    40
      if Token.is_command tok then
59683
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    41
        toks |> get_first (fn (t, i) =>
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    42
          if Token.is_name t then
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    43
            SOME ((Path.explode (Token.content_of t), Token.pos_of t), i)
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    44
              handle ERROR msg => error (msg ^ Position.here (Token.pos_of t))
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    45
          else NONE)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    46
      else NONE
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    47
  | find_file [] = NONE;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    48
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    49
in
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    50
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 57905
diff changeset
    51
fun resolve_files keywords read_files span =
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    52
  (case span of
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    53
    Span (Command_Span (cmd, pos), toks) =>
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 57905
diff changeset
    54
      if Keyword.is_theory_load keywords cmd then
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    55
        (case find_file (clean_tokens toks) of
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    56
          NONE => error ("Bad file argument of command " ^ quote cmd ^ Position.here pos)
59683
d6824d8490be tuned -- more uniform ML vs. Scala;
wenzelm
parents: 58923
diff changeset
    57
        | SOME (path, i) =>
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    58
            let
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    59
              val toks' = toks |> map_index (fn (j, tok) =>
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    60
                if i = j then Token.put_files (read_files cmd path) tok
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    61
                else tok);
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    62
            in Span (Command_Span (cmd, pos), toks') end)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    63
      else span
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    64
  | _ => span);
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    65
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    66
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    67
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    68
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    69