PreviousUpNext

15.4.574  src/lib/compiler/front/parser/raw-syntax/raw-syntax.pkg

## raw-syntax.pkg

# Compiled by:
#     src/lib/compiler/front/parser/parser.sublib



# Here we define the raw syntax produced by the
# Mythryl parser (see compiler/parse/yacc/mythryl.grammar)
# and consumed by the typechecker, rooted at
#    src/lib/compiler/front/typer/main/translate-raw-syntax-to-deep-syntax-g.pkg
# -- which in turn returns deep syntax, defined in
#    src/lib/compiler/front/typer-stuff/deep-syntax/deep-syntax.api
#    src/lib/compiler/front/typer-stuff/deep-syntax/deep-syntax.pkg
#
# Nothing subtle here -- just a simple tree
# representation of Mythryl surface syntax.
#
# SOURCE CODE REGIONS:
#     For debugging purposes, it is necessary to
#     associate source file addresses (i.e., line
#     and column numbers) with the various parts of
#     the syntax tree.
#
#     Rather than burden every syntax tree node type
#     with this information, we segregate it in
#     SOURCE_CODE_REGION_* nodes, one per enum.
#
#     This lets us achieve some separation of concerns
#     between source-file annocations and the rest of
#     the syntax tree semantics.



###                   "The real problem is not whether
###                    machines think but whether men do."
###
###                                    -- B. F. Skinner



