author | wenzelm |
Tue, 12 Dec 2017 17:47:23 +0100 | |
changeset 67190 | 58ab7ddbdb04 |
parent 67188 | bc7a6455e12a |
child 67194 | 1c0a6a957114 |
permissions | -rw-r--r-- |
67172 | 1 |
/* Title: Pure/Thy/latex.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for LaTeX. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67173 | 10 |
import java.io.{File => JFile} |
11 |
||
67172 | 12 |
import scala.annotation.tailrec |
13 |
||
14 |
||
15 |
object Latex |
|
16 |
{ |
|
67173 | 17 |
/** latex errors **/ |
18 |
||
67176
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
19 |
def latex_errors(dir: Path, root_name: String): List[String] = |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
20 |
{ |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
21 |
val root_log_path = dir + Path.explode(root_name).ext("log") |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
22 |
if (root_log_path.is_file) { |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
23 |
for { (msg, pos) <- filter_errors(dir, File.read(root_log_path)) } |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
24 |
yield "Latex error" + Position.here(pos) + ":\n" + cat_lines(split_lines(msg).map(" " + _)) |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
25 |
} |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
26 |
else Nil |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
27 |
} |
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
28 |
|
13b5c3ff1954
re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents:
67174
diff
changeset
|
29 |
|
67173 | 30 |
/* generated .tex file */ |
31 |
||
67184 | 32 |
private val File_Pattern = """^%:%file=(.+)%:%$""".r |
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
33 |
private val Line_Pattern = """^*%:%line=(\d+)%:%$""".r |
67173 | 34 |
|
35 |
def read_tex_file(tex_file: Path): Tex_File = |
|
67174 | 36 |
new Tex_File(tex_file, Line.logical_lines(File.read(tex_file)).toArray) |
67173 | 37 |
|
38 |
final class Tex_File private[Latex](val tex_file: Path, tex_lines: Array[String]) |
|
39 |
{ |
|
40 |
override def toString: String = tex_file.toString |
|
41 |
||
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
42 |
val source_file: Option[String] = |
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
43 |
if (tex_lines.nonEmpty) { |
67173 | 44 |
tex_lines(0) match { |
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
45 |
case File_Pattern(file) => Some(file) |
67173 | 46 |
case _ => None |
47 |
} |
|
48 |
} |
|
49 |
else None |
|
50 |
||
67184 | 51 |
private def prev_lines_iterator(l: Int): Iterator[String] = |
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
52 |
if (0 < l && l <= tex_lines.length) |
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
53 |
Range.inclusive(l - 1, 0, -1).iterator.map(tex_lines(_)) |
67184 | 54 |
else Iterator.empty |
55 |
||
67173 | 56 |
def source_position(l: Int): Option[Position.T] = |
67184 | 57 |
(for { |
58 |
file <- source_file.iterator |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
59 |
s <- prev_lines_iterator(l) |
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
60 |
line <- |
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
61 |
(s match { |
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
62 |
case Line_Pattern(Value.Int(line)) => Some(line) |
67173 | 63 |
case _ => None |
64 |
}) |
|
65 |
} |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
66 |
yield { Position.Line_File(line, file) }).toStream.headOption |
67173 | 67 |
|
68 |
def position(line: Int): Position.T = |
|
67188
bc7a6455e12a
simplified positions -- line is also human-readable in generated .tex file;
wenzelm
parents:
67184
diff
changeset
|
69 |
source_position(line) getOrElse Position.Line_File(line, tex_file.implode) |
67173 | 70 |
} |
71 |
||
72 |
||
73 |
/* latex log */ |
|
74 |
||
75 |
def filter_errors(dir: Path, root_log: String): List[(String, Position.T)] = |
|
67172 | 76 |
{ |
67173 | 77 |
val seen_files = Synchronized(Map.empty[JFile, Tex_File]) |
78 |
||
79 |
def check_tex_file(path: Path): Option[Tex_File] = |
|
80 |
seen_files.change_result(seen => |
|
81 |
seen.get(path.file) match { |
|
82 |
case None => |
|
83 |
if (path.is_file) { |
|
84 |
val tex_file = read_tex_file(path) |
|
85 |
(Some(tex_file), seen + (path.file -> tex_file)) |
|
86 |
} |
|
87 |
else (None, seen) |
|
88 |
case some => (some, seen) |
|
89 |
}) |
|
90 |
||
91 |
def tex_file_position(path: Path, line: Int): Position.T = |
|
92 |
check_tex_file(path) match { |
|
93 |
case Some(tex_file) => tex_file.position(line) |
|
94 |
case None => Position.Line_File(line, path.implode) |
|
95 |
} |
|
96 |
||
67172 | 97 |
object File_Line_Error |
98 |
{ |
|
67190 | 99 |
val Pattern = """^(.*?\.\w\w\w):(\d+): (.*)$""".r |
67173 | 100 |
def unapply(line: String): Option[(Path, Int, String)] = |
67172 | 101 |
line match { |
102 |
case Pattern(file, Value.Int(line), message) => |
|
103 |
val path = File.standard_path(file) |
|
104 |
if (Path.is_wellformed(path)) { |
|
67183 | 105 |
val file = (dir + Path.explode(path)).canonical |
67173 | 106 |
if (file.is_file) Some((file, line, message)) else None |
67172 | 107 |
} |
108 |
else None |
|
109 |
case _ => None |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
object Line_Error |
|
114 |
{ |
|
115 |
val Pattern = """^l\.(\d+) (.*)$""".r |
|
67173 | 116 |
def unapply(line: String): Option[(Int, String)] = |
67172 | 117 |
line match { |
67173 | 118 |
case Pattern(Value.Int(line), message) => Some((line, message)) |
67172 | 119 |
case _ => None |
120 |
} |
|
121 |
} |
|
122 |
||
67173 | 123 |
@tailrec def filter(lines: List[String], result: List[(String, Position.T)]) |
124 |
: List[(String, Position.T)] = |
|
125 |
{ |
|
67172 | 126 |
lines match { |
67173 | 127 |
case File_Line_Error((file, line, msg1)) :: rest1 => |
67183 | 128 |
val pos = tex_file_position(file, line) |
67172 | 129 |
rest1 match { |
67173 | 130 |
case Line_Error((line2, msg2)) :: rest2 if line == line2 => |
131 |
filter(rest2, (Exn.cat_message(msg1, msg2), pos) :: result) |
|
132 |
case _ => |
|
133 |
filter(rest1, (msg1, pos) :: result) |
|
67172 | 134 |
} |
135 |
case _ :: rest => filter(rest, result) |
|
136 |
case Nil => result.reverse |
|
137 |
} |
|
67173 | 138 |
} |
67172 | 139 |
|
67174 | 140 |
filter(Line.logical_lines(root_log), Nil) |
67173 | 141 |
} |
67172 | 142 |
} |