PreviousUpNext

15.4.561  src/lib/compiler/front/parser/main/mythryl-parser-guts.pkg

## mythryl-parser-guts.pkg

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

# Test cases for the #DO facility:
#
# DO printf "Hello, world!\n";
# DO printf "%d blind mice\n" 3;
# DO set_control "highcode::inline_threshold" "16";

stipulate
    package cos =  compile_statistics;                                  # compile_statistics            is from   src/lib/compiler/front/basics/stats/compile-statistics.pkg
    package cp  =  control_print;                                       # control_print                 is from   src/lib/compiler/front/basics/print/control-print.pkg
    package err =  error_message;                                       # error_message                 is from   src/lib/compiler/front/basics/errormsg/error-message.pkg
    package fil =  file__premicrothread;                                # file__premicrothread          is from   src/lib/std/src/posix/file--premicrothread.pkg
    package lnd =  line_number_db;                                      # line_number_db                is from   src/lib/compiler/front/basics/source/line-number-db.pkg
    package lrp =  lr_parser;                                           # lr_parser                     is from   src/app/yacc/lib/parser2.pkg
    package myp =  mythryl_parser;                                      # mythryl_parser                is from   src/lib/compiler/front/parser/main/mythryl-parser.pkg
    package raw =  raw_syntax;                                          # raw_syntax                    is from   src/lib/compiler/front/parser/raw-syntax/raw-syntax.pkg
    package sci =  sourcecode_info;                                     # sourcecode_info               is from   src/lib/compiler/front/basics/source/sourcecode-info.pkg
    package str =  string;                                              # string                        is from   src/lib/std/string.pkg
