# HG changeset patch # User wenzelm # Date 1184192142 -7200 # Node ID 11bf7af10ec8e248d165cad69f9b4faa79fc92eb # Parent cd09234405b67943307fbdd3252b31988766e19b tuned signature; misc cleanup / simplification; diff -r cd09234405b6 -r 11bf7af10ec8 src/Pure/Thy/thy_edit.ML --- a/src/Pure/Thy/thy_edit.ML Thu Jul 12 00:15:41 2007 +0200 +++ b/src/Pure/Thy/thy_edit.ML Thu Jul 12 00:15:42 2007 +0200 @@ -7,12 +7,18 @@ signature THY_EDIT = sig - val read_tokens: Position.T -> (string, 'a) Source.source -> OuterLex.token list - datatype item = - Whitespace of OuterLex.token list | - Junk of OuterLex.token list | - Commandspan of string * OuterLex.token list - val read_items: Position.T -> (string, 'a) Source.source -> item list + val token_source: Position.T -> (string, 'a) Source.source -> + (OuterLex.token, Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) + Source.source + val parse_tokens: Position.T -> (string, 'a) Source.source -> OuterLex.token list + val present_token: OuterLex.token -> output + datatype item_kind = Whitespace | Junk | Commandspan of string + type item = item_kind * OuterLex.token list + val item_source: Position.T -> (string, 'a) Source.source -> + (item, (OuterLex.token, Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) + Source.source) Source.source + val parse_items: Position.T -> (string, 'a) Source.source -> item list + val present_item: item -> output val present_html: Path.T -> Path.T -> unit end; @@ -23,46 +29,18 @@ structure P = OuterParse; -(* tokens *) +(** tokens **) + +(* parse *) fun token_source pos src = Symbol.source true src |> T.source (SOME false) OuterSyntax.get_lexicons pos; -fun read_tokens pos src = Source.exhaust (token_source pos src); +fun parse_tokens pos src = Source.exhaust (token_source pos src); -(* items *) - -datatype item = - Whitespace of T.token list | - Junk of T.token list | - Commandspan of string * T.token list; - -local - -val whitespace = Scan.many1 (T.is_kind T.Space orf T.is_kind T.Comment); -val before_command = Scan.optional whitespace [] -- Scan.ahead P.command; -val body = Scan.repeat1 (Scan.unless before_command P.not_eof); - -val item = - whitespace >> Whitespace || - body >> Junk || - before_command -- P.not_eof -- Scan.optional body [] - >> (fn (((ws, name), c), bs) => Commandspan (name, ws @ [c] @ bs)); - -in - -fun item_source src = src - |> Source.source T.stopper (Scan.bulk item) NONE; - -end; - -fun read_items pos src = - Source.exhaust (item_source (token_source pos src)); - - -(* presentation *) +(* present *) local @@ -79,28 +57,70 @@ | T.EOF => Markup.control | _ => Markup.none; +in + fun present_token tok = Markup.enclose (token_kind_markup (T.kind_of tok)) (Output.output (T.unparse tok)); -val present_tokens = implode o map present_token; +end; + + + +(** items **) + +datatype item_kind = Whitespace | Junk | Commandspan of string; +type item = item_kind * OuterLex.token list; + + +(* parse *) -fun present_item (Whitespace toks) = Markup.enclose Markup.whitespace (present_tokens toks) - | present_item (Junk toks) = Markup.enclose Markup.junk (present_tokens toks) - | present_item (Commandspan (name, toks)) = - Markup.enclose (Markup.commandspan name) (present_tokens toks); +local + +val whitespace = Scan.many1 (T.is_kind T.Space orf T.is_kind T.Comment); +val before_command = Scan.optional whitespace [] -- Scan.ahead P.command; +val body = Scan.repeat1 (Scan.unless before_command P.not_eof); -val present_items = implode o map present_item; +val item = + whitespace >> pair Whitespace || + body >> pair Junk || + before_command -- P.not_eof -- Scan.optional body [] + >> (fn (((ws, name), c), bs) => (Commandspan name, ws @ [c] @ bs)); in +fun item_source pos src = + token_source pos src + |> Source.source T.stopper (Scan.bulk item) NONE; + +end; + +fun parse_items pos src = Source.exhaust (item_source pos src); + + +(* present *) + +local + +fun item_kind_markup Whitespace = Markup.whitespace + | item_kind_markup Junk = Markup.junk + | item_kind_markup (Commandspan name) = Markup.commandspan name; + +in + +fun present_item (kind, toks) = + Markup.enclose (item_kind_markup kind) (implode (map present_token toks)); + +end; + + +(* HTML presentation -- example *) + fun present_html inpath outpath = - read_items (Position.path inpath) (Source.of_string (File.read inpath)) - |> HTML.html_mode present_items + Source.exhaust (item_source (Position.path inpath) (Source.of_string (File.read inpath))) + |> HTML.html_mode (implode o map present_item) |> enclose (HTML.begin_document (Path.implode (Path.base inpath)) ^ "
") ("