1 2(*****************************************************************************) 3(* Main.sml : mosmllex/ mosmlyacc parser for PSL/Sugar *) 4(*****************************************************************************) 5 6(****************************************************************************** 7* Parse a string with a mosmlyacc parser entry point; doesn't report errors 8******************************************************************************) 9fun parseString parser s = 10 let val expr = parser Lexer.Token (Lexing.createLexerString s) 11 in 12 Parsing.clearParser(); 13 expr 14 end 15 handle exn => (Parsing.clearParser(); raise exn); 16 17(****************************************************************************** 18* Parse PSL constructs from a string (no error reporting) 19******************************************************************************) 20 21val parseBexp = parseString Parser.MainBoolean 22and parseSere = parseString Parser.MainSERE 23and parseFl = parseString Parser.MainFL 24and parseObe = parseString Parser.MainOBE 25and parseState = parseString Parser.MainState 26and parsePath = parseString Parser.MainPath 27and parsePathSere = parseString Parser.MainPathSERE 28and parsePathFl = parseString Parser.MainPathFL; 29 30(****************************************************************************** 31* Write a string to a temporary file; save last file name in tmp_name 32******************************************************************************) 33val tmp_name = ref "undefined"; 34 35fun stringToFile s = 36 let open TextIO; 37 val tmp = FileSys.tmpName() 38 val tmpname = tmp^".pslparse"; 39 val outstr = TextIO.openOut tmpname 40 fun out s = output(outstr,s) 41 in 42 (out s; 43 flushOut outstr; 44 closeOut outstr; 45 tmp_name := tmp; 46 tmpname) 47 end; 48 49(****************************************************************************** 50* Auxiliary function to parse from a file without reporting error locations 51******************************************************************************) 52fun parseFileNoReport parser file stream lexbuf = 53 let val expr = parser Lexer.Token lexbuf 54 in 55 Parsing.clearParser(); 56 expr 57 end 58 handle exn => (Parsing.clearParser(); raise exn); 59 60(****************************************************************************** 61* Auxiliary funtion to parse from a file and report error locations 62******************************************************************************) 63fun parseFileReport parser file stream lexbuf = 64 let val expr = 65 parser Lexer.Token lexbuf 66 handle 67 Parsing.ParseError f => 68 let val pos1 = Lexing.getLexemeStart lexbuf 69 val pos2 = Lexing.getLexemeEnd lexbuf 70 in 71 Location.errMsg (file, stream, lexbuf) 72 (Location.Loc(pos1, pos2)) 73 "Syntax error." 74 end 75 | Lexer.LexicalError(msg, pos1, pos2) => 76 if pos1 >= 0 andalso pos2 >= 0 then 77 Location.errMsg (file, stream, lexbuf) 78 (Location.Loc(pos1, pos2)) 79 ("Lexical error: " ^ msg) 80 else 81 (Location.errPrompt ("Lexical error: " ^ msg ^ "\n\n"); 82 raise Fail "Lexical error"); 83 in 84 Parsing.clearParser(); 85 expr 86 end 87 handle exn => (Parsing.clearParser(); raise exn); 88 89(****************************************************************************** 90* Create lexer from instream 91******************************************************************************) 92fun createLexerStream (is : BasicIO.instream) = 93 Lexing.createLexer (fn buff => fn n => Nonstdio.buff_input is buff 0 n) 94 95(****************************************************************************** 96* Parse from a file and report error locations, given a parser entry point 97******************************************************************************) 98fun parse parser file = 99 let val is = Nonstdio.open_in_bin file 100 val lexbuf = createLexerStream is 101 val expr = parseFileReport parser file is lexbuf 102 handle exn => (BasicIO.close_in is; raise exn) 103 in 104 BasicIO.close_in is; 105 expr 106 end 107 108(****************************************************************************** 109* Parse PSL constructs from a file and report error locations 110******************************************************************************) 111val parseFileBexp = parse Parser.MainBoolean 112and parseFileSere = parse Parser.MainSERE 113and parseFileFl = parse Parser.MainFL 114and parseFileObe = parse Parser.MainOBE 115and parseFileState = parse Parser.MainState 116and parseFilePath = parse Parser.MainPath 117and parseFilePathSere = parse Parser.MainPathSERE 118and parseFilePathFl = parse Parser.MainPathFL; 119