-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
(library | ||
(name parse) | ||
(libraries ast compiler)) | ||
(libraries ast compiler menhirLib)) | ||
|
||
(ocamllex lexer) | ||
|
||
(menhir | ||
(modules parser) | ||
(flags --explain)) | ||
; (flags --explain --table) | ||
(flags -lg 1 -la 1 -lc 2) | ||
) | ||
|
||
(rule | ||
(action | ||
(with-stdout-to unitActionsParser.mly | ||
(run menhir | ||
%{dep:parser.mly} | ||
--only-preprocess-u | ||
)))) | ||
|
||
(menhir | ||
(modules unitActionsParser) | ||
(flags --table --external-tokens Parser) | ||
) | ||
|
||
|
||
(rule | ||
(action (with-stdout-to errors.ml | ||
(run menhir | ||
%{dep:parser.mly} | ||
--compile-errors %{dep:errors.messages} | ||
) | ||
)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
toplevel: PLUS | ||
|
||
Missing left operand and right operand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,87 @@ | ||
open Ast | ||
open Compiler.Directive | ||
open Printf | ||
module L = MenhirLib.LexerUtil | ||
module P = Parser | ||
module E = MenhirLib.ErrorReports | ||
module I = UnitActionsParser.MenhirInterpreter | ||
|
||
let parse (input: string) : directive list * assignment list = | ||
try | ||
(* Lexer.reset_lexbuf filename buf ; *) | ||
P.toplevel Lexer.token (Lexing.from_string input) | ||
with P.Error -> | ||
failwith | ||
(Printf.sprintf "Parse error") | ||
let env checkpoint = | ||
match checkpoint with | ||
| I.HandlingError env -> | ||
env | ||
| _ -> | ||
assert false | ||
|
||
let state checkpoint : int = | ||
match I.top (env checkpoint) with | ||
| Some (I.Element (s, _, _, _)) -> | ||
I.number s | ||
| None -> 0 | ||
|
||
let show text positions = | ||
E.extract text positions | ||
|> E.sanitize | ||
|> E.compress | ||
|> E.shorten 20 (* max width 43 *) | ||
|
||
let get text checkpoint i = | ||
match I.get i (env checkpoint) with | ||
| Some (I.Element (_, _, pos1, pos2)) -> | ||
show text (pos1, pos2) | ||
| None -> "???" (* impossible *) | ||
|
||
let succeed _v = | ||
assert false (* impossible *) | ||
|
||
let fail text buffer (checkpoint : _ I.checkpoint) = | ||
let location = L.range (E.last buffer) in | ||
let err = (E.show (show text) buffer) in | ||
let indication = sprintf "Syntax error %s.\n" err in | ||
try | ||
let message = Errors.message (state checkpoint) in | ||
let message = E.expand (get text checkpoint) message in | ||
eprintf "%s%s%s" location indication message; | ||
exit 1 | ||
with Not_found -> | ||
eprintf "%s%s%s\n" location indication | ||
"Unknown error, more informative error messages coming soon"; | ||
exit 1 | ||
|
||
let slow filename text = | ||
let lexbuf = L.init filename (Lexing.from_string text) in | ||
let supplier = I.lexer_lexbuf_to_supplier Lexer.token lexbuf in | ||
let buffer, supplier = E.wrap_supplier supplier in | ||
let checkpoint = UnitActionsParser.Incremental.toplevel lexbuf.lex_curr_p in | ||
I.loop_handle succeed (fail text buffer) supplier checkpoint | ||
|
||
type parse_result = | ||
| Success of directive list * assignment list | ||
| Error of string | ||
|
||
let fast filename : parse_result = | ||
let text, lexbuf = | ||
try | ||
L.read filename | ||
with Sys_error s -> prerr_endline ("File error\n" ^ s); exit 1; | ||
in | ||
match P.toplevel Lexer.token lexbuf with | ||
| d, a -> | ||
Success (d, a) | ||
|
||
| exception DirectiveError msg -> | ||
eprintf "Directive Error: %s\n" msg; exit 1 | ||
|
||
| exception Lexer.Lexer_error msg -> | ||
prerr_endline msg; exit 1 | ||
|
||
| exception Parser.Error -> | ||
Error text | ||
|
||
|
||
let parse (filename: string) : directive list * assignment list = | ||
(* First try fast parser, then use slow parser to generate error if fail *) | ||
begin match fast filename with | ||
| Success (d, a) -> (d, a) | ||
| Error s -> slow filename s | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters