common.anubis 6.96 KB

   *Project*                               Anubis
   
   *Title*              Common Stuff for the Anubis Parser Generator.
   
   *Copyright*               Copyright (c) Alain Proute' 2006.
   
   
   
   *Author*  Alain Proute'
   
   *Created* March 2006
   *Revised* March 2006
   
   
   *Overview*

   This file  contains the public  types and definitions  which are common to  the several
   parts of the Anubis Parser Generator (APG).
   
   *Public*

   
   *Name* APG_Error
   
   *Description*
   
   This type  gathers all  the sorts  of errors which  may occur  during execution  of the
   Anubis parser Generator.
   
public type APG_Error:
   unexpected_end_of_input                  (Int32 line),
   no_parser_name                           (Int32 line),
   keyword_was_expected                     (Int32 line), 
   unknown_declaration_keyword              (Int32 line, String keyword),
   type_description_expected                (Int32 line),
   incorrect_symbol_value                   (Int32 line),
   symbol_expected                          (Int32 line), 
   colon_expected                           (Int32 line),
   incorrect_rule_precedence                (Int32 line),
   misclosed_rule_precedence                (Int32 line),
   incorrect_end_of_rule                    (Int32 line),
   bad_end_of_precedence_declaration        (Int32 line),
   bad_end_of_type_declaration              (Int32 line). 
   

   
   
   
   *Name* en_print
   
   *Description* The function  `en_print` prints in English a  message of type `APG_Error`
   on the standard output.
   
public define One
   en_print
     (
       APG_Error  e
     ). 
   
   
   
   
   
   
   *Name* APG_Grammar
   
   *Description*
   
   This is  the type of (abstract)  grammars.  An object of  this type is  produced by the
   function `read_APG_grammar`  (see `read_grammar.anubis`), and  given as an  argument to
   the function `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque.
   
public type APG_Grammar:...
   
   
   

   *Name* print_grammar
   
   *Description* 
   
   This function  prints a  grammar (of  type `APG_Grammar`) on  the standard  output. The
   preambule and postambule are not printed.
   
public define One
   print_grammar
     (
       APG_Grammar   g
     ). 
   
   
   
   
   
   *Name* APG_Automaton:
  
   This  type represents  abstract  automatons as  they  are constructed  by the  function
   `make_APG_automaton` (see `make_automaton.anubis`). This is an opaque type.
   
public type APG_Automaton:...
   
   
   
   
   
   
   *Private*
   

read tools/basis.anubis   
   
public type APG_Precedence_Dec:
   left          (List(String)                symbol_names),
   right         (List(String)                symbol_names),
   non_assoc     (List(String)                symbol_names).
   
public type APG_Type_Dec:
   type_dec      (String                      type,
                  List(String)                symbol_names).
   
public type APG_Symbol_Value:
   symbol_value  (String                      name,
                  Maybe(String)               value). 
   
public type APG_Grammar_Rule:
   grammar_rule  (Int32                       id,   // the grammar reader generates numbers
                  APG_Symbol_Value            head,
                  List(APG_Symbol_Value)      body,
                  Maybe(String)               precedence). 
   
public type APG_Grammar:
   grammar       (String                      preambule,
                  String                      parser_name,
                  List(APG_Precedence_Dec)    precedence_declarations,
                  List(APG_Type_Dec)          type_declarations,
                  List(APG_Grammar_Rule)      grammar_rules,
                  String                      postambule). 

   
public type APG_Automaton:
   . 
   
   
public define One
   en_print
     (
       APG_Error  e
     ) =
   if e is 
     {
       unexpected_end_of_input(Int32 line) then
         print(integer_to_string(line)+": Unexpected end of input.\n"),
   
       no_parser_name(Int32 line) then
         print(integer_to_string(line)+": No parser name.\n"),
   
       keyword_was_expected(Int32 line) then
         print(integer_to_string(line)+": Keyword was expected.\n"),
   
       unknown_declaration_keyword(Int32 line,String keyword) then
         print(integer_to_string(line)+": Unknown declaration keyword '"+keyword+"'.\n"),
   
       type_description_expected(Int32 line) then
         print(integer_to_string(line)+": Type description expected.\n"),
   
       incorrect_symbol_value(Int32 line) then
         print(integer_to_string(line)+": Incorrect symbol value.\n"),
   
       symbol_expected(Int32 line) then
         print(integer_to_string(line)+": Symbol expected.\n"),
   
       colon_expected(Int32 line) then
         print(integer_to_string(line)+": Colon expected.\n"),
   
       incorrect_rule_precedence(Int32 line) then
         print(integer_to_string(line)+": Incorrect rule precedence.\n"),
   
       misclosed_rule_precedence(Int32 line) then
         print(integer_to_string(line)+": Misclosed rule precedence.\n"),
   
       incorrect_end_of_rule(Int32 line) then
         print(integer_to_string(line)+": Incorrect end of rule.\n"),
   
       bad_end_of_precedence_declaration(line) then 
         print(integer_to_string(line)+": Incorrect end of precedence declaration.\n"),
   
       bad_end_of_type_declaration(line) then 
         print(integer_to_string(line)+": Incorrect end of type declaration.\n"),
   }. 
   
   
   
   
define One
   print
     (
       APG_Precedence_Dec   d
     ) =
   if d is 
     {
       left(names)       then map_forget((String s) |-> print("left        "+s+"\n"),names),
       right(names)      then map_forget((String s) |-> print("right       "+s+"\n"),names), 
       non_assoc(names)  then map_forget((String s) |-> print("non_assoc   "+s+"\n"),names) 
     }.
   

define One
   print
     (
       APG_Type_Dec  d
     ) =
   if d is type_dec(type,names) then 
   map_forget((String s) |-> print(type+"   "+s+"\n"),names). 
   
   
   
define One
   print
     (
       APG_Symbol_Value  sv
     ) =
   if sv is symbol_value(name,mb_value)
   then print(name+" "+ if mb_value is 
                {
                  failure then "", 
                  success(value) then "{"+value+"} "
                }). 
   
   
define One 
   print   
     (
       APG_Grammar_Rule   r
     ) =
   if r is grammar_rule(id,head,body,prec) then 
   print("["+id+"] "); 
   print(head); 
   print(":   "); 
   map_forget(print,body);
   print("\n"). 
   
public define One
   print_grammar
     (
       APG_Grammar   g
     ) =
   if g is  grammar(preambule,
                    parser_name,
                    precedence_declarations,
                    type_declarations,
                    grammar_rules,
                    postambule) then
      print("Parser name: "+parser_name+"\n");
      map_forget(print,precedence_declarations);
      map_forget(print,type_declarations); 
      map_forget(print,grammar_rules).