author | wenzelm |
Sun, 28 Feb 2016 21:20:51 +0100 | |
changeset 62459 | 7a5d88dd8cc9 |
parent 62354 | fdd6989cc8a0 |
permissions | -rw-r--r-- |
24594 | 1 |
(* Title: Pure/ML/ml_parse.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Minimal parsing for SML -- fixing integer numerals. |
|
5 |
*) |
|
6 |
||
7 |
signature ML_PARSE = |
|
8 |
sig |
|
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
9 |
val global_context: use_context |
24594 | 10 |
end; |
11 |
||
12 |
structure ML_Parse: ML_PARSE = |
|
13 |
struct |
|
14 |
||
15 |
(** error handling **) |
|
16 |
||
17 |
fun !!! scan = |
|
18 |
let |
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
43948
diff
changeset
|
19 |
fun get_pos [] = " (end-of-input)" |
48992 | 20 |
| get_pos (tok :: _) = Position.here (ML_Lex.pos_of tok); |
24594 | 21 |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
30682
diff
changeset
|
22 |
fun err (toks, NONE) = (fn () => "SML syntax error" ^ get_pos toks) |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
30682
diff
changeset
|
23 |
| err (toks, SOME msg) = (fn () => "SML syntax error" ^ get_pos toks ^ ": " ^ msg ()); |
24594 | 24 |
in Scan.!! err scan end; |
25 |
||
26 |
fun bad_input x = |
|
30682
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
27 |
(Scan.some (fn tok => (case ML_Lex.kind_of tok of ML_Lex.Error msg => SOME msg | _ => NONE)) :|-- |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
30682
diff
changeset
|
28 |
(fn msg => Scan.fail_with (K (fn () => msg)))) x; |
24594 | 29 |
|
30 |
||
31 |
(** basic parsers **) |
|
32 |
||
27817 | 33 |
fun $$$ x = |
30682
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
34 |
Scan.one (fn tok => ML_Lex.kind_of tok = ML_Lex.Keyword andalso ML_Lex.content_of tok = x) |
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
35 |
>> ML_Lex.content_of; |
24594 | 36 |
|
30682
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
37 |
val int = Scan.one (fn tok => ML_Lex.kind_of tok = ML_Lex.Int) >> ML_Lex.content_of; |
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
38 |
|
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
39 |
val regular = Scan.one ML_Lex.is_regular >> ML_Lex.content_of; |
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
wenzelm
parents:
30672
diff
changeset
|
40 |
val improper = Scan.one ML_Lex.is_improper >> ML_Lex.content_of; |
24594 | 41 |
|
42 |
val blanks = Scan.repeat improper >> implode; |
|
43 |
||
44 |
||
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
45 |
(* global use_context *) |
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
46 |
|
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
47 |
val global_context: use_context = |
62354 | 48 |
{name_space = ML_Name_Space.global, |
48992 | 49 |
str_of_pos = Position.here oo Position.line_file, |
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
50 |
print = writeln, |
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
51 |
error = error}; |
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30636
diff
changeset
|
52 |
|
24594 | 53 |
end; |