author | bulwahn |
Fri, 13 Jul 2012 08:44:42 +0200 | |
changeset 48254 | 63e0ca00b952 |
parent 47469 | ba7fe841c885 |
child 48660 | 730ca503e955 |
permissions | -rw-r--r-- |
34166 | 1 |
/* Title: Pure/Isar/outer_syntax.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Isabelle/Isar outer syntax. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import scala.util.parsing.input.{Reader, CharSequenceReader} |
|
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
11 |
import scala.collection.mutable |
34166 | 12 |
|
13 |
||
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
14 |
object Outer_Syntax |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
15 |
{ |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
16 |
def quote_string(str: String): String = |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
17 |
{ |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
18 |
val result = new StringBuilder(str.length + 10) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
19 |
result += '"' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
20 |
for (s <- Symbol.iterator(str)) { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
21 |
if (s.length == 1) { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
22 |
val c = s(0) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
23 |
if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
24 |
result += '\\' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
25 |
if (c < 10) result += '0' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
26 |
if (c < 100) result += '0' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
27 |
result ++= (c.asInstanceOf[Int].toString) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
28 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
29 |
else result += c |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
30 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
31 |
else result ++= s |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
32 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
33 |
result += '"' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
34 |
result.toString |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
35 |
} |
46626 | 36 |
|
46940 | 37 |
type Decl = (String, Option[(String, List[String])]) |
46941 | 38 |
|
39 |
val empty: Outer_Syntax = new Outer_Syntax() |
|
40 |
def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init()) |
|
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
41 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
42 |
|
46712 | 43 |
final class Outer_Syntax private( |
47469
ba7fe841c885
keyword ";" is declared via prover (as "minor", not "diag");
wenzelm
parents:
46969
diff
changeset
|
44 |
keywords: Map[String, String] = Map.empty, |
46626 | 45 |
lexicon: Scan.Lexicon = Scan.Lexicon.empty, |
46941 | 46 |
val completion: Completion = Completion.empty) |
34166 | 47 |
{ |
38471
0924654b8163
report command token name instead of kind, which can be retrieved later via Outer_Syntax.keyword_kind;
wenzelm
parents:
36956
diff
changeset
|
48 |
def keyword_kind(name: String): Option[String] = keywords.get(name) |
0924654b8163
report command token name instead of kind, which can be retrieved later via Outer_Syntax.keyword_kind;
wenzelm
parents:
36956
diff
changeset
|
49 |
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40459
diff
changeset
|
50 |
def + (name: String, kind: String, replace: String): Outer_Syntax = |
46626 | 51 |
new Outer_Syntax( |
52 |
keywords + (name -> kind), |
|
53 |
lexicon + name, |
|
54 |
if (Keyword.control(kind)) completion else completion + (name, replace)) |
|
34166 | 55 |
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40459
diff
changeset
|
56 |
def + (name: String, kind: String): Outer_Syntax = this + (name, kind, name) |
36947 | 57 |
def + (name: String): Outer_Syntax = this + (name, Keyword.MINOR) |
46940 | 58 |
def + (decl: Outer_Syntax.Decl): Outer_Syntax = |
59 |
decl match { |
|
60 |
case ((name, Some((kind, _)))) => this + (name, kind) |
|
61 |
case ((name, None)) => this + name |
|
62 |
} |
|
34166 | 63 |
|
64 |
def is_command(name: String): Boolean = |
|
40458
12c8c64203b3
treat main theory commands like headings, and nest anything else inside;
wenzelm
parents:
40455
diff
changeset
|
65 |
keyword_kind(name) match { |
36947 | 66 |
case Some(kind) => kind != Keyword.MINOR |
34166 | 67 |
case None => false |
68 |
} |
|
69 |
||
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
70 |
def heading_level(name: String): Option[Int] = |
46969 | 71 |
{ |
72 |
keyword_kind(name) match { |
|
73 |
case _ if name == "header" => Some(0) |
|
74 |
case Some(Keyword.THY_HEADING1) => Some(1) |
|
75 |
case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2) |
|
76 |
case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3) |
|
77 |
case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4) |
|
78 |
case Some(kind) if Keyword.theory(kind) => Some(5) |
|
79 |
case _ => None |
|
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
80 |
} |
46969 | 81 |
} |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
82 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
83 |
def heading_level(command: Command): Option[Int] = |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
84 |
heading_level(command.name) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
85 |
|
34166 | 86 |
|
87 |
/* tokenize */ |
|
88 |
||
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36947
diff
changeset
|
89 |
def scan(input: Reader[Char]): List[Token] = |
34166 | 90 |
{ |
91 |
import lexicon._ |
|
92 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43455
diff
changeset
|
93 |
parseAll(rep(token(is_command)), input) match { |
34166 | 94 |
case Success(tokens, _) => tokens |
34264 | 95 |
case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString) |
34166 | 96 |
} |
97 |
} |
|
98 |
||
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36947
diff
changeset
|
99 |
def scan(input: CharSequence): List[Token] = |
34166 | 100 |
scan(new CharSequenceReader(input)) |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
101 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
102 |
def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) = |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
103 |
{ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
104 |
import lexicon._ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
105 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
106 |
var in: Reader[Char] = new CharSequenceReader(input) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
107 |
val toks = new mutable.ListBuffer[Token] |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
108 |
var ctxt = context |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
109 |
while (!in.atEnd) { |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43455
diff
changeset
|
110 |
parse(token_context(is_command, ctxt), in) match { |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
111 |
case Success((x, c), rest) => { toks += x; ctxt = c; in = rest } |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
112 |
case NoSuccess(_, rest) => |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
113 |
error("Unexpected failure of tokenizing input:\n" + rest.source.toString) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
114 |
} |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
115 |
} |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
116 |
(toks.toList, ctxt) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
117 |
} |
34166 | 118 |
} |