version 0.6.0
Source file

Data Types

type  mod_source_file::t_source_file_container
 Source file container. More...
 

Functions/Subroutines

subroutine mod_source_file::source_file_initialize (source_file, filename)
 Initialize the t_source_file_container from a file. More...
 
subroutine mod_source_file::source_file_initialize_from_string (source_file, string, label)
 Initialize the t_source_file_container from a string. More...
 
character function mod_source_file::source_file_read_character (source_file)
 Read the next character of the source file. More...
 
subroutine mod_source_file::source_file_unread_character (source_file)
 Return to the previous position in the source file. More...
 
subroutine mod_source_file::source_file_finalize (source_file)
 Close a source file. More...
 

Detailed Description

Brief description

The source file manager aims to record the current position of the read cursor and provide a way to set an arbitrary position to the read cursor.

Let us recall the lexer/parser organization with the 3 main units:

                                keyword list
                                      v
        ┏━━━━━━━━━━┓            ┌───────────┐          ┌────────────┐
 files  ┃          ┃ characters │           │  tokens  │            │ ───────>
 ─────> ┃   File   ┃ ──────────>│   Lexer   │ ───────> │   Parser   │  tokens / actions
        ┃          ┃            │           │          │            │ <───────
        ┗━━━━━━━━━━┛            └───────────┘          └────────────┘
       mod_source_file            mod_lexer             mod_parser
                                                        mod_identifier
                                                        mod_scope

Usage

As an example, consider a file notus.txt with the following content:

I
love
Notus <3

The following code opens the file and write every character preceded by the position, line and column number until the EOF is reached. It also diplays the newline flag.

type(t_source_file_container) :: source_file
character :: c
# Open notus.txt
call source_file_initialize(source_file, "notus.txt")
# Write header
write(*,'("P R C NL character")')
# Make a loop until EOF is reached
do while (.not. source_file%eof)
# Read the next character
c = source_file_read_character(source_file)
# Display the character and the source file container information
write(*,'(i2.2," ",i2.2," ",i2.2," ",l1," ",a)') source_file%pos, source_file%line, source_file%column, source_file%newline, c
end do
write(*,'("EOF")')

Expected output:

P R C NL character
01 01 01 F I
02 01 02 T
03 02 01 F l
04 02 02 F o
05 02 03 F v
06 02 04 F e
07 02 05 T
08 03 01 T
09 04 01 F N
10 04 02 F o
11 04 03 F t
12 04 04 F u
13 04 05 F s
14 04 06 F
15 04 07 F <
16 04 08 F 3
EOF

Function/Subroutine Documentation

◆ source_file_finalize()

subroutine mod_source_file::source_file_finalize ( type(t_source_file_container), intent(inout)  source_file)

Deallocate arrays but do not reset flags. They are required for the read/unread routines.

Parameters
[in,out]source_fileSource file container

◆ source_file_initialize()

subroutine mod_source_file::source_file_initialize ( type(t_source_file_container), intent(out)  source_file,
character(len=*), intent(in)  filename 
)

Rank 0 processor reads the source file and spread its content to all processors.

Parameters
[out]source_fileSource file container
[in]filenameSource file name

◆ source_file_initialize_from_string()

subroutine mod_source_file::source_file_initialize_from_string ( type(t_source_file_container), intent(out)  source_file,
character(len=*), intent(in)  string,
character(len=*), intent(in)  label 
)

Rank 0 processor spreads its content to all processors.

Parameters
[out]source_fileSource file container
[in]stringString character
[in]labelLabel for the string

◆ source_file_read_character()

character function mod_source_file::source_file_read_character ( type(t_source_file_container), intent(inout)  source_file)

Behavior:

  • Return only printable characters.
  • If newline character is reached, return '\n' and set newline flag to .true..
  • If end of file (EOF) is reached, return empty character and set eof flag to .true..
  • Carriage return character '\r' is forbidden
Parameters
[in,out]source_fileSource file container
Returns
c: next character in the file.

◆ source_file_unread_character()

subroutine mod_source_file::source_file_unread_character ( type(t_source_file_container), intent(inout)  source_file)
Parameters
[in,out]source_fileSource file container