author | wenzelm |
Sun, 18 Jan 2009 20:05:01 +0100 | |
changeset 29552 | 5b21c79785b0 |
parent 29140 | e7ac5bb20aed |
child 29571 | ba0cf984e593 |
permissions | -rw-r--r-- |
27951 | 1 |
/* Title: Pure/Tools/isabelle_syntax.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Isabelle outer syntax. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import java.util.{Properties, Enumeration} |
|
10 |
||
11 |
||
12 |
object IsabelleSyntax { |
|
13 |
||
14 |
/* string token */ |
|
15 |
||
29552 | 16 |
def append_string(str: String, result: StringBuilder) |
17 |
{ |
|
27951 | 18 |
result.append("\"") |
19 |
for (c <- str) { |
|
20 |
if (c < 32 || c == '\\' || c == '\"') { |
|
27954
4558d93e83b7
append_string: proper backslash in character escapes;
wenzelm
parents:
27951
diff
changeset
|
21 |
result.append("\\") |
27951 | 22 |
if (c < 10) result.append('0') |
23 |
if (c < 100) result.append('0') |
|
24 |
result.append(c.asInstanceOf[Int].toString) |
|
25 |
} |
|
26 |
else result.append(c) |
|
27 |
} |
|
28 |
result.append("\"") |
|
29 |
} |
|
30 |
||
29552 | 31 |
def encode_string(str: String) = |
32 |
{ |
|
27951 | 33 |
val result = new StringBuilder(str.length + 20) |
34 |
append_string(str, result) |
|
35 |
result.toString |
|
36 |
} |
|
37 |
||
38 |
||
29552 | 39 |
/* list */ |
40 |
||
41 |
def append_list[A](append_elem: (A, StringBuilder) => Unit, body: Iterable[A], |
|
42 |
result: StringBuilder) |
|
43 |
{ |
|
44 |
result.append("(") |
|
45 |
val elems = body.elements |
|
46 |
if (elems.hasNext) append_elem(elems.next, result) |
|
47 |
while (elems.hasNext) { |
|
48 |
result.append(", ") |
|
49 |
append_elem(elems.next, result) |
|
50 |
} |
|
51 |
result.append(")") |
|
52 |
} |
|
53 |
||
54 |
def encode_list[A](append_elem: (A, StringBuilder) => Unit, elems: Iterable[A]) = |
|
55 |
{ |
|
56 |
val result = new StringBuilder(20) |
|
57 |
append_list(append_elem, elems, result) |
|
58 |
result.toString |
|
59 |
} |
|
60 |
||
61 |
||
27951 | 62 |
/* properties */ |
63 |
||
29552 | 64 |
def append_properties(props: Properties, result: StringBuilder) |
65 |
{ |
|
27951 | 66 |
result.append("(") |
67 |
val names = props.propertyNames.asInstanceOf[Enumeration[String]] |
|
68 |
while (names.hasMoreElements) { |
|
69 |
val name = names.nextElement; val value = props.getProperty(name) |
|
70 |
append_string(name, result); result.append(" = "); append_string(value, result) |
|
71 |
if (names.hasMoreElements) result.append(", ") |
|
72 |
} |
|
73 |
result.append(")") |
|
74 |
} |
|
75 |
||
76 |
def encode_properties(props: Properties) = { |
|
77 |
val result = new StringBuilder(40) |
|
78 |
append_properties(props, result) |
|
79 |
result.toString |
|
80 |
} |
|
81 |
||
82 |
} |