1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the Q Public License version 1.0. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* The grammar for lexer definitions */
|
|
|
|
|
|
|
|
%{
|
|
|
|
open Syntax
|
|
|
|
|
|
|
|
(* Auxiliaries for the parser. *)
|
|
|
|
|
1998-04-07 05:50:19 -07:00
|
|
|
let named_regexps =
|
|
|
|
(Hashtbl.create 13 : (string, regular_expression) Hashtbl.t)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let regexp_for_string s =
|
|
|
|
let rec re_string n =
|
|
|
|
if n >= String.length s then Epsilon
|
2002-10-28 08:46:50 -08:00
|
|
|
else if succ n = String.length s then
|
|
|
|
Characters (Cset.singleton (Char.code s.[n]))
|
|
|
|
else
|
|
|
|
Sequence
|
|
|
|
(Characters(Cset.singleton (Char.code s.[n])),
|
|
|
|
re_string (succ n))
|
1995-05-04 03:15:53 -07:00
|
|
|
in re_string 0
|
|
|
|
|
2002-10-29 08:23:37 -08:00
|
|
|
let rec remove_as = function
|
|
|
|
| Bind (e,_) -> remove_as e
|
|
|
|
| Epsilon|Eof|Characters _ as e -> e
|
|
|
|
| Sequence (e1, e2) -> Sequence (remove_as e1, remove_as e2)
|
|
|
|
| Alternative (e1, e2) -> Alternative (remove_as e1, remove_as e2)
|
|
|
|
| Repetition e -> Repetition (remove_as e)
|
|
|
|
|
2004-04-29 04:12:49 -07:00
|
|
|
let as_cset = function
|
|
|
|
| Characters s -> s
|
|
|
|
| _ -> raise Cset.Bad
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
%}
|
|
|
|
|
|
|
|
%token <string> Tident
|
1997-04-15 12:18:03 -07:00
|
|
|
%token <int> Tchar
|
1995-05-04 03:15:53 -07:00
|
|
|
%token <string> Tstring
|
|
|
|
%token <Syntax.location> Taction
|
2013-03-09 14:38:52 -08:00
|
|
|
%token Trule Tparse Tparse_shortest Tand Tequal Tend Tor Tunderscore Teof
|
2014-03-14 08:39:32 -07:00
|
|
|
Tlbracket Trbracket Trefill
|
2004-04-29 04:12:49 -07:00
|
|
|
%token Tstar Tmaybe Tplus Tlparen Trparen Tcaret Tdash Tlet Tas Tsharp
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
%right Tas
|
1995-05-04 03:15:53 -07:00
|
|
|
%left Tor
|
2002-04-22 03:06:01 -07:00
|
|
|
%nonassoc CONCAT
|
|
|
|
%nonassoc Tmaybe Tstar Tplus
|
2010-04-19 05:25:06 -07:00
|
|
|
%left Tsharp
|
|
|
|
%nonassoc Tident Tchar Tstring Tunderscore Teof Tlbracket Tlparen
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
%start lexer_definition
|
|
|
|
%type <Syntax.lexer_definition> lexer_definition
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
lexer_definition:
|
2014-04-12 03:17:02 -07:00
|
|
|
header named_regexps refill_handler Trule definition other_definitions
|
|
|
|
header Tend
|
1995-08-25 06:54:14 -07:00
|
|
|
{ {header = $1;
|
2014-03-14 08:39:32 -07:00
|
|
|
refill_handler = $3;
|
|
|
|
entrypoints = $5 :: List.rev $6;
|
|
|
|
trailer = $7} }
|
1995-05-04 03:15:53 -07:00
|
|
|
;
|
|
|
|
header:
|
|
|
|
Taction
|
|
|
|
{ $1 }
|
1998-04-07 05:50:19 -07:00
|
|
|
| /*epsilon*/
|
2013-09-04 08:12:37 -07:00
|
|
|
{ { loc_file = ""; start_pos = 0; end_pos = 0; start_line = 1;
|
|
|
|
start_col = 0 } }
|
1995-05-04 03:15:53 -07:00
|
|
|
;
|
1998-04-07 05:50:19 -07:00
|
|
|
named_regexps:
|
|
|
|
named_regexps Tlet Tident Tequal regexp
|
|
|
|
{ Hashtbl.add named_regexps $3 $5 }
|
|
|
|
| /*epsilon*/
|
|
|
|
{ () }
|
|
|
|
;
|
1995-05-04 03:15:53 -07:00
|
|
|
other_definitions:
|
|
|
|
other_definitions Tand definition
|
|
|
|
{ $3::$1 }
|
1998-04-07 05:50:19 -07:00
|
|
|
| /*epsilon*/
|
1995-05-04 03:15:53 -07:00
|
|
|
{ [] }
|
|
|
|
;
|
2014-03-14 08:39:32 -07:00
|
|
|
refill_handler:
|
|
|
|
| Trefill Taction { Some $2 }
|
|
|
|
| /*empty*/ { None }
|
|
|
|
;
|
1995-05-04 03:15:53 -07:00
|
|
|
definition:
|
2003-02-24 02:59:20 -08:00
|
|
|
Tident arguments Tequal Tparse entry
|
|
|
|
{ {name=$1 ; shortest=false ; args=$2 ; clauses=$5} }
|
|
|
|
| Tident arguments Tequal Tparse_shortest entry
|
|
|
|
{ {name=$1 ; shortest=true ; args=$2 ; clauses=$5} }
|
1995-05-04 03:15:53 -07:00
|
|
|
;
|
2002-12-09 02:44:46 -08:00
|
|
|
|
|
|
|
arguments:
|
|
|
|
Tident arguments { $1::$2 }
|
|
|
|
| /*epsilon*/ { [] }
|
|
|
|
;
|
|
|
|
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
entry:
|
2003-02-24 02:59:20 -08:00
|
|
|
case rest_of_entry
|
|
|
|
{ $1::List.rev $2 }
|
|
|
|
| Tor case rest_of_entry
|
1995-05-04 03:15:53 -07:00
|
|
|
{ $2::List.rev $3 }
|
|
|
|
;
|
2002-10-28 08:46:50 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
rest_of_entry:
|
|
|
|
rest_of_entry Tor case
|
|
|
|
{ $3::$1 }
|
|
|
|
|
|
|
|
|
{ [] }
|
|
|
|
;
|
|
|
|
case:
|
|
|
|
regexp Taction
|
|
|
|
{ ($1,$2) }
|
|
|
|
;
|
|
|
|
regexp:
|
|
|
|
Tunderscore
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Characters Cset.all_chars }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Teof
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Eof }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tchar
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Characters (Cset.singleton $1) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstring
|
|
|
|
{ regexp_for_string $1 }
|
|
|
|
| Tlbracket char_class Trbracket
|
|
|
|
{ Characters $2 }
|
|
|
|
| regexp Tstar
|
|
|
|
{ Repetition $1 }
|
|
|
|
| regexp Tmaybe
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Alternative(Epsilon, $1) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| regexp Tplus
|
2002-10-29 08:23:37 -08:00
|
|
|
{ Sequence(Repetition (remove_as $1), $1) }
|
2004-04-29 04:12:49 -07:00
|
|
|
| regexp Tsharp regexp
|
|
|
|
{
|
|
|
|
let s1 = as_cset $1
|
|
|
|
and s2 = as_cset $3 in
|
|
|
|
Characters (Cset.diff s1 s2)
|
2006-01-04 08:55:50 -08:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
| regexp Tor regexp
|
|
|
|
{ Alternative($1,$3) }
|
|
|
|
| regexp regexp %prec CONCAT
|
|
|
|
{ Sequence($1,$2) }
|
|
|
|
| Tlparen regexp Trparen
|
|
|
|
{ $2 }
|
1998-04-07 05:50:19 -07:00
|
|
|
| Tident
|
|
|
|
{ try
|
|
|
|
Hashtbl.find named_regexps $1
|
|
|
|
with Not_found ->
|
2006-09-12 03:36:50 -07:00
|
|
|
let p = Parsing.symbol_start_pos () in
|
|
|
|
Printf.eprintf "File \"%s\", line %d, character %d:\n\
|
|
|
|
Reference to unbound regexp name `%s'.\n"
|
|
|
|
p.Lexing.pos_fname p.Lexing.pos_lnum
|
|
|
|
(p.Lexing.pos_cnum - p.Lexing.pos_bol)
|
|
|
|
$1;
|
1998-04-07 05:50:19 -07:00
|
|
|
exit 2 }
|
2002-10-28 08:46:50 -08:00
|
|
|
| regexp Tas ident
|
2007-01-29 08:44:16 -08:00
|
|
|
{let p1 = Parsing.rhs_start_pos 3
|
|
|
|
and p2 = Parsing.rhs_end_pos 3 in
|
|
|
|
let p = {
|
2013-09-04 08:12:37 -07:00
|
|
|
loc_file = p1.Lexing.pos_fname ;
|
2007-01-29 08:44:16 -08:00
|
|
|
start_pos = p1.Lexing.pos_cnum ;
|
|
|
|
end_pos = p2.Lexing.pos_cnum ;
|
|
|
|
start_line = p1.Lexing.pos_lnum ;
|
|
|
|
start_col = p1.Lexing.pos_cnum - p1.Lexing.pos_bol ; } in
|
|
|
|
Bind ($1, ($3, p))}
|
1995-05-04 03:15:53 -07:00
|
|
|
;
|
2002-10-28 08:46:50 -08:00
|
|
|
|
|
|
|
ident:
|
|
|
|
Tident {$1}
|
|
|
|
;
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
char_class:
|
|
|
|
Tcaret char_class1
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Cset.complement $2 }
|
1995-05-04 03:15:53 -07:00
|
|
|
| char_class1
|
|
|
|
{ $1 }
|
|
|
|
;
|
|
|
|
char_class1:
|
|
|
|
Tchar Tdash Tchar
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Cset.interval $1 $3 }
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tchar
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Cset.singleton $1 }
|
1995-05-04 03:15:53 -07:00
|
|
|
| char_class1 char_class1 %prec CONCAT
|
2002-10-28 08:46:50 -08:00
|
|
|
{ Cset.union $1 $2 }
|
1995-05-04 03:15:53 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|