Commit 0835feda authored by POTTIER Francois's avatar POTTIER Francois

Fixed [SentenceParser] by collecting positions manually,

for compatibility with both ocamlyacc and Menhir.
parent 16b9a544
......@@ -37,7 +37,7 @@ rule lex = parse
{ try
let nt = Nonterminal.lookup lid in
if StringSet.mem lid Front.grammar.UnparameterizedSyntax.start_symbols then
NONTERMINAL nt
NONTERMINAL (nt, lexbuf.lex_start_p, lexbuf.lex_curr_p)
else
error2 lexbuf (Printf.sprintf "\"%s\" is not a start symbol." lid)
with Not_found ->
......@@ -47,7 +47,7 @@ rule lex = parse
terminal symbol. *)
| (uppercase identchar *) as uid
{ try
TERMINAL (Terminal.lookup uid)
TERMINAL (Terminal.lookup uid, lexbuf.lex_start_p, lexbuf.lex_curr_p)
with Not_found ->
error2 lexbuf (Printf.sprintf "\"%s\" is not a known terminal symbol." uid)
}
......
......@@ -5,12 +5,16 @@
/* It is used also to read a [.messages] file. The entry point is [entry]. */
/* This parser must be compatible with both ocamlyacc and menhir, so we use
$ notation, do not use Menhir's standard library, and collect positions
manually. */
/* ------------------------------------------------------------------------ */
/* Tokens. */
%token COLON EOF EOL
%token<Grammar.Terminal.t> TERMINAL
%token<Grammar.Nonterminal.t> NONTERMINAL
%token<Grammar.Terminal.t * Lexing.position * Lexing.position> TERMINAL
%token<Grammar.Nonterminal.t * Lexing.position * Lexing.position> NONTERMINAL
%token<string> COMMENT
/* only manually-written comments, beginning with a single # */
......@@ -21,10 +25,39 @@
open SentenceParserAux
(* Removing the position information in a terminal or non-terminal symbol. *)
let strip_symbol (x, _, _) = x
(* Removing the position information in a sentence. *)
let strip_sentence (nto, terminals) =
Option.map strip_symbol nto,
List.map strip_symbol terminals
(* Computing the start and end positions of a sentence. *)
let locate_sentence (nto, terminals) =
let opening =
match nto, terminals with
| Some (_, opening, _), _
| None, (_, opening, _) :: _ ->
opening
| None, [] ->
Lexing.dummy_pos (* cannot happen *)
and closing =
match nto, List.rev terminals with
| _, (_, _, closing) :: _
| Some (_, _, closing), _ ->
closing
| None, [] ->
Lexing.dummy_pos (* cannot happen *)
in
Positions.two opening closing,
strip_sentence (nto, terminals)
%}
%type <terminals> terminals
%type <sentence> sentence
%type <located_sentence> located_sentence
%type <SentenceParserAux.sentence option> optional_sentence
......@@ -49,17 +82,14 @@ located_sentences_or_comments:
/* A located sentence. */
located_sentence: sentence
{ let pos = Positions.two
(Parsing.symbol_start_pos())
(Parsing.symbol_end_pos()) in
pos, $1 }
{ locate_sentence $1 }
/* An optional sentence. */
optional_sentence:
| EOF
{ None }
| sentence
{ Some $1 }
{ Some (strip_sentence $1) }
/* A sentence is a pair of an optional non-terminal start symbol and a list
of terminal symbols. It is terminated by a newline. */
......
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