ffmt_lexer Module

Lexer for free-form Fortran.

The lexer never fails: malformed input (e.g. an unterminated string) still produces tokens that cover every byte, flagged where relevant, so the formatter can leave such code alone.


Uses

  • module~~ffmt_lexer~~UsesGraph module~ffmt_lexer ffmt_lexer module~ffmt_strings ffmt_strings module~ffmt_lexer->module~ffmt_strings module~ffmt_tokens ffmt_tokens module~ffmt_lexer->module~ffmt_tokens module~ffmt_tokens->module~ffmt_strings

Used by

  • module~~ffmt_lexer~~UsedByGraph module~ffmt_lexer ffmt_lexer module~ffmt_format ffmt_format module~ffmt_format->module~ffmt_lexer module~ffmt_source ffmt_source module~ffmt_format->module~ffmt_source module~ffmt_case ffmt_case module~ffmt_format->module~ffmt_case module~ffmt_layout ffmt_layout module~ffmt_format->module~ffmt_layout module~ffmt_linebreak ffmt_linebreak module~ffmt_format->module~ffmt_linebreak module~ffmt_relational ffmt_relational module~ffmt_format->module~ffmt_relational module~ffmt_structure ffmt_structure module~ffmt_format->module~ffmt_structure module~ffmt_use_sort ffmt_use_sort module~ffmt_format->module~ffmt_use_sort module~ffmt_whitespace ffmt_whitespace module~ffmt_format->module~ffmt_whitespace module~ffmt_source->module~ffmt_lexer module~ffmt_case->module~ffmt_source module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_format module~ffmt_layout->module~ffmt_source module~ffmt_layout->module~ffmt_case module~ffmt_layout->module~ffmt_structure module~ffmt_layout->module~ffmt_whitespace module~ffmt_linebreak->module~ffmt_source module~ffmt_relational->module~ffmt_source module~ffmt_structure->module~ffmt_source module~ffmt_use_sort->module~ffmt_source module~ffmt_whitespace->module~ffmt_source

Variables

Type Visibility Attributes Name Initial
character(len=1), private, parameter :: CR = achar(13)
character(len=1), private, parameter :: LF = achar(10)
character(len=1), private, parameter :: TAB = achar(9)

Derived Types

type, private ::  lexer_t

Lexer state for one source text.

Components

Type Visibility Attributes Name Initial
logical, public :: glue_next = .false.

the next code token continues the previous one

logical, public :: glue_pending = .false.

the previous line ended with a code character directly followed by &

integer, public :: line = 1

current line

logical, public :: line_start = .true.

only blanks so far on the current line


Functions

private function comment_kind(text) result(kind)

Comment sub-kind (one of the CM_* constants) of a comment.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

comment text, from ! to the end of the line

Return Value integer

private function digits_end(src, i) result(j)

Last digit of the run starting at i, or i - 1 if there is none.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte where the run of digits starts

Return Value integer

private function dotop_at(src, i)

True if a dot operator such as .and. or .true. starts at i.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte to look at

Return Value logical

private function dotop_end(src, i) result(j)

Last byte of the dot operator at i, including a kind suffix on .true._lk / .false._lk.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte of the opening .

Return Value integer

private function fypp_inline_end(src, i) result(j)

Last byte of inline fypp (${...}$, @{...}@, #{...}#) starting at i, or 0.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte to look at

Return Value integer

private elemental function is_blank(c)

True for a blank or tab.

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private elemental function is_code_char(c)

A character that can be part of a code token (not a blank, line break or comment).

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private elemental function is_digit(c)

True for 0 to 9.

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private elemental function is_letter(c)

True for an ASCII letter.

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private elemental function is_name_char(c)

Characters allowed after the first letter of a name ($ is a common extension).

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private function is_preproc_start(src, i)

True if a preprocessor line starts at i (the first non-blank of the line): cpp #..., fypp #:, #!, $: and @:. Inline fypp #{ is not a directive line.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

first non-blank byte of the line

Return Value logical

private elemental function is_quote(c)

True for ' or ".

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: c

the character

Return Value logical

private pure function last_nonblank(src, i, j) result(k)

Last non-blank byte in src(i:j), or 0.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte range to search

integer, intent(in) :: j

byte range to search

Return Value integer

private pure function line_end(src, i) result(j)

Index of the last byte before the line break at or after i.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte to start from

Return Value integer

private function name_end(src, i) result(j)

Last byte of the name starting at i, stopping before inline fypp ${.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

first byte of the name

Return Value integer

private pure function only_blanks_to_eol(src, i)

True if there are only blanks from i to the end of the line.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte to start from

Return Value logical

private function operator_end(src, i) result(j)

Last byte of a two- or one-character operator at i, or 0 if there is none.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte to look at

Return Value integer

private pure function peek(src, i)

Character at i, or NUL outside the string.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte index, possibly outside src

Return Value character(len=1)

private function preproc_end(src, i) result(j)

Last byte of a preprocessor line at i, including continuation lines: a trailing \ for cpp, a trailing & for fypp. The final line break is not included.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

first byte of the directive (#, $ or @)

Return Value integer

private function string_end(src, i, flags) result(j)

Last byte of a string literal whose opening quote is at i. Handles doubled quotes and continuation lines; flags reports STR_* conditions.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

byte of the opening quote

integer, intent(out) :: flags

STR_* bit flags of the literal

Return Value integer


Subroutines

private subroutine add(tokens, lx, src, kind, first, last, sub)

Append a token, maintaining the line count and the glue state for split tokens.

Arguments

Type IntentOptional Attributes Name
type(token_list_t), intent(inout) :: tokens

token list to append to

type(lexer_t), intent(inout) :: lx

lexer state

character(len=*), intent(in) :: src

source text

integer, intent(in) :: kind

token kind, one of the TK_* constants

integer, intent(in) :: first

first byte of the token in src

integer, intent(in) :: last

last byte of the token in src

integer, intent(in), optional :: sub

sub-kind (CM_, CT_, STR_*); 0 if absent

private subroutine lex_number(src, i, j, kind)

Integer or real literal starting at i (a digit, or . followed by a digit).

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

integer, intent(in) :: i

first byte of the literal

integer, intent(out) :: j

last byte of the literal, including any kind suffix

integer, intent(out) :: kind

TK_INT or TK_REAL

public subroutine tokenize(src, tokens)

Split src into tokens.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

type(token_list_t), intent(out) :: tokens

its tokens, covering every byte