herein

    # This package is referenced (only) in:
    #
    #     src/lib/compiler/front/parser/main/parse-mythryl.pkg
    #
    package   mythryl_parser_guts
    : (weak)  Mythryl_Parser_Guts                                       # Mythryl_Parser_Guts   is from   src/lib/compiler/front/parser/main/mythryl-parser-guts.api
    {
        package mlv                                                     # "mlv" == "mythryl_lr_vals"
            =
            mythryl_lr_vals_fun (                                       # mythryl_lr_vals_fun   is from   src/lib/compiler/front/parser/yacc/mythryl.grammar.pkg
                #
                package token =   lrp::token;
            );

        package lex
            =
            mythryl_lex_g (
                #
                package tokens =   mlv::tokens;
            );

                                                                
        package mlp
            =
            make_complete_yacc_parser_with_custom_argument_g (          # make_complete_yacc_parser_with_custom_argument_g      is from   src/app/yacc/lib/make-complete-yacc-parser-with-custom-argument-g.pkg
                #
                package parser_data
                =  mlv::parser_data;

                package lex
                      = lex;
                #
                package lr_parser
                      = lr_parser;                                      # Typically from  ROOT/src/app/yacc/lib/parser2.pkg
            );

        # The following two functions are also defined in build/computil.pkg

        increment_linecount_by =   cos::increment_counterssum_by (cos::make_counterssum' "Source Lines");



        Mythryl_Parse_Result                                            # "Mythryl_Parse_Result" is referenced only here and in    src/lib/compiler/front/parser/main/mythryl-parser-guts.api
          #
          = RAW_DECLARATION raw::Declaration
          | END_OF_FILE                                                 # End of file reached 
          | PARSE_ERROR                                                 # Syntactic or semantic errors.
          ;

        dummy_eof    =   mlv::tokens::eof  (0, 0);
        dummy_semi   =   mlv::tokens::semi (0, 0);
        #
        fun prompt_read_parse_and_return_one_toplevel_mythryl_expression
                (       
                    sourcecode_info  as
                      {
                        source_stream,
                        error_consumer,
                        is_interactive,
                        line_number_db,
                        saw_errors,
                        ...
                      }
                      :
                      sci::Sourcecode_Info
                )
            =
            fn () = {   saw_errors :=  FALSE;
                        #
                        prompt_read_parse_and_return_one_toplevel_mythryl_expression' ();
                    }
            where
                err   =   err::error  sourcecode_info;

       #        complain_match   =   err::match_error_string  sourcecode_info;
                #
                fun parse_error (s, p1, p2)
                    =
                    err (p1, p2) err::ERROR s err::null_error_body;

                lex_arg
                  =
                  { comment_nesting_depth =>  REF 0,
                    line_number_db,
                    stringlist            =>  REF (NIL:     List( String )),
                    #
                    stringtype            =>  REF FALSE,
                    stringstart           =>  REF 0,
                    brack_stack           =>  REF (NIL:     List( Ref(Int) )),
                    #
                    err
                  };


                do_prompt =   REF TRUE;
                prompt    =   REF *myp::primary_prompt;
                #
                fun inputc_source_stream _
                    =
                    fil::read  source_stream;

                exception ABORT_LEX;

                # Read one line of interactive input from user.
                # (This function is called only when parsing
                # interactively entered program text.)                  XXX SUCKO FIXME, actually it doesn't read by lines any more,
                #                                                       and is also (only?) used when executing scripts.
                fun get_line k
                    =
                    {   if *do_prompt
                            #                   
                            if  *saw_errors    raise exception ABORT_LEX;  fi;


                            if *myp::print_interactive_prompts
                                #
                                cp::say
                                    if ( *lex_arg.comment_nesting_depth > 0
                                         or
                                         *lex_arg.stringlist != NIL
                                    )
                                        *myp::secondary_prompt;
                                    else 
                                        *prompt;
                                    fi;

                                cp::flush ();
                            fi;

                            do_prompt :=  FALSE;
                        fi;

                        s =  inputc_source_stream  k;

                        do_prompt
                            :=
                            ((str::get (s, size s - 1) == '\n')
                            except
                                _ = FALSE);
                        s;
                    };

                lexer =     lex::make_lexer                                                     # lex is defined above
                                #
                                (is_interactive   ??   get_line
                                                  ::   inputc_source_stream
                                )
                                #
                                lex_arg;

                lexer'      =   REF (lrp::stream::streamify lexer);

                lookahead   =   if is_interactive   0;
                                else               30;
                                fi;
                #
                fun prompt_read_parse_and_return_one_toplevel_mythryl_expression' ()
                    =
                    {   prompt :=  *myp::primary_prompt;

                        (lrp::stream::get  *lexer')
                            ->
                            (next_token, rest);

                        start_position
                            =
                            lnd::last_change  line_number_db;
                        #
                        fun lines_read ()
                            =
                            lnd::newline_count
                                #
                                line_number_db 
                                #
                                ( start_position,
                                  lnd::last_change line_number_db
                                );


                        # if is_interactive
                        # then lnd::forget_old_positions line_number_db  fi

                        if (mlp::same_token (next_token, dummy_semi)) 
                            #                    
                            lexer' := rest;
                            prompt_read_parse_and_return_one_toplevel_mythryl_expression' ();
                        else 
                            if (mlp::same_token (next_token, dummy_eof))
                                #
                                END_OF_FILE;
                            else
                                prompt :=  *myp::secondary_prompt;

                                (mlp::parse (lookahead, *lexer', parse_error, err))
                                    ->
                                    (raw_declaration, lexer'');

                                increment_linecount_by (lines_read ());

                                lexer' := lexer'';

                                if *saw_errors      PARSE_ERROR;                                # Handled in   src/lib/compiler/front/parser/main/parse-mythryl.pkg
                                else                RAW_DECLARATION raw_declaration;            # Handled in   src/lib/compiler/front/parser/main/parse-mythryl.pkg
                                fi;
                            fi;
                        fi;
                    }
                    except
                        lrp::PARSE_ERROR =>  PARSE_ERROR;                                       # Handled in   src/lib/compiler/front/parser/main/parse-mythryl.pkg
                        ABORT_LEX        =>  PARSE_ERROR;                                       # Handled in   src/lib/compiler/front/parser/main/parse-mythryl.pkg
                    end ;
            end;                                                                                # fun prompt_read_parse_and_return_one_toplevel_mythryl_expression
    };
end;

## (C) 2001 Lucent Technologies, Bell Labs
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2013,
## released per terms of SMLNJ-COPYRIGHT.




Comments and suggestions to: bugs@mythryl.org

PreviousUpNext