package   raw_syntax
: (weak)  Raw_Syntax                                    # Raw_Syntax    is from   src/lib/compiler/front/parser/raw-syntax/raw-syntax.api
{
    include package   symbol;
    include package   fixity;


     # To mark positions in files:

     Source_Code_Position
         =
         Int;                                           # Character position from beginning of stream (base 0) 

     Source_Code_Region
         =
         (Source_Code_Position, Source_Code_Position);  # Start and end position of region 


     # Symbolic path (package::spath) 

     Path =  List( Symbol );

     Fixity_Item X
         =
         { item:                X,
           fixity:              Null_Or( Symbol ),
           source_code_region:  Source_Code_Region
         };

     Literal
         =
         multiword_int::Int;

     Package_Cast X
        =      NO_PACKAGE_CAST
        |    WEAK_PACKAGE_CAST  X
        |  STRONG_PACKAGE_CAST  X
        | PARTIAL_PACKAGE_CAST  X
        ;

    Fun_Kind
        =   PLAIN_FUN
        |  METHOD_FUN
        | MESSAGE_FUN
        ;

    Package_Kind
        = PLAIN_PACKAGE
        | CLASS_PACKAGE
        | CLASS2_PACKAGE
        ;

    Raw_Expression

        # Core expressions are those which don't
        # involve module stuff -- bread and butter
        # variables, constants, addition, if-then-else etc etc:


        = VARIABLE_IN_EXPRESSION            Path                                                #  Variable.                          
        | IMPLICIT_THUNK_PARAMETER          Path                                                #  #x
        | INT_CONSTANT_IN_EXPRESSION        Literal                                             #  Integer.                           
        | UNT_CONSTANT_IN_EXPRESSION        Literal                                             #  Unt literal.                      
        | FLOAT_CONSTANT_IN_EXPRESSION      String                                              #  Floating point coded by its string.
        | STRING_CONSTANT_IN_EXPRESSION     String                                              #  String.                            
        | CHAR_CONSTANT_IN_EXPRESSION       String                                              #  Char.                              
        | FN_EXPRESSION                     List( Case_Rule )                                   #  Anonymous function definition.     
        | RECORD_SELECTOR_EXPRESSION        Symbol                                              #  Selector of a record field.        
        | PRE_FIXITY_EXPRESSION             List( Fixity_Item( Raw_Expression ) )               #  Expressions before fixity parsing. 
        | APPLY_EXPRESSION            { function: Raw_Expression, argument: Raw_Expression }    #  Function application.              
        | OBJECT_FIELD_EXPRESSION     { object:   Raw_Expression, field: Symbol }               #  object->field.
        | CASE_EXPRESSION             { expression: Raw_Expression, rules: List( Case_Rule ) }  #  Case expression.                   
        | LET_EXPRESSION              { declaration: Declaration, expression: Raw_Expression }  #  Let expression.                    
        | SEQUENCE_EXPRESSION         List( Raw_Expression )                                    #  Sequence of expressions.           
        | RECORD_IN_EXPRESSION           List ((Symbol, Raw_Expression))                                #  Record.                            
        | LIST_EXPRESSION             List( Raw_Expression )                                    #  [list, in, square, brackets]          
        | TUPLE_EXPRESSION            List( Raw_Expression )                                    #  Tuple (derived form).              
        | VECTOR_IN_EXPRESSION           List( Raw_Expression )                                         #  Vector.                            
        | TYPE_CONSTRAINT_EXPRESSION  { expression: Raw_Expression, constraint: Any_Type }      #  Type constraint.                   
        | EXCEPT_EXPRESSION           { expression: Raw_Expression, rules: List( Case_Rule ) }  #  Exception handler.                 
        | RAISE_EXPRESSION            Raw_Expression                                            #  Raise an exception.                
        | AND_EXPRESSION          (Raw_Expression, Raw_Expression)                              #  'and' (derived form).          
        | OR_EXPRESSION           (Raw_Expression, Raw_Expression)                              #  'or' (derived form).           
        | WHILE_EXPRESSION            { test: Raw_Expression, expression: Raw_Expression }      #  'while' (derived form).            
        | IF_EXPRESSION               { test_case: Raw_Expression,
                                         then_case: Raw_Expression,
                                         else_case: Raw_Expression }                            #  If-then-else (derived form).       
        | SOURCE_CODE_REGION_FOR_EXPRESSION  (Raw_Expression, Source_Code_Region)               #  For error messages.                



    also
    Case_Rule                   #  Rules for case functions and exception handlers.
        =
        CASE_RULE
            {
              pattern:    Case_Pattern,
              expression: Raw_Expression
            }



    also
    Case_Pattern

        # Here we define patterns for 'case'
        # statements.  These are also used in
        # 'fun' function definitions and in
        # 'except' statements, both of which
        # include disguised case statements:


        = WILDCARD_PATTERN                                                              #  Empty pattern.                       
        | VARIABLE_IN_PATTERN             Path                                          #  Variable pattern.                    
        | INT_CONSTANT_IN_PATTERN         Literal                                       #  Integer literal.                     
        | UNT_CONSTANT_IN_PATTERN         Literal                                       #  Unsigned integer literal.
        | STRING_CONSTANT_IN_PATTERN      String                                        #  String literal.                      
        | CHAR_CONSTANT_IN_PATTERN   String                                             #  Character literal.                   
        | LIST_PATTERN                    List( Case_Pattern )                          #  [list, in, square, brackets]         
        | TUPLE_PATTERN                   List( Case_Pattern )                          #  Tuple.                               
        | PRE_FIXITY_PATTERN              List( Fixity_Item( Case_Pattern ) )                   #  Patterns prior to fixity parsing.    
        | APPLY_PATTERN                   { constructor: Case_Pattern, argument: Case_Pattern } #  Constructor unpacking.               
        | TYPE_CONSTRAINT_PATTERN         { pattern: Case_Pattern,     type_constraint: Any_Type }      #  Type constraint.                     
        | VECTOR_PATTERN                  List( Case_Pattern )                          #  Vector.                              
        | OR_PATTERN                      List( Case_Pattern )                          #  '|'-pattern.                         
        | AS_PATTERN                      { variable_pattern:   Case_Pattern,
                                            expression_pattern: Case_Pattern            #  'as' expressions.
                                          }
        | RECORD_PATTERN                  { definition:     List( ((Symbol, Case_Pattern)) ),
                                            is_incomplete:  Bool                        #  Record.
                                          }
        | SOURCE_CODE_REGION_FOR_PATTERN  (Case_Pattern, Source_Code_Region)            #  For error msgs etc.                  



    also
    Package_Expression

        # Here we define 'package'- (i.e., module-) -valued
        # expressions.  We may reference a pre-existing package
        # by name, define one by explicitly listing its elements,
        # modify an exising one via api constraint, or
        # generate a new one via generic expansion:

        = PACKAGE_BY_NAME                   Path                                        #  Variable package.                    
        | PACKAGE_DEFINITION                Declaration                                 #  Defined package.                     
        | CALL_OF_GENERIC                  (Path, List ((Package_Expression, Bool)))    #  Application (user-generated).        
        | INTERNAL_CALL_OF_GENERIC         (Path, List ((Package_Expression, Bool)))    #  Application (compiler-generated).    
        | LET_IN_PACKAGE                   (Declaration, Package_Expression)            #  'stipulate' in package.                      
        | PACKAGE_CAST                     (Package_Expression,
                                              Package_Cast( Api_Expression ))           #  Package cast per API.
        | SOURCE_CODE_REGION_FOR_PACKAGE    (Package_Expression, Source_Code_Region)    #  For error msgs etc.                  



    also
    Generic_Expression

        # Here we define 'generic'-valued expressions.
        # Much as with 'package's, We may reference a
        # pre-existing generic by name, define one by
        # explicitly listing its parameters and body,
        # or generate a new one via higher-order generic
        # expansion:


        = GENERIC_BY_NAME     (Path, Package_Cast Generic_Api_Expression)               #  generic variable.                    
        | LET_IN_GENERIC      (Declaration, Generic_Expression)
        | GENERIC_DEFINITION  {                                                         #  Explicit generic definition.         
             parameters:         List( (Null_Or( Symbol ), Api_Expression)),
             body:               Package_Expression,
             constraint:         Package_Cast( Api_Expression )
          }
        | CONSTRAINED_CALL_OF_GENERIC  (Path,                                           #  Application.                         
                                List ((Package_Expression, Bool)),                      #  Parameter (s).                       
                                Package_Cast( Generic_Api_Expression ))                 #  Package cast per api.
        | SOURCE_CODE_REGION_FOR_GENERIC  (Generic_Expression, Source_Code_Region)      #  For debugging msgs etc.              



    also
    Api_Expression

        # Here we define 'api'-valued expressions.
        # Currently we can only reference a pre-existing
        # api by name, or else define one by
        # explicitly listing its elements, although
        # allowing APIs to take parameters is a
        # common and easy extension:
        #
        = API_BY_NAME                 Symbol                                            #  API variable.                        
        | API_WITH_WHERE_SPECS        (Api_Expression, List( Where_Spec ))              #  Api with 'where' spec.               
        | API_DEFINITION              List( Api_Element )                               #  Defined api.                 
        | SOURCE_CODE_REGION_FOR_API  (Api_Expression, Source_Code_Region)              #  For debugging msgs etc.              



    also
    Where_Spec

        # Define the '... where ...' clauses which
        # may be appended to api constraints:

        = WHERE_TYPE       (List( Symbol ), List( Typevar ), Any_Type)
        | WHERE_PACKAGE  (List( Symbol ), List( Symbol ))



    also
    Generic_Api_Expression 

        # generic-api valued expressions.
        # Once again, we can define one explicitly
        # or reference a pre-defined one by name:

        = GENERIC_API_BY_NAME     Symbol                                                #  Generic api variable.                
        | GENERIC_API_DEFINITION  {                                                     #  Generic api definition.      
              parameter: List( (Null_Or (Symbol), Api_Expression) ),
              result:    Api_Expression
          }
        | SOURCE_CODE_REGION_FOR_GENERIC_API  (Generic_Api_Expression,                  #  For error messages etc.              
                                                     Source_Code_Region)



    also
    Api_Element

        # Here we define the various things that
        # can appear inside an api definition:

        = GENERICS_IN_API               List( (Symbol, Generic_Api_Expression) )        #  Generic.                             
        | VALUES_IN_API                 List( (Symbol, Any_Type) )                      #  Value.                               
        | EXCEPTIONS_IN_API             List( (Symbol, Null_Or( Any_Type )) )           #  Exception.                           
        | PACKAGE_SHARING_IN_API        List( Path )                                    #  Package sharing.                     
        | TYPE_SHARING_IN_API           List( Path )                                    #  Type sharing.                        
        | IMPORT_IN_API                 Api_Expression                                  #  Include specifier.                   

        | PACKAGES_IN_API               List ( (Symbol,                                 #  Package.                             
                                                 Api_Expression,
                                                 Null_Or( Path )) )

        | TYPES_IN_API                  ( (List( (Symbol,                                               #  Type.                                
                                                   List( Typevar ),
                                                   Null_Or( Any_Type ))
                                                  ),
                                                  Bool))

        | VALCONS_IN_API                { sumtypes:     List( Sumtype ),
                                          with_types:   List( Named_Type )
                                        }

        | SOURCE_CODE_REGION_FOR_API_ELEMENT  (Api_Element, Source_Code_Region) #  For error messages etc.              



    also
    Declaration

        # Here we define the declarations which may
        # appear in 'stipulate' statements and package
        # definitions:


        = VALUE_DECLARATIONS             ((List( Named_Value ), List( Typevar )) )      # Values.                               
        | FIELD_DECLARATIONS             ((List( Named_Field ), List( Typevar )) )      # OOP 'field' declarations.
        | EXCEPTION_DECLARATIONS           List( Named_Exception )                              # Exception.                            
        | PACKAGE_DECLARATIONS             List( Named_Package )                                # Packages.                             
        | TYPE_DECLARATIONS                List( Named_Type      )                              # Type declarations.                    
        | GENERIC_DECLARATIONS             List( Named_Generic   )                              # Generics.                             
        | API_DECLARATIONS                 List( Named_Api )                                    # APIs.                         
        | GENERIC_API_DECLARATIONS         List( Named_Generic_Api )                            # generic APIs.                 
        | LOCAL_DECLARATIONS               (Declaration, Declaration)                           # Local declarations.                   
        | SEQUENTIAL_DECLARATIONS          List( Declaration )                                  # Sequences of declarations.            
        | INCLUDE_DECLARATIONS             List( Path )                                         # 'include's of other package.          
        | OVERLOADED_VARIABLE_DECLARATION (Symbol, Any_Type, List(Raw_Expression), Bool)        # Operator overloading.
        | FIXITY_DECLARATIONS              { fixity: Fixity, ops: List( Symbol ) }              # Operator fixities.                    
        | FUNCTION_DECLARATIONS            ((List( Named_Function ), List( Typevar )) ) # Mutually recursive functions. 
        | NADA_FUNCTION_DECLARATIONS       ((List( Nada_Named_Function), List(Typevar)))        # Mutually recursive functions. 

        | RECURSIVE_VALUE_DECLARATIONS     ( (List( Named_Recursive_Value ),                    # Recursive values.                     
                                              List( Typevar ))
                                           )

        | SUMTYPE_DECLARATIONS     { sumtypes:  List( Sumtype ),                # BAR | ZOT part of   Foo = BAR | ZOT.
                                             with_types:                List( Named_Type )
                                           }

        | SOURCE_CODE_REGION_FOR_DECLARATION  (Declaration, Source_Code_Region)                 # For error messages etc.               

        | PRE_COMPILE_CODE                 String                                               # Support for    #DO set_control "FOO" "BAR"<eol>


    also
    Named_Field

        # OOP 'field' declarations
        #
        = NAMED_FIELD { name:  Symbol,
                        type:  Any_Type,
                        init:  Null_Or( Raw_Expression )
                      }

        | SOURCE_CODE_REGION_FOR_NAMED_FIELD  (Named_Field, Source_Code_Region)



    also
    Named_Value

        # Your everyday vanilla 'stipulate' named values.
        # The 'lazy' flag is in support of a
        # SML/NJ extension to SML proper,
        # carried over into Mythryl:
        #
        = NAMED_VALUE
              {
                pattern:    Case_Pattern,
                expression: Raw_Expression,
                is_lazy:    Bool
              }

        | SOURCE_CODE_REGION_FOR_NAMED_VALUE  (Named_Value, Source_Code_Region)



    also
    Named_Recursive_Value

        #  Namings for the 'let rec ...' construct: 
        #
        = NAMED_RECURSIVE_VALUE
              {
                variable_symbol:  Symbol,
                fixity:           Null_Or( (Symbol, Source_Code_Region) ),
                expression:       Raw_Expression,
                null_or_type:     Null_Or( Any_Type ),
                is_lazy:          Bool
              }

        | SOURCE_CODE_REGION_FOR_RECURSIVELY_NAMED_VALUE  (Named_Recursive_Value, Source_Code_Region)



    also
    Named_Function

        # Handle 'fun f (x) => x;
        #             f (y) => y;
        #             ...
        #         end;'
        # constructs, one pattern_clause per alternative:
        #
        = NAMED_FUNCTION
            {
              kind:             Fun_Kind,
              pattern_clauses:  List( Pattern_Clause ),
              is_lazy:          Bool,
              null_or_type:     Null_Or(Any_Type)
            }

        | SOURCE_CODE_REGION_FOR_NAMED_FUNCTION  (Named_Function, Source_Code_Region)



    also
    Pattern_Clause

        = PATTERN_CLAUSE
              {
                patterns:     List(  Fixity_Item(     Case_Pattern ) ),
                result_type:  Null_Or( Any_Type ),
                expression:   Raw_Expression
              }


    also
    Nada_Named_Function

        # Handle 'fun f (x)=x | f (y)=y | ...' constructs,
        # one Nada_Pattern_Clause per alternative.  This
        # is dead code from an aborted line of development;
        # these rules should probably be removed unless they
        # find a use soon, along with the other *nada*
        # stuff here.  XXX BUGGO FIXME.
        #
        = NADA_NAMED_FUNCTION  (List( Nada_Pattern_Clause ), Bool)                              #  Bool indicates whether lazy 
        | SOURCE_CODE_REGION_FOR_NADA_NAMED_FUNCTION  (Nada_Named_Function, Source_Code_Region)



    also
    Nada_Pattern_Clause

        = NADA_PATTERN_CLAUSE  {   pattern:     Case_Pattern,
                                      result_type:  Null_Or( Any_Type ),
                                      expression:  Raw_Expression
                                  }



    also
    Named_Type

        = NAMED_TYPE { name_symbol:     Symbol,
                       definition:      Any_Type,
                       typevars:        List( Typevar )
                     }

        | SOURCE_CODE_REGION_FOR_NAMED_TYPE  (Named_Type, Source_Code_Region)



    also
    Sumtype

        = SUM_TYPE { name_symbol:       Symbol,
                       typevars:        List( Typevar ),
                       right_hand_side: Sumtype_Right_Hand_Side,
                       is_lazy:         Bool
                     }

        | SOURCE_CODE_REGION_FOR_UNION_TYPE  (Sumtype, Source_Code_Region)



    also
    Sumtype_Right_Hand_Side

        # The first case handles vanilla union type definitions,
        # the second case handles 'Foo == abc::Bar' ones:


        = VALCONS   List( (Symbol, Null_Or( Any_Type )) )
        | REPLICAS            List( Symbol )



    also
    Named_Exception

        = NAMED_EXCEPTION            { exception_symbol: Symbol,                        #  Explicit exception definition.               
                                       exception_type:   Null_Or( Any_Type )
                                     }

        | DUPLICATE_NAMED_EXCEPTION  {   exception_symbol: Symbol,                      #  Defined by equality.                         
                                         equal_to:         Path
                                     }

        | SOURCE_CODE_REGION_FOR_NAMED_EXCEPTION  (Named_Exception, Source_Code_Region)



    also
    Named_Package

        = NAMED_PACKAGE { name_symbol: Symbol,
                          definition:  Package_Expression,
                          constraint:  Package_Cast( Api_Expression ),
                          kind:        Package_Kind
                        }

        | SOURCE_CODE_REGION_FOR_NAMED_PACKAGE  (Named_Package, Source_Code_Region)



    also
    Named_Generic

        = NAMED_GENERIC  {    name_symbol: Symbol,
                                  definition: Generic_Expression
                             }

        | SOURCE_CODE_REGION_FOR_NAMED_GENERIC  (Named_Generic, Source_Code_Region)



    also
    Named_Api

        = NAMED_API  {   name_symbol: Symbol,
                                   definition: Api_Expression
                               }

        | SOURCE_CODE_REGION_FOR_NAMED_API  (Named_Api, Source_Code_Region)



    also
    Named_Generic_Api

        = NAMED_GENERIC_API  {   name_symbol: Symbol,
                                           definition: Generic_Api_Expression
                                       }

        | SOURCE_REGION_FOR_NAMED_GENERIC_API  (Named_Generic_Api, Source_Code_Region)



    also
    Typevar

        = TYPEVAR                           Symbol
        | SOURCE_CODE_REGION_FOR_TYPEVAR    (Typevar, Source_Code_Region)



    also
    Any_Type 

        = TYPEVAR_TYPE           Typevar                                                #  Type variable.                       
        | TYPE_TYPE                   (List( Symbol ), List( Any_Type ))                        #  Type constructor.                    
        | RECORD_TYPE                  List( (Symbol, Any_Type) )                               #  Record.                              
        | TUPLE_TYPE                   List( Any_Type )                                         #  Tuple.                               
        | SOURCE_CODE_REGION_FOR_TYPE  (Any_Type, Source_Code_Region);                          #  For error messages etc.              



};      #  package raw_syntax 



Comments and suggestions to: bugs@mythryl.org

PreviousUpNext