Commit 63aaf75e authored by POTTIER Francois's avatar POTTIER Francois

Cleanup of [SentenceLexer], including dead code removal,

replacing [error1] with [error2], and replacing [update_loc] with [new_line].
parent 85b19a53
......@@ -7,19 +7,10 @@
open SentenceParser
open Grammar
(* Updates the line counter, which is used in some error messages. *)
let update_loc lexbuf n =
let pos = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <- { pos with
pos_lnum = pos.pos_lnum + n;
pos_bol = pos.pos_cnum;
}
(* A short-hand. *)
let error1 lexbuf msg =
Error.error (Positions.one (lexeme_start_p lexbuf)) msg
let error2 lexbuf msg =
Error.error (Positions.two lexbuf.lex_start_p lexbuf.lex_curr_p) msg
}
......@@ -38,15 +29,17 @@ let comment = '#' [^'\010''\013']* newline
let skip = newline whitespace* newline
rule lex = parse
(* An identifier that begins with an lowercase letter is considered a
non-terminal symbol. It should be a start symbol. *)
| (lowercase identchar *) as lid
{ try
let nt = Nonterminal.lookup lid in
if StringSet.mem lid Front.grammar.UnparameterizedSyntax.start_symbols then
NONTERMINAL nt
else
error1 lexbuf (Printf.sprintf "\"%s\" is not a start symbol." lid)
error2 lexbuf (Printf.sprintf "\"%s\" is not a start symbol." lid)
with Not_found ->
error1 lexbuf (Printf.sprintf "\"%s\" is not a known non-terminal symbol." lid)
error2 lexbuf (Printf.sprintf "\"%s\" is not a known non-terminal symbol." lid)
}
(* An identifier that begins with an uppercase letter is considered a
terminal symbol. *)
......@@ -54,17 +47,17 @@ rule lex = parse
{ try
TERMINAL (Terminal.lookup uid)
with Not_found ->
error1 lexbuf (Printf.sprintf "\"%s\" is not a known terminal symbol." uid)
error2 lexbuf (Printf.sprintf "\"%s\" is not a known terminal symbol." uid)
}
(* Whitespace is ignored. *)
| whitespace
{ lexbuf.lex_start_p <- lexbuf.lex_curr_p; lex lexbuf }
{ lex lexbuf }
(* The end of a line is translated to [EOL]. *)
| newline
{ update_loc lexbuf 1; EOL }
{ new_line lexbuf; EOL }
(* A comment is ignored. *)
| comment
{ update_loc lexbuf 1; lexbuf.lex_start_p <- lexbuf.lex_curr_p; lex lexbuf }
{ new_line lexbuf; lex lexbuf }
(* The end of file is translated to [EOF]. *)
| eof
{ EOF }
......@@ -72,33 +65,5 @@ rule lex = parse
| ':'
{ COLON }
| _
{ error1 lexbuf "unexpected character." }
(* Note that [block] cannot fail. *)
and block buffer = parse
(* A blank line (or the end of file) signals the end of this block of text. *)
| (newline as n) whitespace* newline
{ update_loc lexbuf 2; Buffer.add_string buffer n; Buffer.contents buffer }
| eof
{ Buffer.contents buffer }
(* Anything else is considered part of the text.
(There is no syntax for comments here.) *)
| newline as n
{ update_loc lexbuf 1; Buffer.add_string buffer n; block buffer lexbuf }
| _ as c
{ Buffer.add_char buffer c; block buffer lexbuf }
(* Note that [skip] cannot fail. *)
{ error2 lexbuf "unexpected character." }
and skip = parse
(* Whitespace, newlines, comments are ignored. *)
| whitespace
{ lexbuf.lex_start_p <- lexbuf.lex_curr_p; skip lexbuf }
| newline
| comment
{ update_loc lexbuf 1; lexbuf.lex_start_p <- lexbuf.lex_curr_p; skip lexbuf }
(* Anything else causes us to stop. *)
| _
| eof
{ () }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment