123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(******************************************************************************)(* *)(* Menhir *)(* *)(* Copyright Inria. All rights reserved. This file is distributed under *)(* the terms of the GNU Library General Public License version 2, with a *)(* special exception on linking, as described in the file LICENSE. *)(* *)(******************************************************************************)(* This module provides some type and function definitions
that help deal with the keywords that we recognize within
semantic actions. *)(* ------------------------------------------------------------------------- *)(* Types. *)(* The user can request position information either at type
[int] (a simple offset) or at type [Lexing.position]. *)typeflavor=|FlavorOffset|FlavorPosition|FlavorLocation(* The user can request position information about the $start or $end
of a symbol. Also, $symbolstart requests the computation of the
start position of the first nonempty element in a production. *)typewhere=|WhereSymbolStart|WhereStart|WhereEnd(* The user can request position information about a production's
left-hand side or about one of the symbols in its right-hand
side, which he can refer to by position or by name. *)typesubject=|Before|Left|RightNamedofstring(* Keywords inside semantic actions. They allow access to semantic
values or to position information. *)typekeyword=|Positionofsubject*where*flavor(* Constants. *)letstartpos=Position(Left,WhereStart,FlavorPosition)(* $startpos *)letendpos=Position(Left,WhereEnd,FlavorPosition)(* $endpos *)(* ------------------------------------------------------------------------- *)(* These auxiliary functions help map a [Position] keyword to the
name of the variable that the keyword is replaced with. *)letwhere=function|WhereSymbolStart->"symbolstart"|WhereStart->"start"|WhereEnd->"end"letsubject=function|Before->"__0_"|Left->""|RightNamedid->Printf.sprintf"_%s_"idletflavor=function|FlavorPosition->"pos"|FlavorOffset->"ofs"|FlavorLocation->"loc"letposvarswf=matchw,fwith|_,(FlavorOffset|FlavorPosition)->Printf.sprintf"_%s%s%s"(wherew)(flavorf)(subjects)|WhereSymbolStart,FlavorLocation->"_sloc"|WhereStart,FlavorLocation->Printf.sprintf"_loc%s"(subjects)|_->assertfalseletkposvarkeyword=matchkeywordwith|Position(s,w,f)->posvarswfletprint_subject=function|Before->"($0)"|Left->""|RightNamedid->Printf.sprintf"(%s)"idletprintkeyword=matchkeywordwith|Position(s,w,f)->matchw,fwith|_,(FlavorOffset|FlavorPosition)->Printf.sprintf"$%s%s%s"(wherew)(flavorf)(print_subjects)|WhereSymbolStart,FlavorLocation->"$sloc"|WhereStart,FlavorLocation->Printf.sprintf"$loc%s"(print_subjects)|_->assertfalse(* ------------------------------------------------------------------------- *)(* Sets of keywords. *)moduleKeywordSet=structincludeSet.Make(structtypet=keywordletcompare=compareend)letmapfkeywords=fold(funkeywordaccu->add(fkeyword)accu)keywordsemptyend