| author | wenzelm | 
| Wed, 02 Feb 2011 15:04:09 +0100 | |
| changeset 41683 | 73dde8006820 | 
| parent 30682 | dcb233670c98 | 
| child 43947 | 9b00f09f7721 | 
| 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 | |
| 9 | val fix_ints: string -> string | |
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 10 | val global_context: use_context | 
| 24594 | 11 | end; | 
| 12 | ||
| 13 | structure ML_Parse: ML_PARSE = | |
| 14 | struct | |
| 15 | ||
| 16 | (** error handling **) | |
| 17 | ||
| 18 | fun !!! scan = | |
| 19 | let | |
| 20 | fun get_pos [] = " (past end-of-file!)" | |
| 30682 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 21 | | get_pos (tok :: _) = Position.str_of (ML_Lex.pos_of tok); | 
| 24594 | 22 | |
| 23 | fun err (toks, NONE) = "SML syntax error" ^ get_pos toks | |
| 24 | | err (toks, SOME msg) = "SML syntax error" ^ get_pos toks ^ ": " ^ msg; | |
| 25 | in Scan.!! err scan end; | |
| 26 | ||
| 27 | fun bad_input x = | |
| 30682 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 28 | (Scan.some (fn tok => (case ML_Lex.kind_of tok of ML_Lex.Error msg => SOME msg | _ => NONE)) :|-- | 
| 24594 | 29 | (fn msg => Scan.fail_with (K msg))) x; | 
| 30 | ||
| 31 | ||
| 32 | (** basic parsers **) | |
| 33 | ||
| 27817 | 34 | fun $$$ x = | 
| 30682 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 35 | 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: 
30672diff
changeset | 36 | >> ML_Lex.content_of; | 
| 24594 | 37 | |
| 30682 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 38 | 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: 
30672diff
changeset | 39 | |
| 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 40 | val regular = Scan.one ML_Lex.is_regular >> ML_Lex.content_of; | 
| 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 41 | val improper = Scan.one ML_Lex.is_improper >> ML_Lex.content_of; | 
| 24594 | 42 | |
| 43 | val blanks = Scan.repeat improper >> implode; | |
| 44 | ||
| 45 | ||
| 46 | (* fix_ints *) | |
| 47 | ||
| 48 | (*approximation only -- corrupts numeric record field patterns *) | |
| 49 | val fix_int = | |
| 50 | $$$ "#" ^^ blanks ^^ int || | |
| 51 | ($$$ "infix" || $$$ "infixr") ^^ blanks ^^ int || | |
| 52 |   int >> (fn x => "(" ^ x ^ ":int)") ||
 | |
| 53 | regular || | |
| 54 | bad_input; | |
| 55 | ||
| 56 | fun do_fix_ints s = | |
| 57 | Source.of_string s | |
| 30682 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 58 | |> ML_Lex.source | 
| 
dcb233670c98
eliminated non-canonical alias structure T = ML_Lex;
 wenzelm parents: 
30672diff
changeset | 59 | |> Source.source ML_Lex.stopper (Scan.bulk (!!! fix_int)) NONE | 
| 24594 | 60 | |> Source.exhaust | 
| 61 | |> implode; | |
| 62 | ||
| 63 | val fix_ints = if ml_system_fix_ints then do_fix_ints else I; | |
| 64 | ||
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 65 | |
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 66 | (* global use_context *) | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 67 | |
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 68 | val global_context: use_context = | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 69 |  {tune_source = fix_ints,
 | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 70 | name_space = ML_Name_Space.global, | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 71 | str_of_pos = Position.str_of oo Position.line_file, | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 72 | print = writeln, | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 73 | error = error}; | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30636diff
changeset | 74 | |
| 24594 | 75 | end; |